Code

add -OQa to command_line in check_hpjd.c. Correct nagiosplug-Bugs-889948, 846329
[nagiosplug.git] / plugins / check_hpjd.c
1 /******************************************************************************
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 *****************************************************************************/
19 #include "common.h"
20 #include "popen.h"
21 #include "utils.h"
22 #include "netutils.h"
24 #define DEFAULT_COMMUNITY "public"
26 const char *progname = "check_hpjd";
27 const char *revision = "$Revision$";
28 const char *copyright = "2000-2003";
29 const char *email = "nagiosplug-devel@lists.sourceforge.net";
31 const char *option_summary = "-H host [-C community]\n";
33 #define HPJD_LINE_STATUS           ".1.3.6.1.4.1.11.2.3.9.1.1.2.1"
34 #define HPJD_PAPER_STATUS          ".1.3.6.1.4.1.11.2.3.9.1.1.2.2"
35 #define HPJD_INTERVENTION_REQUIRED ".1.3.6.1.4.1.11.2.3.9.1.1.2.3"
36 #define HPJD_GD_PERIPHERAL_ERROR   ".1.3.6.1.4.1.11.2.3.9.1.1.2.6"
37 #define HPJD_GD_PAPER_JAM          ".1.3.6.1.4.1.11.2.3.9.1.1.2.8"
38 #define HPJD_GD_PAPER_OUT          ".1.3.6.1.4.1.11.2.3.9.1.1.2.9"
39 #define HPJD_GD_TONER_LOW          ".1.3.6.1.4.1.11.2.3.9.1.1.2.10"
40 #define HPJD_GD_PAGE_PUNT          ".1.3.6.1.4.1.11.2.3.9.1.1.2.11"
41 #define HPJD_GD_MEMORY_OUT         ".1.3.6.1.4.1.11.2.3.9.1.1.2.12"
42 #define HPJD_GD_DOOR_OPEN          ".1.3.6.1.4.1.11.2.3.9.1.1.2.17"
43 #define HPJD_GD_PAPER_OUTPUT       ".1.3.6.1.4.1.11.2.3.9.1.1.2.19"
44 #define HPJD_GD_STATUS_DISPLAY     ".1.3.6.1.4.1.11.2.3.9.1.1.3"
46 #define ONLINE          0
47 #define OFFLINE         1
49 int process_arguments (int, char **);
50 int validate_arguments (void);
51 void print_help (void);
52 void print_usage (void);
54 char *community = NULL;
55 char *address = NULL;
57 int
58 main (int argc, char **argv)
59 {
60         char command_line[1024];
61         int result;
62         int line;
63         char input_buffer[MAX_INPUT_BUFFER];
64         char query_string[512];
65         char *errmsg;
66         char *temp_buffer;
67         int line_status = ONLINE;
68         int paper_status = 0;
69         int intervention_required = 0;
70         int peripheral_error = 0;
71         int paper_jam = 0;
72         int paper_out = 0;
73         int toner_low = 0;
74         int page_punt = 0;
75         int memory_out = 0;
76         int door_open = 0;
77         int paper_output = 0;
78         char display_message[MAX_INPUT_BUFFER];
80         errmsg = malloc(MAX_INPUT_BUFFER);
82         setlocale (LC_ALL, "");
83         bindtextdomain (PACKAGE, LOCALEDIR);
84         textdomain (PACKAGE);
86         if (process_arguments (argc, argv) != OK)
87                 usage (_("Incorrect arguments supplied\n"));
89         /* removed ' 2>1' at end of command 10/27/1999 - EG */
90         /* create the query string */
91         sprintf
92                 (query_string,
93                  "%s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0",
94                  HPJD_LINE_STATUS,
95                  HPJD_PAPER_STATUS,
96                  HPJD_INTERVENTION_REQUIRED,
97                  HPJD_GD_PERIPHERAL_ERROR,
98                  HPJD_GD_PAPER_JAM,
99                  HPJD_GD_PAPER_OUT,
100                  HPJD_GD_TONER_LOW,
101                  HPJD_GD_PAGE_PUNT,
102                  HPJD_GD_MEMORY_OUT,
103                  HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, HPJD_GD_STATUS_DISPLAY);
105         /* get the command to run */
106         sprintf (command_line, "%s -OQa -m : -v 1 -c %s %s %s", PATH_TO_SNMPGET, community, 
107                                                                         address, query_string);
109         /* run the command */
110         child_process = spopen (command_line);
111         if (child_process == NULL) {
112                 printf (_("Could not open pipe: %s\n"), command_line);
113                 return STATE_UNKNOWN;
114         }
116         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
117         if (child_stderr == NULL) {
118                 printf (_("Could not open stderr for %s\n"), command_line);
119         }
121         result = STATE_OK;
123         line = 0;
124         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
126                 /* strip the newline character from the end of the input */
127                 if (input_buffer[strlen (input_buffer) - 1] == '\n')
128                         input_buffer[strlen (input_buffer) - 1] = 0;
130                 line++;
132                 temp_buffer = strtok (input_buffer, "=");
133                 temp_buffer = strtok (NULL, "=");
135                 if (temp_buffer == NULL) {
137                                 result = STATE_UNKNOWN;
138                                 strcpy (errmsg, input_buffer);
140                 } else {
142                         switch (line) {
144                         case 1:                                                                         /* 1st line should contain the line status */
145                                 line_status = atoi (temp_buffer);
146                                 break;
147                         case 2:                                                                         /* 2nd line should contain the paper status */
148                                 paper_status = atoi (temp_buffer);
149                                 break;
150                         case 3:                                                                         /* 3rd line should be intervention required */
151                                 intervention_required = atoi (temp_buffer);
152                                 break;
153                         case 4:                                                                         /* 4th line should be peripheral error */
154                                 peripheral_error = atoi (temp_buffer);
155                                 break;
156                         case 5:                                                                         /* 5th line should contain the paper jam status */
157                                 paper_jam = atoi (temp_buffer);
158                                 break;
159                         case 6:                                                                         /* 6th line should contain the paper out status */
160                                 paper_out = atoi (temp_buffer);
161                                 break;
162                         case 7:                                                                         /* 7th line should contain the toner low status */
163                                 toner_low = atoi (temp_buffer);
164                                 break;
165                         case 8:                                                                         /* did data come too slow for engine */
166                                 page_punt = atoi (temp_buffer);
167                                 break;
168                         case 9:                                                                         /* did we run out of memory */
169                                 memory_out = atoi (temp_buffer);
170                                 break;
171                         case 10:                                                                                /* is there a door open */
172                                 door_open = atoi (temp_buffer);
173                                 break;
174                         case 11:                                                                                /* is output tray full */
175                                 paper_output = atoi (temp_buffer);
176                                 break;
177                         case 12:                                                                                /* display panel message */
178                                 strcpy (display_message, temp_buffer + 1);
179                                 break;
180                         default:
181                                 break;
182                         }
184                 }
186                 /* break out of the read loop if we encounter an error */
187                 if (result != STATE_OK)
188                         break;
189         }
191         /* WARNING if output found on stderr */
192         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
193                 result = max_state (result, STATE_WARNING);
194                 /* remove CRLF */
195                 if (input_buffer[strlen (input_buffer) - 1] == '\n')
196                         input_buffer[strlen (input_buffer) - 1] = 0;
197                 sprintf (errmsg, "%s", input_buffer );
199         }
200         
201         /* close stderr */
202         (void) fclose (child_stderr);
204         /* close the pipe */
205         if (spclose (child_process))
206                 result = max_state (result, STATE_WARNING);
208         /* if there wasn't any output, display an error */
209         if (line == 0) {
211                 /* might not be the problem, but most likely is. */
212                 result = STATE_UNKNOWN ;
213                 asprintf (&errmsg, "%s : Timeout from host %s\n", errmsg, address );
214                  
215         }
217         /* if we had no read errors, check the printer status results... */
218         if (result == STATE_OK) {
220                 if (paper_jam) {
221                         result = STATE_WARNING;
222                         strcpy (errmsg, _("Paper Jam"));
223                 }
224                 else if (paper_out) {
225                         result = STATE_WARNING;
226                         strcpy (errmsg, _("Out of Paper"));
227                 }
228                 else if (line_status == OFFLINE) {
229                         if (strcmp (errmsg, "POWERSAVE ON") != 0) {
230                                 result = STATE_WARNING;
231                                 strcpy (errmsg, _("Printer Offline"));
232                         }
233                 }
234                 else if (peripheral_error) {
235                         result = STATE_WARNING;
236                         strcpy (errmsg, _("Peripheral Error"));
237                 }
238                 else if (intervention_required) {
239                         result = STATE_WARNING;
240                         strcpy (errmsg, _("Intervention Required"));
241                 }
242                 else if (toner_low) {
243                         result = STATE_WARNING;
244                         strcpy (errmsg, _("Toner Low"));
245                 }
246                 else if (memory_out) {
247                         result = STATE_WARNING;
248                         strcpy (errmsg, _("Insufficient Memory"));
249                 }
250                 else if (door_open) {
251                         result = STATE_WARNING;
252                         strcpy (errmsg, _("A Door is Open"));
253                 }
254                 else if (paper_output) {
255                         result = STATE_WARNING;
256                         strcpy (errmsg, _("Output Tray is Full"));
257                 }
258                 else if (page_punt) {
259                         result = STATE_WARNING;
260                         strcpy (errmsg, _("Data too Slow for Engine"));
261                 }
262                 else if (paper_status) {
263                         result = STATE_WARNING;
264                         strcpy (errmsg, _("Unknown Paper Error"));
265                 }
266         }
268         if (result == STATE_OK)
269                 printf (_("Printer ok - (%s)\n"), display_message);
271         else if (result == STATE_UNKNOWN) {
273                 printf ("%s\n", errmsg);
275                 /* if printer could not be reached, escalate to critical */
276                 if (strstr (errmsg, "Timeout"))
277                         result = STATE_CRITICAL;
278         }
280         else if (result == STATE_WARNING)
281                 printf ("%s (%s)\n", errmsg, display_message);
283         return result;
290 /* process command-line arguments */
291 int
292 process_arguments (int argc, char **argv)
294         int c;
296         int option = 0;
297         static struct option longopts[] = {
298                 {"hostname", required_argument, 0, 'H'},
299                 {"community", required_argument, 0, 'C'},
300 /*              {"critical",       required_argument,0,'c'}, */
301 /*              {"warning",        required_argument,0,'w'}, */
302 /*              {"port",           required_argument,0,'P'}, */
303                 {"version", no_argument, 0, 'V'},
304                 {"help", no_argument, 0, 'h'},
305                 {0, 0, 0, 0}
306         };
308         if (argc < 2)
309                 return ERROR;
311         
312         while (1) {
313                 c = getopt_long (argc, argv, "+hVH:C:", longopts, &option);
315                 if (c == -1 || c == EOF || c == 1)
316                         break;
318                 switch (c) {
319                 case 'H':                                                                       /* hostname */
320                         if (is_host (optarg)) {
321                                 address = strscpy(address, optarg) ;
322                         }
323                         else {
324                                 usage2 (_("Invalid host name"), optarg);
325                         }
326                         break;
327                 case 'C':                                                                       /* community */
328                         community = strscpy (community, optarg);
329                         break;
330                 case 'V':                                                                       /* version */
331                         print_revision (progname, revision);
332                         exit (STATE_OK);
333                 case 'h':                                                                       /* help */
334                         print_help ();
335                         exit (STATE_OK);
336                 case '?':                                                                       /* help */
337                         usage (_("Invalid argument\n"));
338                 }
339         }
341         c = optind;
342         if (address == NULL) {
343                 if (is_host (argv[c])) {
344                         address = argv[c++];
345                 }
346                 else {
347                         usage2 (_("Invalid host name"), argv[c]);
348                 }
349         }
350         
351         if (community == NULL) {
352                 if (argv[c] != NULL )
353                         community = argv[c];
354                 else
355                         community = strdup (DEFAULT_COMMUNITY);
356         }
358         return validate_arguments ();
365 int
366 validate_arguments (void)
368         return OK;
375 \f
376 void
377 print_help (void)
379         print_revision (progname, revision);
381         printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
382         printf (COPYRIGHT, copyright, email);
384         printf (_("\
385 This plugin tests the STATUS of an HP printer with a JetDirect card.\n\
386 Net-snmp must be installed on the computer running the plugin.\n\n"));
388         print_usage ();
390         printf (_(UT_HELP_VRSN));
392         printf (_("\
393  -C, --community=STRING\n\
394     The SNMP community name (default=%s)\n"), DEFAULT_COMMUNITY);
396         printf (_(UT_SUPPORT));
402 void
403 print_usage (void)
405         printf (_("\
406 Usage: %s -H host [-C community]\n"), progname);
407         printf (_(UT_HLP_VRS), progname, progname);