Code

fix a variety of compiler warnings about qualifier discards and other pedantic stuff
[nagiosplug.git] / plugins / check_hpjd.c
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";
32 void
33 print_usage (void)
34 {
35         printf (_("\
36 Usage: %s -H host [-C community]\n"), progname);
37         printf (_(UT_HLP_VRS), progname, progname);
38 }
40 void
41 print_help (void)
42 {
43         print_revision (progname, revision);
45         printf (_(COPYRIGHT), copyright, email);
47         printf (_("\
48 This plugin tests the STATUS of an HP printer with a JetDirect card.\n\
49 Net-snmp must be installed on the computer running the plugin.\n\n"));
51         print_usage ();
53         printf (_(UT_HELP_VRSN));
55         printf (_("\
56  -C, --community=STRING\n\
57     The SNMP community name (default=%s)\n"), DEFAULT_COMMUNITY);
59         printf (_(UT_SUPPORT));
60 }
61 \f
62 #define HPJD_LINE_STATUS                ".1.3.6.1.4.1.11.2.3.9.1.1.2.1"
63 #define HPJD_PAPER_STATUS               ".1.3.6.1.4.1.11.2.3.9.1.1.2.2"
64 #define HPJD_INTERVENTION_REQUIRED      ".1.3.6.1.4.1.11.2.3.9.1.1.2.3"
65 #define HPJD_GD_PERIPHERAL_ERROR        ".1.3.6.1.4.1.11.2.3.9.1.1.2.6"
66 #define HPJD_GD_PAPER_JAM               ".1.3.6.1.4.1.11.2.3.9.1.1.2.8"
67 #define HPJD_GD_PAPER_OUT               ".1.3.6.1.4.1.11.2.3.9.1.1.2.9"
68 #define HPJD_GD_TONER_LOW               ".1.3.6.1.4.1.11.2.3.9.1.1.2.10"
69 #define HPJD_GD_PAGE_PUNT               ".1.3.6.1.4.1.11.2.3.9.1.1.2.11"
70 #define HPJD_GD_MEMORY_OUT              ".1.3.6.1.4.1.11.2.3.9.1.1.2.12"
71 #define HPJD_GD_DOOR_OPEN               ".1.3.6.1.4.1.11.2.3.9.1.1.2.17"
72 #define HPJD_GD_PAPER_OUTPUT            ".1.3.6.1.4.1.11.2.3.9.1.1.2.19"
73 #define HPJD_GD_STATUS_DISPLAY          ".1.3.6.1.4.1.11.2.3.9.1.1.3"
75 #define ONLINE          0
76 #define OFFLINE         1
78 int process_arguments (int, char **);
79 int validate_arguments (void);
81 char *community = DEFAULT_COMMUNITY;
82 char *address = NULL;
84 int
85 main (int argc, char **argv)
86 {
87         char command_line[1024];
88         int result;
89         int line;
90         char input_buffer[MAX_INPUT_BUFFER];
91         char query_string[512];
92         char error_message[MAX_INPUT_BUFFER];
93         char *temp_buffer;
94         int line_status = ONLINE;
95         int paper_status = 0;
96         int intervention_required = 0;
97         int peripheral_error = 0;
98         int paper_jam = 0;
99         int paper_out = 0;
100         int toner_low = 0;
101         int page_punt = 0;
102         int memory_out = 0;
103         int door_open = 0;
104         int paper_output = 0;
105         char display_message[MAX_INPUT_BUFFER];
106         char *temp ;
108         if (process_arguments (argc, argv) != OK)
109                 usage (_("Invalid command arguments supplied\n"));
111         /* removed ' 2>1' at end of command 10/27/1999 - EG */
112         /* create the query string */
113         sprintf
114                 (query_string,
115                  "%s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0",
116                  HPJD_LINE_STATUS,
117                  HPJD_PAPER_STATUS,
118                  HPJD_INTERVENTION_REQUIRED,
119                  HPJD_GD_PERIPHERAL_ERROR,
120                  HPJD_GD_PAPER_JAM,
121                  HPJD_GD_PAPER_OUT,
122                  HPJD_GD_TONER_LOW,
123                  HPJD_GD_PAGE_PUNT,
124                  HPJD_GD_MEMORY_OUT,
125                  HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, HPJD_GD_STATUS_DISPLAY);
127         /* get the command to run */
128         sprintf (command_line, "%s -m : -v 1 -c %s %s %s", PATH_TO_SNMPGET, community, 
129                                                                         address, query_string);
131         /* run the command */
132         child_process = spopen (command_line);
133         if (child_process == NULL) {
134                 printf (_("Could not open pipe: %s\n"), command_line);
135                 return STATE_UNKNOWN;
136         }
138         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
139         if (child_stderr == NULL) {
140                 printf (_("Could not open stderr for %s\n"), command_line);
141         }
143         result = STATE_OK;
145         line = 0;
146         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
148                 /* strip the newline character from the end of the input */
149                 if (input_buffer[strlen (input_buffer) - 1] == '\n')
150                         input_buffer[strlen (input_buffer) - 1] = 0;
152                 line++;
154                 temp_buffer = strtok (input_buffer, "=");
155                 temp_buffer = strtok (NULL, "=");
157                 switch (line) {
159                 case 1:                                                                         /* 1st line should contain the line status */
160                         if (temp_buffer != NULL)
161                                 line_status = atoi (temp_buffer);
162                         else {
163                                 result = STATE_UNKNOWN;
164                                 strcpy (error_message, input_buffer);
165                         }
166                         break;
168                 case 2:                                                                         /* 2nd line should contain the paper status */
169                         if (temp_buffer != NULL)
170                                 paper_status = atoi (temp_buffer);
171                         else {
172                                 result = STATE_UNKNOWN;
173                                 strcpy (error_message, input_buffer);
174                         }
175                         break;
177                 case 3:                                                                         /* 3rd line should be intervention required */
178                         if (temp_buffer != NULL)
179                                 intervention_required = atoi (temp_buffer);
180                         else {
181                                 result = STATE_UNKNOWN;
182                                 strcpy (error_message, input_buffer);
183                         }
184                         break;
186                 case 4:                                                                         /* 4th line should be peripheral error */
187                         if (temp_buffer != NULL)
188                                 peripheral_error = atoi (temp_buffer);
189                         else {
190                                 result = STATE_UNKNOWN;
191                                 strcpy (error_message, input_buffer);
192                         }
193                         break;
195                 case 5:                                                                         /* 5th line should contain the paper jam status */
196                         if (temp_buffer != NULL)
197                                 paper_jam = atoi (temp_buffer);
198                         else {
199                                 result = STATE_UNKNOWN;
200                                 strcpy (error_message, input_buffer);
201                         }
202                         break;
204                 case 6:                                                                         /* 6th line should contain the paper out status */
205                         if (temp_buffer != NULL)
206                                 paper_out = atoi (temp_buffer);
207                         else {
208                                 result = STATE_UNKNOWN;
209                                 strcpy (error_message, input_buffer);
210                         }
211                         break;
213                 case 7:                                                                         /* 7th line should contain the toner low status */
214                         if (temp_buffer != NULL)
215                                 toner_low = atoi (temp_buffer);
216                         else {
217                                 result = STATE_UNKNOWN;
218                                 strcpy (error_message, input_buffer);
219                         }
220                         break;
222                 case 8:                                                                         /* did data come too slow for engine */
223                         if (temp_buffer != NULL)
224                                 page_punt = atoi (temp_buffer);
225                         else {
226                                 result = STATE_UNKNOWN;
227                                 strcpy (error_message, input_buffer);
228                         }
229                         break;
231                 case 9:                                                                         /* did we run out of memory */
232                         if (temp_buffer != NULL)
233                                 memory_out = atoi (temp_buffer);
234                         else {
235                                 result = STATE_UNKNOWN;
236                                 strcpy (error_message, input_buffer);
237                         }
238                         break;
240                 case 10:                                                                                /* is there a door open */
241                         if (temp_buffer != NULL)
242                                 door_open = atoi (temp_buffer);
243                         else {
244                                 result = STATE_UNKNOWN;
245                                 strcpy (error_message, input_buffer);
246                         }
247                         break;
249                 case 11:                                                                                /* is output tray full */
250                         if (temp_buffer != NULL)
251                                 paper_output = atoi (temp_buffer);
252                         else {
253                                 result = STATE_UNKNOWN;
254                                 strcpy (error_message, input_buffer);
255                         }
256                         break;
258                 case 12:                                                                                /* display panel message */
259                         if (temp_buffer != NULL)
260                                 strcpy (display_message, temp_buffer + 1);
261                         else {
262                                 result = STATE_UNKNOWN;
263                                 strcpy (error_message, input_buffer);
264                         }
265                         break;
267                 default:
268                         break;
269                 }
271                 /* break out of the read loop if we encounter an error */
272                 if (result != STATE_OK)
273                         break;
274         }
276         /* WARNING if output found on stderr */
277         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
278                 result = max_state (result, STATE_WARNING);
279                 /* remove CRLF */
280                 if (input_buffer[strlen (input_buffer) - 1] == '\n')
281                         input_buffer[strlen (input_buffer) - 1] = 0;
282                 sprintf (error_message, "%s", input_buffer );
284         }
285         
286         /* close stderr */
287         (void) fclose (child_stderr);
289         /* close the pipe */
290         if (spclose (child_process))
291                 result = max_state (result, STATE_WARNING);
293         /* if there wasn't any output, display an error */
294         if (line == 0) {
296                 /* might not be the problem, but most likely is. */
297                 result = STATE_UNKNOWN ;
298                 asprintf (&temp, error_message);
299                 sprintf (error_message, "%s : Timeout from host %s\n", temp, address );
300                  
301         }
303         /* if we had no read errors, check the printer status results... */
304         if (result == STATE_OK) {
306                 if (paper_jam) {
307                         result = STATE_WARNING;
308                         strcpy (error_message, _("Paper Jam"));
309                 }
310                 else if (paper_out) {
311                         result = STATE_WARNING;
312                         strcpy (error_message, _("Out of Paper"));
313                 }
314                 else if (line_status == OFFLINE) {
315                         if (strcmp (error_message, "POWERSAVE ON") != 0) {
316                                 result = STATE_WARNING;
317                                 strcpy (error_message, _("Printer Offline"));
318                         }
319                 }
320                 else if (peripheral_error) {
321                         result = STATE_WARNING;
322                         strcpy (error_message, _("Peripheral Error"));
323                 }
324                 else if (intervention_required) {
325                         result = STATE_WARNING;
326                         strcpy (error_message, _("Intervention Required"));
327                 }
328                 else if (toner_low) {
329                         result = STATE_WARNING;
330                         strcpy (error_message, _("Toner Low"));
331                 }
332                 else if (memory_out) {
333                         result = STATE_WARNING;
334                         strcpy (error_message, _("Insufficient Memory"));
335                 }
336                 else if (door_open) {
337                         result = STATE_WARNING;
338                         strcpy (error_message, _("A Door is Open"));
339                 }
340                 else if (paper_output) {
341                         result = STATE_WARNING;
342                         strcpy (error_message, _("Output Tray is Full"));
343                 }
344                 else if (page_punt) {
345                         result = STATE_WARNING;
346                         strcpy (error_message, _("Data too Slow for Engine"));
347                 }
348                 else if (paper_status) {
349                         result = STATE_WARNING;
350                         strcpy (error_message, _("Unknown Paper Error"));
351                 }
352         }
354         if (result == STATE_OK)
355                 printf (_("Printer ok - (%s)\n"), display_message);
357         else if (result == STATE_UNKNOWN) {
359                 printf ("%s\n", error_message);
361                 /* if printer could not be reached, escalate to critical */
362                 if (strstr (error_message, "Timeout"))
363                         result = STATE_CRITICAL;
364         }
366         else if (result == STATE_WARNING)
367                 printf ("%s (%s)\n", error_message, display_message);
369         return result;
376 /* process command-line arguments */
377 int
378 process_arguments (int argc, char **argv)
380         int c;
382         int option_index = 0;
383         static struct option long_options[] = {
384                 {"hostname", required_argument, 0, 'H'},
385                 {"community", required_argument, 0, 'C'},
386 /*              {"critical",       required_argument,0,'c'}, */
387 /*              {"warning",        required_argument,0,'w'}, */
388 /*              {"port",           required_argument,0,'P'}, */
389                 {"version", no_argument, 0, 'V'},
390                 {"help", no_argument, 0, 'h'},
391                 {0, 0, 0, 0}
392         };
394         if (argc < 2)
395                 return ERROR;
397         
398         while (1) {
399                 c = getopt_long (argc, argv, "+hVH:C:", long_options, &option_index);
401                 if (c == -1 || c == EOF || c == 1)
402                         break;
404                 switch (c) {
405                 case 'H':                                                                       /* hostname */
406                         if (is_host (optarg)) {
407                                 address = strscpy(address, optarg) ;
408                         }
409                         else {
410                                 usage (_("Invalid host name\n"));
411                         }
412                         break;
413                 case 'C':                                                                       /* community */
414                         community = strscpy (community, optarg);
415                         break;
416                 case 'V':                                                                       /* version */
417                         print_revision (progname, revision);
418                         exit (STATE_OK);
419                 case 'h':                                                                       /* help */
420                         print_help ();
421                         exit (STATE_OK);
422                 case '?':                                                                       /* help */
423                         usage (_("Invalid argument\n"));
424                 }
425         }
427         c = optind;
428         if (address == NULL) {
429                 if (is_host (argv[c])) {
430                         address = argv[c++];
431                 }
432                 else {
433                         usage (_("Invalid host name"));
434                 }
435         }
436         
437         if (argv[c] != NULL ) {
438                 community = argv[c];
439         }
441         return validate_arguments ();
448 int
449 validate_arguments (void)
451         return OK;