1 /******************************************************************************
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 * $Id$
18 *****************************************************************************/
20 const char *progname = "check_hpjd";
21 const char *revision = "$Revision$";
22 const char *copyright = "2000-2004";
23 const char *email = "nagiosplug-devel@lists.sourceforge.net";
25 #include "common.h"
26 #include "popen.h"
27 #include "utils.h"
28 #include "netutils.h"
30 #define DEFAULT_COMMUNITY "public"
33 const char *option_summary = "-H host [-C community]\n";
35 #define HPJD_LINE_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.1"
36 #define HPJD_PAPER_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.2"
37 #define HPJD_INTERVENTION_REQUIRED ".1.3.6.1.4.1.11.2.3.9.1.1.2.3"
38 #define HPJD_GD_PERIPHERAL_ERROR ".1.3.6.1.4.1.11.2.3.9.1.1.2.6"
39 #define HPJD_GD_PAPER_JAM ".1.3.6.1.4.1.11.2.3.9.1.1.2.8"
40 #define HPJD_GD_PAPER_OUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.9"
41 #define HPJD_GD_TONER_LOW ".1.3.6.1.4.1.11.2.3.9.1.1.2.10"
42 #define HPJD_GD_PAGE_PUNT ".1.3.6.1.4.1.11.2.3.9.1.1.2.11"
43 #define HPJD_GD_MEMORY_OUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.12"
44 #define HPJD_GD_DOOR_OPEN ".1.3.6.1.4.1.11.2.3.9.1.1.2.17"
45 #define HPJD_GD_PAPER_OUTPUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.19"
46 #define HPJD_GD_STATUS_DISPLAY ".1.3.6.1.4.1.11.2.3.9.1.1.3"
48 #define ONLINE 0
49 #define OFFLINE 1
51 int process_arguments (int, char **);
52 int validate_arguments (void);
53 void print_help (void);
54 void print_usage (void);
56 char *community = NULL;
57 char *address = NULL;
59 int
60 main (int argc, char **argv)
61 {
62 char command_line[1024];
63 int result = STATE_UNKNOWN;
64 int line;
65 char input_buffer[MAX_INPUT_BUFFER];
66 char query_string[512];
67 char *errmsg;
68 char *temp_buffer;
69 int line_status = ONLINE;
70 int paper_status = 0;
71 int intervention_required = 0;
72 int peripheral_error = 0;
73 int paper_jam = 0;
74 int paper_out = 0;
75 int toner_low = 0;
76 int page_punt = 0;
77 int memory_out = 0;
78 int door_open = 0;
79 int paper_output = 0;
80 char display_message[MAX_INPUT_BUFFER];
82 errmsg = malloc(MAX_INPUT_BUFFER);
84 setlocale (LC_ALL, "");
85 bindtextdomain (PACKAGE, LOCALEDIR);
86 textdomain (PACKAGE);
88 if (process_arguments (argc, argv) == ERROR)
89 usage4 (_("Could not parse arguments"));
91 /* removed ' 2>1' at end of command 10/27/1999 - EG */
92 /* create the query string */
93 sprintf
94 (query_string,
95 "%s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0",
96 HPJD_LINE_STATUS,
97 HPJD_PAPER_STATUS,
98 HPJD_INTERVENTION_REQUIRED,
99 HPJD_GD_PERIPHERAL_ERROR,
100 HPJD_GD_PAPER_JAM,
101 HPJD_GD_PAPER_OUT,
102 HPJD_GD_TONER_LOW,
103 HPJD_GD_PAGE_PUNT,
104 HPJD_GD_MEMORY_OUT,
105 HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, HPJD_GD_STATUS_DISPLAY);
107 /* get the command to run */
108 sprintf (command_line, "%s -OQa -m : -v 1 -c %s %s %s", PATH_TO_SNMPGET, community,
109 address, query_string);
111 /* run the command */
112 child_process = spopen (command_line);
113 if (child_process == NULL) {
114 printf (_("Could not open pipe: %s\n"), command_line);
115 return STATE_UNKNOWN;
116 }
118 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
119 if (child_stderr == NULL) {
120 printf (_("Could not open stderr for %s\n"), command_line);
121 }
123 result = STATE_OK;
125 line = 0;
126 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
128 /* strip the newline character from the end of the input */
129 if (input_buffer[strlen (input_buffer) - 1] == '\n')
130 input_buffer[strlen (input_buffer) - 1] = 0;
132 line++;
134 temp_buffer = strtok (input_buffer, "=");
135 temp_buffer = strtok (NULL, "=");
137 if (temp_buffer == NULL) {
139 result = STATE_UNKNOWN;
140 strcpy (errmsg, input_buffer);
142 } else {
144 switch (line) {
146 case 1: /* 1st line should contain the line status */
147 line_status = atoi (temp_buffer);
148 break;
149 case 2: /* 2nd line should contain the paper status */
150 paper_status = atoi (temp_buffer);
151 break;
152 case 3: /* 3rd line should be intervention required */
153 intervention_required = atoi (temp_buffer);
154 break;
155 case 4: /* 4th line should be peripheral error */
156 peripheral_error = atoi (temp_buffer);
157 break;
158 case 5: /* 5th line should contain the paper jam status */
159 paper_jam = atoi (temp_buffer);
160 break;
161 case 6: /* 6th line should contain the paper out status */
162 paper_out = atoi (temp_buffer);
163 break;
164 case 7: /* 7th line should contain the toner low status */
165 toner_low = atoi (temp_buffer);
166 break;
167 case 8: /* did data come too slow for engine */
168 page_punt = atoi (temp_buffer);
169 break;
170 case 9: /* did we run out of memory */
171 memory_out = atoi (temp_buffer);
172 break;
173 case 10: /* is there a door open */
174 door_open = atoi (temp_buffer);
175 break;
176 case 11: /* is output tray full */
177 paper_output = atoi (temp_buffer);
178 break;
179 case 12: /* display panel message */
180 strcpy (display_message, temp_buffer + 1);
181 break;
182 default:
183 break;
184 }
186 }
188 /* break out of the read loop if we encounter an error */
189 if (result != STATE_OK)
190 break;
191 }
193 /* WARNING if output found on stderr */
194 if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
195 result = max_state (result, STATE_WARNING);
196 /* remove CRLF */
197 if (input_buffer[strlen (input_buffer) - 1] == '\n')
198 input_buffer[strlen (input_buffer) - 1] = 0;
199 sprintf (errmsg, "%s", input_buffer );
201 }
203 /* close stderr */
204 (void) fclose (child_stderr);
206 /* close the pipe */
207 if (spclose (child_process))
208 result = max_state (result, STATE_WARNING);
210 /* if there wasn't any output, display an error */
211 if (line == 0) {
213 /* might not be the problem, but most likely is. */
214 result = STATE_UNKNOWN ;
215 asprintf (&errmsg, "%s : Timeout from host %s\n", errmsg, address );
217 }
219 /* if we had no read errors, check the printer status results... */
220 if (result == STATE_OK) {
222 if (paper_jam) {
223 result = STATE_WARNING;
224 strcpy (errmsg, _("Paper Jam"));
225 }
226 else if (paper_out) {
227 result = STATE_WARNING;
228 strcpy (errmsg, _("Out of Paper"));
229 }
230 else if (line_status == OFFLINE) {
231 if (strcmp (errmsg, "POWERSAVE ON") != 0) {
232 result = STATE_WARNING;
233 strcpy (errmsg, _("Printer Offline"));
234 }
235 }
236 else if (peripheral_error) {
237 result = STATE_WARNING;
238 strcpy (errmsg, _("Peripheral Error"));
239 }
240 else if (intervention_required) {
241 result = STATE_WARNING;
242 strcpy (errmsg, _("Intervention Required"));
243 }
244 else if (toner_low) {
245 result = STATE_WARNING;
246 strcpy (errmsg, _("Toner Low"));
247 }
248 else if (memory_out) {
249 result = STATE_WARNING;
250 strcpy (errmsg, _("Insufficient Memory"));
251 }
252 else if (door_open) {
253 result = STATE_WARNING;
254 strcpy (errmsg, _("A Door is Open"));
255 }
256 else if (paper_output) {
257 result = STATE_WARNING;
258 strcpy (errmsg, _("Output Tray is Full"));
259 }
260 else if (page_punt) {
261 result = STATE_WARNING;
262 strcpy (errmsg, _("Data too Slow for Engine"));
263 }
264 else if (paper_status) {
265 result = STATE_WARNING;
266 strcpy (errmsg, _("Unknown Paper Error"));
267 }
268 }
270 if (result == STATE_OK)
271 printf (_("Printer ok - (%s)\n"), display_message);
273 else if (result == STATE_UNKNOWN) {
275 printf ("%s\n", errmsg);
277 /* if printer could not be reached, escalate to critical */
278 if (strstr (errmsg, "Timeout"))
279 result = STATE_CRITICAL;
280 }
282 else if (result == STATE_WARNING)
283 printf ("%s (%s)\n", errmsg, display_message);
285 return result;
286 }
289 /* process command-line arguments */
290 int
291 process_arguments (int argc, char **argv)
292 {
293 int c;
295 int option = 0;
296 static struct option longopts[] = {
297 {"hostname", required_argument, 0, 'H'},
298 {"community", required_argument, 0, 'C'},
299 /* {"critical", required_argument,0,'c'}, */
300 /* {"warning", required_argument,0,'w'}, */
301 /* {"port", required_argument,0,'P'}, */
302 {"version", no_argument, 0, 'V'},
303 {"help", no_argument, 0, 'h'},
304 {0, 0, 0, 0}
305 };
307 if (argc < 2)
308 return ERROR;
311 while (1) {
312 c = getopt_long (argc, argv, "+hVH:C:", longopts, &option);
314 if (c == -1 || c == EOF || c == 1)
315 break;
317 switch (c) {
318 case 'H': /* hostname */
319 if (is_host (optarg)) {
320 address = strscpy(address, optarg) ;
321 }
322 else {
323 usage2 (_("Invalid hostname/address"), optarg);
324 }
325 break;
326 case 'C': /* community */
327 community = strscpy (community, optarg);
328 break;
329 case 'V': /* version */
330 print_revision (progname, revision);
331 exit (STATE_OK);
332 case 'h': /* help */
333 print_help ();
334 exit (STATE_OK);
335 case '?': /* help */
336 usage2 (_("Unknown argument"), optarg);
337 }
338 }
340 c = optind;
341 if (address == NULL) {
342 if (is_host (argv[c])) {
343 address = argv[c++];
344 }
345 else {
346 usage2 (_("Invalid hostname/address"), argv[c]);
347 }
348 }
350 if (community == NULL) {
351 if (argv[c] != NULL )
352 community = argv[c];
353 else
354 community = strdup (DEFAULT_COMMUNITY);
355 }
357 return validate_arguments ();
358 }
361 int
362 validate_arguments (void)
363 {
364 return OK;
365 }
368 void
369 print_help (void)
370 {
371 print_revision (progname, revision);
373 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
374 printf (COPYRIGHT, copyright, email);
376 printf (_("\
377 This plugin tests the STATUS of an HP printer with a JetDirect card.\n\
378 Net-snmp must be installed on the computer running the plugin.\n\n"));
380 print_usage ();
382 printf (_(UT_HELP_VRSN));
384 printf (_("\
385 -C, --community=STRING\n\
386 The SNMP community name (default=%s)\n"), DEFAULT_COMMUNITY);
388 printf (_(UT_SUPPORT));
389 }
393 void
394 print_usage (void)
395 {
396 printf ("Usage: %s -H host [-C community]\n", progname);
397 }