Code

Strip leading spaces on dns return value (689563 - Simon L Nielsen)
[nagiosplug.git] / plugins / check_procs.c
index 27cd2daee7f17b8be3330952b05896935672242b..755ed8609814b3aefcd5dc7787bf1ea26321bc7c 100644 (file)
@@ -34,7 +34,7 @@
 *
 ******************************************************************************/
 
-#define PROGNAME "check_snmp"
+const char *progname = "check_procs";
 #define REVISION "$Revision$"
 #define COPYRIGHT "1999-2002"
 #define AUTHOR "Ethan Galstad"
@@ -61,7 +61,7 @@ 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
@@ -72,11 +72,12 @@ int options = 0;
 int verbose = FALSE;
 int uid;
 int ppid;
-char *statopts = NULL;
-char *prog = NULL;
-char *args = NULL;
-char *fmt = NULL;
+char *statopts = "";
+char *prog = "";
+char *args = "";
+char *fmt = "";
 char tmp[MAX_INPUT_BUFFER];
+const char *zombie = "Z";
 
 int
 main (int argc, char **argv)
@@ -89,15 +90,14 @@ 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");
 
@@ -117,23 +117,27 @@ main (int argc, char **argv)
        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;
@@ -149,6 +153,10 @@ main (int argc, char **argv)
 #endif
                        if (options == resultsum)
                                procs++;
+               } 
+               /* This should not happen */
+               else if (verbose) {
+                       printf("Not parseable: %s", input_buffer);
                }
        }
 
@@ -277,8 +285,6 @@ process_arguments (int argc, char **argv)
        };
 #endif
 
-       asprintf (&fmt, "");
-
        for (c = 1; c < argc; c++)
                if (strcmp ("-to", argv[c]) == 0)
                        strcpy (argv[c], "-t");
@@ -300,12 +306,12 @@ process_arguments (int argc, char **argv)
                        print_help ();
                        exit (STATE_OK);
                case 'V':                                                                       /* version */
-                       print_revision (PROGNAME, 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);
                        }
@@ -327,7 +333,7 @@ process_arguments (int argc, char **argv)
                        }
                        else {
                                printf ("%s: Critical Process Count must be an integer!\n\n",
-                                       my_basename (argv[0]));
+                                       progname);
                                print_usage ();
                                exit (STATE_UNKNOWN);
                        }
@@ -347,18 +353,18 @@ process_arguments (int argc, char **argv)
                        }
                        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) {
-                               asprintf (&fmt, "%s%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 */
@@ -389,7 +395,7 @@ process_arguments (int argc, char **argv)
                                uid = pw->pw_uid;
                        }
                        user = pw->pw_name;
-                       asprintf (&fmt, "%s%sUID = %d (%s)", (options ? ", " : ""), fmt,
+                       asprintf (&fmt, "%s%sUID = %d (%s)", fmt, (options ? ", " : ""),
                                  uid, user);
                        options |= USER;
                        break;
@@ -464,7 +470,7 @@ if (wmax >= 0 && wmin == -1)
 void
 print_help (void)
 {
-       print_revision (PROGNAME, REVISION);
+       print_revision (progname, REVISION);
        printf
                ("Copyright (c) %s %s <%s>\n\n%s\n",
                 COPYRIGHT, AUTHOR, EMAIL, SUMMARY);