1 /*****************************************************************************
2 *
3 * Nagios check_hpjd plugin
4 *
5 * License: GPL
6 * Copyright (c) 2000-2007 Nagios Plugins Development Team
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
11 *
12 * This file contains the check_hpjd plugin
13 *
14 * This plugin tests the STATUS of an HP printer with a JetDirect card.
15 * Net-SNMP must be installed on the computer running the plugin.
16 *
17 *
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 *
31 * $Id$
32 *
33 *****************************************************************************/
35 const char *progname = "check_hpjd";
36 const char *revision = "$Revision$";
37 const char *copyright = "2000-2007";
38 const char *email = "nagiosplug-devel@lists.sourceforge.net";
40 #include "common.h"
41 #include "popen.h"
42 #include "utils.h"
43 #include "netutils.h"
45 #define DEFAULT_COMMUNITY "public"
48 const char *option_summary = "-H host [-C community]\n";
50 #define HPJD_LINE_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.1"
51 #define HPJD_PAPER_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.2"
52 #define HPJD_INTERVENTION_REQUIRED ".1.3.6.1.4.1.11.2.3.9.1.1.2.3"
53 #define HPJD_GD_PERIPHERAL_ERROR ".1.3.6.1.4.1.11.2.3.9.1.1.2.6"
54 #define HPJD_GD_PAPER_OUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.8"
55 #define HPJD_GD_PAPER_JAM ".1.3.6.1.4.1.11.2.3.9.1.1.2.9"
56 #define HPJD_GD_TONER_LOW ".1.3.6.1.4.1.11.2.3.9.1.1.2.10"
57 #define HPJD_GD_PAGE_PUNT ".1.3.6.1.4.1.11.2.3.9.1.1.2.11"
58 #define HPJD_GD_MEMORY_OUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.12"
59 #define HPJD_GD_DOOR_OPEN ".1.3.6.1.4.1.11.2.3.9.1.1.2.17"
60 #define HPJD_GD_PAPER_OUTPUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.19"
61 #define HPJD_GD_STATUS_DISPLAY ".1.3.6.1.4.1.11.2.3.9.1.1.3"
63 #define ONLINE 0
64 #define OFFLINE 1
66 int process_arguments (int, char **);
67 int validate_arguments (void);
68 void print_help (void);
69 void print_usage (void);
71 char *community = NULL;
72 char *address = NULL;
74 int
75 main (int argc, char **argv)
76 {
77 char command_line[1024];
78 int result = STATE_UNKNOWN;
79 int line;
80 char input_buffer[MAX_INPUT_BUFFER];
81 char query_string[512];
82 char *errmsg;
83 char *temp_buffer;
84 int line_status = ONLINE;
85 int paper_status = 0;
86 int intervention_required = 0;
87 int peripheral_error = 0;
88 int paper_jam = 0;
89 int paper_out = 0;
90 int toner_low = 0;
91 int page_punt = 0;
92 int memory_out = 0;
93 int door_open = 0;
94 int paper_output = 0;
95 char display_message[MAX_INPUT_BUFFER];
97 errmsg = malloc(MAX_INPUT_BUFFER);
99 setlocale (LC_ALL, "");
100 bindtextdomain (PACKAGE, LOCALEDIR);
101 textdomain (PACKAGE);
103 /* Parse extra opts if any */
104 argv=np_extra_opts (&argc, argv, progname);
106 if (process_arguments (argc, argv) == ERROR)
107 usage4 (_("Could not parse arguments"));
109 /* removed ' 2>1' at end of command 10/27/1999 - EG */
110 /* create the query string */
111 sprintf
112 (query_string,
113 "%s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0",
114 HPJD_LINE_STATUS,
115 HPJD_PAPER_STATUS,
116 HPJD_INTERVENTION_REQUIRED,
117 HPJD_GD_PERIPHERAL_ERROR,
118 HPJD_GD_PAPER_JAM,
119 HPJD_GD_PAPER_OUT,
120 HPJD_GD_TONER_LOW,
121 HPJD_GD_PAGE_PUNT,
122 HPJD_GD_MEMORY_OUT,
123 HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, HPJD_GD_STATUS_DISPLAY);
125 /* get the command to run */
126 sprintf (command_line, "%s -OQa -m : -v 1 -c %s %s %s", PATH_TO_SNMPGET, community,
127 address, query_string);
129 /* run the command */
130 child_process = spopen (command_line);
131 if (child_process == NULL) {
132 printf (_("Could not open pipe: %s\n"), command_line);
133 return STATE_UNKNOWN;
134 }
136 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
137 if (child_stderr == NULL) {
138 printf (_("Could not open stderr for %s\n"), command_line);
139 }
141 result = STATE_OK;
143 line = 0;
144 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
146 /* strip the newline character from the end of the input */
147 if (input_buffer[strlen (input_buffer) - 1] == '\n')
148 input_buffer[strlen (input_buffer) - 1] = 0;
150 line++;
152 temp_buffer = strtok (input_buffer, "=");
153 temp_buffer = strtok (NULL, "=");
155 if (temp_buffer == NULL && line < 13) {
157 result = STATE_UNKNOWN;
158 strcpy (errmsg, input_buffer);
160 } else {
162 switch (line) {
164 case 1: /* 1st line should contain the line status */
165 line_status = atoi (temp_buffer);
166 break;
167 case 2: /* 2nd line should contain the paper status */
168 paper_status = atoi (temp_buffer);
169 break;
170 case 3: /* 3rd line should be intervention required */
171 intervention_required = atoi (temp_buffer);
172 break;
173 case 4: /* 4th line should be peripheral error */
174 peripheral_error = atoi (temp_buffer);
175 break;
176 case 5: /* 5th line should contain the paper jam status */
177 paper_jam = atoi (temp_buffer);
178 break;
179 case 6: /* 6th line should contain the paper out status */
180 paper_out = atoi (temp_buffer);
181 break;
182 case 7: /* 7th line should contain the toner low status */
183 toner_low = atoi (temp_buffer);
184 break;
185 case 8: /* did data come too slow for engine */
186 page_punt = atoi (temp_buffer);
187 break;
188 case 9: /* did we run out of memory */
189 memory_out = atoi (temp_buffer);
190 break;
191 case 10: /* is there a door open */
192 door_open = atoi (temp_buffer);
193 break;
194 case 11: /* is output tray full */
195 paper_output = atoi (temp_buffer);
196 break;
197 case 12: /* display panel message */
198 strcpy (display_message, temp_buffer + 1);
199 break;
200 default: /* fold multiline message */
201 strncat (display_message, input_buffer,
202 sizeof (display_message) - strlen (display_message) - 1);
203 }
205 }
207 /* break out of the read loop if we encounter an error */
208 if (result != STATE_OK)
209 break;
210 }
212 /* WARNING if output found on stderr */
213 if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
214 result = max_state (result, STATE_WARNING);
215 /* remove CRLF */
216 if (input_buffer[strlen (input_buffer) - 1] == '\n')
217 input_buffer[strlen (input_buffer) - 1] = 0;
218 sprintf (errmsg, "%s", input_buffer );
220 }
222 /* close stderr */
223 (void) fclose (child_stderr);
225 /* close the pipe */
226 if (spclose (child_process))
227 result = max_state (result, STATE_WARNING);
229 /* if there wasn't any output, display an error */
230 if (line == 0) {
232 /* might not be the problem, but most likely is. */
233 result = STATE_UNKNOWN ;
234 asprintf (&errmsg, "%s : Timeout from host %s\n", errmsg, address );
236 }
238 /* if we had no read errors, check the printer status results... */
239 if (result == STATE_OK) {
241 if (paper_jam) {
242 result = STATE_WARNING;
243 strcpy (errmsg, _("Paper Jam"));
244 }
245 else if (paper_out) {
246 result = STATE_WARNING;
247 strcpy (errmsg, _("Out of Paper"));
248 }
249 else if (line_status == OFFLINE) {
250 if (strcmp (errmsg, "POWERSAVE ON") != 0) {
251 result = STATE_WARNING;
252 strcpy (errmsg, _("Printer Offline"));
253 }
254 }
255 else if (peripheral_error) {
256 result = STATE_WARNING;
257 strcpy (errmsg, _("Peripheral Error"));
258 }
259 else if (intervention_required) {
260 result = STATE_WARNING;
261 strcpy (errmsg, _("Intervention Required"));
262 }
263 else if (toner_low) {
264 result = STATE_WARNING;
265 strcpy (errmsg, _("Toner Low"));
266 }
267 else if (memory_out) {
268 result = STATE_WARNING;
269 strcpy (errmsg, _("Insufficient Memory"));
270 }
271 else if (door_open) {
272 result = STATE_WARNING;
273 strcpy (errmsg, _("A Door is Open"));
274 }
275 else if (paper_output) {
276 result = STATE_WARNING;
277 strcpy (errmsg, _("Output Tray is Full"));
278 }
279 else if (page_punt) {
280 result = STATE_WARNING;
281 strcpy (errmsg, _("Data too Slow for Engine"));
282 }
283 else if (paper_status) {
284 result = STATE_WARNING;
285 strcpy (errmsg, _("Unknown Paper Error"));
286 }
287 }
289 if (result == STATE_OK)
290 printf (_("Printer ok - (%s)\n"), display_message);
292 else if (result == STATE_UNKNOWN) {
294 printf ("%s\n", errmsg);
296 /* if printer could not be reached, escalate to critical */
297 if (strstr (errmsg, "Timeout"))
298 result = STATE_CRITICAL;
299 }
301 else if (result == STATE_WARNING)
302 printf ("%s (%s)\n", errmsg, display_message);
304 return result;
305 }
308 /* process command-line arguments */
309 int
310 process_arguments (int argc, char **argv)
311 {
312 int c;
314 int option = 0;
315 static struct option longopts[] = {
316 {"hostname", required_argument, 0, 'H'},
317 {"community", required_argument, 0, 'C'},
318 /* {"critical", required_argument,0,'c'}, */
319 /* {"warning", required_argument,0,'w'}, */
320 /* {"port", required_argument,0,'P'}, */
321 {"version", no_argument, 0, 'V'},
322 {"help", no_argument, 0, 'h'},
323 {0, 0, 0, 0}
324 };
326 if (argc < 2)
327 return ERROR;
330 while (1) {
331 c = getopt_long (argc, argv, "+hVH:C:", longopts, &option);
333 if (c == -1 || c == EOF || c == 1)
334 break;
336 switch (c) {
337 case 'H': /* hostname */
338 if (is_host (optarg)) {
339 address = strscpy(address, optarg) ;
340 }
341 else {
342 usage2 (_("Invalid hostname/address"), optarg);
343 }
344 break;
345 case 'C': /* community */
346 community = strscpy (community, optarg);
347 break;
348 case 'V': /* version */
349 print_revision (progname, revision);
350 exit (STATE_OK);
351 case 'h': /* help */
352 print_help ();
353 exit (STATE_OK);
354 case '?': /* help */
355 usage5 ();
356 }
357 }
359 c = optind;
360 if (address == NULL) {
361 if (is_host (argv[c])) {
362 address = argv[c++];
363 }
364 else {
365 usage2 (_("Invalid hostname/address"), argv[c]);
366 }
367 }
369 if (community == NULL) {
370 if (argv[c] != NULL )
371 community = argv[c];
372 else
373 community = strdup (DEFAULT_COMMUNITY);
374 }
376 return validate_arguments ();
377 }
380 int
381 validate_arguments (void)
382 {
383 return OK;
384 }
387 void
388 print_help (void)
389 {
390 print_revision (progname, revision);
392 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
393 printf (COPYRIGHT, copyright, email);
395 printf ("%s\n", _("This plugin tests the STATUS of an HP printer with a JetDirect card."));
396 printf ("%s\n", _("Net-snmp must be installed on the computer running the plugin."));
398 printf ("\n\n");
400 print_usage ();
402 printf (_(UT_HELP_VRSN));
403 printf (_(UT_EXTRA_OPTS));
405 printf (" %s\n", "-C, --community=STRING");
406 printf (" %s", _("The SNMP community name "));
407 printf (_("(default=%s)"), DEFAULT_COMMUNITY);
408 printf ("\n");
410 #ifdef NP_EXTRA_OPTS
411 printf ("\n");
412 printf ("%s\n", _("Notes:"));
413 printf (_(UT_EXTRA_OPTS_NOTES));
414 #endif
416 printf (_(UT_SUPPORT));
417 }
421 void
422 print_usage (void)
423 {
424 printf (_("Usage:"));
425 printf ("%s -H host [-C community]\n", progname);
426 }