Code

Souped up check_procs with different metrics
[nagiosplug.git] / plugins / check_nagios.c
index 04258354c9e3f33b59e451e61a53dce5193d7b47..64a828b99e1cdb8896cd0158b3dc3c56866e10f8 100644 (file)
@@ -30,7 +30,7 @@
 #include "popen.h"
 #include "utils.h"
 
-#define PROGNAME "check_nagios"
+const char *progname = "check_nagios";
 
 int process_arguments (int, char **);
 void print_usage (void);
@@ -40,6 +40,8 @@ char *status_log = NULL;
 char *process_string = NULL;
 int expire_minutes = 0;
 
+int verbose = 0;
+
 int
 main (int argc, char **argv)
 {
@@ -51,6 +53,15 @@ main (int argc, char **argv)
        time_t current_time;
        char *temp_ptr;
        FILE *fp;
+       int procuid = 0;
+       int procppid = 0;
+       int procvsz = 0;
+       int procrss = 0;
+       float procpcpu = 0;
+       char procstat[8];
+       char procprog[MAX_INPUT_BUFFER];
+       char *procargs;
+       int pos, cols;
 
        if (process_arguments (argc, argv) == ERROR)
                usage ("Could not parse arguments\n");
@@ -82,33 +93,44 @@ main (int argc, char **argv)
        fclose (fp);
 
        /* run the command to check for the Nagios process.. */
-       child_process = spopen (PS_RAW_COMMAND);
+       child_process = spopen (PS_COMMAND);
        if (child_process == NULL) {
-               printf ("Could not open pipe: %s\n", PS_RAW_COMMAND);
+               printf ("Could not open pipe: %s\n", PS_COMMAND);
                return STATE_UNKNOWN;
        }
 
        child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
        if (child_stderr == NULL) {
-               printf ("Could not open stderr for %s\n", PS_RAW_COMMAND);
+               printf ("Could not open stderr for %s\n", PS_COMMAND);
        }
 
-       /* cound the number of matching Nagios processes... */
+       fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
+
+       /* count the number of matching Nagios processes... */
        while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
-               if (strstr (input_buffer, process_string))
-                       proc_entries++;
+               cols = sscanf (input_buffer, PS_FORMAT, PS_VARLIST);
+               if ( cols >= 6 ) {
+                       asprintf (&procargs, "%s", input_buffer + pos);
+                       strip (procargs);
+                       
+                       if (!strstr(procargs, argv[0]) && strstr(procargs, process_string)) {
+                               proc_entries++;
+                               if (verbose)
+                                       printf ("Found process: %s\n", procargs);
+                       }
+               }
        }
 
        /* If we get anything on stderr, at least set warning */
        while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
-               result = max (result, STATE_WARNING);
+               result = max_state (result, STATE_WARNING);
 
        /* close stderr */
        (void) fclose (child_stderr);
 
        /* close the pipe */
        if (spclose (child_process))
-               result = max (result, STATE_WARNING);
+               result = max_state (result, STATE_WARNING);
 
        /* reset the alarm handler */
        alarm (0);
@@ -144,7 +166,6 @@ process_arguments (int argc, char **argv)
 {
        int c;
 
-#ifdef HAVE_GETOPT_H
        int option_index = 0;
        static struct option long_options[] = {
                {"filename", required_argument, 0, 'F'},
@@ -152,9 +173,9 @@ process_arguments (int argc, char **argv)
                {"command", required_argument, 0, 'C'},
                {"version", no_argument, 0, 'V'},
                {"help", no_argument, 0, 'h'},
+               {"verbose", no_argument, 0, 'v'},
                {0, 0, 0, 0}
        };
-#endif
 
        if (argc < 2)
                return ERROR;
@@ -166,45 +187,44 @@ process_arguments (int argc, char **argv)
                else
                        terminate (STATE_UNKNOWN,
                                                                 "Expiration time must be an integer (seconds)\nType '%s -h' for additional help\n",
-                                                                PROGNAME);
+                                                                progname);
                process_string = argv[3];
                return OK;
        }
 
        while (1) {
-#ifdef HAVE_GETOPT_H
-               c = getopt_long (argc, argv, "+hVF:C:e:", long_options, &option_index);
-#else
-               c = getopt (argc, argv, "+hVF:C:e:");
-#endif
+               c = getopt_long (argc, argv, "+hVvF:C:e:", long_options, &option_index);
 
                if (c == -1 || c == EOF || c == 1)
                        break;
 
                switch (c) {
                case '?':                                                                       /* print short usage statement if args not parsable */
-                       printf ("%s: Unknown argument: %c\n\n", my_basename (argv[0]), optopt);
+                       printf ("%s: Unknown argument: %c\n\n", progname, optopt);
                        print_usage ();
                        exit (STATE_UNKNOWN);
                case 'h':                                                                       /* help */
                        print_help ();
                        exit (STATE_OK);
                case 'V':                                                                       /* version */
-                       print_revision (my_basename (argv[0]), "$Revision$");
+                       print_revision (progname, "$Revision$");
                        exit (STATE_OK);
-               case 'F':                                                                       /* hostname */
+               case 'F':                                                                       /* status log */
                        status_log = optarg;
                        break;
-               case 'C':                                                                       /* hostname */
+               case 'C':                                                                       /* command */
                        process_string = optarg;
                        break;
-               case 'e':                                                                       /* hostname */
+               case 'e':                                                                       /* expiry time */
                        if (is_intnonneg (optarg))
                                expire_minutes = atoi (optarg);
                        else
                                terminate (STATE_UNKNOWN,
                                                                         "Expiration time must be an integer (seconds)\nType '%s -h' for additional help\n",
-                                                                        PROGNAME);
+                                                                        progname);
+                       break;
+               case 'v':
+                       verbose++;
                        break;
                }
        }
@@ -213,11 +233,11 @@ process_arguments (int argc, char **argv)
        if (status_log == NULL)
                terminate (STATE_UNKNOWN,
                                                         "You must provide the status_log\nType '%s -h' for additional help\n",
-                                                        PROGNAME);
+                                                        progname);
        else if (process_string == NULL)
                terminate (STATE_UNKNOWN,
                                                         "You must provide a process string\nType '%s -h' for additional help\n",
-                                                        PROGNAME);
+                                                        progname);
 
        return OK;
 }
@@ -231,7 +251,7 @@ print_usage (void)
 {
        printf
                ("Usage: %s -F <status log file> -e <expire_minutes> -C <process_string>\n",
-                PROGNAME);
+                progname);
 }
 
 
@@ -241,7 +261,7 @@ print_usage (void)
 void
 print_help (void)
 {
-       print_revision (PROGNAME, "$Revision$");
+       print_revision (progname, "$Revision$");
        printf
                ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n"
                 "This plugin attempts to check the status of the Nagios process on the local\n"
@@ -263,5 +283,5 @@ print_help (void)
                 "-V, --version\n"
                 "   Print version information\n\n"
                 "Example:\n"
-                "   ./check_nagios -H /usr/local/nagios/var/status.log -e 5 -C /usr/local/nagios/bin/nagios\n");
+                "   ./check_nagios -F /usr/local/nagios/var/status.log -e 5 -C /usr/local/nagios/bin/nagios\n");
 }