Fix INADDR_NONE value (for systems which don't define it).
[monitoring-plugins.git] / plugins / check_hpjd.c
blob8a60ce8c855fd2bcd33a0f756d30c80af73515e4
1 /******************************************************************************
3 * Nagios check_hpjd plugin
5 * License: GPL
6 * Copyright (c) 1999-2006 nagios-plugins team
8 * Last Modified: $Date$
10 * Description:
12 * This file contains the check_hpjd plugin
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.
18 * License Information:
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 * $Id$
35 *****************************************************************************/
37 const char *progname = "check_hpjd";
38 const char *revision = "$Revision$";
39 const char *copyright = "2000-2006";
40 const char *email = "nagiosplug-devel@lists.sourceforge.net";
42 #include "common.h"
43 #include "popen.h"
44 #include "utils.h"
45 #include "netutils.h"
47 #define DEFAULT_COMMUNITY "public"
50 const char *option_summary = "-H host [-C community]\n";
52 #define HPJD_LINE_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.1"
53 #define HPJD_PAPER_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.2"
54 #define HPJD_INTERVENTION_REQUIRED ".1.3.6.1.4.1.11.2.3.9.1.1.2.3"
55 #define HPJD_GD_PERIPHERAL_ERROR ".1.3.6.1.4.1.11.2.3.9.1.1.2.6"
56 #define HPJD_GD_PAPER_OUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.8"
57 #define HPJD_GD_PAPER_JAM ".1.3.6.1.4.1.11.2.3.9.1.1.2.9"
58 #define HPJD_GD_TONER_LOW ".1.3.6.1.4.1.11.2.3.9.1.1.2.10"
59 #define HPJD_GD_PAGE_PUNT ".1.3.6.1.4.1.11.2.3.9.1.1.2.11"
60 #define HPJD_GD_MEMORY_OUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.12"
61 #define HPJD_GD_DOOR_OPEN ".1.3.6.1.4.1.11.2.3.9.1.1.2.17"
62 #define HPJD_GD_PAPER_OUTPUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.19"
63 #define HPJD_GD_STATUS_DISPLAY ".1.3.6.1.4.1.11.2.3.9.1.1.3"
65 #define ONLINE 0
66 #define OFFLINE 1
68 int process_arguments (int, char **);
69 int validate_arguments (void);
70 void print_help (void);
71 void print_usage (void);
73 char *community = NULL;
74 char *address = NULL;
76 int
77 main (int argc, char **argv)
79 char command_line[1024];
80 int result = STATE_UNKNOWN;
81 int line;
82 char input_buffer[MAX_INPUT_BUFFER];
83 char query_string[512];
84 char *errmsg;
85 char *temp_buffer;
86 int line_status = ONLINE;
87 int paper_status = 0;
88 int intervention_required = 0;
89 int peripheral_error = 0;
90 int paper_jam = 0;
91 int paper_out = 0;
92 int toner_low = 0;
93 int page_punt = 0;
94 int memory_out = 0;
95 int door_open = 0;
96 int paper_output = 0;
97 char display_message[MAX_INPUT_BUFFER];
99 errmsg = malloc(MAX_INPUT_BUFFER);
101 setlocale (LC_ALL, "");
102 bindtextdomain (PACKAGE, LOCALEDIR);
103 textdomain (PACKAGE);
105 if (process_arguments (argc, argv) == ERROR)
106 usage4 (_("Could not parse arguments"));
108 /* removed ' 2>1' at end of command 10/27/1999 - EG */
109 /* create the query string */
110 sprintf
111 (query_string,
112 "%s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0",
113 HPJD_LINE_STATUS,
114 HPJD_PAPER_STATUS,
115 HPJD_INTERVENTION_REQUIRED,
116 HPJD_GD_PERIPHERAL_ERROR,
117 HPJD_GD_PAPER_JAM,
118 HPJD_GD_PAPER_OUT,
119 HPJD_GD_TONER_LOW,
120 HPJD_GD_PAGE_PUNT,
121 HPJD_GD_MEMORY_OUT,
122 HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, HPJD_GD_STATUS_DISPLAY);
124 /* get the command to run */
125 sprintf (command_line, "%s -OQa -m : -v 1 -c %s %s %s", PATH_TO_SNMPGET, community,
126 address, query_string);
128 /* run the command */
129 child_process = spopen (command_line);
130 if (child_process == NULL) {
131 printf (_("Could not open pipe: %s\n"), command_line);
132 return STATE_UNKNOWN;
135 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
136 if (child_stderr == NULL) {
137 printf (_("Could not open stderr for %s\n"), command_line);
140 result = STATE_OK;
142 line = 0;
143 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
145 /* strip the newline character from the end of the input */
146 if (input_buffer[strlen (input_buffer) - 1] == '\n')
147 input_buffer[strlen (input_buffer) - 1] = 0;
149 line++;
151 temp_buffer = strtok (input_buffer, "=");
152 temp_buffer = strtok (NULL, "=");
154 if (temp_buffer == NULL && line < 13) {
156 result = STATE_UNKNOWN;
157 strcpy (errmsg, input_buffer);
159 } else {
161 switch (line) {
163 case 1: /* 1st line should contain the line status */
164 line_status = atoi (temp_buffer);
165 break;
166 case 2: /* 2nd line should contain the paper status */
167 paper_status = atoi (temp_buffer);
168 break;
169 case 3: /* 3rd line should be intervention required */
170 intervention_required = atoi (temp_buffer);
171 break;
172 case 4: /* 4th line should be peripheral error */
173 peripheral_error = atoi (temp_buffer);
174 break;
175 case 5: /* 5th line should contain the paper jam status */
176 paper_jam = atoi (temp_buffer);
177 break;
178 case 6: /* 6th line should contain the paper out status */
179 paper_out = atoi (temp_buffer);
180 break;
181 case 7: /* 7th line should contain the toner low status */
182 toner_low = atoi (temp_buffer);
183 break;
184 case 8: /* did data come too slow for engine */
185 page_punt = atoi (temp_buffer);
186 break;
187 case 9: /* did we run out of memory */
188 memory_out = atoi (temp_buffer);
189 break;
190 case 10: /* is there a door open */
191 door_open = atoi (temp_buffer);
192 break;
193 case 11: /* is output tray full */
194 paper_output = atoi (temp_buffer);
195 break;
196 case 12: /* display panel message */
197 strcpy (display_message, temp_buffer + 1);
198 break;
199 default: /* fold multiline message */
200 strncat (display_message, input_buffer,
201 sizeof (display_message) - strlen (display_message) - 1);
206 /* break out of the read loop if we encounter an error */
207 if (result != STATE_OK)
208 break;
211 /* WARNING if output found on stderr */
212 if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
213 result = max_state (result, STATE_WARNING);
214 /* remove CRLF */
215 if (input_buffer[strlen (input_buffer) - 1] == '\n')
216 input_buffer[strlen (input_buffer) - 1] = 0;
217 sprintf (errmsg, "%s", input_buffer );
221 /* close stderr */
222 (void) fclose (child_stderr);
224 /* close the pipe */
225 if (spclose (child_process))
226 result = max_state (result, STATE_WARNING);
228 /* if there wasn't any output, display an error */
229 if (line == 0) {
231 /* might not be the problem, but most likely is. */
232 result = STATE_UNKNOWN ;
233 asprintf (&errmsg, "%s : Timeout from host %s\n", errmsg, address );
237 /* if we had no read errors, check the printer status results... */
238 if (result == STATE_OK) {
240 if (paper_jam) {
241 result = STATE_WARNING;
242 strcpy (errmsg, _("Paper Jam"));
244 else if (paper_out) {
245 result = STATE_WARNING;
246 strcpy (errmsg, _("Out of Paper"));
248 else if (line_status == OFFLINE) {
249 if (strcmp (errmsg, "POWERSAVE ON") != 0) {
250 result = STATE_WARNING;
251 strcpy (errmsg, _("Printer Offline"));
254 else if (peripheral_error) {
255 result = STATE_WARNING;
256 strcpy (errmsg, _("Peripheral Error"));
258 else if (intervention_required) {
259 result = STATE_WARNING;
260 strcpy (errmsg, _("Intervention Required"));
262 else if (toner_low) {
263 result = STATE_WARNING;
264 strcpy (errmsg, _("Toner Low"));
266 else if (memory_out) {
267 result = STATE_WARNING;
268 strcpy (errmsg, _("Insufficient Memory"));
270 else if (door_open) {
271 result = STATE_WARNING;
272 strcpy (errmsg, _("A Door is Open"));
274 else if (paper_output) {
275 result = STATE_WARNING;
276 strcpy (errmsg, _("Output Tray is Full"));
278 else if (page_punt) {
279 result = STATE_WARNING;
280 strcpy (errmsg, _("Data too Slow for Engine"));
282 else if (paper_status) {
283 result = STATE_WARNING;
284 strcpy (errmsg, _("Unknown Paper Error"));
288 if (result == STATE_OK)
289 printf (_("Printer ok - (%s)\n"), display_message);
291 else if (result == STATE_UNKNOWN) {
293 printf ("%s\n", errmsg);
295 /* if printer could not be reached, escalate to critical */
296 if (strstr (errmsg, "Timeout"))
297 result = STATE_CRITICAL;
300 else if (result == STATE_WARNING)
301 printf ("%s (%s)\n", errmsg, display_message);
303 return result;
307 /* process command-line arguments */
309 process_arguments (int argc, char **argv)
311 int c;
313 int option = 0;
314 static struct option longopts[] = {
315 {"hostname", required_argument, 0, 'H'},
316 {"community", required_argument, 0, 'C'},
317 /* {"critical", required_argument,0,'c'}, */
318 /* {"warning", required_argument,0,'w'}, */
319 /* {"port", required_argument,0,'P'}, */
320 {"version", no_argument, 0, 'V'},
321 {"help", no_argument, 0, 'h'},
322 {0, 0, 0, 0}
325 if (argc < 2)
326 return ERROR;
329 while (1) {
330 c = getopt_long (argc, argv, "+hVH:C:", longopts, &option);
332 if (c == -1 || c == EOF || c == 1)
333 break;
335 switch (c) {
336 case 'H': /* hostname */
337 if (is_host (optarg)) {
338 address = strscpy(address, optarg) ;
340 else {
341 usage2 (_("Invalid hostname/address"), optarg);
343 break;
344 case 'C': /* community */
345 community = strscpy (community, optarg);
346 break;
347 case 'V': /* version */
348 print_revision (progname, revision);
349 exit (STATE_OK);
350 case 'h': /* help */
351 print_help ();
352 exit (STATE_OK);
353 case '?': /* help */
354 usage5 ();
358 c = optind;
359 if (address == NULL) {
360 if (is_host (argv[c])) {
361 address = argv[c++];
363 else {
364 usage2 (_("Invalid hostname/address"), argv[c]);
368 if (community == NULL) {
369 if (argv[c] != NULL )
370 community = argv[c];
371 else
372 community = strdup (DEFAULT_COMMUNITY);
375 return validate_arguments ();
380 validate_arguments (void)
382 return OK;
386 void
387 print_help (void)
389 print_revision (progname, revision);
391 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
392 printf (COPYRIGHT, copyright, email);
394 printf ("%s\n", _("This plugin tests the STATUS of an HP printer with a JetDirect card."));
395 printf ("%s\n", _("Net-snmp must be installed on the computer running the plugin."));
397 printf ("\n\n");
399 print_usage ();
401 printf (_(UT_HELP_VRSN));
403 printf (" %s\n", "-C, --community=STRING");
404 printf (" %s", _("The SNMP community name "));
405 printf (_("(default=%s)"), DEFAULT_COMMUNITY);
407 printf (_(UT_SUPPORT));
412 void
413 print_usage (void)
415 printf (_("Usage:"));
416 printf ("%s -H host [-C community]\n", progname);