Code

reverting my changes from !=TRUE to == ERROR, that's not good ;-( sorry
[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 * $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         }
202         
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 );
216                  
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;
290 /* process command-line arguments */
291 int
292 process_arguments (int argc, char **argv)
294         int c;
296         int option = 0;
297         static struct option longopts[] = {
298                 {"hostname", required_argument, 0, 'H'},
299                 {"community", required_argument, 0, 'C'},
300 /*              {"critical",       required_argument,0,'c'}, */
301 /*              {"warning",        required_argument,0,'w'}, */
302 /*              {"port",           required_argument,0,'P'}, */
303                 {"version", no_argument, 0, 'V'},
304                 {"help", no_argument, 0, 'h'},
305                 {0, 0, 0, 0}
306         };
308         if (argc < 2)
309                 return ERROR;
311         
312         while (1) {
313                 c = getopt_long (argc, argv, "+hVH:C:", longopts, &option);
315                 if (c == -1 || c == EOF || c == 1)
316                         break;
318                 switch (c) {
319                 case 'H':                                                                       /* hostname */
320                         if (is_host (optarg)) {
321                                 address = strscpy(address, optarg) ;
322                         }
323                         else {
324                                 usage2 (_("Invalid hostname/address"), optarg);
325                         }
326                         break;
327                 case 'C':                                                                       /* community */
328                         community = strscpy (community, optarg);
329                         break;
330                 case 'V':                                                                       /* version */
331                         print_revision (progname, revision);
332                         exit (STATE_OK);
333                 case 'h':                                                                       /* help */
334                         print_help ();
335                         exit (STATE_OK);
336                 case '?':                                                                       /* help */
337                         printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
338                         print_usage ();
339                         exit (STATE_UNKNOWN);
340                 }
341         }
343         c = optind;
344         if (address == NULL) {
345                 if (is_host (argv[c])) {
346                         address = argv[c++];
347                 }
348                 else {
349                         usage2 (_("Invalid hostname/address"), argv[c]);
350                 }
351         }
352         
353         if (community == NULL) {
354                 if (argv[c] != NULL )
355                         community = argv[c];
356                 else
357                         community = strdup (DEFAULT_COMMUNITY);
358         }
360         return validate_arguments ();
365 int
366 validate_arguments (void)
368         return OK;
373 void
374 print_help (void)
376         print_revision (progname, revision);
378         printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
379         printf (COPYRIGHT, copyright, email);
381         printf (_("\
382 This plugin tests the STATUS of an HP printer with a JetDirect card.\n\
383 Net-snmp must be installed on the computer running the plugin.\n\n"));
385         print_usage ();
387         printf (_(UT_HELP_VRSN));
389         printf (_("\
390  -C, --community=STRING\n\
391     The SNMP community name (default=%s)\n"), DEFAULT_COMMUNITY);
393         printf (_(UT_SUPPORT));
398 void
399 print_usage (void)
401         printf ("Usage: %s -H host [-C community]\n"), progname);