Code

Fix translations when extra-opts aren't enabled
[nagiosplug.git] / plugins / check_hpjd.c
1 /*****************************************************************************
2
3 * Nagios check_hpjd plugin
4
5 * License: GPL
6 * Copyright (c) 2000-2007 Nagios Plugins Development Team
7
8 * Description:
9
10 * This file contains the check_hpjd plugin
11
12 * This plugin tests the STATUS of an HP printer with a JetDirect card.
13 * Net-SNMP must be installed on the computer running the plugin.
14
15
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
20
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 * GNU General Public License for more details.
25
26 * You should have received a copy of the GNU General Public License
27 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28
29
30 *****************************************************************************/
32 const char *progname = "check_hpjd";
33 const char *copyright = "2000-2007";
34 const char *email = "nagiosplug-devel@lists.sourceforge.net";
36 #include "common.h"
37 #include "popen.h"
38 #include "utils.h"
39 #include "netutils.h"
41 #define DEFAULT_COMMUNITY "public"
44 const char *option_summary = "-H host [-C community]\n";
46 #define HPJD_LINE_STATUS           ".1.3.6.1.4.1.11.2.3.9.1.1.2.1"
47 #define HPJD_PAPER_STATUS          ".1.3.6.1.4.1.11.2.3.9.1.1.2.2"
48 #define HPJD_INTERVENTION_REQUIRED ".1.3.6.1.4.1.11.2.3.9.1.1.2.3"
49 #define HPJD_GD_PERIPHERAL_ERROR   ".1.3.6.1.4.1.11.2.3.9.1.1.2.6"
50 #define HPJD_GD_PAPER_OUT          ".1.3.6.1.4.1.11.2.3.9.1.1.2.8"
51 #define HPJD_GD_PAPER_JAM          ".1.3.6.1.4.1.11.2.3.9.1.1.2.9"
52 #define HPJD_GD_TONER_LOW          ".1.3.6.1.4.1.11.2.3.9.1.1.2.10"
53 #define HPJD_GD_PAGE_PUNT          ".1.3.6.1.4.1.11.2.3.9.1.1.2.11"
54 #define HPJD_GD_MEMORY_OUT         ".1.3.6.1.4.1.11.2.3.9.1.1.2.12"
55 #define HPJD_GD_DOOR_OPEN          ".1.3.6.1.4.1.11.2.3.9.1.1.2.17"
56 #define HPJD_GD_PAPER_OUTPUT       ".1.3.6.1.4.1.11.2.3.9.1.1.2.19"
57 #define HPJD_GD_STATUS_DISPLAY     ".1.3.6.1.4.1.11.2.3.9.1.1.3"
59 #define ONLINE          0
60 #define OFFLINE         1
62 int process_arguments (int, char **);
63 int validate_arguments (void);
64 void print_help (void);
65 void print_usage (void);
67 char *community = NULL;
68 char *address = NULL;
70 int
71 main (int argc, char **argv)
72 {
73         char command_line[1024];
74         int result = STATE_UNKNOWN;
75         int line;
76         char input_buffer[MAX_INPUT_BUFFER];
77         char query_string[512];
78         char *errmsg;
79         char *temp_buffer;
80         int line_status = ONLINE;
81         int paper_status = 0;
82         int intervention_required = 0;
83         int peripheral_error = 0;
84         int paper_jam = 0;
85         int paper_out = 0;
86         int toner_low = 0;
87         int page_punt = 0;
88         int memory_out = 0;
89         int door_open = 0;
90         int paper_output = 0;
91         char display_message[MAX_INPUT_BUFFER];
93         errmsg = malloc(MAX_INPUT_BUFFER);
95         setlocale (LC_ALL, "");
96         bindtextdomain (PACKAGE, LOCALEDIR);
97         textdomain (PACKAGE);
99         /* Parse extra opts if any */
100         argv=np_extra_opts (&argc, argv, progname);
102         if (process_arguments (argc, argv) == ERROR)
103                 usage4 (_("Could not parse arguments"));
105         /* removed ' 2>1' at end of command 10/27/1999 - EG */
106         /* create the query string */
107         sprintf
108                 (query_string,
109                  "%s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0",
110                  HPJD_LINE_STATUS,
111                  HPJD_PAPER_STATUS,
112                  HPJD_INTERVENTION_REQUIRED,
113                  HPJD_GD_PERIPHERAL_ERROR,
114                  HPJD_GD_PAPER_JAM,
115                  HPJD_GD_PAPER_OUT,
116                  HPJD_GD_TONER_LOW,
117                  HPJD_GD_PAGE_PUNT,
118                  HPJD_GD_MEMORY_OUT,
119                  HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, HPJD_GD_STATUS_DISPLAY);
121         /* get the command to run */
122         sprintf (command_line, "%s -OQa -m : -v 1 -c %s %s %s", PATH_TO_SNMPGET, community,
123                                                                         address, query_string);
125         /* run the command */
126         child_process = spopen (command_line);
127         if (child_process == NULL) {
128                 printf (_("Could not open pipe: %s\n"), command_line);
129                 return STATE_UNKNOWN;
130         }
132         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
133         if (child_stderr == NULL) {
134                 printf (_("Could not open stderr for %s\n"), command_line);
135         }
137         result = STATE_OK;
139         line = 0;
140         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
142                 /* strip the newline character from the end of the input */
143                 if (input_buffer[strlen (input_buffer) - 1] == '\n')
144                         input_buffer[strlen (input_buffer) - 1] = 0;
146                 line++;
148                 temp_buffer = strtok (input_buffer, "=");
149                 temp_buffer = strtok (NULL, "=");
151                 if (temp_buffer == NULL && line < 13) {
153                                 result = STATE_UNKNOWN;
154                                 strcpy (errmsg, input_buffer);
156                 } else {
158                         switch (line) {
160                         case 1:                                                                         /* 1st line should contain the line status */
161                                 line_status = atoi (temp_buffer);
162                                 break;
163                         case 2:                                                                         /* 2nd line should contain the paper status */
164                                 paper_status = atoi (temp_buffer);
165                                 break;
166                         case 3:                                                                         /* 3rd line should be intervention required */
167                                 intervention_required = atoi (temp_buffer);
168                                 break;
169                         case 4:                                                                         /* 4th line should be peripheral error */
170                                 peripheral_error = atoi (temp_buffer);
171                                 break;
172                         case 5:                                                                         /* 5th line should contain the paper jam status */
173                                 paper_jam = atoi (temp_buffer);
174                                 break;
175                         case 6:                                                                         /* 6th line should contain the paper out status */
176                                 paper_out = atoi (temp_buffer);
177                                 break;
178                         case 7:                                                                         /* 7th line should contain the toner low status */
179                                 toner_low = atoi (temp_buffer);
180                                 break;
181                         case 8:                                                                         /* did data come too slow for engine */
182                                 page_punt = atoi (temp_buffer);
183                                 break;
184                         case 9:                                                                         /* did we run out of memory */
185                                 memory_out = atoi (temp_buffer);
186                                 break;
187                         case 10:                                                                                /* is there a door open */
188                                 door_open = atoi (temp_buffer);
189                                 break;
190                         case 11:                                                                                /* is output tray full */
191                                 paper_output = atoi (temp_buffer);
192                                 break;
193                         case 12:                                                                                /* display panel message */
194                                 strcpy (display_message, temp_buffer + 1);
195                                 break;
196                         default:                                                                                /* fold multiline message */
197                                 strncat (display_message, input_buffer,
198                                                 sizeof (display_message) - strlen (display_message) - 1);
199                         }
201                 }
203                 /* break out of the read loop if we encounter an error */
204                 if (result != STATE_OK)
205                         break;
206         }
208         /* WARNING if output found on stderr */
209         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
210                 result = max_state (result, STATE_WARNING);
211                 /* remove CRLF */
212                 if (input_buffer[strlen (input_buffer) - 1] == '\n')
213                         input_buffer[strlen (input_buffer) - 1] = 0;
214                 sprintf (errmsg, "%s", input_buffer );
216         }
218         /* close stderr */
219         (void) fclose (child_stderr);
221         /* close the pipe */
222         if (spclose (child_process))
223                 result = max_state (result, STATE_WARNING);
225         /* if there wasn't any output, display an error */
226         if (line == 0) {
228                 /* might not be the problem, but most likely is. */
229                 result = STATE_UNKNOWN ;
230                 asprintf (&errmsg, "%s : Timeout from host %s\n", errmsg, address );
232         }
234         /* if we had no read errors, check the printer status results... */
235         if (result == STATE_OK) {
237                 if (paper_jam) {
238                         result = STATE_WARNING;
239                         strcpy (errmsg, _("Paper Jam"));
240                 }
241                 else if (paper_out) {
242                         result = STATE_WARNING;
243                         strcpy (errmsg, _("Out of Paper"));
244                 }
245                 else if (line_status == OFFLINE) {
246                         if (strcmp (errmsg, "POWERSAVE ON") != 0) {
247                                 result = STATE_WARNING;
248                                 strcpy (errmsg, _("Printer Offline"));
249                         }
250                 }
251                 else if (peripheral_error) {
252                         result = STATE_WARNING;
253                         strcpy (errmsg, _("Peripheral Error"));
254                 }
255                 else if (intervention_required) {
256                         result = STATE_WARNING;
257                         strcpy (errmsg, _("Intervention Required"));
258                 }
259                 else if (toner_low) {
260                         result = STATE_WARNING;
261                         strcpy (errmsg, _("Toner Low"));
262                 }
263                 else if (memory_out) {
264                         result = STATE_WARNING;
265                         strcpy (errmsg, _("Insufficient Memory"));
266                 }
267                 else if (door_open) {
268                         result = STATE_WARNING;
269                         strcpy (errmsg, _("A Door is Open"));
270                 }
271                 else if (paper_output) {
272                         result = STATE_WARNING;
273                         strcpy (errmsg, _("Output Tray is Full"));
274                 }
275                 else if (page_punt) {
276                         result = STATE_WARNING;
277                         strcpy (errmsg, _("Data too Slow for Engine"));
278                 }
279                 else if (paper_status) {
280                         result = STATE_WARNING;
281                         strcpy (errmsg, _("Unknown Paper Error"));
282                 }
283         }
285         if (result == STATE_OK)
286                 printf (_("Printer ok - (%s)\n"), display_message);
288         else if (result == STATE_UNKNOWN) {
290                 printf ("%s\n", errmsg);
292                 /* if printer could not be reached, escalate to critical */
293                 if (strstr (errmsg, "Timeout"))
294                         result = STATE_CRITICAL;
295         }
297         else if (result == STATE_WARNING)
298                 printf ("%s (%s)\n", errmsg, display_message);
300         return result;
304 /* process command-line arguments */
305 int
306 process_arguments (int argc, char **argv)
308         int c;
310         int option = 0;
311         static struct option longopts[] = {
312                 {"hostname", required_argument, 0, 'H'},
313                 {"community", required_argument, 0, 'C'},
314 /*              {"critical",       required_argument,0,'c'}, */
315 /*              {"warning",        required_argument,0,'w'}, */
316 /*              {"port",           required_argument,0,'P'}, */
317                 {"version", no_argument, 0, 'V'},
318                 {"help", no_argument, 0, 'h'},
319                 {0, 0, 0, 0}
320         };
322         if (argc < 2)
323                 return ERROR;
326         while (1) {
327                 c = getopt_long (argc, argv, "+hVH:C:", longopts, &option);
329                 if (c == -1 || c == EOF || c == 1)
330                         break;
332                 switch (c) {
333                 case 'H':                                                                       /* hostname */
334                         if (is_host (optarg)) {
335                                 address = strscpy(address, optarg) ;
336                         }
337                         else {
338                                 usage2 (_("Invalid hostname/address"), optarg);
339                         }
340                         break;
341                 case 'C':                                                                       /* community */
342                         community = strscpy (community, optarg);
343                         break;
344                 case 'V':                                                                       /* version */
345                         print_revision (progname, NP_VERSION);
346                         exit (STATE_OK);
347                 case 'h':                                                                       /* help */
348                         print_help ();
349                         exit (STATE_OK);
350                 case '?':                                                                       /* help */
351                         usage5 ();
352                 }
353         }
355         c = optind;
356         if (address == NULL) {
357                 if (is_host (argv[c])) {
358                         address = argv[c++];
359                 }
360                 else {
361                         usage2 (_("Invalid hostname/address"), argv[c]);
362                 }
363         }
365         if (community == NULL) {
366                 if (argv[c] != NULL )
367                         community = argv[c];
368                 else
369                         community = strdup (DEFAULT_COMMUNITY);
370         }
372         return validate_arguments ();
376 int
377 validate_arguments (void)
379         return OK;
383 void
384 print_help (void)
386         print_revision (progname, NP_VERSION);
388         printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
389         printf (COPYRIGHT, copyright, email);
391         printf ("%s\n", _("This plugin tests the STATUS of an HP printer with a JetDirect card."));
392         printf ("%s\n", _("Net-snmp must be installed on the computer running the plugin."));
394         printf ("\n\n");
396         print_usage ();
398         printf (UT_HELP_VRSN);
399         printf (UT_EXTRA_OPTS);
401         printf (" %s\n", "-C, --community=STRING");
402         printf ("    %s", _("The SNMP community name "));
403         printf (_("(default=%s)"), DEFAULT_COMMUNITY);
404         printf ("\n");
406 #ifdef NP_EXTRA_OPTS
407         printf ("\n");
408         printf ("%s\n", _("Notes:"));
409         printf (UT_EXTRA_OPTS_NOTES);
410 #endif
412         printf (UT_SUPPORT);
417 void
418 print_usage (void)
420   printf (_("Usage:"));
421         printf ("%s -H host [-C community]\n", progname);