Code

Add newline after "Usage:" in --help
[nagiosplug.git] / plugins / check_hpjd.c
index 9d8fae2b6f3f1bc72670be18fe00cb92cc78a442..51a0099ac281e9272a0e079dd96df0974af56b89 100644 (file)
-/******************************************************************************
-*
-* CHECK_HPJD.C
-*
-* Program: HP printer plugin for Nagios
+/*****************************************************************************
+* 
+* Nagios check_hpjd plugin
+* 
 * License: GPL
-* Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
-*
-* Last Modified: $Date$
-*
-* Command line: CHECK_HPJD <ip_address> [community]
-*
+* Copyright (c) 2000-2007 Nagios Plugins Development Team
+* 
 * 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
+* 
+* This file contains the check_hpjd plugin
+* 
+* This plugin tests the STATUS of an HP printer with a JetDirect card.
+* Net-SNMP must be installed on the computer running the plugin.
+* 
+* 
+* 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
+* the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
-*
+* 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
-*
+* 
 * You should have received a copy of the GNU General Public License
-* along with this program; if not, write to the Free Software
-* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-*
+* along with this program.  If not, see <http://www.gnu.org/licenses/>.
+* 
+* 
 *****************************************************************************/
 
+const char *progname = "check_hpjd";
+const char *copyright = "2000-2007";
+const char *email = "nagiosplug-devel@lists.sourceforge.net";
+
 #include "common.h"
 #include "popen.h"
 #include "utils.h"
+#include "netutils.h"
 
-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 HPJD_INTERVENTION_REQUIRED     ".1.3.6.1.4.1.11.2.3.9.1.1.2.3"
-#define HPJD_GD_PERIPHERAL_ERROR       ".1.3.6.1.4.1.11.2.3.9.1.1.2.6"
-#define HPJD_GD_PAPER_JAM              ".1.3.6.1.4.1.11.2.3.9.1.1.2.8"
-#define HPJD_GD_PAPER_OUT              ".1.3.6.1.4.1.11.2.3.9.1.1.2.9"
-#define HPJD_GD_TONER_LOW              ".1.3.6.1.4.1.11.2.3.9.1.1.2.10"
-#define HPJD_GD_PAGE_PUNT              ".1.3.6.1.4.1.11.2.3.9.1.1.2.11"
-#define HPJD_GD_MEMORY_OUT             ".1.3.6.1.4.1.11.2.3.9.1.1.2.12"
-#define HPJD_GD_DOOR_OPEN              ".1.3.6.1.4.1.11.2.3.9.1.1.2.17"
-#define HPJD_GD_PAPER_OUTPUT           ".1.3.6.1.4.1.11.2.3.9.1.1.2.19"
-#define HPJD_GD_STATUS_DISPLAY         ".1.3.6.1.4.1.11.2.3.9.1.1.3"
+#define DEFAULT_COMMUNITY "public"
+
+
+const char *option_summary = "-H host [-C community]\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 HPJD_INTERVENTION_REQUIRED ".1.3.6.1.4.1.11.2.3.9.1.1.2.3"
+#define HPJD_GD_PERIPHERAL_ERROR   ".1.3.6.1.4.1.11.2.3.9.1.1.2.6"
+#define HPJD_GD_PAPER_OUT          ".1.3.6.1.4.1.11.2.3.9.1.1.2.8"
+#define HPJD_GD_PAPER_JAM          ".1.3.6.1.4.1.11.2.3.9.1.1.2.9"
+#define HPJD_GD_TONER_LOW          ".1.3.6.1.4.1.11.2.3.9.1.1.2.10"
+#define HPJD_GD_PAGE_PUNT          ".1.3.6.1.4.1.11.2.3.9.1.1.2.11"
+#define HPJD_GD_MEMORY_OUT         ".1.3.6.1.4.1.11.2.3.9.1.1.2.12"
+#define HPJD_GD_DOOR_OPEN          ".1.3.6.1.4.1.11.2.3.9.1.1.2.17"
+#define HPJD_GD_PAPER_OUTPUT       ".1.3.6.1.4.1.11.2.3.9.1.1.2.19"
+#define HPJD_GD_STATUS_DISPLAY     ".1.3.6.1.4.1.11.2.3.9.1.1.3"
 
 #define ONLINE         0
 #define OFFLINE                1
 
-#define DEFAULT_COMMUNITY "public"
-
 int process_arguments (int, char **);
 int validate_arguments (void);
 void print_help (void);
 void print_usage (void);
 
-char *community = DEFAULT_COMMUNITY;
+char *community = NULL;
 char *address = NULL;
 
-
 int
 main (int argc, char **argv)
 {
        char command_line[1024];
-       int result;
+       int result = STATE_UNKNOWN;
        int line;
        char input_buffer[MAX_INPUT_BUFFER];
        char query_string[512];
-       char error_message[MAX_INPUT_BUFFER];
+       char *errmsg;
        char *temp_buffer;
        int line_status = ONLINE;
        int paper_status = 0;
@@ -119,8 +90,17 @@ main (int argc, char **argv)
        int paper_output = 0;
        char display_message[MAX_INPUT_BUFFER];
 
-       if (process_arguments (argc, argv) != OK)
-               usage ("Invalid command arguments supplied\n");
+       errmsg = malloc(MAX_INPUT_BUFFER);
+
+       setlocale (LC_ALL, "");
+       bindtextdomain (PACKAGE, LOCALEDIR);
+       textdomain (PACKAGE);
+
+       /* Parse extra opts if any */
+       argv=np_extra_opts (&argc, argv, progname);
+
+       if (process_arguments (argc, argv) == ERROR)
+               usage4 (_("Could not parse arguments"));
 
        /* removed ' 2>1' at end of command 10/27/1999 - EG */
        /* create the query string */
@@ -139,19 +119,19 @@ 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 -OQa -m : -v 1 -c %s %s %s", PATH_TO_SNMPGET, community,
+                                                                       address, query_string);
 
        /* run the command */
        child_process = spopen (command_line);
        if (child_process == NULL) {
-               printf ("Could not open pipe: %s\n", command_line);
+               printf (_("Could not open pipe: %s\n"), command_line);
                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", command_line);
+               printf (_("Could not open stderr for %s\n"), command_line);
        }
 
        result = STATE_OK;
@@ -168,118 +148,56 @@ main (int argc, char **argv)
                temp_buffer = strtok (input_buffer, "=");
                temp_buffer = strtok (NULL, "=");
 
-               switch (line) {
+               if (temp_buffer == NULL && line < 13) {
 
-               case 1:                                                                         /* 1st line should contain the line status */
-                       if (temp_buffer != NULL)
-                               line_status = atoi (temp_buffer);
-                       else {
                                result = STATE_UNKNOWN;
-                               strcpy (error_message, input_buffer);
-                       }
-                       break;
+                               strcpy (errmsg, input_buffer);
 
-               case 2:                                                                         /* 2nd line should contain the paper status */
-                       if (temp_buffer != NULL)
-                               paper_status = atoi (temp_buffer);
-                       else {
-                               result = STATE_UNKNOWN;
-                               strcpy (error_message, input_buffer);
-                       }
-                       break;
+               } else {
 
-               case 3:                                                                         /* 3rd line should be intervention required */
-                       if (temp_buffer != NULL)
-                               intervention_required = atoi (temp_buffer);
-                       else {
-                               result = STATE_UNKNOWN;
-                               strcpy (error_message, input_buffer);
-                       }
-                       break;
+                       switch (line) {
 
-               case 4:                                                                         /* 4th line should be peripheral error */
-                       if (temp_buffer != NULL)
+                       case 1:                                                                         /* 1st line should contain the line status */
+                               line_status = atoi (temp_buffer);
+                               break;
+                       case 2:                                                                         /* 2nd line should contain the paper status */
+                               paper_status = atoi (temp_buffer);
+                               break;
+                       case 3:                                                                         /* 3rd line should be intervention required */
+                               intervention_required = atoi (temp_buffer);
+                               break;
+                       case 4:                                                                         /* 4th line should be peripheral error */
                                peripheral_error = atoi (temp_buffer);
-                       else {
-                               result = STATE_UNKNOWN;
-                               strcpy (error_message, input_buffer);
-                       }
-                       break;
-
-               case 5:                                                                         /* 5th line should contain the paper jam status */
-                       if (temp_buffer != NULL)
+                               break;
+                       case 5:                                                                         /* 5th line should contain the paper jam status */
                                paper_jam = atoi (temp_buffer);
-                       else {
-                               result = STATE_UNKNOWN;
-                               strcpy (error_message, input_buffer);
-                       }
-                       break;
-
-               case 6:                                                                         /* 6th line should contain the paper out status */
-                       if (temp_buffer != NULL)
+                               break;
+                       case 6:                                                                         /* 6th line should contain the paper out status */
                                paper_out = atoi (temp_buffer);
-                       else {
-                               result = STATE_UNKNOWN;
-                               strcpy (error_message, input_buffer);
-                       }
-                       break;
-
-               case 7:                                                                         /* 7th line should contain the toner low status */
-                       if (temp_buffer != NULL)
+                               break;
+                       case 7:                                                                         /* 7th line should contain the toner low status */
                                toner_low = atoi (temp_buffer);
-                       else {
-                               result = STATE_UNKNOWN;
-                               strcpy (error_message, input_buffer);
-                       }
-                       break;
-
-               case 8:                                                                         /* did data come too slow for engine */
-                       if (temp_buffer != NULL)
+                               break;
+                       case 8:                                                                         /* did data come too slow for engine */
                                page_punt = atoi (temp_buffer);
-                       else {
-                               result = STATE_UNKNOWN;
-                               strcpy (error_message, input_buffer);
-                       }
-                       break;
-
-               case 9:                                                                         /* did we run out of memory */
-                       if (temp_buffer != NULL)
+                               break;
+                       case 9:                                                                         /* did we run out of memory */
                                memory_out = atoi (temp_buffer);
-                       else {
-                               result = STATE_UNKNOWN;
-                               strcpy (error_message, input_buffer);
-                       }
-                       break;
-
-               case 10:                                                                                /* is there a door open */
-                       if (temp_buffer != NULL)
+                               break;
+                       case 10:                                                                                /* is there a door open */
                                door_open = atoi (temp_buffer);
-                       else {
-                               result = STATE_UNKNOWN;
-                               strcpy (error_message, input_buffer);
-                       }
-                       break;
-
-               case 11:                                                                                /* is output tray full */
-                       if (temp_buffer != NULL)
+                               break;
+                       case 11:                                                                                /* is output tray full */
                                paper_output = atoi (temp_buffer);
-                       else {
-                               result = STATE_UNKNOWN;
-                               strcpy (error_message, input_buffer);
-                       }
-                       break;
-
-               case 12:                                                                                /* display panel message */
-                       if (temp_buffer != NULL)
+                               break;
+                       case 12:                                                                                /* display panel message */
                                strcpy (display_message, temp_buffer + 1);
-                       else {
-                               result = STATE_UNKNOWN;
-                               strcpy (error_message, input_buffer);
+                               break;
+                       default:                                                                                /* fold multiline message */
+                               strncat (display_message, input_buffer,
+                                               sizeof (display_message) - strlen (display_message) - 1);
                        }
-                       break;
 
-               default:
-                       break;
                }
 
                /* break out of the read loop if we encounter an error */
@@ -288,8 +206,14 @@ 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 (errmsg, "%s", input_buffer );
+
+       }
 
        /* close stderr */
        (void) fclose (child_stderr);
@@ -301,14 +225,10 @@ 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 ;
+               asprintf (&errmsg, "%s : Timeout from host %s\n", errmsg, address );
 
-               /* might not be the problem, but most likely is.. */
-               result = STATE_UNKNOWN;
-               sprintf (error_message, "Timeout: No response from %s\n", address);
        }
 
        /* if we had no read errors, check the printer status results... */
@@ -316,82 +236,79 @@ main (int argc, char **argv)
 
                if (paper_jam) {
                        result = STATE_WARNING;
-                       strcpy (error_message, "Paper Jam");
+                       strcpy (errmsg, _("Paper Jam"));
                }
                else if (paper_out) {
                        result = STATE_WARNING;
-                       strcpy (error_message, "Out of Paper");
+                       strcpy (errmsg, _("Out of Paper"));
                }
                else if (line_status == OFFLINE) {
-                       if (strcmp (error_message, "POWERSAVE ON") != 0) {
+                       if (strcmp (errmsg, "POWERSAVE ON") != 0) {
                                result = STATE_WARNING;
-                               strcpy (error_message, "Printer Offline");
+                               strcpy (errmsg, _("Printer Offline"));
                        }
                }
                else if (peripheral_error) {
                        result = STATE_WARNING;
-                       strcpy (error_message, "Peripheral Error");
+                       strcpy (errmsg, _("Peripheral Error"));
                }
                else if (intervention_required) {
                        result = STATE_WARNING;
-                       strcpy (error_message, "Intervention Required");
+                       strcpy (errmsg, _("Intervention Required"));
                }
                else if (toner_low) {
                        result = STATE_WARNING;
-                       strcpy (error_message, "Toner Low");
+                       strcpy (errmsg, _("Toner Low"));
                }
                else if (memory_out) {
                        result = STATE_WARNING;
-                       strcpy (error_message, "Insufficient Memory");
+                       strcpy (errmsg, _("Insufficient Memory"));
                }
                else if (door_open) {
                        result = STATE_WARNING;
-                       strcpy (error_message, "A Door is Open");
+                       strcpy (errmsg, _("A Door is Open"));
                }
                else if (paper_output) {
                        result = STATE_WARNING;
-                       strcpy (error_message, "Output Tray is Full");
+                       strcpy (errmsg, _("Output Tray is Full"));
                }
                else if (page_punt) {
                        result = STATE_WARNING;
-                       strcpy (error_message, "Data too Slow for Engine");
+                       strcpy (errmsg, _("Data too Slow for Engine"));
                }
                else if (paper_status) {
                        result = STATE_WARNING;
-                       strcpy (error_message, "Unknown Paper Error");
+                       strcpy (errmsg, _("Unknown Paper Error"));
                }
        }
 
        if (result == STATE_OK)
-               printf ("Printer ok - (%s)\n", display_message);
+               printf (_("Printer ok - (%s)\n"), display_message);
 
        else if (result == STATE_UNKNOWN) {
 
-               printf ("%s\n", error_message);
+               printf ("%s\n", errmsg);
 
                /* if printer could not be reached, escalate to critical */
-               if (strstr (error_message, "Timeout"))
+               if (strstr (errmsg, "Timeout"))
                        result = STATE_CRITICAL;
        }
 
        else if (result == STATE_WARNING)
-               printf ("%s (%s)\n", error_message, display_message);
+               printf ("%s (%s)\n", errmsg, display_message);
 
        return result;
 }
 
 
-
-
-
 /* process command-line arguments */
 int
 process_arguments (int argc, char **argv)
 {
        int c;
 
-       int option_index = 0;
-       static struct option long_options[] = {
+       int option = 0;
+       static struct option longopts[] = {
                {"hostname", required_argument, 0, 'H'},
                {"community", required_argument, 0, 'C'},
 /*             {"critical",       required_argument,0,'c'}, */
@@ -405,9 +322,9 @@ process_arguments (int argc, char **argv)
        if (argc < 2)
                return ERROR;
 
-       
+
        while (1) {
-               c = getopt_long (argc, argv, "+hVH:C:", long_options, &option_index);
+               c = getopt_long (argc, argv, "+hVH:C:", longopts, &option);
 
                if (c == -1 || c == EOF || c == 1)
                        break;
@@ -418,20 +335,20 @@ process_arguments (int argc, char **argv)
                                address = strscpy(address, optarg) ;
                        }
                        else {
-                               usage ("Invalid host name\n");
+                               usage2 (_("Invalid hostname/address"), optarg);
                        }
                        break;
                case 'C':                                                                       /* community */
                        community = strscpy (community, optarg);
                        break;
                case 'V':                                                                       /* version */
-                       print_revision (progname, REVISION);
+                       print_revision (progname, NP_VERSION);
                        exit (STATE_OK);
                case 'h':                                                                       /* help */
                        print_help ();
                        exit (STATE_OK);
                case '?':                                                                       /* help */
-                       usage ("Invalid argument\n");
+                       usage5 ();
                }
        }
 
@@ -441,21 +358,21 @@ process_arguments (int argc, char **argv)
                        address = argv[c++];
                }
                else {
-                       usage ("Invalid host name");
+                       usage2 (_("Invalid hostname/address"), argv[c]);
                }
        }
-       
-       if (argv[c] != NULL ) {
-               community = argv[c];
+
+       if (community == NULL) {
+               if (argv[c] != NULL )
+                       community = argv[c];
+               else
+                       community = strdup (DEFAULT_COMMUNITY);
        }
 
        return validate_arguments ();
 }
 
 
-
-
-
 int
 validate_arguments (void)
 {
@@ -463,68 +380,37 @@ 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, NP_VERSION);
+
+       printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
+       printf (COPYRIGHT, copyright, email);
+
+       printf ("%s\n", _("This plugin tests the STATUS of an HP printer with a JetDirect card."));
+       printf ("%s\n", _("Net-snmp must be installed on the computer running the plugin."));
+
+       printf ("\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 (default=%s)\n"
-                " -h, --help\n"
-                "   Print detailed help screen\n"
-                " -V, --version\n" "   Print version information\n\n",DEFAULT_COMMUNITY);
-       support ();
-}
 
+       printf (UT_HELP_VRSN);
+       printf (UT_EXTRA_OPTS);
 
+       printf (" %s\n", "-C, --community=STRING");
+       printf ("    %s", _("The SNMP community name "));
+       printf (_("(default=%s)"), DEFAULT_COMMUNITY);
+       printf ("\n");
+
+       printf (UT_SUPPORT);
+}
 
 
 
 void
 print_usage (void)
 {
-       printf
-               ("Usage: %s -H host [-C community]\n"
-                "       %s --help\n"
-                "       %s --version\n", progname, progname, progname);
+  printf ("%s\n", _("Usage:"));
+       printf ("%s -H host [-C community]\n", 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");
-*/