Code

b281c33a0694cb7c07bad6c7af6c1ec36f5e4c4b
[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_OUT          ".1.3.6.1.4.1.11.2.3.9.1.1.2.8"
40 #define HPJD_GD_PAPER_JAM          ".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 && line < 13) {
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:                                                                                /* fold multiline message */
183                                 strncat (display_message, input_buffer, 
184                                                 sizeof (display_message) - strlen (display_message) - 1);
185                         }
187                 }
189                 /* break out of the read loop if we encounter an error */
190                 if (result != STATE_OK)
191                         break;
192         }
194         /* WARNING if output found on stderr */
195         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
196                 result = max_state (result, STATE_WARNING);
197                 /* remove CRLF */
198                 if (input_buffer[strlen (input_buffer) - 1] == '\n')
199                         input_buffer[strlen (input_buffer) - 1] = 0;
200                 sprintf (errmsg, "%s", input_buffer );
202         }
203         
204         /* close stderr */
205         (void) fclose (child_stderr);
207         /* close the pipe */
208         if (spclose (child_process))
209                 result = max_state (result, STATE_WARNING);
211         /* if there wasn't any output, display an error */
212         if (line == 0) {
214                 /* might not be the problem, but most likely is. */
215                 result = STATE_UNKNOWN ;
216                 asprintf (&errmsg, "%s : Timeout from host %s\n", errmsg, address );
217                  
218         }
220         /* if we had no read errors, check the printer status results... */
221         if (result == STATE_OK) {
223                 if (paper_jam) {
224                         result = STATE_WARNING;
225                         strcpy (errmsg, _("Paper Jam"));
226                 }
227                 else if (paper_out) {
228                         result = STATE_WARNING;
229                         strcpy (errmsg, _("Out of Paper"));
230                 }
231                 else if (line_status == OFFLINE) {
232                         if (strcmp (errmsg, "POWERSAVE ON") != 0) {
233                                 result = STATE_WARNING;
234                                 strcpy (errmsg, _("Printer Offline"));
235                         }
236                 }
237                 else if (peripheral_error) {
238                         result = STATE_WARNING;
239                         strcpy (errmsg, _("Peripheral Error"));
240                 }
241                 else if (intervention_required) {
242                         result = STATE_WARNING;
243                         strcpy (errmsg, _("Intervention Required"));
244                 }
245                 else if (toner_low) {
246                         result = STATE_WARNING;
247                         strcpy (errmsg, _("Toner Low"));
248                 }
249                 else if (memory_out) {
250                         result = STATE_WARNING;
251                         strcpy (errmsg, _("Insufficient Memory"));
252                 }
253                 else if (door_open) {
254                         result = STATE_WARNING;
255                         strcpy (errmsg, _("A Door is Open"));
256                 }
257                 else if (paper_output) {
258                         result = STATE_WARNING;
259                         strcpy (errmsg, _("Output Tray is Full"));
260                 }
261                 else if (page_punt) {
262                         result = STATE_WARNING;
263                         strcpy (errmsg, _("Data too Slow for Engine"));
264                 }
265                 else if (paper_status) {
266                         result = STATE_WARNING;
267                         strcpy (errmsg, _("Unknown Paper Error"));
268                 }
269         }
271         if (result == STATE_OK)
272                 printf (_("Printer ok - (%s)\n"), display_message);
274         else if (result == STATE_UNKNOWN) {
276                 printf ("%s\n", errmsg);
278                 /* if printer could not be reached, escalate to critical */
279                 if (strstr (errmsg, "Timeout"))
280                         result = STATE_CRITICAL;
281         }
283         else if (result == STATE_WARNING)
284                 printf ("%s (%s)\n", errmsg, display_message);
286         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                         usage2 (_("Unknown argument"), optarg);
338                 }
339         }
341         c = optind;
342         if (address == NULL) {
343                 if (is_host (argv[c])) {
344                         address = argv[c++];
345                 }
346                 else {
347                         usage2 (_("Invalid hostname/address"), argv[c]);
348                 }
349         }
350         
351         if (community == NULL) {
352                 if (argv[c] != NULL )
353                         community = argv[c];
354                 else
355                         community = strdup (DEFAULT_COMMUNITY);
356         }
358         return validate_arguments ();
362 int
363 validate_arguments (void)
365         return OK;
369 void
370 print_help (void)
372         print_revision (progname, revision);
374         printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
375         printf (COPYRIGHT, copyright, email);
377         printf (_("\
378 This plugin tests the STATUS of an HP printer with a JetDirect card.\n\
379 Net-snmp must be installed on the computer running the plugin.\n\n"));
381         print_usage ();
383         printf (_(UT_HELP_VRSN));
385         printf (_("\
386  -C, --community=STRING\n\
387     The SNMP community name (default=%s)\n"), DEFAULT_COMMUNITY);
389         printf (_(UT_SUPPORT));
394 void
395 print_usage (void)
397         printf ("Usage: %s -H host [-C community]\n", progname);