[Nagiosplug-checkins] CVS: nagiosplug/plugins check_procs.c,1.2,1.3

Karl DeBisschop kdebisschop at users.sourceforge.net
Tue Oct 22 01:20:02 CEST 2002


Update of /cvsroot/nagiosplug/nagiosplug/plugins
In directory usw-pr-cvs1:/tmp/cvs-serv18607/plugins

Modified Files:
	check_procs.c 
Log Message:
remove call_getopt, replace ssprintf with asprintf

Index: check_procs.c
===================================================================
RCS file: /cvsroot/nagiosplug/nagiosplug/plugins/check_procs.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -r1.2 -r1.3
*** check_procs.c	19 Jun 2002 03:09:10 -0000	1.2
--- check_procs.c	22 Oct 2002 08:19:05 -0000	1.3
***************
*** 35,38 ****
--- 35,49 ----
  ******************************************************************************/
  
+ #define PROGNAME "check_snmp"
+ #define REVISION "$Revision$"
+ #define COPYRIGHT "1999-2002"
+ #define AUTHOR "Ethan Galstad"
+ #define EMAIL "nagios at nagios.org"
+ #define SUMMARY "Check the number of currently running processes and generates WARNING or\n\
+ CRITICAL states if the process count is outside the specified threshold\n\
+ ranges. The process count can be filtered by process owner, parent process\n\
+ PID, current state (e.g., 'Z'), or may be the total number of running\n\
+ processes\n"
+ 
  #include "config.h"
  #include <pwd.h>
***************
*** 42,49 ****
  
  int process_arguments (int, char **);
- int call_getopt (int, char **);
  int validate_arguments (void);
  void print_usage (void);
! void print_help (char *);
  
  int wmax = -1;
--- 53,59 ----
  
  int process_arguments (int, char **);
  int validate_arguments (void);
  void print_usage (void);
! void print_help (void);
  
  int wmax = -1;
***************
*** 66,70 ****
  char *prog = NULL;
  char *args = NULL;
! char *format = NULL;
  char tmp[MAX_INPUT_BUFFER];
  
--- 76,80 ----
  char *prog = NULL;
  char *args = NULL;
! char *fmt = NULL;
  char tmp[MAX_INPUT_BUFFER];
  
***************
*** 189,204 ****
  		printf ("%d ", uid);
  
! 	if (cmax >= 0 && cmin >= 0 && cmax < cmin) {
  		if (procs > cmax && procs < cmin) {
! 			printf (format, "CRITICAL", procs);
  			return STATE_CRITICAL;
  		}
  	}
  	else if (cmax >= 0 && procs > cmax) {
! 		printf (format, "CRITICAL", procs);
  		return STATE_CRITICAL;
  	}
  	else if (cmin >= 0 && procs < cmin) {
! 		printf (format, "CRITICAL", procs);
  		return STATE_CRITICAL;
  	}
--- 199,220 ----
  		printf ("%d ", uid);
  
!  	if (wmax == -1 && cmax == -1 && wmin == -1 && cmin == -1) {
! 		if (result == STATE_UNKNOWN)
! 			result = STATE_OK;
! 		printf (fmt, "OK", procs);
! 		return result;
!  	}
! 	else if (cmax >= 0 && cmin >= 0 && cmax < cmin) {
  		if (procs > cmax && procs < cmin) {
! 			printf (fmt, "CRITICAL", procs);
  			return STATE_CRITICAL;
  		}
  	}
  	else if (cmax >= 0 && procs > cmax) {
! 		printf (fmt, "CRITICAL", procs);
  		return STATE_CRITICAL;
  	}
  	else if (cmin >= 0 && procs < cmin) {
! 		printf (fmt, "CRITICAL", procs);
  		return STATE_CRITICAL;
  	}
***************
*** 206,215 ****
  	if (wmax >= 0 && wmin >= 0 && wmax < wmin) {
  		if (procs > wmax && procs < wmin) {
! 			printf (format, "CRITICAL", procs);
  			return STATE_CRITICAL;
  		}
  	}
  	else if (wmax >= 0 && procs > wmax) {
! 		printf (format, "WARNING", procs);
  		if ( !(result == STATE_CRITICAL) ) {
  			return STATE_WARNING;
--- 222,231 ----
  	if (wmax >= 0 && wmin >= 0 && wmax < wmin) {
  		if (procs > wmax && procs < wmin) {
! 			printf (fmt, "CRITICAL", procs);
  			return STATE_CRITICAL;
  		}
  	}
  	else if (wmax >= 0 && procs > wmax) {
! 		printf (fmt, "WARNING", procs);
  		if ( !(result == STATE_CRITICAL) ) {
  			return STATE_WARNING;
***************
*** 221,225 ****
  	}
  	else if (wmin >= 0 && procs < wmin) {
! 		printf (format, "WARNING", procs);
  		if ( !(result == STATE_CRITICAL) ) {
  			return STATE_WARNING;
--- 237,241 ----
  	}
  	else if (wmin >= 0 && procs < wmin) {
! 		printf (fmt, "WARNING", procs);
  		if ( !(result == STATE_CRITICAL) ) {
  			return STATE_WARNING;
***************
*** 231,235 ****
  	}
  
! 	printf (format, "OK", procs);
  	if ( result == STATE_UNKNOWN ) {
  		result = STATE_OK;
--- 247,251 ----
  	}
  
! 	printf (fmt, "OK", procs);
  	if ( result == STATE_UNKNOWN ) {
  		result = STATE_OK;
***************
*** 242,278 ****
  process_arguments (int argc, char **argv)
  {
- 	int c;
- 
- 	if (argc < 2)
- 		return ERROR;
- 
- 	for (c = 1; c < argc; c++)
- 		if (strcmp ("-to", argv[c]) == 0)
- 			strcpy (argv[c], "-t");
- 
- 	c = 0;
- 	while (c += (call_getopt (argc - c, &argv[c]))) {
- 		if (argc <= c)
- 			break;
- 		if (wmax == -1)
- 			wmax = atoi (argv[c]);
- 		else if (cmax == -1)
- 			cmax = atoi (argv[c]);
- 		else if (statopts == NULL) {
- 			statopts = strscpy (statopts, argv[c]);
- 			format =
- 				strscat (format,
- 								 ssprintf (NULL, "%sSTATE = %s", (options ? ", " : ""),
- 													 statopts));
- 			options |= STAT;
- 		}
- 	}
- 
- 	return validate_arguments ();
- }
- 
- int
- call_getopt (int argc, char **argv)
- {
  	int c, i = 1;
  	char *user;
--- 258,261 ----
***************
*** 295,323 ****
  #endif
  
  	while (1) {
  #ifdef HAVE_GETOPT_H
! 		c =
! 			getopt_long (argc, argv, "+Vvht:c:w:p:s:u:C:a:", long_options,
! 									 &option_index);
  #else
! 		c = getopt (argc, argv, "+Vvht:c:w:p:s:u:C:a:");
  #endif
! 
! 		if (c == EOF)
  			break;
  
- 		i++;
- 		switch (c) {
- 		case 't':
- 		case 'c':
- 		case 'w':
- 		case 'p':
- 		case 's':
- 		case 'a':
- 		case 'u':
- 		case 'C':
- 			i++;
- 		}
- 
  		switch (c) {
  		case '?':									/* help */
--- 278,296 ----
  #endif
  
+ 	asprintf (&fmt, "");
+ 
+ 	for (c = 1; c < argc; c++)
+ 		if (strcmp ("-to", argv[c]) == 0)
+ 			strcpy (argv[c], "-t");
+ 
  	while (1) {
  #ifdef HAVE_GETOPT_H
! 		c =	getopt_long (argc, argv, "Vvht:c:w:p:s:u:C:a:", long_options, &option_index);
  #else
! 		c = getopt (argc, argv, "Vvht:c:w:p:s:u:C:a:");
  #endif
! 		if (c == -1 || c == EOF)
  			break;
  
  		switch (c) {
  		case '?':									/* help */
***************
*** 325,337 ****
  			exit (STATE_UNKNOWN);
  		case 'h':									/* help */
! 			print_help (my_basename (argv[0]));
  			exit (STATE_OK);
  		case 'V':									/* version */
! 			print_revision (my_basename (argv[0]), "$Revision$");
  			exit (STATE_OK);
  		case 't':									/* timeout period */
  			if (!is_integer (optarg)) {
  				printf ("%s: Timeout Interval must be an integer!\n\n",
! 								my_basename (argv[0]));
  				print_usage ();
  				exit (STATE_UNKNOWN);
--- 298,310 ----
  			exit (STATE_UNKNOWN);
  		case 'h':									/* help */
! 			print_help ();
  			exit (STATE_OK);
  		case 'V':									/* version */
! 			print_revision (PROGNAME, REVISION);
  			exit (STATE_OK);
  		case 't':									/* timeout period */
  			if (!is_integer (optarg)) {
  				printf ("%s: Timeout Interval must be an integer!\n\n",
! 				        my_basename (argv[0]));
  				print_usage ();
  				exit (STATE_UNKNOWN);
***************
*** 355,359 ****
  			else {
  				printf ("%s: Critical Process Count must be an integer!\n\n",
! 								my_basename (argv[0]));
  				print_usage ();
  				exit (STATE_UNKNOWN);
--- 328,332 ----
  			else {
  				printf ("%s: Critical Process Count must be an integer!\n\n",
! 				        my_basename (argv[0]));
  				print_usage ();
  				exit (STATE_UNKNOWN);
***************
*** 375,379 ****
  			else {
  				printf ("%s: Warning Process Count must be an integer!\n\n",
! 								my_basename (argv[0]));
  				print_usage ();
  				exit (STATE_UNKNOWN);
--- 348,352 ----
  			else {
  				printf ("%s: Warning Process Count must be an integer!\n\n",
! 				        my_basename (argv[0]));
  				print_usage ();
  				exit (STATE_UNKNOWN);
***************
*** 381,401 ****
  		case 'p':									/* process id */
  			if (sscanf (optarg, "%d%[^0-9]", &ppid, tmp) == 1) {
! 				format =
! 					strscat (format,
! 									 ssprintf (NULL, "%sPPID = %d", (options ? ", " : ""),
! 														 ppid));
  				options |= PPID;
  				break;
  			}
  			printf ("%s: Parent Process ID must be an integer!\n\n",
! 							my_basename (argv[0]));
  			print_usage ();
  			exit (STATE_UNKNOWN);
  		case 's':									/* status */
! 			statopts = strscpy (statopts, optarg);
! 			format =
! 				strscat (format,
! 								 ssprintf (NULL, "%sSTATE = %s", (options ? ", " : ""),
! 													 statopts));
  			options |= STAT;
  			break;
--- 354,368 ----
  		case 'p':									/* process id */
  			if (sscanf (optarg, "%d%[^0-9]", &ppid, tmp) == 1) {
! 				asprintf (&fmt, "%s%sPPID = %d", (options ? ", " : ""), ppid);
  				options |= PPID;
  				break;
  			}
  			printf ("%s: Parent Process ID must be an integer!\n\n",
! 			        my_basename (argv[0]));
  			print_usage ();
  			exit (STATE_UNKNOWN);
  		case 's':									/* status */
! 			asprintf (&statopts, "%s", optarg);
! 			asprintf (&fmt, "%s%sSTATE = %s", fmt, (options ? ", " : ""), statopts);
  			options |= STAT;
  			break;
***************
*** 423,445 ****
  			}
  			user = pw->pw_name;
! 			format =
! 				strscat (format,
! 								 ssprintf (NULL, "%sUID = %d (%s)", (options ? ", " : ""),
! 													 uid, user));
  			options |= USER;
  			break;
  		case 'C':									/* command */
! 			prog = strscpy (prog, optarg);
! 			format =
! 				strscat (format,
! 								 ssprintf (NULL, "%scommand name %s", (options ? ", " : ""),
! 													 prog));
  			options |= PROG;
  			break;
  		case 'a':									/* args (full path name with args) */
! 			args = strscpy (args, optarg);
! 			format =
! 				strscat (format,
! 								 ssprintf (NULL, "%sargs %s", (options ? ", " : ""), args));
  			options |= ARGS;
  			break;
--- 390,406 ----
  			}
  			user = pw->pw_name;
! 			asprintf (&fmt, "%s%sUID = %d (%s)", (options ? ", " : ""), fmt,
! 			          uid, user);
  			options |= USER;
  			break;
  		case 'C':									/* command */
! 			asprintf (&prog, "%s", optarg);
! 			asprintf (&fmt, "%s%scommand name %s", fmt, (options ? ", " : ""),
! 			          prog);
  			options |= PROG;
  			break;
  		case 'a':									/* args (full path name with args) */
! 			asprintf (&args, "%s", optarg);
! 			asprintf (&fmt, "%s%sargs %s", fmt, (options ? ", " : ""), args);
  			options |= ARGS;
  			break;
***************
*** 449,453 ****
  		}
  	}
! 	return i;
  }
  
--- 410,426 ----
  		}
  	}
! 
! 	c = optind;
! 	if (wmax == -1 && argv[c])
! 		wmax = atoi (argv[c++]);
! 	if (cmax == -1 && argv[c])
! 		cmax = atoi (argv[c++]);
! 	if (statopts == NULL && argv[c]) {
! 		asprintf (&statopts, "%s", argv[c++]);
! 		asprintf (&fmt, "%s%sSTATE = %s", fmt, (options ? ", " : ""), statopts);
! 		options |= STAT;
! 	}
! 
! 	return validate_arguments ();
  }
  
***************
*** 457,461 ****
  {
  
! 	if (wmax >= 0 && wmin == -1)
  		wmin = 0;
  	if (cmax >= 0 && cmin == -1)
--- 430,434 ----
  {
  
! if (wmax >= 0 && wmin == -1)
  		wmin = 0;
  	if (cmax >= 0 && cmin == -1)
***************
*** 472,487 ****
  	}
  
! 	if (wmax == -1 && cmax == -1 && wmin == -1 && cmin == -1) {
! 		printf ("At least one threshold must be set\n");
! 		return ERROR;
! 	}
  
  	if (options == 0) {
  		options = 1;
! 		format = ssprintf (format, "%%s - %%d processes running\n");
  	}
  	else {
! 		format =
! 			ssprintf (format, "%%s - %%d processes running with %s\n", format);
  	}
  
--- 445,459 ----
  	}
  
! /* 	if (wmax == -1 && cmax == -1 && wmin == -1 && cmin == -1) { */
! /* 		printf ("At least one threshold must be set\n"); */
! /* 		return ERROR; */
! /* 	} */
  
  	if (options == 0) {
  		options = 1;
! 		asprintf (&fmt, "%%s - %%d processes running\n");
  	}
  	else {
! 		asprintf (&fmt, "%%s - %%d processes running with %s\n", fmt);
  	}
  
***************
*** 491,504 ****
  
  void
! print_help (char *cmd)
  {
! 	print_revision (cmd, "$Revision$");
  	printf
! 		("Copyright (c) 1999 Ethan Galstad (nagios at nagios.org)\n\n"
! 		 "This plugin checks the number of currently running processes and\n"
! 		 "generates WARNING or CRITICAL states if the process count is outside\n"
! 		 "the specified threshold ranges. The process count can be filtered by\n"
! 		 "process owner, parent process PID, current state (e.g., 'Z'), or may\n"
! 		 "be the total number of running processes\n\n");
  	print_usage ();
  	printf
--- 463,472 ----
  
  void
! print_help (void)
  {
! 	print_revision (PROGNAME, REVISION);
  	printf
! 		("Copyright (c) %s %s <%s>\n\n%s\n",
! 		 COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
  	print_usage ();
  	printf





More information about the Commits mailing list