1 /******************************************************************************
2 *
3 * CHECK_HPJD.C
4 *
5 * Program: HP printer plugin for Nagios
6 * License: GPL
7 * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
8 *
9 * Last Modified: $Date$
10 *
11 * Command line: CHECK_HPJD <ip_address> [community]
12 *
13 * Description:
14 *
15 * This plugin will attempt to check the status of an HP printer. The
16 * printer must have a JetDirect card installed and TCP/IP protocol
17 * stack enabled. This plugin has only been tested on a few printers
18 * and may not work well on all models of JetDirect cards. Multiple
19 * port JetDirect devices must have an IP address assigned to each
20 * port in order to be monitored.
21 *
22 * Dependencies:
23 *
24 * This plugin used the 'snmpget' command included with the UCD-SNMP
25 * package. If you don't have the package installed you will need to
26 * download it from http://ucd-snmp.ucdavis.edu before you can use
27 * this plugin.
28 *
29 * Return Values:
30 *
31 * UNKNOWN = The plugin could not read/process the output from the printer
32 * OK = Printer looks normal
33 * WARNING = Low toner, paper jam, intervention required, paper out, etc.
34 * CRITICAL = The printer could not be reached (it's probably turned off)
35 *
36 * Acknowledgements:
37 *
38 * The idea for the plugin (as well as some code) were taken from Jim
39 * Trocki's pinter alert script in his "mon" utility, found at
40 * http://www.kernel.org/software/mon
41 *
42 * Notes:
43 * 'JetDirect' is copyrighted by Hewlett-Packard.
44 * HP, please don't sue me... :-)
45 *
46 * License Information:
47 *
48 * This program is free software; you can redistribute it and/or modify
49 * it under the terms of the GNU General Public License as published by
50 * the Free Software Foundation; either version 2 of the License, or
51 * (at your option) any later version.
52 *
53 * This program is distributed in the hope that it will be useful,
54 * but WITHOUT ANY WARRANTY; without even the implied warranty of
55 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
56 * GNU General Public License for more details.
57 *
58 * You should have received a copy of the GNU General Public License
59 * along with this program; if not, write to the Free Software
60 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
61 *
62 *****************************************************************************/
64 #include "common.h"
65 #include "popen.h"
66 #include "utils.h"
68 const char *progname = "check_hpjd";
69 #define REVISION "$Revision$"
70 #define COPYRIGHT "2000-2002"
72 #define HPJD_LINE_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.1"
73 #define HPJD_PAPER_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.2"
74 #define HPJD_INTERVENTION_REQUIRED ".1.3.6.1.4.1.11.2.3.9.1.1.2.3"
75 #define HPJD_GD_PERIPHERAL_ERROR ".1.3.6.1.4.1.11.2.3.9.1.1.2.6"
76 #define HPJD_GD_PAPER_JAM ".1.3.6.1.4.1.11.2.3.9.1.1.2.8"
77 #define HPJD_GD_PAPER_OUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.9"
78 #define HPJD_GD_TONER_LOW ".1.3.6.1.4.1.11.2.3.9.1.1.2.10"
79 #define HPJD_GD_PAGE_PUNT ".1.3.6.1.4.1.11.2.3.9.1.1.2.11"
80 #define HPJD_GD_MEMORY_OUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.12"
81 #define HPJD_GD_DOOR_OPEN ".1.3.6.1.4.1.11.2.3.9.1.1.2.17"
82 #define HPJD_GD_PAPER_OUTPUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.19"
83 #define HPJD_GD_STATUS_DISPLAY ".1.3.6.1.4.1.11.2.3.9.1.1.3"
85 #define ONLINE 0
86 #define OFFLINE 1
88 int process_arguments (int, char **);
89 int validate_arguments (void);
90 void print_help (void);
91 void print_usage (void);
93 char *community = NULL;
94 char *address = "127.0.0.1";
97 int
98 main (int argc, char **argv)
99 {
100 char command_line[1024];
101 int result;
102 int line;
103 char input_buffer[MAX_INPUT_BUFFER];
104 char query_string[512];
105 char error_message[MAX_INPUT_BUFFER];
106 char *temp_buffer;
107 int line_status = ONLINE;
108 int paper_status = 0;
109 int intervention_required = 0;
110 int peripheral_error = 0;
111 int paper_jam = 0;
112 int paper_out = 0;
113 int toner_low = 0;
114 int page_punt = 0;
115 int memory_out = 0;
116 int door_open = 0;
117 int paper_output = 0;
118 char display_message[MAX_INPUT_BUFFER];
120 if (process_arguments (argc, argv) != OK)
121 usage ("Invalid command arguments supplied\n");
123 /* removed ' 2>1' at end of command 10/27/1999 - EG */
124 /* create the query string */
125 sprintf
126 (query_string,
127 "%s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0",
128 HPJD_LINE_STATUS,
129 HPJD_PAPER_STATUS,
130 HPJD_INTERVENTION_REQUIRED,
131 HPJD_GD_PERIPHERAL_ERROR,
132 HPJD_GD_PAPER_JAM,
133 HPJD_GD_PAPER_OUT,
134 HPJD_GD_TONER_LOW,
135 HPJD_GD_PAGE_PUNT,
136 HPJD_GD_MEMORY_OUT,
137 HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, HPJD_GD_STATUS_DISPLAY);
139 /* get the command to run */
140 sprintf (command_line, "%s -m : -v 1 %s -c %s %s", PATH_TO_SNMPGET, address,
141 community, query_string);
143 /* run the command */
144 child_process = spopen (command_line);
145 if (child_process == NULL) {
146 printf ("Could not open pipe: %s\n", command_line);
147 return STATE_UNKNOWN;
148 }
150 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
151 if (child_stderr == NULL) {
152 printf ("Could not open stderr for %s\n", command_line);
153 }
155 result = STATE_OK;
157 line = 0;
158 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
160 /* strip the newline character from the end of the input */
161 if (input_buffer[strlen (input_buffer) - 1] == '\n')
162 input_buffer[strlen (input_buffer) - 1] = 0;
164 line++;
166 temp_buffer = strtok (input_buffer, "=");
167 temp_buffer = strtok (NULL, "=");
169 switch (line) {
171 case 1: /* 1st line should contain the line status */
172 if (temp_buffer != NULL)
173 line_status = atoi (temp_buffer);
174 else {
175 result = STATE_UNKNOWN;
176 strcpy (error_message, input_buffer);
177 }
178 break;
180 case 2: /* 2nd line should contain the paper status */
181 if (temp_buffer != NULL)
182 paper_status = atoi (temp_buffer);
183 else {
184 result = STATE_UNKNOWN;
185 strcpy (error_message, input_buffer);
186 }
187 break;
189 case 3: /* 3rd line should be intervention required */
190 if (temp_buffer != NULL)
191 intervention_required = atoi (temp_buffer);
192 else {
193 result = STATE_UNKNOWN;
194 strcpy (error_message, input_buffer);
195 }
196 break;
198 case 4: /* 4th line should be peripheral error */
199 if (temp_buffer != NULL)
200 peripheral_error = atoi (temp_buffer);
201 else {
202 result = STATE_UNKNOWN;
203 strcpy (error_message, input_buffer);
204 }
205 break;
207 case 5: /* 5th line should contain the paper jam status */
208 if (temp_buffer != NULL)
209 paper_jam = atoi (temp_buffer);
210 else {
211 result = STATE_UNKNOWN;
212 strcpy (error_message, input_buffer);
213 }
214 break;
216 case 6: /* 6th line should contain the paper out status */
217 if (temp_buffer != NULL)
218 paper_out = atoi (temp_buffer);
219 else {
220 result = STATE_UNKNOWN;
221 strcpy (error_message, input_buffer);
222 }
223 break;
225 case 7: /* 7th line should contain the toner low status */
226 if (temp_buffer != NULL)
227 toner_low = atoi (temp_buffer);
228 else {
229 result = STATE_UNKNOWN;
230 strcpy (error_message, input_buffer);
231 }
232 break;
234 case 8: /* did data come too slow for engine */
235 if (temp_buffer != NULL)
236 page_punt = atoi (temp_buffer);
237 else {
238 result = STATE_UNKNOWN;
239 strcpy (error_message, input_buffer);
240 }
241 break;
243 case 9: /* did we run out of memory */
244 if (temp_buffer != NULL)
245 memory_out = atoi (temp_buffer);
246 else {
247 result = STATE_UNKNOWN;
248 strcpy (error_message, input_buffer);
249 }
250 break;
252 case 10: /* is there a door open */
253 if (temp_buffer != NULL)
254 door_open = atoi (temp_buffer);
255 else {
256 result = STATE_UNKNOWN;
257 strcpy (error_message, input_buffer);
258 }
259 break;
261 case 11: /* is output tray full */
262 if (temp_buffer != NULL)
263 paper_output = atoi (temp_buffer);
264 else {
265 result = STATE_UNKNOWN;
266 strcpy (error_message, input_buffer);
267 }
268 break;
270 case 12: /* display panel message */
271 if (temp_buffer != NULL)
272 strcpy (display_message, temp_buffer + 1);
273 else {
274 result = STATE_UNKNOWN;
275 strcpy (error_message, input_buffer);
276 }
277 break;
279 default:
280 break;
281 }
283 /* break out of the read loop if we encounter an error */
284 if (result != STATE_OK)
285 break;
286 }
288 /* WARNING if output found on stderr */
289 if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
290 result = max_state (result, STATE_WARNING);
292 /* close stderr */
293 (void) fclose (child_stderr);
295 /* close the pipe */
296 if (spclose (child_process))
297 result = max_state (result, STATE_WARNING);
299 /* if there wasn't any output, display an error */
300 if (line == 0) {
302 /*
303 result=STATE_UNKNOWN;
304 strcpy(error_message,"Error: Could not read plugin output\n");
305 */
307 /* might not be the problem, but most likely is.. */
308 result = STATE_UNKNOWN;
309 sprintf (error_message, "Timeout: No response from %s\n", address);
310 }
312 /* if we had no read errors, check the printer status results... */
313 if (result == STATE_OK) {
315 if (paper_jam) {
316 result = STATE_WARNING;
317 strcpy (error_message, "Paper Jam");
318 }
319 else if (paper_out) {
320 result = STATE_WARNING;
321 strcpy (error_message, "Out of Paper");
322 }
323 else if (line_status == OFFLINE) {
324 if (strcmp (error_message, "POWERSAVE ON") != 0) {
325 result = STATE_WARNING;
326 strcpy (error_message, "Printer Offline");
327 }
328 }
329 else if (peripheral_error) {
330 result = STATE_WARNING;
331 strcpy (error_message, "Peripheral Error");
332 }
333 else if (intervention_required) {
334 result = STATE_WARNING;
335 strcpy (error_message, "Intervention Required");
336 }
337 else if (toner_low) {
338 result = STATE_WARNING;
339 strcpy (error_message, "Toner Low");
340 }
341 else if (memory_out) {
342 result = STATE_WARNING;
343 strcpy (error_message, "Insufficient Memory");
344 }
345 else if (door_open) {
346 result = STATE_WARNING;
347 strcpy (error_message, "A Door is Open");
348 }
349 else if (paper_output) {
350 result = STATE_WARNING;
351 strcpy (error_message, "Output Tray is Full");
352 }
353 else if (page_punt) {
354 result = STATE_WARNING;
355 strcpy (error_message, "Data too Slow for Engine");
356 }
357 else if (paper_status) {
358 result = STATE_WARNING;
359 strcpy (error_message, "Unknown Paper Error");
360 }
361 }
363 if (result == STATE_OK)
364 printf ("Printer ok - (%s)\n", display_message);
366 else if (result == STATE_UNKNOWN) {
368 printf ("%s\n", error_message);
370 /* if printer could not be reached, escalate to critical */
371 if (strstr (error_message, "Timeout"))
372 result = STATE_CRITICAL;
373 }
375 else if (result == STATE_WARNING)
376 printf ("%s (%s)\n", error_message, display_message);
378 return result;
379 }
385 /* process command-line arguments */
386 int
387 process_arguments (int argc, char **argv)
388 {
389 int c;
391 #ifdef HAVE_GETOPT_H
392 int option_index = 0;
393 static struct option long_options[] = {
394 {"hostname", required_argument, 0, 'H'},
395 {"expect", required_argument, 0, 'e'},
396 /* {"critical", required_argument,0,'c'}, */
397 /* {"warning", required_argument,0,'w'}, */
398 /* {"port", required_argument,0,'P'}, */
399 {"verbose", no_argument, 0, 'v'},
400 {"version", no_argument, 0, 'V'},
401 {"help", no_argument, 0, 'h'},
402 {0, 0, 0, 0}
403 };
404 #endif
406 if (argc < 2)
407 return ERROR;
409 for (c = 1; c < argc; c++) {
410 if (strcmp ("-to", argv[c]) == 0)
411 strcpy (argv[c], "-t");
412 else if (strcmp ("-wt", argv[c]) == 0)
413 strcpy (argv[c], "-w");
414 else if (strcmp ("-ct", argv[c]) == 0)
415 strcpy (argv[c], "-c");
416 }
418 while (1) {
419 #ifdef HAVE_GETOPT_H
420 c = getopt_long (argc, argv, "+hVH:C:", long_options, &option_index);
421 #else
422 c = getopt (argc, argv, "+?hVH:C:");
423 #endif
425 if (c == -1 || c == EOF || c == 1)
426 break;
428 switch (c) {
429 case 'H': /* hostname */
430 if (is_host (optarg)) {
431 address = optarg;
432 }
433 else {
434 usage ("Invalid host name\n");
435 }
436 break;
437 case 'C': /* community */
438 community = optarg;
439 break;
440 case 'V': /* version */
441 print_revision (progname, REVISION);
442 exit (STATE_OK);
443 case 'h': /* help */
444 print_help ();
445 exit (STATE_OK);
446 case '?': /* help */
447 usage ("Invalid argument\n");
448 }
449 }
451 c = optind;
452 if (address == NULL) {
453 if (is_host (argv[c])) {
454 address = argv[c++];
455 }
456 else {
457 usage ("Invalid host name");
458 }
459 }
461 if (community == NULL) {
462 community = argv[c++];
463 }
465 return validate_arguments ();
466 }
472 int
473 validate_arguments (void)
474 {
475 return OK;
476 }
482 void
483 print_help (void)
484 {
485 print_revision (progname, REVISION);
486 printf
487 ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n"
488 "This plugin tests the STATUS of an HP printer with a JetDirect card.\n"
489 "Net-snmp must be installed on the computer running the plugin.\n\n");
490 print_usage ();
491 printf
492 ("\nOptions:\n"
493 " -H, --hostname=STRING or IPADDRESS\n"
494 " Check server on the indicated host\n"
495 " -C, --community=STRING\n"
496 " The SNMP community name\n"
497 " -h, --help\n"
498 " Print detailed help screen\n"
499 " -V, --version\n" " Print version information\n\n");
500 support ();
501 }
507 void
508 print_usage (void)
509 {
510 printf
511 ("Usage: %s -H host [-C community]\n"
512 " %s --help\n"
513 " %s --version\n", progname, progname, progname);
514 }
517 /*
518 if(argc<2||argc>3){
519 printf("Incorrect number of arguments supplied\n");
520 printf("\n");
521 print_revision(argv[0],"$Revision$");
522 printf("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n");
523 printf("License: GPL\n");
524 printf("\n");
525 printf("Usage: %s <ip_address> [community]\n",argv[0]);
526 printf("\n");
527 printf("Note:\n");
528 printf(" <ip_address> = The IP address of the JetDirect card\n");
529 printf(" [community] = An optional community string used for SNMP communication\n");
530 printf(" with the JetDirect card. The default is 'public'.\n");
531 printf("\n");
532 return STATE_UNKNOWN;
533 }
535 // get the IP address of the JetDirect device
536 strcpy(address,argv[1]);
538 // get the community name to use for SNMP communication
539 if(argc>=3)
540 strcpy(community,argv[2]);
541 else
542 strcpy(community,"public");
543 */