Code

afs checking
[nagiosplug.git] / plugins / check_hpjd.c
index 9d1878a4509035e6b5054962a015172d93a74641..d8583724391aadcc644ea91fac3d9d0015b16e2d 100644 (file)
@@ -65,7 +65,9 @@
 #include "popen.h"
 #include "utils.h"
 
-#define PROGNAME "check_hpjd"
+const char *progname = "check_hpjd";
+#define REVISION "$Revision$"
+#define COPYRIGHT "2000-2002"
 
 #define HPJD_LINE_STATUS               ".1.3.6.1.4.1.11.2.3.9.1.1.2.1"
 #define HPJD_PAPER_STATUS              ".1.3.6.1.4.1.11.2.3.9.1.1.2.2"
 #define ONLINE         0
 #define OFFLINE                1
 
+#define DEFAULT_COMMUNITY "public"
+
 int process_arguments (int, char **);
-int call_getopt (int, char **);
 int validate_arguments (void);
 void print_help (void);
 void print_usage (void);
 
-char *community = NULL;
+char *community = DEFAULT_COMMUNITY;
 char *address = NULL;
 
+
 int
 main (int argc, char **argv)
 {
@@ -135,7 +139,7 @@ main (int argc, char **argv)
                 HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, HPJD_GD_STATUS_DISPLAY);
 
        /* get the command to run */
-       sprintf (command_line, "%s -m : -v 1 %s %s %s", PATH_TO_SNMPGET, address,
+       sprintf (command_line, "%s -m : -v 1 %s -c %s %s", PATH_TO_SNMPGET, address,
                                         community, query_string);
 
        /* run the command */
@@ -285,14 +289,14 @@ main (int argc, char **argv)
 
        /* WARNING if output found on stderr */
        if (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);
 
        /* if there wasn't any output, display an error */
        if (line == 0) {
@@ -386,102 +390,42 @@ 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");
-               else if (strcmp ("-wt", argv[c]) == 0)
-                       strcpy (argv[c], "-w");
-               else if (strcmp ("-ct", argv[c]) == 0)
-                       strcpy (argv[c], "-c");
-       }
-
-
-
-       c = 0;
-       while ((c += (call_getopt (argc - c, &argv[c]))) < argc) {
-
-               if (is_option (argv[c]))
-                       continue;
-
-               if (address == NULL) {
-                       if (is_host (argv[c])) {
-                               address = argv[c];
-                       }
-                       else {
-                               usage ("Invalid host name");
-                       }
-               }
-               else if (community == NULL) {
-                       community = argv[c];
-               }
-       }
-
-       if (address == NULL)
-               address = strscpy (NULL, "127.0.0.1");
-
-       return validate_arguments ();
-}
-
-
-
-
-
-
-int
-call_getopt (int argc, char **argv)
-{
-       int c, i = 0;
-
-#ifdef HAVE_GETOPT_H
        int option_index = 0;
        static struct option long_options[] = {
                {"hostname", required_argument, 0, 'H'},
-               {"expect", required_argument, 0, 'e'},
+               {"community", required_argument, 0, 'C'},
 /*             {"critical",       required_argument,0,'c'}, */
 /*             {"warning",        required_argument,0,'w'}, */
 /*             {"port",           required_argument,0,'P'}, */
-               {"verbose", no_argument, 0, 'v'},
                {"version", no_argument, 0, 'V'},
                {"help", no_argument, 0, 'h'},
                {0, 0, 0, 0}
        };
-#endif
 
+       if (argc < 2)
+               return ERROR;
+
+       
        while (1) {
-#ifdef HAVE_GETOPT_H
                c = getopt_long (argc, argv, "+hVH:C:", long_options, &option_index);
-#else
-               c = getopt (argc, argv, "+?hVH:C:");
-#endif
-
-               i++;
 
                if (c == -1 || c == EOF || c == 1)
                        break;
 
-               switch (c) {
-               case 'H':
-               case 'C':
-                       i++;
-               }
-
                switch (c) {
                case 'H':                                                                       /* hostname */
                        if (is_host (optarg)) {
-                               address = optarg;
+                               address = strscpy(address, optarg) ;
                        }
                        else {
                                usage ("Invalid host name\n");
                        }
                        break;
                case 'C':                                                                       /* community */
-                       community = optarg;
+                       community = strscpy (community, optarg);
                        break;
                case 'V':                                                                       /* version */
-                       print_revision (PROGNAME, "$Revision$");
+                       print_revision (progname, REVISION);
                        exit (STATE_OK);
                case 'h':                                                                       /* help */
                        print_help ();
@@ -490,7 +434,22 @@ call_getopt (int argc, char **argv)
                        usage ("Invalid argument\n");
                }
        }
-       return i;
+
+       c = optind;
+       if (address == NULL) {
+               if (is_host (argv[c])) {
+                       address = argv[c++];
+               }
+               else {
+                       usage ("Invalid host name");
+               }
+       }
+       
+       if (argv[c] != NULL ) {
+               community = argv[c];
+       }
+
+       return validate_arguments ();
 }
 
 
@@ -510,21 +469,21 @@ validate_arguments (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 tests the STATUS of an HP printer with a JetDirect card.\n"
-                "Ucd-snmp must be installed on the computer running the plugin.\n\n");
+                "Net-snmp must be installed on the computer running the plugin.\n\n");
        print_usage ();
        printf
                ("\nOptions:\n"
                 " -H, --hostname=STRING or IPADDRESS\n"
                 "   Check server on the indicated host\n"
                 " -C, --community=STRING\n"
-                "   The SNMP community name\n"
+                "   The SNMP community name (default=%s)\n"
                 " -h, --help\n"
                 "   Print detailed help screen\n"
-                " -V, --version\n" "   Print version information\n\n");
+                " -V, --version\n" "   Print version information\n\n",DEFAULT_COMMUNITY);
        support ();
 }
 
@@ -538,7 +497,7 @@ print_usage (void)
        printf
                ("Usage: %s -H host [-C community]\n"
                 "       %s --help\n"
-                "       %s --version\n", PROGNAME, PROGNAME, PROGNAME);
+                "       %s --version\n", progname, progname, progname);
 }