Code

cleanup to suppress various strict compiler warnings
[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"
23 const char *progname = "check_hpjd";
24 const char *revision = "$Revision$";
25 const char *authors = "Nagios Plugin Development Team";
26 const char *email = "nagiosplug-devel@lists.sourceforge.net";
27 const char *copyright = "2000-2003";
29 const char *summary = "\
30 This plugin tests the STATUS of an HP printer with a JetDirect card.\n\
31 Net-snmp must be installed on the computer running the plugin.\n\n";
33 const char *option_summary = "-H host [-C community]\n";
35 const char *options = "\
36  -H, --hostname=STRING or IPADDRESS\n\
37    Check server on the indicated host\n\
38  -C, --community=STRING\n\
39    The SNMP community name (default=%s)\n\
40  -h, --help\n\
41    Print detailed help screen\n\
42  -V, --version\n\
43    Print version information\n\n";
45 #define HPJD_LINE_STATUS                ".1.3.6.1.4.1.11.2.3.9.1.1.2.1"
46 #define HPJD_PAPER_STATUS               ".1.3.6.1.4.1.11.2.3.9.1.1.2.2"
47 #define HPJD_INTERVENTION_REQUIRED      ".1.3.6.1.4.1.11.2.3.9.1.1.2.3"
48 #define HPJD_GD_PERIPHERAL_ERROR        ".1.3.6.1.4.1.11.2.3.9.1.1.2.6"
49 #define HPJD_GD_PAPER_JAM               ".1.3.6.1.4.1.11.2.3.9.1.1.2.8"
50 #define HPJD_GD_PAPER_OUT               ".1.3.6.1.4.1.11.2.3.9.1.1.2.9"
51 #define HPJD_GD_TONER_LOW               ".1.3.6.1.4.1.11.2.3.9.1.1.2.10"
52 #define HPJD_GD_PAGE_PUNT               ".1.3.6.1.4.1.11.2.3.9.1.1.2.11"
53 #define HPJD_GD_MEMORY_OUT              ".1.3.6.1.4.1.11.2.3.9.1.1.2.12"
54 #define HPJD_GD_DOOR_OPEN               ".1.3.6.1.4.1.11.2.3.9.1.1.2.17"
55 #define HPJD_GD_PAPER_OUTPUT            ".1.3.6.1.4.1.11.2.3.9.1.1.2.19"
56 #define HPJD_GD_STATUS_DISPLAY          ".1.3.6.1.4.1.11.2.3.9.1.1.3"
58 #define ONLINE          0
59 #define OFFLINE         1
61 #define DEFAULT_COMMUNITY "public"
63 int process_arguments (int, char **);
64 int validate_arguments (void);
65 void print_help (void);
66 void print_usage (void);
68 char *community = DEFAULT_COMMUNITY;
69 char *address = NULL;
72 int
73 main (int argc, char **argv)
74 {
75         char command_line[1024];
76         int result;
77         int line;
78         char input_buffer[MAX_INPUT_BUFFER];
79         char query_string[512];
80         char error_message[MAX_INPUT_BUFFER];
81         char *temp_buffer;
82         int line_status = ONLINE;
83         int paper_status = 0;
84         int intervention_required = 0;
85         int peripheral_error = 0;
86         int paper_jam = 0;
87         int paper_out = 0;
88         int toner_low = 0;
89         int page_punt = 0;
90         int memory_out = 0;
91         int door_open = 0;
92         int paper_output = 0;
93         char display_message[MAX_INPUT_BUFFER];
95         if (process_arguments (argc, argv) != OK)
96                 usage ("Invalid command arguments supplied\n");
98         /* removed ' 2>1' at end of command 10/27/1999 - EG */
99         /* create the query string */
100         sprintf
101                 (query_string,
102                  "%s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0",
103                  HPJD_LINE_STATUS,
104                  HPJD_PAPER_STATUS,
105                  HPJD_INTERVENTION_REQUIRED,
106                  HPJD_GD_PERIPHERAL_ERROR,
107                  HPJD_GD_PAPER_JAM,
108                  HPJD_GD_PAPER_OUT,
109                  HPJD_GD_TONER_LOW,
110                  HPJD_GD_PAGE_PUNT,
111                  HPJD_GD_MEMORY_OUT,
112                  HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, HPJD_GD_STATUS_DISPLAY);
114         /* get the command to run */
115         sprintf (command_line, "%s -m : -v 1 %s -c %s %s", PATH_TO_SNMPGET, address,
116                                          community, query_string);
118         /* run the command */
119         child_process = spopen (command_line);
120         if (child_process == NULL) {
121                 printf ("Could not open pipe: %s\n", command_line);
122                 return STATE_UNKNOWN;
123         }
125         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
126         if (child_stderr == NULL) {
127                 printf ("Could not open stderr for %s\n", command_line);
128         }
130         result = STATE_OK;
132         line = 0;
133         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
135                 /* strip the newline character from the end of the input */
136                 if (input_buffer[strlen (input_buffer) - 1] == '\n')
137                         input_buffer[strlen (input_buffer) - 1] = 0;
139                 line++;
141                 temp_buffer = strtok (input_buffer, "=");
142                 temp_buffer = strtok (NULL, "=");
144                 switch (line) {
146                 case 1:                                                                         /* 1st line should contain the line status */
147                         if (temp_buffer != NULL)
148                                 line_status = atoi (temp_buffer);
149                         else {
150                                 result = STATE_UNKNOWN;
151                                 strcpy (error_message, input_buffer);
152                         }
153                         break;
155                 case 2:                                                                         /* 2nd line should contain the paper status */
156                         if (temp_buffer != NULL)
157                                 paper_status = atoi (temp_buffer);
158                         else {
159                                 result = STATE_UNKNOWN;
160                                 strcpy (error_message, input_buffer);
161                         }
162                         break;
164                 case 3:                                                                         /* 3rd line should be intervention required */
165                         if (temp_buffer != NULL)
166                                 intervention_required = atoi (temp_buffer);
167                         else {
168                                 result = STATE_UNKNOWN;
169                                 strcpy (error_message, input_buffer);
170                         }
171                         break;
173                 case 4:                                                                         /* 4th line should be peripheral error */
174                         if (temp_buffer != NULL)
175                                 peripheral_error = atoi (temp_buffer);
176                         else {
177                                 result = STATE_UNKNOWN;
178                                 strcpy (error_message, input_buffer);
179                         }
180                         break;
182                 case 5:                                                                         /* 5th line should contain the paper jam status */
183                         if (temp_buffer != NULL)
184                                 paper_jam = atoi (temp_buffer);
185                         else {
186                                 result = STATE_UNKNOWN;
187                                 strcpy (error_message, input_buffer);
188                         }
189                         break;
191                 case 6:                                                                         /* 6th line should contain the paper out status */
192                         if (temp_buffer != NULL)
193                                 paper_out = atoi (temp_buffer);
194                         else {
195                                 result = STATE_UNKNOWN;
196                                 strcpy (error_message, input_buffer);
197                         }
198                         break;
200                 case 7:                                                                         /* 7th line should contain the toner low status */
201                         if (temp_buffer != NULL)
202                                 toner_low = atoi (temp_buffer);
203                         else {
204                                 result = STATE_UNKNOWN;
205                                 strcpy (error_message, input_buffer);
206                         }
207                         break;
209                 case 8:                                                                         /* did data come too slow for engine */
210                         if (temp_buffer != NULL)
211                                 page_punt = atoi (temp_buffer);
212                         else {
213                                 result = STATE_UNKNOWN;
214                                 strcpy (error_message, input_buffer);
215                         }
216                         break;
218                 case 9:                                                                         /* did we run out of memory */
219                         if (temp_buffer != NULL)
220                                 memory_out = atoi (temp_buffer);
221                         else {
222                                 result = STATE_UNKNOWN;
223                                 strcpy (error_message, input_buffer);
224                         }
225                         break;
227                 case 10:                                                                                /* is there a door open */
228                         if (temp_buffer != NULL)
229                                 door_open = atoi (temp_buffer);
230                         else {
231                                 result = STATE_UNKNOWN;
232                                 strcpy (error_message, input_buffer);
233                         }
234                         break;
236                 case 11:                                                                                /* is output tray full */
237                         if (temp_buffer != NULL)
238                                 paper_output = atoi (temp_buffer);
239                         else {
240                                 result = STATE_UNKNOWN;
241                                 strcpy (error_message, input_buffer);
242                         }
243                         break;
245                 case 12:                                                                                /* display panel message */
246                         if (temp_buffer != NULL)
247                                 strcpy (display_message, temp_buffer + 1);
248                         else {
249                                 result = STATE_UNKNOWN;
250                                 strcpy (error_message, input_buffer);
251                         }
252                         break;
254                 default:
255                         break;
256                 }
258                 /* break out of the read loop if we encounter an error */
259                 if (result != STATE_OK)
260                         break;
261         }
263         /* WARNING if output found on stderr */
264         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
265                 result = max_state (result, STATE_WARNING);
267         /* close stderr */
268         (void) fclose (child_stderr);
270         /* close the pipe */
271         if (spclose (child_process))
272                 result = max_state (result, STATE_WARNING);
274         /* if there wasn't any output, display an error */
275         if (line == 0) {
277                 /*
278                    result=STATE_UNKNOWN;
279                    strcpy(error_message,"Error: Could not read plugin output\n");
280                  */
282                 /* might not be the problem, but most likely is.. */
283                 result = STATE_UNKNOWN;
284                 sprintf (error_message, "Timeout: No response from %s\n", address);
285         }
287         /* if we had no read errors, check the printer status results... */
288         if (result == STATE_OK) {
290                 if (paper_jam) {
291                         result = STATE_WARNING;
292                         strcpy (error_message, "Paper Jam");
293                 }
294                 else if (paper_out) {
295                         result = STATE_WARNING;
296                         strcpy (error_message, "Out of Paper");
297                 }
298                 else if (line_status == OFFLINE) {
299                         if (strcmp (error_message, "POWERSAVE ON") != 0) {
300                                 result = STATE_WARNING;
301                                 strcpy (error_message, "Printer Offline");
302                         }
303                 }
304                 else if (peripheral_error) {
305                         result = STATE_WARNING;
306                         strcpy (error_message, "Peripheral Error");
307                 }
308                 else if (intervention_required) {
309                         result = STATE_WARNING;
310                         strcpy (error_message, "Intervention Required");
311                 }
312                 else if (toner_low) {
313                         result = STATE_WARNING;
314                         strcpy (error_message, "Toner Low");
315                 }
316                 else if (memory_out) {
317                         result = STATE_WARNING;
318                         strcpy (error_message, "Insufficient Memory");
319                 }
320                 else if (door_open) {
321                         result = STATE_WARNING;
322                         strcpy (error_message, "A Door is Open");
323                 }
324                 else if (paper_output) {
325                         result = STATE_WARNING;
326                         strcpy (error_message, "Output Tray is Full");
327                 }
328                 else if (page_punt) {
329                         result = STATE_WARNING;
330                         strcpy (error_message, "Data too Slow for Engine");
331                 }
332                 else if (paper_status) {
333                         result = STATE_WARNING;
334                         strcpy (error_message, "Unknown Paper Error");
335                 }
336         }
338         if (result == STATE_OK)
339                 printf ("Printer ok - (%s)\n", display_message);
341         else if (result == STATE_UNKNOWN) {
343                 printf ("%s\n", error_message);
345                 /* if printer could not be reached, escalate to critical */
346                 if (strstr (error_message, "Timeout"))
347                         result = STATE_CRITICAL;
348         }
350         else if (result == STATE_WARNING)
351                 printf ("%s (%s)\n", error_message, display_message);
353         return result;
360 /* process command-line arguments */
361 int
362 process_arguments (int argc, char **argv)
364         int c;
366         int option_index = 0;
367         static struct option long_options[] = {
368                 {"hostname", required_argument, 0, 'H'},
369                 {"community", required_argument, 0, 'C'},
370 /*              {"critical",       required_argument,0,'c'}, */
371 /*              {"warning",        required_argument,0,'w'}, */
372 /*              {"port",           required_argument,0,'P'}, */
373                 {"version", no_argument, 0, 'V'},
374                 {"help", no_argument, 0, 'h'},
375                 {0, 0, 0, 0}
376         };
378         if (argc < 2)
379                 return ERROR;
381         
382         while (1) {
383                 c = getopt_long (argc, argv, "+hVH:C:", long_options, &option_index);
385                 if (c == -1 || c == EOF || c == 1)
386                         break;
388                 switch (c) {
389                 case 'H':                                                                       /* hostname */
390                         if (is_host (optarg)) {
391                                 address = strscpy(address, optarg) ;
392                         }
393                         else {
394                                 usage ("Invalid host name\n");
395                         }
396                         break;
397                 case 'C':                                                                       /* community */
398                         community = strscpy (community, optarg);
399                         break;
400                 case 'V':                                                                       /* version */
401                         print_revision (progname, revision);
402                         exit (STATE_OK);
403                 case 'h':                                                                       /* help */
404                         print_help ();
405                         exit (STATE_OK);
406                 case '?':                                                                       /* help */
407                         usage ("Invalid argument\n");
408                 }
409         }
411         c = optind;
412         if (address == NULL) {
413                 if (is_host (argv[c])) {
414                         address = argv[c++];
415                 }
416                 else {
417                         usage ("Invalid host name");
418                 }
419         }
420         
421         if (argv[c] != NULL ) {
422                 community = argv[c];
423         }
425         return validate_arguments ();
432 int
433 validate_arguments (void)
435         return OK;
442 void
443 print_help (void)
445         print_revision (progname, revision);
446         printf ("Copyright (c) %s %s\n\t<%s>\n\n", copyright, authors, email);
447         printf (summary);
448         print_usage ();
449         printf ("\nOptions:\n");
450         printf (options, DEFAULT_COMMUNITY);
451         support ();
454 void
455 print_usage (void)
457         printf ("\
458 Usage:\n\
459  %s %s\n\
460  %s (-h | --help) for detailed help\n\
461  %s (-V | --version) for version information\n",
462                 progname, option_summary, progname, progname);
466 /*
467         if(argc<2||argc>3){
468         printf("Incorrect number of arguments supplied\n");
469         print_revision(argv[0],"$Revision$");
470         printf("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n");
471         printf("License: GPL\n");
472         printf("Usage: %s <ip_address> [community]\n",argv[0]);
473         printf("Note:\n");
474         printf(" <ip_address>     = The IP address of the JetDirect card\n");
475         printf(" [community]      = An optional community string used for SNMP communication\n");
476         printf("                    with the JetDirect card.  The default is 'public'.\n");
477         return STATE_UNKNOWN;
478         }
479         // get the IP address of the JetDirect device
480         strcpy(address,argv[1]);
481         // get the community name to use for SNMP communication
482         if(argc>=3)
483         strcpy(community,argv[2]);
484         else
485         strcpy(community,"public");
486 */