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 if (process_arguments (argc, argv) == ERROR)
104 usage4 (_("Could not parse arguments"));
106 /* removed ' 2>1' at end of command 10/27/1999 - EG */
107 /* create the query string */
108 sprintf
109 (query_string,
110 "%s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0",
111 HPJD_LINE_STATUS,
112 HPJD_PAPER_STATUS,
113 HPJD_INTERVENTION_REQUIRED,
114 HPJD_GD_PERIPHERAL_ERROR,
115 HPJD_GD_PAPER_JAM,
116 HPJD_GD_PAPER_OUT,
117 HPJD_GD_TONER_LOW,
118 HPJD_GD_PAGE_PUNT,
119 HPJD_GD_MEMORY_OUT,
120 HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, HPJD_GD_STATUS_DISPLAY);
122 /* get the command to run */
123 sprintf (command_line, "%s -OQa -m : -v 1 -c %s %s %s", PATH_TO_SNMPGET, community,
124 address, query_string);
126 /* run the command */
127 child_process = spopen (command_line);
128 if (child_process == NULL) {
129 printf (_("Could not open pipe: %s\n"), command_line);
130 return STATE_UNKNOWN;
131 }
133 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
134 if (child_stderr == NULL) {
135 printf (_("Could not open stderr for %s\n"), command_line);
136 }
138 result = STATE_OK;
140 line = 0;
141 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
143 /* strip the newline character from the end of the input */
144 if (input_buffer[strlen (input_buffer) - 1] == '\n')
145 input_buffer[strlen (input_buffer) - 1] = 0;
147 line++;
149 temp_buffer = strtok (input_buffer, "=");
150 temp_buffer = strtok (NULL, "=");
152 if (temp_buffer == NULL && line < 13) {
154 result = STATE_UNKNOWN;
155 strcpy (errmsg, input_buffer);
157 } else {
159 switch (line) {
161 case 1: /* 1st line should contain the line status */
162 line_status = atoi (temp_buffer);
163 break;
164 case 2: /* 2nd line should contain the paper status */
165 paper_status = atoi (temp_buffer);
166 break;
167 case 3: /* 3rd line should be intervention required */
168 intervention_required = atoi (temp_buffer);
169 break;
170 case 4: /* 4th line should be peripheral error */
171 peripheral_error = atoi (temp_buffer);
172 break;
173 case 5: /* 5th line should contain the paper jam status */
174 paper_jam = atoi (temp_buffer);
175 break;
176 case 6: /* 6th line should contain the paper out status */
177 paper_out = atoi (temp_buffer);
178 break;
179 case 7: /* 7th line should contain the toner low status */
180 toner_low = atoi (temp_buffer);
181 break;
182 case 8: /* did data come too slow for engine */
183 page_punt = atoi (temp_buffer);
184 break;
185 case 9: /* did we run out of memory */
186 memory_out = atoi (temp_buffer);
187 break;
188 case 10: /* is there a door open */
189 door_open = atoi (temp_buffer);
190 break;
191 case 11: /* is output tray full */
192 paper_output = atoi (temp_buffer);
193 break;
194 case 12: /* display panel message */
195 strcpy (display_message, temp_buffer + 1);
196 break;
197 default: /* fold multiline message */
198 strncat (display_message, input_buffer,
199 sizeof (display_message) - strlen (display_message) - 1);
200 }
202 }
204 /* break out of the read loop if we encounter an error */
205 if (result != STATE_OK)
206 break;
207 }
209 /* WARNING if output found on stderr */
210 if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
211 result = max_state (result, STATE_WARNING);
212 /* remove CRLF */
213 if (input_buffer[strlen (input_buffer) - 1] == '\n')
214 input_buffer[strlen (input_buffer) - 1] = 0;
215 sprintf (errmsg, "%s", input_buffer );
217 }
219 /* close stderr */
220 (void) fclose (child_stderr);
222 /* close the pipe */
223 if (spclose (child_process))
224 result = max_state (result, STATE_WARNING);
226 /* if there wasn't any output, display an error */
227 if (line == 0) {
229 /* might not be the problem, but most likely is. */
230 result = STATE_UNKNOWN ;
231 asprintf (&errmsg, "%s : Timeout from host %s\n", errmsg, address );
233 }
235 /* if we had no read errors, check the printer status results... */
236 if (result == STATE_OK) {
238 if (paper_jam) {
239 result = STATE_WARNING;
240 strcpy (errmsg, _("Paper Jam"));
241 }
242 else if (paper_out) {
243 result = STATE_WARNING;
244 strcpy (errmsg, _("Out of Paper"));
245 }
246 else if (line_status == OFFLINE) {
247 if (strcmp (errmsg, "POWERSAVE ON") != 0) {
248 result = STATE_WARNING;
249 strcpy (errmsg, _("Printer Offline"));
250 }
251 }
252 else if (peripheral_error) {
253 result = STATE_WARNING;
254 strcpy (errmsg, _("Peripheral Error"));
255 }
256 else if (intervention_required) {
257 result = STATE_WARNING;
258 strcpy (errmsg, _("Intervention Required"));
259 }
260 else if (toner_low) {
261 result = STATE_WARNING;
262 strcpy (errmsg, _("Toner Low"));
263 }
264 else if (memory_out) {
265 result = STATE_WARNING;
266 strcpy (errmsg, _("Insufficient Memory"));
267 }
268 else if (door_open) {
269 result = STATE_WARNING;
270 strcpy (errmsg, _("A Door is Open"));
271 }
272 else if (paper_output) {
273 result = STATE_WARNING;
274 strcpy (errmsg, _("Output Tray is Full"));
275 }
276 else if (page_punt) {
277 result = STATE_WARNING;
278 strcpy (errmsg, _("Data too Slow for Engine"));
279 }
280 else if (paper_status) {
281 result = STATE_WARNING;
282 strcpy (errmsg, _("Unknown Paper Error"));
283 }
284 }
286 if (result == STATE_OK)
287 printf (_("Printer ok - (%s)\n"), display_message);
289 else if (result == STATE_UNKNOWN) {
291 printf ("%s\n", errmsg);
293 /* if printer could not be reached, escalate to critical */
294 if (strstr (errmsg, "Timeout"))
295 result = STATE_CRITICAL;
296 }
298 else if (result == STATE_WARNING)
299 printf ("%s (%s)\n", errmsg, display_message);
301 return result;
302 }
305 /* process command-line arguments */
306 int
307 process_arguments (int argc, char **argv)
308 {
309 int c;
311 int option = 0;
312 static struct option longopts[] = {
313 {"hostname", required_argument, 0, 'H'},
314 {"community", required_argument, 0, 'C'},
315 /* {"critical", required_argument,0,'c'}, */
316 /* {"warning", required_argument,0,'w'}, */
317 /* {"port", required_argument,0,'P'}, */
318 {"version", no_argument, 0, 'V'},
319 {"help", no_argument, 0, 'h'},
320 {0, 0, 0, 0}
321 };
323 if (argc < 2)
324 return ERROR;
327 while (1) {
328 c = getopt_long (argc, argv, "+hVH:C:", longopts, &option);
330 if (c == -1 || c == EOF || c == 1)
331 break;
333 switch (c) {
334 case 'H': /* hostname */
335 if (is_host (optarg)) {
336 address = strscpy(address, optarg) ;
337 }
338 else {
339 usage2 (_("Invalid hostname/address"), optarg);
340 }
341 break;
342 case 'C': /* community */
343 community = strscpy (community, optarg);
344 break;
345 case 'V': /* version */
346 print_revision (progname, revision);
347 exit (STATE_OK);
348 case 'h': /* help */
349 print_help ();
350 exit (STATE_OK);
351 case '?': /* help */
352 usage5 ();
353 }
354 }
356 c = optind;
357 if (address == NULL) {
358 if (is_host (argv[c])) {
359 address = argv[c++];
360 }
361 else {
362 usage2 (_("Invalid hostname/address"), argv[c]);
363 }
364 }
366 if (community == NULL) {
367 if (argv[c] != NULL )
368 community = argv[c];
369 else
370 community = strdup (DEFAULT_COMMUNITY);
371 }
373 return validate_arguments ();
374 }
377 int
378 validate_arguments (void)
379 {
380 return OK;
381 }
384 void
385 print_help (void)
386 {
387 print_revision (progname, revision);
389 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
390 printf (COPYRIGHT, copyright, email);
392 printf ("%s\n", _("This plugin tests the STATUS of an HP printer with a JetDirect card."));
393 printf ("%s\n", _("Net-snmp must be installed on the computer running the plugin."));
395 printf ("\n\n");
397 print_usage ();
399 printf (_(UT_HELP_VRSN));
401 printf (" %s\n", "-C, --community=STRING");
402 printf (" %s", _("The SNMP community name "));
403 printf (_("(default=%s)"), DEFAULT_COMMUNITY);
405 printf ("\n");
406 printf (_(UT_SUPPORT));
407 }
411 void
412 print_usage (void)
413 {
414 printf (_("Usage:"));
415 printf ("%s -H host [-C community]\n", progname);
416 }