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 *****************************************************************************/
19 #include "common.h"
20 #include "popen.h"
21 #include "utils.h"
22 #include "netutils.h"
24 #define DEFAULT_COMMUNITY "public"
26 const char *progname = "check_hpjd";
27 const char *revision = "$Revision$";
28 const char *copyright = "2000-2003";
29 const char *email = "nagiosplug-devel@lists.sourceforge.net";
31 const char *option_summary = "-H host [-C community]\n";
33 #define HPJD_LINE_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.1"
34 #define HPJD_PAPER_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.2"
35 #define HPJD_INTERVENTION_REQUIRED ".1.3.6.1.4.1.11.2.3.9.1.1.2.3"
36 #define HPJD_GD_PERIPHERAL_ERROR ".1.3.6.1.4.1.11.2.3.9.1.1.2.6"
37 #define HPJD_GD_PAPER_JAM ".1.3.6.1.4.1.11.2.3.9.1.1.2.8"
38 #define HPJD_GD_PAPER_OUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.9"
39 #define HPJD_GD_TONER_LOW ".1.3.6.1.4.1.11.2.3.9.1.1.2.10"
40 #define HPJD_GD_PAGE_PUNT ".1.3.6.1.4.1.11.2.3.9.1.1.2.11"
41 #define HPJD_GD_MEMORY_OUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.12"
42 #define HPJD_GD_DOOR_OPEN ".1.3.6.1.4.1.11.2.3.9.1.1.2.17"
43 #define HPJD_GD_PAPER_OUTPUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.19"
44 #define HPJD_GD_STATUS_DISPLAY ".1.3.6.1.4.1.11.2.3.9.1.1.3"
46 #define ONLINE 0
47 #define OFFLINE 1
49 int process_arguments (int, char **);
50 int validate_arguments (void);
51 void print_help (void);
52 void print_usage (void);
54 char *community = NULL;
55 char *address = NULL;
57 int
58 main (int argc, char **argv)
59 {
60 char command_line[1024];
61 int result;
62 int line;
63 char input_buffer[MAX_INPUT_BUFFER];
64 char query_string[512];
65 char *errmsg;
66 char *temp_buffer;
67 int line_status = ONLINE;
68 int paper_status = 0;
69 int intervention_required = 0;
70 int peripheral_error = 0;
71 int paper_jam = 0;
72 int paper_out = 0;
73 int toner_low = 0;
74 int page_punt = 0;
75 int memory_out = 0;
76 int door_open = 0;
77 int paper_output = 0;
78 char display_message[MAX_INPUT_BUFFER];
80 errmsg = malloc(MAX_INPUT_BUFFER);
82 if (process_arguments (argc, argv) != OK)
83 usage (_("Invalid command arguments supplied\n"));
85 /* removed ' 2>1' at end of command 10/27/1999 - EG */
86 /* create the query string */
87 sprintf
88 (query_string,
89 "%s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0",
90 HPJD_LINE_STATUS,
91 HPJD_PAPER_STATUS,
92 HPJD_INTERVENTION_REQUIRED,
93 HPJD_GD_PERIPHERAL_ERROR,
94 HPJD_GD_PAPER_JAM,
95 HPJD_GD_PAPER_OUT,
96 HPJD_GD_TONER_LOW,
97 HPJD_GD_PAGE_PUNT,
98 HPJD_GD_MEMORY_OUT,
99 HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, HPJD_GD_STATUS_DISPLAY);
101 /* get the command to run */
102 sprintf (command_line, "%s -m : -v 1 -c %s %s %s", PATH_TO_SNMPGET, community,
103 address, query_string);
105 /* run the command */
106 child_process = spopen (command_line);
107 if (child_process == NULL) {
108 printf (_("Could not open pipe: %s\n"), command_line);
109 return STATE_UNKNOWN;
110 }
112 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
113 if (child_stderr == NULL) {
114 printf (_("Could not open stderr for %s\n"), command_line);
115 }
117 result = STATE_OK;
119 line = 0;
120 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
122 /* strip the newline character from the end of the input */
123 if (input_buffer[strlen (input_buffer) - 1] == '\n')
124 input_buffer[strlen (input_buffer) - 1] = 0;
126 line++;
128 temp_buffer = strtok (input_buffer, "=");
129 temp_buffer = strtok (NULL, "=");
131 if (temp_buffer != NULL) {
133 result = STATE_UNKNOWN;
134 strcpy (errmsg, input_buffer);
136 } else {
138 switch (line) {
140 case 1: /* 1st line should contain the line status */
141 line_status = atoi (temp_buffer);
142 break;
143 case 2: /* 2nd line should contain the paper status */
144 paper_status = atoi (temp_buffer);
145 break;
146 case 3: /* 3rd line should be intervention required */
147 intervention_required = atoi (temp_buffer);
148 break;
149 case 4: /* 4th line should be peripheral error */
150 peripheral_error = atoi (temp_buffer);
151 break;
152 case 5: /* 5th line should contain the paper jam status */
153 paper_jam = atoi (temp_buffer);
154 break;
155 case 6: /* 6th line should contain the paper out status */
156 paper_out = atoi (temp_buffer);
157 break;
158 case 7: /* 7th line should contain the toner low status */
159 toner_low = atoi (temp_buffer);
160 break;
161 case 8: /* did data come too slow for engine */
162 page_punt = atoi (temp_buffer);
163 break;
164 case 9: /* did we run out of memory */
165 memory_out = atoi (temp_buffer);
166 break;
167 case 10: /* is there a door open */
168 door_open = atoi (temp_buffer);
169 break;
170 case 11: /* is output tray full */
171 paper_output = atoi (temp_buffer);
172 break;
173 case 12: /* display panel message */
174 strcpy (display_message, temp_buffer + 1);
175 break;
176 default:
177 break;
178 }
180 }
182 /* break out of the read loop if we encounter an error */
183 if (result != STATE_OK)
184 break;
185 }
187 /* WARNING if output found on stderr */
188 if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
189 result = max_state (result, STATE_WARNING);
190 /* remove CRLF */
191 if (input_buffer[strlen (input_buffer) - 1] == '\n')
192 input_buffer[strlen (input_buffer) - 1] = 0;
193 sprintf (errmsg, "%s", input_buffer );
195 }
197 /* close stderr */
198 (void) fclose (child_stderr);
200 /* close the pipe */
201 if (spclose (child_process))
202 result = max_state (result, STATE_WARNING);
204 /* if there wasn't any output, display an error */
205 if (line == 0) {
207 /* might not be the problem, but most likely is. */
208 result = STATE_UNKNOWN ;
209 asprintf (&errmsg, "%s : Timeout from host %s\n", errmsg, address );
211 }
213 /* if we had no read errors, check the printer status results... */
214 if (result == STATE_OK) {
216 if (paper_jam) {
217 result = STATE_WARNING;
218 strcpy (errmsg, _("Paper Jam"));
219 }
220 else if (paper_out) {
221 result = STATE_WARNING;
222 strcpy (errmsg, _("Out of Paper"));
223 }
224 else if (line_status == OFFLINE) {
225 if (strcmp (errmsg, "POWERSAVE ON") != 0) {
226 result = STATE_WARNING;
227 strcpy (errmsg, _("Printer Offline"));
228 }
229 }
230 else if (peripheral_error) {
231 result = STATE_WARNING;
232 strcpy (errmsg, _("Peripheral Error"));
233 }
234 else if (intervention_required) {
235 result = STATE_WARNING;
236 strcpy (errmsg, _("Intervention Required"));
237 }
238 else if (toner_low) {
239 result = STATE_WARNING;
240 strcpy (errmsg, _("Toner Low"));
241 }
242 else if (memory_out) {
243 result = STATE_WARNING;
244 strcpy (errmsg, _("Insufficient Memory"));
245 }
246 else if (door_open) {
247 result = STATE_WARNING;
248 strcpy (errmsg, _("A Door is Open"));
249 }
250 else if (paper_output) {
251 result = STATE_WARNING;
252 strcpy (errmsg, _("Output Tray is Full"));
253 }
254 else if (page_punt) {
255 result = STATE_WARNING;
256 strcpy (errmsg, _("Data too Slow for Engine"));
257 }
258 else if (paper_status) {
259 result = STATE_WARNING;
260 strcpy (errmsg, _("Unknown Paper Error"));
261 }
262 }
264 if (result == STATE_OK)
265 printf (_("Printer ok - (%s)\n"), display_message);
267 else if (result == STATE_UNKNOWN) {
269 printf ("%s\n", errmsg);
271 /* if printer could not be reached, escalate to critical */
272 if (strstr (errmsg, "Timeout"))
273 result = STATE_CRITICAL;
274 }
276 else if (result == STATE_WARNING)
277 printf ("%s (%s)\n", errmsg, display_message);
279 return result;
280 }
286 /* process command-line arguments */
287 int
288 process_arguments (int argc, char **argv)
289 {
290 int c;
292 int option = 0;
293 static struct option longopts[] = {
294 {"hostname", required_argument, 0, 'H'},
295 {"community", required_argument, 0, 'C'},
296 /* {"critical", required_argument,0,'c'}, */
297 /* {"warning", required_argument,0,'w'}, */
298 /* {"port", required_argument,0,'P'}, */
299 {"version", no_argument, 0, 'V'},
300 {"help", no_argument, 0, 'h'},
301 {0, 0, 0, 0}
302 };
304 if (argc < 2)
305 return ERROR;
308 while (1) {
309 c = getopt_long (argc, argv, "+hVH:C:", longopts, &option);
311 if (c == -1 || c == EOF || c == 1)
312 break;
314 switch (c) {
315 case 'H': /* hostname */
316 if (is_host (optarg)) {
317 address = strscpy(address, optarg) ;
318 }
319 else {
320 usage (_("Invalid host name\n"));
321 }
322 break;
323 case 'C': /* community */
324 community = strscpy (community, optarg);
325 break;
326 case 'V': /* version */
327 print_revision (progname, revision);
328 exit (STATE_OK);
329 case 'h': /* help */
330 print_help ();
331 exit (STATE_OK);
332 case '?': /* help */
333 usage (_("Invalid argument\n"));
334 }
335 }
337 c = optind;
338 if (address == NULL) {
339 if (is_host (argv[c])) {
340 address = argv[c++];
341 }
342 else {
343 usage (_("Invalid host name"));
344 }
345 }
347 if (community == NULL) {
348 if (argv[c] != NULL )
349 community = argv[c];
350 else
351 community = strdup (DEFAULT_COMMUNITY);
352 }
354 return validate_arguments ();
355 }
361 int
362 validate_arguments (void)
363 {
364 return OK;
365 }
371 \f
372 void
373 print_help (void)
374 {
375 print_revision (progname, revision);
377 printf (_(COPYRIGHT), copyright, email);
379 printf (_("\
380 This plugin tests the STATUS of an HP printer with a JetDirect card.\n\
381 Net-snmp must be installed on the computer running the plugin.\n\n"));
383 print_usage ();
385 printf (_(UT_HELP_VRSN));
387 printf (_("\
388 -C, --community=STRING\n\
389 The SNMP community name (default=%s)\n"), DEFAULT_COMMUNITY);
391 printf (_(UT_SUPPORT));
392 }
397 void
398 print_usage (void)
399 {
400 printf (_("\
401 Usage: %s -H host [-C community]\n"), progname);
402 printf (_(UT_HLP_VRS), progname, progname);
403 }