Code

Added argc checks before attempting to access argv[]
[nagiosplug.git] / plugins / check_nagios.c
index 04258354c9e3f33b59e451e61a53dce5193d7b47..5c4cd4a3a7d769126b7bea5df1d25f45a612c4e4 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);
@@ -93,22 +93,22 @@ main (int argc, char **argv)
                printf ("Could not open stderr for %s\n", PS_RAW_COMMAND);
        }
 
-       /* cound the number of matching Nagios processes... */
+       /* count the number of matching Nagios processes... */
        while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
-               if (strstr (input_buffer, process_string))
+               if (!strstr(input_buffer, argv[0]) && strstr(input_buffer, process_string))
                        proc_entries++;
        }
 
        /* 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);
@@ -166,7 +166,7 @@ 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;
        }
@@ -183,14 +183,14 @@ process_arguments (int argc, char **argv)
 
                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 */
                        status_log = optarg;
@@ -204,7 +204,7 @@ 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);
                        break;
                }
        }
@@ -213,11 +213,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 +231,7 @@ print_usage (void)
 {
        printf
                ("Usage: %s -F <status log file> -e <expire_minutes> -C <process_string>\n",
-                PROGNAME);
+                progname);
 }
 
 
@@ -241,7 +241,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 +263,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");
 }