diff --git a/plugins/check_procs.c b/plugins/check_procs.c
index 9a3dc015f7ab1e7a44ac87947e49fb93d6a779ab..ceaff958f74b8ad56589d53a1f8481a176470a1a 100644 (file)
--- a/plugins/check_procs.c
+++ b/plugins/check_procs.c
*
******************************************************************************/
+const char *progname = "check_procs";
+#define REVISION "$Revision$"
+#define COPYRIGHT "1999-2002"
+#define AUTHOR "Ethan Galstad"
+#define EMAIL "nagios@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>
#include "common.h"
#include "utils.h"
int process_arguments (int, char **);
-int call_getopt (int, char **);
int validate_arguments (void);
void print_usage (void);
-void print_help (char *);
+void print_help (void);
int wmax = -1;
int cmax = -1;
int wmin = -1;
int cmin = -1;
-int options = 0;
+int options = 0; /* bitmask of filter criteria to test against */
#define ALL 1
#define STAT 2
#define PPID 4
int verbose = FALSE;
int uid;
int ppid;
-char *statopts = NULL;
-char *prog = NULL;
-char *args = NULL;
-char *format = NULL;
+char *statopts = "";
+char *prog = "";
+char *args = "";
+char *fmt = "";
char tmp[MAX_INPUT_BUFFER];
+const char *zombie = "Z";
int
main (int argc, char **argv)
char procprog[MAX_INPUT_BUFFER];
char *procargs;
- int resultsum = 0;
- int found = 0;
- int procs = 0;
- int pos;
+ int resultsum = 0; /* bitmask of the filter criteria met by a process */
+ int found = 0; /* counter for number of lines returned in `ps` output */
+ int procs = 0; /* counter for number of processes meeting filter criteria */
+ int pos; /* number of spaces before 'args' in `ps` output */
+ int cols; /* number of columns in ps output */
int result = STATE_UNKNOWN;
- procargs = malloc (MAX_INPUT_BUFFER);
-
if (process_arguments (argc, argv) == ERROR)
usage ("Unable to parse command line\n");
fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
- if (
#ifdef USE_PS_VARS
- sscanf (input_buffer, PS_FORMAT, PS_VARLIST) >= 4
+ cols = sscanf (input_buffer, PS_FORMAT, PS_VARLIST);
#else
- sscanf (input_buffer, PS_FORMAT, procstat, &procuid, &procppid, &pos,
- procprog) >= 4
+ cols = sscanf (input_buffer, PS_FORMAT, procstat, &procuid,
+ &procppid, &pos, procprog);
#endif
- ) {
+ /* Zombie processes do not give a procprog command */
+ if ( cols == 3 && strstr(procstat, zombie) ) {
+ strcpy(procprog, "");
+ cols = 4;
+ }
+ if ( cols >= 4 ) {
found++;
resultsum = 0;
- procargs = strcpy (procargs, &input_buffer[pos]);
- strip (procargs);
+ asprintf (&procargs, "%s", input_buffer + pos);
+ strip (procargs);
if ((options & STAT) && (strstr (statopts, procstat)))
resultsum |= STAT;
- if ((options & ARGS) && (strstr (procargs, args) == procargs))
+ if ((options & ARGS) && procargs && (strstr (procargs, args) == procargs))
resultsum |= ARGS;
- if ((options & PROG) && (strcmp (prog, procprog) == 0))
+ if ((options & PROG) && procprog && (strcmp (prog, procprog) == 0))
resultsum |= PROG;
if ((options & PPID) && (procppid == ppid))
resultsum |= PPID;
#endif
if (options == resultsum)
procs++;
+ }
+ /* This should not happen */
+ else if (verbose) {
+ printf("Not parseable: %s", input_buffer);
}
}
if (verbose && (options & USER))
printf ("%d ", uid);
- if (cmax >= 0 && cmin >= 0 && cmax < cmin) {
+ 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 (format, "CRITICAL", procs);
+ printf (fmt, "CRITICAL", procs);
return STATE_CRITICAL;
}
}
else if (cmax >= 0 && procs > cmax) {
- printf (format, "CRITICAL", procs);
+ printf (fmt, "CRITICAL", procs);
return STATE_CRITICAL;
}
else if (cmin >= 0 && procs < cmin) {
- printf (format, "CRITICAL", procs);
+ printf (fmt, "CRITICAL", procs);
return STATE_CRITICAL;
}
if (wmax >= 0 && wmin >= 0 && wmax < wmin) {
if (procs > wmax && procs < wmin) {
- printf (format, "CRITICAL", procs);
+ printf (fmt, "CRITICAL", procs);
return STATE_CRITICAL;
}
}
else if (wmax >= 0 && procs > wmax) {
- printf (format, "WARNING", procs);
+ printf (fmt, "WARNING", procs);
if ( !(result == STATE_CRITICAL) ) {
return STATE_WARNING;
}
/*return max (result, STATE_WARNING); */
}
else if (wmin >= 0 && procs < wmin) {
- printf (format, "WARNING", procs);
+ printf (fmt, "WARNING", procs);
if ( !(result == STATE_CRITICAL) ) {
return STATE_WARNING;
}
/*return max (result, STATE_WARNING); */
}
- printf (format, "OK", procs);
+ printf (fmt, "OK", procs);
if ( result == STATE_UNKNOWN ) {
result = STATE_OK;
}
int
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;
+ int c = 1;
char *user;
struct passwd *pw;
-#ifdef HAVE_GETOPT_H
int option_index = 0;
static struct option long_options[] = {
{"warning", required_argument, 0, 'w'},
{"verbose", no_argument, 0, 'v'},
{0, 0, 0, 0}
};
-#endif
+
+ 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
+ c = getopt_long (argc, argv, "Vvht:c:w:p:s:u:C:a:", long_options, &option_index);
- if (c == EOF)
+ if (c == -1 || 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 */
print_usage ();
exit (STATE_UNKNOWN);
case 'h': /* help */
- print_help (my_basename (argv[0]));
+ print_help ();
exit (STATE_OK);
case 'V': /* version */
- print_revision (my_basename (argv[0]), "$Revision$");
+ 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]));
+ progname);
print_usage ();
exit (STATE_UNKNOWN);
}
}
else {
printf ("%s: Critical Process Count must be an integer!\n\n",
- my_basename (argv[0]));
+ progname);
print_usage ();
exit (STATE_UNKNOWN);
}
}
else {
printf ("%s: Warning Process Count must be an integer!\n\n",
- my_basename (argv[0]));
+ progname);
print_usage ();
exit (STATE_UNKNOWN);
}
case 'p': /* process id */
if (sscanf (optarg, "%d%[^0-9]", &ppid, tmp) == 1) {
- format =
- strscat (format,
- ssprintf (NULL, "%sPPID = %d", (options ? ", " : ""),
- ppid));
+ asprintf (&fmt, "%s%sPPID = %d", fmt, (options ? ", " : ""), ppid);
options |= PPID;
break;
}
printf ("%s: Parent Process ID must be an integer!\n\n",
- my_basename (argv[0]));
+ progname);
print_usage ();
exit (STATE_UNKNOWN);
case 's': /* status */
- statopts = strscpy (statopts, optarg);
- format =
- strscat (format,
- ssprintf (NULL, "%sSTATE = %s", (options ? ", " : ""),
- statopts));
+ asprintf (&statopts, "%s", optarg);
+ asprintf (&fmt, "%s%sSTATE = %s", fmt, (options ? ", " : ""), statopts);
options |= STAT;
break;
case 'u': /* user or user id */
uid = pw->pw_uid;
}
user = pw->pw_name;
- format =
- strscat (format,
- ssprintf (NULL, "%sUID = %d (%s)", (options ? ", " : ""),
- uid, user));
+ asprintf (&fmt, "%s%sUID = %d (%s)", fmt, (options ? ", " : ""),
+ uid, user);
options |= USER;
break;
case 'C': /* command */
- prog = strscpy (prog, optarg);
- format =
- strscat (format,
- ssprintf (NULL, "%scommand name %s", (options ? ", " : ""),
- prog));
+ asprintf (&prog, "%s", optarg);
+ asprintf (&fmt, "%s%scommand name %s", fmt, (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));
+ asprintf (&args, "%s", optarg);
+ asprintf (&fmt, "%s%sargs %s", fmt, (options ? ", " : ""), args);
options |= ARGS;
break;
case 'v': /* command */
break;
}
}
- return i;
+
+ 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 ();
}
validate_arguments ()
{
- if (wmax >= 0 && wmin == -1)
+if (wmax >= 0 && wmin == -1)
wmin = 0;
if (cmax >= 0 && cmin == -1)
cmin = 0;
}
}
- if (wmax == -1 && cmax == -1 && wmin == -1 && cmin == -1) {
- printf ("At least one threshold must be set\n");
- return ERROR;
- }
+/* 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");
+ asprintf (&fmt, "%%s - %%d processes running\n");
}
else {
- format =
- ssprintf (format, "%%s - %%d processes running with %s\n", format);
+ asprintf (&fmt, "%%s - %%d processes running with %s\n", fmt);
}
return options;
void
-print_help (char *cmd)
+print_help (void)
{
- print_revision (cmd, "$Revision$");
+ print_revision (progname, REVISION);
printf
- ("Copyright (c) 1999 Ethan Galstad (nagios@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");
+ ("Copyright (c) %s %s <%s>\n\n%s\n",
+ COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
print_usage ();
printf
("\nRequired Arguments:\n"