Revert "Make use of xasprintf instead of asprintf"
[monitoring-plugins.git] / plugins / check_hpjd.c
blob5fe06984f23cf95f60c55a92ffff674dafe5bf77
1 /*****************************************************************************
2 *
3 * Monitoring check_hpjd plugin
4 *
5 * License: GPL
6 * Copyright (c) 2000-2007 Monitoring Plugins Development Team
7 *
8 * Description:
9 *
10 * This file contains the check_hpjd plugin
12 * This plugin tests the STATUS of an HP printer with a JetDirect card.
13 * Net-SNMP must be installed on the computer running the plugin.
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 *****************************************************************************/
32 const char *progname = "check_hpjd";
33 const char *copyright = "2000-2007";
34 const char *email = "devel@monitoring-plugins.org";
36 #include "common.h"
37 #include "popen.h"
38 #include "utils.h"
39 #include "netutils.h"
41 #define DEFAULT_COMMUNITY "public"
42 #define DEFAULT_PORT "161"
44 const char *option_summary = "-H host [-C community]\n";
46 #define HPJD_LINE_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.1"
47 #define HPJD_PAPER_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.2"
48 #define HPJD_INTERVENTION_REQUIRED ".1.3.6.1.4.1.11.2.3.9.1.1.2.3"
49 #define HPJD_GD_PERIPHERAL_ERROR ".1.3.6.1.4.1.11.2.3.9.1.1.2.6"
50 #define HPJD_GD_PAPER_OUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.8"
51 #define HPJD_GD_PAPER_JAM ".1.3.6.1.4.1.11.2.3.9.1.1.2.9"
52 #define HPJD_GD_TONER_LOW ".1.3.6.1.4.1.11.2.3.9.1.1.2.10"
53 #define HPJD_GD_PAGE_PUNT ".1.3.6.1.4.1.11.2.3.9.1.1.2.11"
54 #define HPJD_GD_MEMORY_OUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.12"
55 #define HPJD_GD_DOOR_OPEN ".1.3.6.1.4.1.11.2.3.9.1.1.2.17"
56 #define HPJD_GD_PAPER_OUTPUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.19"
57 #define HPJD_GD_STATUS_DISPLAY ".1.3.6.1.4.1.11.2.3.9.1.1.3"
59 #define ONLINE 0
60 #define OFFLINE 1
62 int process_arguments (int, char **);
63 int validate_arguments (void);
64 void print_help (void);
65 void print_usage (void);
67 char *community = NULL;
68 char *address = NULL;
69 char *port = NULL;
71 int
72 main (int argc, char **argv)
74 char command_line[1024];
75 int result = STATE_UNKNOWN;
76 int line;
77 char input_buffer[MAX_INPUT_BUFFER];
78 char query_string[512];
79 char *errmsg;
80 char *temp_buffer;
81 int line_status = ONLINE;
82 int paper_status = 0;
83 int intervention_required = 0;
84 int peripheral_error = 0;
85 int paper_jam = 0;
86 int paper_out = 0;
87 int toner_low = 0;
88 int page_punt = 0;
89 int memory_out = 0;
90 int door_open = 0;
91 int paper_output = 0;
92 char display_message[MAX_INPUT_BUFFER];
94 errmsg = malloc(MAX_INPUT_BUFFER);
96 setlocale (LC_ALL, "");
97 bindtextdomain (PACKAGE, LOCALEDIR);
98 textdomain (PACKAGE);
100 /* Parse extra opts if any */
101 argv=np_extra_opts (&argc, argv, progname);
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:%hd %s", PATH_TO_SNMPGET, community,
124 address, port, 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;
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);
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);
204 /* break out of the read loop if we encounter an error */
205 if (result != STATE_OK)
206 break;
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 );
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 xasprintf (&errmsg, "%s : Timeout from host %s\n", errmsg, address );
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"));
242 else if (paper_out) {
243 result = STATE_WARNING;
244 strcpy (errmsg, _("Out of Paper"));
246 else if (line_status == OFFLINE) {
247 if (strcmp (errmsg, "POWERSAVE ON") != 0) {
248 result = STATE_WARNING;
249 strcpy (errmsg, _("Printer Offline"));
252 else if (peripheral_error) {
253 result = STATE_WARNING;
254 strcpy (errmsg, _("Peripheral Error"));
256 else if (intervention_required) {
257 result = STATE_WARNING;
258 strcpy (errmsg, _("Intervention Required"));
260 else if (toner_low) {
261 result = STATE_WARNING;
262 strcpy (errmsg, _("Toner Low"));
264 else if (memory_out) {
265 result = STATE_WARNING;
266 strcpy (errmsg, _("Insufficient Memory"));
268 else if (door_open) {
269 result = STATE_WARNING;
270 strcpy (errmsg, _("A Door is Open"));
272 else if (paper_output) {
273 result = STATE_WARNING;
274 strcpy (errmsg, _("Output Tray is Full"));
276 else if (page_punt) {
277 result = STATE_WARNING;
278 strcpy (errmsg, _("Data too Slow for Engine"));
280 else if (paper_status) {
281 result = STATE_WARNING;
282 strcpy (errmsg, _("Unknown Paper Error"));
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;
298 else if (result == STATE_WARNING)
299 printf ("%s (%s)\n", errmsg, display_message);
301 return result;
305 /* process command-line arguments */
307 process_arguments (int argc, char **argv)
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}
323 if (argc < 2)
324 return ERROR;
327 while (1) {
328 c = getopt_long (argc, argv, "+hVH:C:p:", 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) ;
338 else {
339 usage2 (_("Invalid hostname/address"), optarg);
341 break;
342 case 'C': /* community */
343 community = strscpy (community, optarg);
344 break;
345 case 'p':
346 if (!is_intpos(optarg))
347 usage2 (_("Port must be a positive short integer"), optarg);
348 else
349 port = atoi(optarg);
350 break;
351 case 'V': /* version */
352 print_revision (progname, NP_VERSION);
353 exit (STATE_OK);
354 case 'h': /* help */
355 print_help ();
356 exit (STATE_OK);
357 case '?': /* help */
358 usage5 ();
362 c = optind;
363 if (address == NULL) {
364 if (is_host (argv[c])) {
365 address = argv[c++];
367 else {
368 usage2 (_("Invalid hostname/address"), argv[c]);
372 if (community == NULL) {
373 if (argv[c] != NULL )
374 community = argv[c];
375 else
376 community = strdup (DEFAULT_COMMUNITY);
379 if (port == NULL) {
380 if (argv[c] != NULL )
381 port = argv[c];
382 else
383 port = atoi (DEFAULT_PORT);
386 return validate_arguments ();
391 validate_arguments (void)
393 return OK;
397 void
398 print_help (void)
400 print_revision (progname, NP_VERSION);
402 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
403 printf (COPYRIGHT, copyright, email);
405 printf ("%s\n", _("This plugin tests the STATUS of an HP printer with a JetDirect card."));
406 printf ("%s\n", _("Net-snmp must be installed on the computer running the plugin."));
408 printf ("\n\n");
410 print_usage ();
412 printf (UT_HELP_VRSN);
413 printf (UT_EXTRA_OPTS);
415 printf (" %s\n", "-C, --community=STRING");
416 printf (" %s", _("The SNMP community name "));
417 printf (_("(default=%s)"), DEFAULT_COMMUNITY);
418 printf ("\n");
419 printf (" %s\n", "-p, --port=STRING");
420 printf (" %s", _("Specify the port to check "));
421 printf (_("(default=%s)"), DEFAULT_PORT);
422 printf ("\n");
424 printf (UT_SUPPORT);
429 void
430 print_usage (void)
432 printf ("%s\n", _("Usage:"));
433 printf ("%s -H host [-C community] [-p port]\n", progname);