Code

Added NLM test (Phil Randal)
[nagiosplug.git] / plugins / check_hpjd.c
index 6d42cd3866091f048d6601d9464fd3fb9f7f0ec0..6503d52d4defe9aef71cd5450b381c9b26e8e561 100644 (file)
@@ -1,50 +1,5 @@
 /******************************************************************************
 *
-* CHECK_HPJD.C
-*
-* Program: HP printer plugin for Nagios
-* License: GPL
-* Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
-*
-* Last Modified: $Date$
-*
-* Command line: CHECK_HPJD <ip_address> [community]
-*
-* Description:
-*
-* This plugin will attempt to check the status of an HP printer.  The
-* printer must have a JetDirect card installed and TCP/IP protocol
-* stack enabled.  This plugin has only been tested on a few printers
-* and may not work well on all models of JetDirect cards.  Multiple
-* port JetDirect devices must have an IP address assigned to each
-* port in order to be monitored.
-*
-* Dependencies:
-*
-* This plugin used the 'snmpget' command included with the UCD-SNMP
-* package.  If you don't have the package installed you will need to
-* download it from http://ucd-snmp.ucdavis.edu before you can use
-* this plugin.
-*
-* Return Values:
-*
-* UNKNOWN      = The plugin could not read/process the output from the printer
-* OK           = Printer looks normal
-* WARNING      = Low toner, paper jam, intervention required, paper out, etc.
-* CRITICAL     = The printer could not be reached (it's probably turned off)
-*
-* Acknowledgements:
-*
-* The idea for the plugin (as well as some code) were taken from Jim
-* Trocki's pinter alert script in his "mon" utility, found at
-* http://www.kernel.org/software/mon
-*
-* Notes:
-* 'JetDirect' is copyrighted by Hewlett-Packard.  
-*                    HP, please don't sue me... :-)
-*
-* License Information:
-*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 #include "popen.h"
 #include "utils.h"
 
-#define PROGNAME "check_hpjd"
+const char *progname = "check_hpjd";
+const char *revision = "$Revision$";
+const char *authors = "Nagios Plugin Development Team";
+const char *email = "nagiosplug-devel@lists.sourceforge.net";
+const char *copyright = "2000-2003";
+
+const char *summary = "\
+This plugin tests the STATUS of an HP printer with a JetDirect card.\n\
+Net-snmp must be installed on the computer running the plugin.\n\n";
+
+const char *option_summary = "-H host [-C community]\n";
+
+const char *options = "\
+ -H, --hostname=STRING or IPADDRESS\n\
+   Check server on the indicated host\n\
+ -C, --community=STRING\n\
+   The SNMP community name (default=%s)\n\
+ -h, --help\n\
+   Print detailed help screen\n\
+ -V, --version\n\
+   Print version information\n\n";
 
 #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)
 {
@@ -114,6 +91,7 @@ main (int argc, char **argv)
        int door_open = 0;
        int paper_output = 0;
        char display_message[MAX_INPUT_BUFFER];
+       char *temp ;
 
        if (process_arguments (argc, argv) != OK)
                usage ("Invalid command arguments supplied\n");
@@ -135,8 +113,8 @@ 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 -c %s %s", PATH_TO_SNMPGET, address,
-                                        community, query_string);
+       sprintf (command_line, "%s -m : -v 1 -c %s %s %s", PATH_TO_SNMPGET, community, 
+                                                                       address, query_string);
 
        /* run the command */
        child_process = spopen (command_line);
@@ -284,9 +262,15 @@ main (int argc, char **argv)
        }
 
        /* WARNING if output found on stderr */
-       if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
+       if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
                result = max_state (result, STATE_WARNING);
+               /* remove CRLF */
+               if (input_buffer[strlen (input_buffer) - 1] == '\n')
+                       input_buffer[strlen (input_buffer) - 1] = 0;
+               sprintf (error_message, "%s", input_buffer );
 
+       }
+       
        /* close stderr */
        (void) fclose (child_stderr);
 
@@ -297,14 +281,11 @@ main (int argc, char **argv)
        /* if there wasn't any output, display an error */
        if (line == 0) {
 
-               /*
-                  result=STATE_UNKNOWN;
-                  strcpy(error_message,"Error: Could not read plugin output\n");
-                */
-
-               /* might not be the problem, but most likely is.. */
-               result = STATE_UNKNOWN;
-               sprintf (error_message, "Timeout: No response from %s\n", address);
+               /* might not be the problem, but most likely is. */
+               result = STATE_UNKNOWN ;
+               asprintf (&temp, error_message);
+               sprintf (error_message, "%s : Timeout from host %s\n", temp, address );
+                
        }
 
        /* if we had no read errors, check the printer status results... */
@@ -386,102 +367,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 +411,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,62 +446,25 @@ validate_arguments (void)
 void
 print_help (void)
 {
-       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"
-                "Net-snmp must be installed on the computer running the plugin.\n\n");
+       print_revision (progname, revision);
+       printf ("Copyright (c) %s %s\n\t<%s>\n\n", copyright, authors, email);
+       printf (summary);
        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"
-                " -h, --help\n"
-                "   Print detailed help screen\n"
-                " -V, --version\n" "   Print version information\n\n");
+       printf ("\nOptions:\n");
+       printf (options, DEFAULT_COMMUNITY);
        support ();
 }
 
-
-
-
-
 void
 print_usage (void)
 {
-       printf
-               ("Usage: %s -H host [-C community]\n"
-                "       %s --help\n"
-                "       %s --version\n", PROGNAME, PROGNAME, PROGNAME);
+       printf ("\
+Usage:\n\
+ %s %s\n\
+ %s (-h | --help) for detailed help\n\
+ %s (-V | --version) for version information\n",
+               progname, option_summary, progname, progname);
 }
 
 
-/*
-       if(argc<2||argc>3){
-       printf("Incorrect number of arguments supplied\n");
-       printf("\n");
-       print_revision(argv[0],"$Revision$");
-       printf("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n");
-       printf("License: GPL\n");
-       printf("\n");
-       printf("Usage: %s <ip_address> [community]\n",argv[0]);
-       printf("\n");
-       printf("Note:\n");
-       printf(" <ip_address>     = The IP address of the JetDirect card\n");
-       printf(" [community]      = An optional community string used for SNMP communication\n");
-       printf("                    with the JetDirect card.  The default is 'public'.\n");
-       printf("\n");
-       return STATE_UNKNOWN;
-       }
 
-       // get the IP address of the JetDirect device
-       strcpy(address,argv[1]);
-       
-       // get the community name to use for SNMP communication
-       if(argc>=3)
-       strcpy(community,argv[2]);
-       else
-       strcpy(community,"public");
-*/