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"
23 const char *progname = "check_hpjd";
24 const char *revision = "$Revision$";
25 const char *authors = "Nagios Plugin Development Team";
26 const char *email = "nagiosplug-devel@lists.sourceforge.net";
27 const char *copyright = "2000-2003";
29 const char *summary = "\
30 This plugin tests the STATUS of an HP printer with a JetDirect card.\n\
31 Net-snmp must be installed on the computer running the plugin.\n\n";
33 const char *option_summary = "-H host [-C community]\n";
35 const char *options = "\
36 -H, --hostname=STRING or IPADDRESS\n\
37 Check server on the indicated host\n\
38 -C, --community=STRING\n\
39 The SNMP community name (default=%s)\n\
40 -h, --help\n\
41 Print detailed help screen\n\
42 -V, --version\n\
43 Print version information\n\n";
45 #define HPJD_LINE_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.1"
46 #define HPJD_PAPER_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.2"
47 #define HPJD_INTERVENTION_REQUIRED ".1.3.6.1.4.1.11.2.3.9.1.1.2.3"
48 #define HPJD_GD_PERIPHERAL_ERROR ".1.3.6.1.4.1.11.2.3.9.1.1.2.6"
49 #define HPJD_GD_PAPER_JAM ".1.3.6.1.4.1.11.2.3.9.1.1.2.8"
50 #define HPJD_GD_PAPER_OUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.9"
51 #define HPJD_GD_TONER_LOW ".1.3.6.1.4.1.11.2.3.9.1.1.2.10"
52 #define HPJD_GD_PAGE_PUNT ".1.3.6.1.4.1.11.2.3.9.1.1.2.11"
53 #define HPJD_GD_MEMORY_OUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.12"
54 #define HPJD_GD_DOOR_OPEN ".1.3.6.1.4.1.11.2.3.9.1.1.2.17"
55 #define HPJD_GD_PAPER_OUTPUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.19"
56 #define HPJD_GD_STATUS_DISPLAY ".1.3.6.1.4.1.11.2.3.9.1.1.3"
58 #define ONLINE 0
59 #define OFFLINE 1
61 #define DEFAULT_COMMUNITY "public"
63 int process_arguments (int, char **);
64 int validate_arguments (void);
65 void print_help (void);
66 void print_usage (void);
68 char *community = DEFAULT_COMMUNITY;
69 char *address = NULL;
72 int
73 main (int argc, char **argv)
74 {
75 char command_line[1024];
76 int result;
77 int line;
78 char input_buffer[MAX_INPUT_BUFFER];
79 char query_string[512];
80 char error_message[MAX_INPUT_BUFFER];
81 char *temp_buffer;
82 int line_status = ONLINE;
83 int paper_status = 0;
84 int intervention_required = 0;
85 int peripheral_error = 0;
86 int paper_jam = 0;
87 int paper_out = 0;
88 int toner_low = 0;
89 int page_punt = 0;
90 int memory_out = 0;
91 int door_open = 0;
92 int paper_output = 0;
93 char display_message[MAX_INPUT_BUFFER];
94 char *temp ;
96 if (process_arguments (argc, argv) != OK)
97 usage ("Invalid command arguments supplied\n");
99 /* removed ' 2>1' at end of command 10/27/1999 - EG */
100 /* create the query string */
101 sprintf
102 (query_string,
103 "%s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0",
104 HPJD_LINE_STATUS,
105 HPJD_PAPER_STATUS,
106 HPJD_INTERVENTION_REQUIRED,
107 HPJD_GD_PERIPHERAL_ERROR,
108 HPJD_GD_PAPER_JAM,
109 HPJD_GD_PAPER_OUT,
110 HPJD_GD_TONER_LOW,
111 HPJD_GD_PAGE_PUNT,
112 HPJD_GD_MEMORY_OUT,
113 HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, HPJD_GD_STATUS_DISPLAY);
115 /* get the command to run */
116 sprintf (command_line, "%s -m : -v 1 -c %s %s %s", PATH_TO_SNMPGET, community,
117 address, query_string);
119 /* run the command */
120 child_process = spopen (command_line);
121 if (child_process == NULL) {
122 printf ("Could not open pipe: %s\n", command_line);
123 return STATE_UNKNOWN;
124 }
126 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
127 if (child_stderr == NULL) {
128 printf ("Could not open stderr for %s\n", command_line);
129 }
131 result = STATE_OK;
133 line = 0;
134 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
136 /* strip the newline character from the end of the input */
137 if (input_buffer[strlen (input_buffer) - 1] == '\n')
138 input_buffer[strlen (input_buffer) - 1] = 0;
140 line++;
142 temp_buffer = strtok (input_buffer, "=");
143 temp_buffer = strtok (NULL, "=");
145 switch (line) {
147 case 1: /* 1st line should contain the line status */
148 if (temp_buffer != NULL)
149 line_status = atoi (temp_buffer);
150 else {
151 result = STATE_UNKNOWN;
152 strcpy (error_message, input_buffer);
153 }
154 break;
156 case 2: /* 2nd line should contain the paper status */
157 if (temp_buffer != NULL)
158 paper_status = atoi (temp_buffer);
159 else {
160 result = STATE_UNKNOWN;
161 strcpy (error_message, input_buffer);
162 }
163 break;
165 case 3: /* 3rd line should be intervention required */
166 if (temp_buffer != NULL)
167 intervention_required = atoi (temp_buffer);
168 else {
169 result = STATE_UNKNOWN;
170 strcpy (error_message, input_buffer);
171 }
172 break;
174 case 4: /* 4th line should be peripheral error */
175 if (temp_buffer != NULL)
176 peripheral_error = atoi (temp_buffer);
177 else {
178 result = STATE_UNKNOWN;
179 strcpy (error_message, input_buffer);
180 }
181 break;
183 case 5: /* 5th line should contain the paper jam status */
184 if (temp_buffer != NULL)
185 paper_jam = atoi (temp_buffer);
186 else {
187 result = STATE_UNKNOWN;
188 strcpy (error_message, input_buffer);
189 }
190 break;
192 case 6: /* 6th line should contain the paper out status */
193 if (temp_buffer != NULL)
194 paper_out = atoi (temp_buffer);
195 else {
196 result = STATE_UNKNOWN;
197 strcpy (error_message, input_buffer);
198 }
199 break;
201 case 7: /* 7th line should contain the toner low status */
202 if (temp_buffer != NULL)
203 toner_low = atoi (temp_buffer);
204 else {
205 result = STATE_UNKNOWN;
206 strcpy (error_message, input_buffer);
207 }
208 break;
210 case 8: /* did data come too slow for engine */
211 if (temp_buffer != NULL)
212 page_punt = atoi (temp_buffer);
213 else {
214 result = STATE_UNKNOWN;
215 strcpy (error_message, input_buffer);
216 }
217 break;
219 case 9: /* did we run out of memory */
220 if (temp_buffer != NULL)
221 memory_out = atoi (temp_buffer);
222 else {
223 result = STATE_UNKNOWN;
224 strcpy (error_message, input_buffer);
225 }
226 break;
228 case 10: /* is there a door open */
229 if (temp_buffer != NULL)
230 door_open = atoi (temp_buffer);
231 else {
232 result = STATE_UNKNOWN;
233 strcpy (error_message, input_buffer);
234 }
235 break;
237 case 11: /* is output tray full */
238 if (temp_buffer != NULL)
239 paper_output = atoi (temp_buffer);
240 else {
241 result = STATE_UNKNOWN;
242 strcpy (error_message, input_buffer);
243 }
244 break;
246 case 12: /* display panel message */
247 if (temp_buffer != NULL)
248 strcpy (display_message, temp_buffer + 1);
249 else {
250 result = STATE_UNKNOWN;
251 strcpy (error_message, input_buffer);
252 }
253 break;
255 default:
256 break;
257 }
259 /* break out of the read loop if we encounter an error */
260 if (result != STATE_OK)
261 break;
262 }
264 /* WARNING if output found on stderr */
265 if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
266 result = max_state (result, STATE_WARNING);
267 /* remove CRLF */
268 if (input_buffer[strlen (input_buffer) - 1] == '\n')
269 input_buffer[strlen (input_buffer) - 1] = 0;
270 sprintf (error_message, "%s", input_buffer );
272 }
274 /* close stderr */
275 (void) fclose (child_stderr);
277 /* close the pipe */
278 if (spclose (child_process))
279 result = max_state (result, STATE_WARNING);
281 /* if there wasn't any output, display an error */
282 if (line == 0) {
284 /* might not be the problem, but most likely is. */
285 result = STATE_UNKNOWN ;
286 asprintf (&temp, error_message);
287 sprintf (error_message, "%s : Timeout from host %s\n", temp, address );
289 }
291 /* if we had no read errors, check the printer status results... */
292 if (result == STATE_OK) {
294 if (paper_jam) {
295 result = STATE_WARNING;
296 strcpy (error_message, "Paper Jam");
297 }
298 else if (paper_out) {
299 result = STATE_WARNING;
300 strcpy (error_message, "Out of Paper");
301 }
302 else if (line_status == OFFLINE) {
303 if (strcmp (error_message, "POWERSAVE ON") != 0) {
304 result = STATE_WARNING;
305 strcpy (error_message, "Printer Offline");
306 }
307 }
308 else if (peripheral_error) {
309 result = STATE_WARNING;
310 strcpy (error_message, "Peripheral Error");
311 }
312 else if (intervention_required) {
313 result = STATE_WARNING;
314 strcpy (error_message, "Intervention Required");
315 }
316 else if (toner_low) {
317 result = STATE_WARNING;
318 strcpy (error_message, "Toner Low");
319 }
320 else if (memory_out) {
321 result = STATE_WARNING;
322 strcpy (error_message, "Insufficient Memory");
323 }
324 else if (door_open) {
325 result = STATE_WARNING;
326 strcpy (error_message, "A Door is Open");
327 }
328 else if (paper_output) {
329 result = STATE_WARNING;
330 strcpy (error_message, "Output Tray is Full");
331 }
332 else if (page_punt) {
333 result = STATE_WARNING;
334 strcpy (error_message, "Data too Slow for Engine");
335 }
336 else if (paper_status) {
337 result = STATE_WARNING;
338 strcpy (error_message, "Unknown Paper Error");
339 }
340 }
342 if (result == STATE_OK)
343 printf ("Printer ok - (%s)\n", display_message);
345 else if (result == STATE_UNKNOWN) {
347 printf ("%s\n", error_message);
349 /* if printer could not be reached, escalate to critical */
350 if (strstr (error_message, "Timeout"))
351 result = STATE_CRITICAL;
352 }
354 else if (result == STATE_WARNING)
355 printf ("%s (%s)\n", error_message, display_message);
357 return result;
358 }
364 /* process command-line arguments */
365 int
366 process_arguments (int argc, char **argv)
367 {
368 int c;
370 int option_index = 0;
371 static struct option long_options[] = {
372 {"hostname", required_argument, 0, 'H'},
373 {"community", required_argument, 0, 'C'},
374 /* {"critical", required_argument,0,'c'}, */
375 /* {"warning", required_argument,0,'w'}, */
376 /* {"port", required_argument,0,'P'}, */
377 {"version", no_argument, 0, 'V'},
378 {"help", no_argument, 0, 'h'},
379 {0, 0, 0, 0}
380 };
382 if (argc < 2)
383 return ERROR;
386 while (1) {
387 c = getopt_long (argc, argv, "+hVH:C:", long_options, &option_index);
389 if (c == -1 || c == EOF || c == 1)
390 break;
392 switch (c) {
393 case 'H': /* hostname */
394 if (is_host (optarg)) {
395 address = strscpy(address, optarg) ;
396 }
397 else {
398 usage ("Invalid host name\n");
399 }
400 break;
401 case 'C': /* community */
402 community = strscpy (community, optarg);
403 break;
404 case 'V': /* version */
405 print_revision (progname, revision);
406 exit (STATE_OK);
407 case 'h': /* help */
408 print_help ();
409 exit (STATE_OK);
410 case '?': /* help */
411 usage ("Invalid argument\n");
412 }
413 }
415 c = optind;
416 if (address == NULL) {
417 if (is_host (argv[c])) {
418 address = argv[c++];
419 }
420 else {
421 usage ("Invalid host name");
422 }
423 }
425 if (argv[c] != NULL ) {
426 community = argv[c];
427 }
429 return validate_arguments ();
430 }
436 int
437 validate_arguments (void)
438 {
439 return OK;
440 }
446 void
447 print_help (void)
448 {
449 print_revision (progname, revision);
450 printf ("Copyright (c) %s %s\n\t<%s>\n\n", copyright, authors, email);
451 printf (summary);
452 print_usage ();
453 printf ("\nOptions:\n");
454 printf (options, DEFAULT_COMMUNITY);
455 support ();
456 }
458 void
459 print_usage (void)
460 {
461 printf ("\
462 Usage:\n\
463 %s %s\n\
464 %s (-h | --help) for detailed help\n\
465 %s (-V | --version) for version information\n",
466 progname, option_summary, progname, progname);
467 }