1 /*****************************************************************************
3 * Nagios check_hpjd plugin
6 * Copyright (c) 2000-2007 Nagios 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
= "nagiosplug-devel@lists.sourceforge.net";
41 #define DEFAULT_COMMUNITY "public"
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
;
71 main (int argc
, char **argv
)
73 char command_line
[1024];
74 int result
= STATE_UNKNOWN
;
76 char input_buffer
[MAX_INPUT_BUFFER
];
77 char query_string
[512];
80 int line_status
= ONLINE
;
82 int intervention_required
= 0;
83 int peripheral_error
= 0;
91 char display_message
[MAX_INPUT_BUFFER
];
93 errmsg
= malloc(MAX_INPUT_BUFFER
);
95 setlocale (LC_ALL
, "");
96 bindtextdomain (PACKAGE
, LOCALEDIR
);
99 /* Parse extra opts if any */
100 argv
=np_extra_opts (&argc
, argv
, progname
);
102 if (process_arguments (argc
, argv
) == ERROR
)
103 usage4 (_("Could not parse arguments"));
105 /* removed ' 2>1' at end of command 10/27/1999 - EG */
106 /* create the query string */
109 "%s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0",
112 HPJD_INTERVENTION_REQUIRED
,
113 HPJD_GD_PERIPHERAL_ERROR
,
119 HPJD_GD_DOOR_OPEN
, HPJD_GD_PAPER_OUTPUT
, HPJD_GD_STATUS_DISPLAY
);
121 /* get the command to run */
122 sprintf (command_line
, "%s -OQa -m : -v 1 -c %s %s %s", PATH_TO_SNMPGET
, community
,
123 address
, query_string
);
125 /* run the command */
126 child_process
= spopen (command_line
);
127 if (child_process
== NULL
) {
128 printf (_("Could not open pipe: %s\n"), command_line
);
129 return STATE_UNKNOWN
;
132 child_stderr
= fdopen (child_stderr_array
[fileno (child_process
)], "r");
133 if (child_stderr
== NULL
) {
134 printf (_("Could not open stderr for %s\n"), command_line
);
140 while (fgets (input_buffer
, MAX_INPUT_BUFFER
- 1, child_process
)) {
142 /* strip the newline character from the end of the input */
143 if (input_buffer
[strlen (input_buffer
) - 1] == '\n')
144 input_buffer
[strlen (input_buffer
) - 1] = 0;
148 temp_buffer
= strtok (input_buffer
, "=");
149 temp_buffer
= strtok (NULL
, "=");
151 if (temp_buffer
== NULL
&& line
< 13) {
153 result
= STATE_UNKNOWN
;
154 strcpy (errmsg
, input_buffer
);
160 case 1: /* 1st line should contain the line status */
161 line_status
= atoi (temp_buffer
);
163 case 2: /* 2nd line should contain the paper status */
164 paper_status
= atoi (temp_buffer
);
166 case 3: /* 3rd line should be intervention required */
167 intervention_required
= atoi (temp_buffer
);
169 case 4: /* 4th line should be peripheral error */
170 peripheral_error
= atoi (temp_buffer
);
172 case 5: /* 5th line should contain the paper jam status */
173 paper_jam
= atoi (temp_buffer
);
175 case 6: /* 6th line should contain the paper out status */
176 paper_out
= atoi (temp_buffer
);
178 case 7: /* 7th line should contain the toner low status */
179 toner_low
= atoi (temp_buffer
);
181 case 8: /* did data come too slow for engine */
182 page_punt
= atoi (temp_buffer
);
184 case 9: /* did we run out of memory */
185 memory_out
= atoi (temp_buffer
);
187 case 10: /* is there a door open */
188 door_open
= atoi (temp_buffer
);
190 case 11: /* is output tray full */
191 paper_output
= atoi (temp_buffer
);
193 case 12: /* display panel message */
194 strcpy (display_message
, temp_buffer
+ 1);
196 default: /* fold multiline message */
197 strncat (display_message
, input_buffer
,
198 sizeof (display_message
) - strlen (display_message
) - 1);
203 /* break out of the read loop if we encounter an error */
204 if (result
!= STATE_OK
)
208 /* WARNING if output found on stderr */
209 if (fgets (input_buffer
, MAX_INPUT_BUFFER
- 1, child_stderr
)) {
210 result
= max_state (result
, STATE_WARNING
);
212 if (input_buffer
[strlen (input_buffer
) - 1] == '\n')
213 input_buffer
[strlen (input_buffer
) - 1] = 0;
214 sprintf (errmsg
, "%s", input_buffer
);
219 (void) fclose (child_stderr
);
222 if (spclose (child_process
))
223 result
= max_state (result
, STATE_WARNING
);
225 /* if there wasn't any output, display an error */
228 /* might not be the problem, but most likely is. */
229 result
= STATE_UNKNOWN
;
230 asprintf (&errmsg
, "%s : Timeout from host %s\n", errmsg
, address
);
234 /* if we had no read errors, check the printer status results... */
235 if (result
== STATE_OK
) {
238 result
= STATE_WARNING
;
239 strcpy (errmsg
, _("Paper Jam"));
241 else if (paper_out
) {
242 result
= STATE_WARNING
;
243 strcpy (errmsg
, _("Out of Paper"));
245 else if (line_status
== OFFLINE
) {
246 if (strcmp (errmsg
, "POWERSAVE ON") != 0) {
247 result
= STATE_WARNING
;
248 strcpy (errmsg
, _("Printer Offline"));
251 else if (peripheral_error
) {
252 result
= STATE_WARNING
;
253 strcpy (errmsg
, _("Peripheral Error"));
255 else if (intervention_required
) {
256 result
= STATE_WARNING
;
257 strcpy (errmsg
, _("Intervention Required"));
259 else if (toner_low
) {
260 result
= STATE_WARNING
;
261 strcpy (errmsg
, _("Toner Low"));
263 else if (memory_out
) {
264 result
= STATE_WARNING
;
265 strcpy (errmsg
, _("Insufficient Memory"));
267 else if (door_open
) {
268 result
= STATE_WARNING
;
269 strcpy (errmsg
, _("A Door is Open"));
271 else if (paper_output
) {
272 result
= STATE_WARNING
;
273 strcpy (errmsg
, _("Output Tray is Full"));
275 else if (page_punt
) {
276 result
= STATE_WARNING
;
277 strcpy (errmsg
, _("Data too Slow for Engine"));
279 else if (paper_status
) {
280 result
= STATE_WARNING
;
281 strcpy (errmsg
, _("Unknown Paper Error"));
285 if (result
== STATE_OK
)
286 printf (_("Printer ok - (%s)\n"), display_message
);
288 else if (result
== STATE_UNKNOWN
) {
290 printf ("%s\n", errmsg
);
292 /* if printer could not be reached, escalate to critical */
293 if (strstr (errmsg
, "Timeout"))
294 result
= STATE_CRITICAL
;
297 else if (result
== STATE_WARNING
)
298 printf ("%s (%s)\n", errmsg
, display_message
);
304 /* process command-line arguments */
306 process_arguments (int argc
, char **argv
)
311 static struct option longopts
[] = {
312 {"hostname", required_argument
, 0, 'H'},
313 {"community", required_argument
, 0, 'C'},
314 /* {"critical", required_argument,0,'c'}, */
315 /* {"warning", required_argument,0,'w'}, */
316 /* {"port", required_argument,0,'P'}, */
317 {"version", no_argument
, 0, 'V'},
318 {"help", no_argument
, 0, 'h'},
327 c
= getopt_long (argc
, argv
, "+hVH:C:", longopts
, &option
);
329 if (c
== -1 || c
== EOF
|| c
== 1)
333 case 'H': /* hostname */
334 if (is_host (optarg
)) {
335 address
= strscpy(address
, optarg
) ;
338 usage2 (_("Invalid hostname/address"), optarg
);
341 case 'C': /* community */
342 community
= strscpy (community
, optarg
);
344 case 'V': /* version */
345 print_revision (progname
, NP_VERSION
);
356 if (address
== NULL
) {
357 if (is_host (argv
[c
])) {
361 usage2 (_("Invalid hostname/address"), argv
[c
]);
365 if (community
== NULL
) {
366 if (argv
[c
] != NULL
)
369 community
= strdup (DEFAULT_COMMUNITY
);
372 return validate_arguments ();
377 validate_arguments (void)
386 print_revision (progname
, NP_VERSION
);
388 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
389 printf (COPYRIGHT
, copyright
, email
);
391 printf ("%s\n", _("This plugin tests the STATUS of an HP printer with a JetDirect card."));
392 printf ("%s\n", _("Net-snmp must be installed on the computer running the plugin."));
398 printf (_(UT_HELP_VRSN
));
399 printf (_(UT_EXTRA_OPTS
));
401 printf (" %s\n", "-C, --community=STRING");
402 printf (" %s", _("The SNMP community name "));
403 printf (_("(default=%s)"), DEFAULT_COMMUNITY
);
408 printf ("%s\n", _("Notes:"));
409 printf (_(UT_EXTRA_OPTS_NOTES
));
412 printf (_(UT_SUPPORT
));
420 printf (_("Usage:"));
421 printf ("%s -H host [-C community]\n", progname
);