Code

5fdfc8ac3c59e0ffed631fc001f58aa7595dc783
[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 #include "common.h"
21 #include "popen.h"
22 #include "utils.h"
23 #include "netutils.h"
25 #define DEFAULT_COMMUNITY "public"
27 const char *progname = "check_hpjd";
28 const char *revision = "$Revision$";
29 const char *copyright = "2000-2003";
30 const char *email = "nagiosplug-devel@lists.sourceforge.net";
32 const char *option_summary = "-H host [-C community]\n";
34 #define HPJD_LINE_STATUS           ".1.3.6.1.4.1.11.2.3.9.1.1.2.1"
35 #define HPJD_PAPER_STATUS          ".1.3.6.1.4.1.11.2.3.9.1.1.2.2"
36 #define HPJD_INTERVENTION_REQUIRED ".1.3.6.1.4.1.11.2.3.9.1.1.2.3"
37 #define HPJD_GD_PERIPHERAL_ERROR   ".1.3.6.1.4.1.11.2.3.9.1.1.2.6"
38 #define HPJD_GD_PAPER_JAM          ".1.3.6.1.4.1.11.2.3.9.1.1.2.8"
39 #define HPJD_GD_PAPER_OUT          ".1.3.6.1.4.1.11.2.3.9.1.1.2.9"
40 #define HPJD_GD_TONER_LOW          ".1.3.6.1.4.1.11.2.3.9.1.1.2.10"
41 #define HPJD_GD_PAGE_PUNT          ".1.3.6.1.4.1.11.2.3.9.1.1.2.11"
42 #define HPJD_GD_MEMORY_OUT         ".1.3.6.1.4.1.11.2.3.9.1.1.2.12"
43 #define HPJD_GD_DOOR_OPEN          ".1.3.6.1.4.1.11.2.3.9.1.1.2.17"
44 #define HPJD_GD_PAPER_OUTPUT       ".1.3.6.1.4.1.11.2.3.9.1.1.2.19"
45 #define HPJD_GD_STATUS_DISPLAY     ".1.3.6.1.4.1.11.2.3.9.1.1.3"
47 #define ONLINE          0
48 #define OFFLINE         1
50 int process_arguments (int, char **);
51 int validate_arguments (void);
52 void print_help (void);
53 void print_usage (void);
55 char *community = NULL;
56 char *address = NULL;
58 int
59 main (int argc, char **argv)
60 {
61         char command_line[1024];
62         int result;
63         int line;
64         char input_buffer[MAX_INPUT_BUFFER];
65         char query_string[512];
66         char *errmsg;
67         char *temp_buffer;
68         int line_status = ONLINE;
69         int paper_status = 0;
70         int intervention_required = 0;
71         int peripheral_error = 0;
72         int paper_jam = 0;
73         int paper_out = 0;
74         int toner_low = 0;
75         int page_punt = 0;
76         int memory_out = 0;
77         int door_open = 0;
78         int paper_output = 0;
79         char display_message[MAX_INPUT_BUFFER];
81         errmsg = malloc(MAX_INPUT_BUFFER);
83         setlocale (LC_ALL, "");
84         bindtextdomain (PACKAGE, LOCALEDIR);
85         textdomain (PACKAGE);
87         if (process_arguments (argc, argv) != TRUE)
88                 usage (_("check_hpjd: could not parse arguments\n"));
90         /* removed ' 2>1' at end of command 10/27/1999 - EG */
91         /* create the query string */
92         sprintf
93                 (query_string,
94                  "%s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0",
95                  HPJD_LINE_STATUS,
96                  HPJD_PAPER_STATUS,
97                  HPJD_INTERVENTION_REQUIRED,
98                  HPJD_GD_PERIPHERAL_ERROR,
99                  HPJD_GD_PAPER_JAM,
100                  HPJD_GD_PAPER_OUT,
101                  HPJD_GD_TONER_LOW,
102                  HPJD_GD_PAGE_PUNT,
103                  HPJD_GD_MEMORY_OUT,
104                  HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, HPJD_GD_STATUS_DISPLAY);
106         /* get the command to run */
107         sprintf (command_line, "%s -OQa -m : -v 1 -c %s %s %s", PATH_TO_SNMPGET, community, 
108                                                                         address, query_string);
110         /* run the command */
111         child_process = spopen (command_line);
112         if (child_process == NULL) {
113                 printf (_("Could not open pipe: %s\n"), command_line);
114                 return STATE_UNKNOWN;
115         }
117         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
118         if (child_stderr == NULL) {
119                 printf (_("Could not open stderr for %s\n"), command_line);
120         }
122         result = STATE_OK;
124         line = 0;
125         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
127                 /* strip the newline character from the end of the input */
128                 if (input_buffer[strlen (input_buffer) - 1] == '\n')
129                         input_buffer[strlen (input_buffer) - 1] = 0;
131                 line++;
133                 temp_buffer = strtok (input_buffer, "=");
134                 temp_buffer = strtok (NULL, "=");
136                 if (temp_buffer == NULL) {
138                                 result = STATE_UNKNOWN;
139                                 strcpy (errmsg, input_buffer);
141                 } else {
143                         switch (line) {
145                         case 1:                                                                         /* 1st line should contain the line status */
146                                 line_status = atoi (temp_buffer);
147                                 break;
148                         case 2:                                                                         /* 2nd line should contain the paper status */
149                                 paper_status = atoi (temp_buffer);
150                                 break;
151                         case 3:                                                                         /* 3rd line should be intervention required */
152                                 intervention_required = atoi (temp_buffer);
153                                 break;
154                         case 4:                                                                         /* 4th line should be peripheral error */
155                                 peripheral_error = atoi (temp_buffer);
156                                 break;
157                         case 5:                                                                         /* 5th line should contain the paper jam status */
158                                 paper_jam = atoi (temp_buffer);
159                                 break;
160                         case 6:                                                                         /* 6th line should contain the paper out status */
161                                 paper_out = atoi (temp_buffer);
162                                 break;
163                         case 7:                                                                         /* 7th line should contain the toner low status */
164                                 toner_low = atoi (temp_buffer);
165                                 break;
166                         case 8:                                                                         /* did data come too slow for engine */
167                                 page_punt = atoi (temp_buffer);
168                                 break;
169                         case 9:                                                                         /* did we run out of memory */
170                                 memory_out = atoi (temp_buffer);
171                                 break;
172                         case 10:                                                                                /* is there a door open */
173                                 door_open = atoi (temp_buffer);
174                                 break;
175                         case 11:                                                                                /* is output tray full */
176                                 paper_output = atoi (temp_buffer);
177                                 break;
178                         case 12:                                                                                /* display panel message */
179                                 strcpy (display_message, temp_buffer + 1);
180                                 break;
181                         default:
182                                 break;
183                         }
185                 }
187                 /* break out of the read loop if we encounter an error */
188                 if (result != STATE_OK)
189                         break;
190         }
192         /* WARNING if output found on stderr */
193         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
194                 result = max_state (result, STATE_WARNING);
195                 /* remove CRLF */
196                 if (input_buffer[strlen (input_buffer) - 1] == '\n')
197                         input_buffer[strlen (input_buffer) - 1] = 0;
198                 sprintf (errmsg, "%s", input_buffer );
200         }
201         
202         /* close stderr */
203         (void) fclose (child_stderr);
205         /* close the pipe */
206         if (spclose (child_process))
207                 result = max_state (result, STATE_WARNING);
209         /* if there wasn't any output, display an error */
210         if (line == 0) {
212                 /* might not be the problem, but most likely is. */
213                 result = STATE_UNKNOWN ;
214                 asprintf (&errmsg, "%s : Timeout from host %s\n", errmsg, address );
215                  
216         }
218         /* if we had no read errors, check the printer status results... */
219         if (result == STATE_OK) {
221                 if (paper_jam) {
222                         result = STATE_WARNING;
223                         strcpy (errmsg, _("Paper Jam"));
224                 }
225                 else if (paper_out) {
226                         result = STATE_WARNING;
227                         strcpy (errmsg, _("Out of Paper"));
228                 }
229                 else if (line_status == OFFLINE) {
230                         if (strcmp (errmsg, "POWERSAVE ON") != 0) {
231                                 result = STATE_WARNING;
232                                 strcpy (errmsg, _("Printer Offline"));
233                         }
234                 }
235                 else if (peripheral_error) {
236                         result = STATE_WARNING;
237                         strcpy (errmsg, _("Peripheral Error"));
238                 }
239                 else if (intervention_required) {
240                         result = STATE_WARNING;
241                         strcpy (errmsg, _("Intervention Required"));
242                 }
243                 else if (toner_low) {
244                         result = STATE_WARNING;
245                         strcpy (errmsg, _("Toner Low"));
246                 }
247                 else if (memory_out) {
248                         result = STATE_WARNING;
249                         strcpy (errmsg, _("Insufficient Memory"));
250                 }
251                 else if (door_open) {
252                         result = STATE_WARNING;
253                         strcpy (errmsg, _("A Door is Open"));
254                 }
255                 else if (paper_output) {
256                         result = STATE_WARNING;
257                         strcpy (errmsg, _("Output Tray is Full"));
258                 }
259                 else if (page_punt) {
260                         result = STATE_WARNING;
261                         strcpy (errmsg, _("Data too Slow for Engine"));
262                 }
263                 else if (paper_status) {
264                         result = STATE_WARNING;
265                         strcpy (errmsg, _("Unknown Paper Error"));
266                 }
267         }
269         if (result == STATE_OK)
270                 printf (_("Printer ok - (%s)\n"), display_message);
272         else if (result == STATE_UNKNOWN) {
274                 printf ("%s\n", errmsg);
276                 /* if printer could not be reached, escalate to critical */
277                 if (strstr (errmsg, "Timeout"))
278                         result = STATE_CRITICAL;
279         }
281         else if (result == STATE_WARNING)
282                 printf ("%s (%s)\n", errmsg, display_message);
284         return result;
289 /* process command-line arguments */
290 int
291 process_arguments (int argc, char **argv)
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;
310         
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                         printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
337                         print_usage ();
338                         exit (STATE_UNKNOWN);
339                 }
340         }
342         c = optind;
343         if (address == NULL) {
344                 if (is_host (argv[c])) {
345                         address = argv[c++];
346                 }
347                 else {
348                         usage2 (_("Invalid hostname/address"), argv[c]);
349                 }
350         }
351         
352         if (community == NULL) {
353                 if (argv[c] != NULL )
354                         community = argv[c];
355                 else
356                         community = strdup (DEFAULT_COMMUNITY);
357         }
359         return validate_arguments ();
364 int
365 validate_arguments (void)
367         return OK;
372 void
373 print_help (void)
375         print_revision (progname, revision);
377         printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
378         printf (COPYRIGHT, copyright, email);
380         printf (_("\
381 This plugin tests the STATUS of an HP printer with a JetDirect card.\n\
382 Net-snmp must be installed on the computer running the plugin.\n\n"));
384         print_usage ();
386         printf (_(UT_HELP_VRSN));
388         printf (_("\
389  -C, --community=STRING\n\
390     The SNMP community name (default=%s)\n"), DEFAULT_COMMUNITY);
392         printf (_(UT_SUPPORT));
397 void
398 print_usage (void)
400         printf (_("\
401 Usage: %s -H host [-C community]\n"), progname);
402         printf (_(UT_HLP_VRS), progname, progname);