1 /*****************************************************************************
3 * Monitoring check_hpjd plugin
6 * Copyright (c) 2000-2007 Monitoring Plugins Development Team
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";
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"
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
;
72 main (int argc
, char **argv
)
74 char command_line
[1024];
75 int result
= STATE_UNKNOWN
;
77 char input_buffer
[MAX_INPUT_BUFFER
];
78 char query_string
[512];
81 int line_status
= ONLINE
;
83 int intervention_required
= 0;
84 int peripheral_error
= 0;
92 char display_message
[MAX_INPUT_BUFFER
];
94 errmsg
= malloc(MAX_INPUT_BUFFER
);
96 setlocale (LC_ALL
, "");
97 bindtextdomain (PACKAGE
, LOCALEDIR
);
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 */
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",
113 HPJD_INTERVENTION_REQUIRED
,
114 HPJD_GD_PERIPHERAL_ERROR
,
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
);
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;
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
);
161 case 1: /* 1st line should contain the line status */
162 line_status
= atoi (temp_buffer
);
164 case 2: /* 2nd line should contain the paper status */
165 paper_status
= atoi (temp_buffer
);
167 case 3: /* 3rd line should be intervention required */
168 intervention_required
= atoi (temp_buffer
);
170 case 4: /* 4th line should be peripheral error */
171 peripheral_error
= atoi (temp_buffer
);
173 case 5: /* 5th line should contain the paper jam status */
174 paper_jam
= atoi (temp_buffer
);
176 case 6: /* 6th line should contain the paper out status */
177 paper_out
= atoi (temp_buffer
);
179 case 7: /* 7th line should contain the toner low status */
180 toner_low
= atoi (temp_buffer
);
182 case 8: /* did data come too slow for engine */
183 page_punt
= atoi (temp_buffer
);
185 case 9: /* did we run out of memory */
186 memory_out
= atoi (temp_buffer
);
188 case 10: /* is there a door open */
189 door_open
= atoi (temp_buffer
);
191 case 11: /* is output tray full */
192 paper_output
= atoi (temp_buffer
);
194 case 12: /* display panel message */
195 strcpy (display_message
, temp_buffer
+ 1);
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
)
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
);
213 if (input_buffer
[strlen (input_buffer
) - 1] == '\n')
214 input_buffer
[strlen (input_buffer
) - 1] = 0;
215 sprintf (errmsg
, "%s", input_buffer
);
220 (void) fclose (child_stderr
);
223 if (spclose (child_process
))
224 result
= max_state (result
, STATE_WARNING
);
226 /* if there wasn't any output, display an error */
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
) {
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
);
305 /* process command-line arguments */
307 process_arguments (int argc
, char **argv
)
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'},
328 c
= getopt_long (argc
, argv
, "+hVH:C:p:", longopts
, &option
);
330 if (c
== -1 || c
== EOF
|| c
== 1)
334 case 'H': /* hostname */
335 if (is_host (optarg
)) {
336 address
= strscpy(address
, optarg
) ;
339 usage2 (_("Invalid hostname/address"), optarg
);
342 case 'C': /* community */
343 community
= strscpy (community
, optarg
);
346 if (!is_intpos(optarg
))
347 usage2 (_("Port must be a positive short integer"), optarg
);
351 case 'V': /* version */
352 print_revision (progname
, NP_VERSION
);
363 if (address
== NULL
) {
364 if (is_host (argv
[c
])) {
368 usage2 (_("Invalid hostname/address"), argv
[c
]);
372 if (community
== NULL
) {
373 if (argv
[c
] != NULL
)
376 community
= strdup (DEFAULT_COMMUNITY
);
380 if (argv
[c
] != NULL
)
383 port
= atoi (DEFAULT_PORT
);
386 return validate_arguments ();
391 validate_arguments (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."));
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
);
419 printf (" %s\n", "-p, --port=STRING");
420 printf (" %s", _("Specify the port to check "));
421 printf (_("(default=%s)"), DEFAULT_PORT
);
432 printf ("%s\n", _("Usage:"));
433 printf ("%s -H host [-C community] [-p port]\n", progname
);