1 /******************************************************************************
2 *
3 * Program: SNMP plugin for Nagios
4 * License: GPL
5 *
6 * License Information:
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 *./plugins/check_snmp 127.0.0.1 -c public -o .1.3.6.1.4.1.2021.9.1.2.1
23 *
24 *****************************************************************************/
26 #define PROGNAME "check_snmp"
27 #define REVISION "$Revision$"
28 #define COPYRIGHT "1999-2002"
29 #define AUTHOR "Ethan Galstad"
30 #define EMAIL "nagios@nagios.org"
31 #define SUMMARY "Check status of remote machines using SNMP.\n"
33 #define OPTIONS "\
34 -H <ip_address> -o <OID> [-w warn_range] [-c crit_range] \n\
35 [-C community] [-s string] [-r regex] [-R regexi] [-t timeout]\n\
36 [-l label] [-u units] [-p port-number] [-d delimiter]\n\
37 [-D output-delimiter]"
39 #define LONGOPTIONS "\
40 -H, --hostname=HOST\n\
41 Name or IP address of the device you wish to query\n\
42 -o, --oid=OID(s)\n\
43 Object identifier(s) whose value you wish to query\n\
44 -w, --warning=INTEGER_RANGE(s)\n\
45 Range(s) which will not result in a WARNING status\n\
46 -c, --critical=INTEGER_RANGE(s)\n\
47 Range(s) which will not result in a CRITICAL status\n\
48 -C, --community=STRING\n\
49 Optional community string for SNMP communication\n\
50 (default is \"%s\")\n\
51 -u, --units=STRING\n\
52 Units label(s) for output data (e.g., 'sec.').\n\
53 -p, --port=STRING\n\
54 UDP port number target is listening on. Default is \"%s\"\n\
55 -d, --delimiter=STRING\n\
56 Delimiter to use when parsing returned data. Default is \"%s\"\n\
57 Any data on the right hand side of the delimiter is considered\n\
58 to be the data that should be used in the evaluation.\n\
59 -t, --timeout=INTEGER\n\
60 Seconds to wait before plugin times out (see also nagios server timeout).\n\
61 Default is %d seconds\n\
62 -D, --output-delimiter=STRING\n\
63 Separates output on multiple OID requests\n\
64 -s, --string=STRING\n\
65 Return OK state (for that OID) if STRING is an exact match\n\
66 -r, --ereg=REGEX\n\
67 Return OK state (for that OID) if extended regular expression REGEX matches\n\
68 -R, --eregi=REGEX\n\
69 Return OK state (for that OID) if case-insensitive extended REGEX matches\n\
70 -l, --label=STRING\n\
71 Prefix label for output from plugin (default -s 'SNMP')\n\
72 -v, --verbose\n\
73 Debugging the output\n\
75 "
77 #define NOTES "\
78 - This plugin uses the 'snmpget' command included with the NET-SNMP package.\n\
79 If you don't have the package installed, you will need to download it from\n\
80 http://net-snmp.sourceforge.net before you can use this plugin.\n\
81 - Multiple OIDs may be indicated by a comma- or space-delimited list (lists with\n\
82 internal spaces must be quoted) [max 8 OIDs]\n\
83 - Ranges are inclusive and are indicated with colons. When specified as\n\
84 'min:max' a STATE_OK will be returned if the result is within the indicated\n\
85 range or is equal to the upper or lower bound. A non-OK state will be\n\
86 returned if the result is outside the specified range.\n\
87 - If specified in the order 'max:min' a non-OK state will be returned if the\n\
88 result is within the (inclusive) range.\n\
89 - Upper or lower bounds may be omitted to skip checking the respective limit.\n\
90 - Bare integers are interpreted as upper limits.\n\
91 - When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'\n\
92 - Note that only one string and one regex may be checked at present\n\
93 - All evaluation methods other than PR, STR, and SUBSTR expect that the value\n\
94 returned from the SNMP query is an unsigned integer.\n"
96 #define DESCRIPTION "\
97 This plugin gets system information on a remote server via snmp.\n"
99 #define DEFAULT_COMMUNITY "public"
100 #define DEFAULT_PORT "161"
101 #define DEFAULT_TIMEOUT 10
103 #include "common.h"
104 #include "utils.h"
105 #include "popen.h"
107 #define mark(a) ((a)!=0?"*":"")
109 #define CHECK_UNDEF 0
110 #define CRIT_PRESENT 1
111 #define CRIT_STRING 2
112 #define CRIT_REGEX 4
113 #define CRIT_GT 8
114 #define CRIT_LT 16
115 #define CRIT_GE 32
116 #define CRIT_LE 64
117 #define CRIT_EQ 128
118 #define CRIT_NE 256
119 #define CRIT_RANGE 512
120 #define WARN_PRESENT 1024
121 #define WARN_STRING 2048
122 #define WARN_REGEX 4096
123 #define WARN_GT 8192
124 #define WARN_LT 16384
125 #define WARN_GE 32768
126 #define WARN_LE 65536
127 #define WARN_EQ 131072
128 #define WARN_NE 262144
129 #define WARN_RANGE 524288
131 #define MAX_OIDS 8
132 #define MAX_DELIM_LENGTH 8
133 #define DEFAULT_DELIMITER "="
134 #define DEFAULT_OUTPUT_DELIMITER " "
136 void print_usage (void);
137 void print_help (void);
138 int process_arguments (int, char **);
139 int validate_arguments (void);
140 int check_num (int);
141 char *clarify_message (char *);
142 int lu_getll (unsigned long *, char *);
143 int lu_getul (unsigned long *, char *);
144 char *thisarg (char *str);
145 char *nextarg (char *str);
147 #ifdef HAVE_REGEX_H
148 #include <regex.h>
149 char regex_expect[MAX_INPUT_BUFFER] = "";
150 regex_t preg;
151 regmatch_t pmatch[10];
152 char timestamp[10] = "";
153 char regex[MAX_INPUT_BUFFER];
154 char errbuf[MAX_INPUT_BUFFER];
155 int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
156 int eflags = 0;
157 int errcode, excode;
158 #endif
160 char *server_address = NULL;
161 char *community = NULL;
162 char *oid = "";
163 char *label = NULL;
164 char *units = NULL;
165 char *port = DEFAULT_PORT;
166 char string_value[MAX_INPUT_BUFFER] = "";
167 char **labels = NULL;
168 char **unitv = NULL;
169 int nlabels = 0;
170 int labels_size = 8;
171 int nunits = 0;
172 int unitv_size = 8;
173 int verbose = FALSE;
174 unsigned long lower_warn_lim[MAX_OIDS];
175 unsigned long upper_warn_lim[MAX_OIDS];
176 unsigned long lower_crit_lim[MAX_OIDS];
177 unsigned long upper_crit_lim[MAX_OIDS];
178 unsigned long response_value[MAX_OIDS];
179 int check_warning_value = FALSE;
180 int check_critical_value = FALSE;
181 int eval_method[MAX_OIDS];
182 char *delimiter = NULL;
183 char *output_delim = NULL;
186 int
187 main (int argc, char **argv)
188 {
189 int i = 0;
190 int iresult = STATE_UNKNOWN;
191 int found = 0;
192 int result = STATE_DEPENDENT;
193 char input_buffer[MAX_INPUT_BUFFER];
194 char *command_line = NULL;
195 char *response = NULL;
196 char *outbuff = "";
197 char *output = NULL;
198 char *ptr = NULL;
199 char *p2 = NULL;
200 char *show = NULL;
202 labels = malloc (labels_size);
203 unitv = malloc (unitv_size);
204 for (i = 0; i < MAX_OIDS; i++)
205 eval_method[i] = CHECK_UNDEF;
206 i = 0;
208 if (process_arguments (argc, argv) == ERROR)
209 usage ("Incorrect arguments supplied\n");
211 /* create the command line to execute */
212 asprintf (&command_line, "%s -m ALL -v 1 -c %s %s:%s %s",
213 PATH_TO_SNMPGET, community, server_address, port, oid);
214 if (verbose)
215 printf ("%s\n", command_line);
217 /* run the command */
218 child_process = spopen (command_line);
219 if (child_process == NULL) {
220 printf ("Could not open pipe: %s\n", command_line);
221 exit (STATE_UNKNOWN);
222 }
224 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
225 if (child_stderr == NULL) {
226 printf ("Could not open stderr for %s\n", command_line);
227 }
229 asprintf (&output, "");
230 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
231 asprintf (&output, "%s%s", output, input_buffer);
233 if (verbose)
234 printf ("%s\n", output);
236 ptr = output;
238 while (ptr) {
240 ptr = strstr (ptr, delimiter);
241 if (ptr == NULL)
242 break;
244 ptr += strlen (delimiter);
245 ptr += strspn (ptr, " ");
247 found++;
249 if (ptr[0] == '"') {
250 ptr++;
251 response = strpcpy (response, ptr, "\"");
252 ptr = strpbrk (ptr, "\"");
253 ptr += strspn (ptr, "\"\n");
254 }
255 else {
256 response = strpcpy (response, ptr, "\n");
257 ptr = strpbrk (ptr, "\n");
258 ptr += strspn (ptr, "\n");
259 while
260 (strstr (ptr, delimiter) &&
261 strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter)) {
262 response = strpcat (response, ptr, "\n");
263 ptr = strpbrk (ptr, "\n");
264 }
265 if (ptr && strstr (ptr, delimiter) == NULL) {
266 response = strscat (response, ptr);
267 ptr = NULL;
268 }
269 }
271 if (strstr (response, "Gauge: "))
272 show = strstr (response, "Gauge: ") + 7;
273 else if (strstr (response, "Gauge32: "))
274 show = strstr (response, "Gauge32: ") + 9;
275 else
276 show = response;
277 p2 = show;
279 iresult = STATE_DEPENDENT;
281 if (eval_method[i] & CRIT_PRESENT) {
282 iresult = STATE_CRITICAL;
283 } else if (eval_method[i] & WARN_PRESENT) {
284 iresult = STATE_WARNING;
285 }
287 if (eval_method[i] & CRIT_GT ||
288 eval_method[i] & CRIT_LT ||
289 eval_method[i] & CRIT_GE ||
290 eval_method[i] & CRIT_LE ||
291 eval_method[i] & CRIT_EQ ||
292 eval_method[i] & CRIT_NE ||
293 eval_method[i] & WARN_GT ||
294 eval_method[i] & WARN_LT ||
295 eval_method[i] & WARN_GE ||
296 eval_method[i] & WARN_LE ||
297 eval_method[i] & WARN_EQ || eval_method[i] & WARN_NE) {
298 p2 = strpbrk (p2, "0123456789");
299 response_value[i] = strtoul (p2, NULL, 10);
300 iresult = check_num (i);
301 asprintf (&show, "%lu", response_value[i]);
302 /*asprintf (&show, "%s", response); */
303 }
305 else if (eval_method[i] & CRIT_STRING) {
306 if (strcmp (response, string_value))
307 iresult = STATE_CRITICAL;
308 else
309 iresult = STATE_OK;
310 }
312 else if (eval_method[i] & CRIT_REGEX) {
313 #ifdef HAVE_REGEX_H
314 excode = regexec (&preg, response, 10, pmatch, eflags);
315 if (excode == 0) {
316 iresult = STATE_OK;
317 }
318 else if (excode != REG_NOMATCH) {
319 regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
320 printf ("Execute Error: %s\n", errbuf);
321 exit (STATE_CRITICAL);
322 }
323 else {
324 iresult = STATE_CRITICAL;
325 }
326 #else
327 printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
328 exit (STATE_UNKNOWN);
329 #endif
330 }
332 if (response && iresult == STATE_DEPENDENT)
333 iresult = STATE_OK;
334 else if (eval_method[i] & CRIT_PRESENT)
335 iresult = STATE_CRITICAL;
336 else
337 iresult = STATE_WARNING;
339 result = max_state (result, iresult);
341 if (nlabels > 1 && i < nlabels && labels[i] != NULL)
342 asprintf (&outbuff, "%s%s%s %s%s%s", outbuff,
343 (i == 0) ? " " : output_delim,
344 labels[i], mark (iresult), show, mark (iresult));
345 else
346 asprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim,
347 mark (iresult), show, mark (iresult));
349 if (nunits > 0 && i < nunits)
350 asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
352 i++;
354 } /* end while */
356 if (found == 0)
357 terminate
358 (STATE_UNKNOWN,
359 "%s problem - No data recieved from host\nCMD: %s\n",
360 label, command_line);
362 /* WARNING if output found on stderr */
363 if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
364 result = max_state (result, STATE_WARNING);
366 /* close stderr */
367 (void) fclose (child_stderr);
369 /* close the pipe */
370 if (spclose (child_process))
371 result = max_state (result, STATE_WARNING);
373 if (nunits > 0)
374 printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units);
375 else
376 printf ("%s %s -%s\n", label, state_text (result), outbuff);
378 return result;
379 }
381 /* process command-line arguments */
382 int
383 process_arguments (int argc, char **argv)
384 {
385 char *ptr;
386 int c = 1;
387 int j = 0, jj = 0;
389 #ifdef HAVE_GETOPT_H
390 int option_index = 0;
391 static struct option long_options[] = {
392 STD_LONG_OPTS,
393 {"community", required_argument, 0, 'C'},
394 {"oid", required_argument, 0, 'o'},
395 {"object", required_argument, 0, 'o'},
396 {"delimiter", required_argument, 0, 'd'},
397 {"output-delimiter", required_argument, 0, 'D'},
398 {"string", required_argument, 0, 's'},
399 {"regex", required_argument, 0, 'r'},
400 {"ereg", required_argument, 0, 'r'},
401 {"eregi", required_argument, 0, 'R'},
402 {"label", required_argument, 0, 'l'},
403 {"units", required_argument, 0, 'u'},
404 {"port", required_argument, 0, 'p'},
405 {0, 0, 0, 0}
406 };
407 #endif
409 if (argc < 2)
410 return ERROR;
412 /* reverse compatibility for very old non-POSIX usage forms */
413 for (c = 1; c < argc; c++) {
414 if (strcmp ("-to", argv[c]) == 0)
415 strcpy (argv[c], "-t");
416 if (strcmp ("-wv", argv[c]) == 0)
417 strcpy (argv[c], "-w");
418 if (strcmp ("-cv", argv[c]) == 0)
419 strcpy (argv[c], "-c");
420 }
422 while (1) {
423 #ifdef HAVE_GETOPT_H
424 c =
425 getopt_long (argc, argv, "hvVt:c:w:H:C:o:e:E:d:D:s:R:r:l:u:p:",
426 long_options, &option_index);
427 #else
428 c = getopt (argc, argv, "hvVt:c:w:H:C:o:e:E:d:D:s:R:r:l:u:p:");
429 #endif
431 if (c == -1 || c == EOF)
432 break;
434 switch (c) {
435 case '?': /* usage */
436 usage3 ("Unknown argument", optopt);
437 case 'h': /* help */
438 print_help ();
439 exit (STATE_OK);
440 case 'V': /* version */
441 print_revision (PROGNAME, REVISION);
442 exit (STATE_OK);
443 case 'v': /* verbose */
444 verbose = TRUE;
445 break;
446 case 't': /* timeout period */
447 if (!is_integer (optarg))
448 usage2 ("Timeout Interval must be an integer", optarg);
449 timeout_interval = atoi (optarg);
450 break;
451 case 'e': /* PRELIMINARY - may change */
452 eval_method[j] |= WARN_PRESENT;
453 for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
454 ptr[0] = ' '; /* relpace comma with space */
455 for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
456 eval_method[++j] |= WARN_PRESENT;
457 asprintf (&oid, "%s %s", (oid?oid:""), optarg);
458 break;
459 case 'E': /* PRELIMINARY - may change */
460 eval_method[j] |= WARN_PRESENT;
461 for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
462 ptr[0] = ' '; /* relpace comma with space */
463 for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
464 eval_method[++j] |= CRIT_PRESENT;
465 asprintf (&oid, "%s %s", (oid?oid:""), optarg);
466 break;
467 case 'c': /* critical time threshold */
468 if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
469 printf ("Invalid critical threshold: %s\n", optarg);
470 print_usage ();
471 exit (STATE_UNKNOWN);
472 }
473 for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
474 if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
475 eval_method[jj] |= CRIT_LT;
476 if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
477 eval_method[jj] |= CRIT_GT;
478 (ptr = index (ptr, ',')) ? ptr++ : ptr;
479 }
480 break;
481 case 'w': /* warning time threshold */
482 if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
483 printf ("Invalid warning threshold: %s\n", optarg);
484 print_usage ();
485 exit (STATE_UNKNOWN);
486 }
487 for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
488 if (lu_getll (&lower_warn_lim[jj], ptr) == 1)
489 eval_method[jj] |= WARN_LT;
490 if (lu_getul (&upper_warn_lim[jj], ptr) == 1)
491 eval_method[jj] |= WARN_GT;
492 (ptr = index (ptr, ',')) ? ptr++ : ptr;
493 }
494 break;
495 case 'H': /* Host or server */
496 server_address = strscpy (server_address, optarg);
497 break;
498 case 'C': /* group or community */
499 community = strscpy (community, optarg);
500 break;
501 case 'o': /* object identifier */
502 for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
503 ptr[0] = ' '; /* relpace comma with space */
504 for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
505 j++; /* count OIDs */
506 asprintf (&oid, "%s %s", (oid?oid:""), optarg);
507 break;
508 case 'd': /* delimiter */
509 delimiter = strscpy (delimiter, optarg);
510 break;
511 case 'D': /* output-delimiter */
512 output_delim = strscpy (output_delim, optarg);
513 break;
514 case 's': /* string or substring */
515 strncpy (string_value, optarg, sizeof (string_value) - 1);
516 string_value[sizeof (string_value) - 1] = 0;
517 eval_method[jj++] = CRIT_STRING;
518 break;
519 case 'R': /* regex */
520 #ifdef HAVE_REGEX_H
521 cflags = REG_ICASE;
522 #endif
523 case 'r': /* regex */
524 #ifdef HAVE_REGEX_H
525 cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
526 strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
527 regex_expect[sizeof (regex_expect) - 1] = 0;
528 errcode = regcomp (&preg, regex_expect, cflags);
529 if (errcode != 0) {
530 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
531 printf ("Could Not Compile Regular Expression");
532 return ERROR;
533 }
534 eval_method[jj++] = CRIT_REGEX;
535 #else
536 printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
537 exit (STATE_UNKNOWN);
538 #endif
539 break;
540 case 'l': /* label */
541 label = optarg;
542 nlabels++;
543 if (nlabels >= labels_size) {
544 labels_size += 8;
545 labels = realloc (labels, labels_size);
546 if (labels == NULL)
547 terminate (STATE_UNKNOWN,
548 "Could not realloc() labels[%d]", nlabels);
549 }
550 labels[nlabels - 1] = optarg;
551 ptr = thisarg (optarg);
552 if (strstr (ptr, "'") == ptr)
553 labels[nlabels - 1] = ptr + 1;
554 else
555 labels[nlabels - 1] = ptr;
556 while (ptr && (ptr = nextarg (ptr))) {
557 if (nlabels >= labels_size) {
558 labels_size += 8;
559 labels = realloc (labels, labels_size);
560 if (labels == NULL)
561 terminate (STATE_UNKNOWN, "Could not realloc() labels\n");
562 }
563 labels++;
564 ptr = thisarg (ptr);
565 if (strstr (ptr, "'") == ptr)
566 labels[nlabels - 1] = ptr + 1;
567 else
568 labels[nlabels - 1] = ptr;
569 }
570 break;
571 case 'u': /* units */
572 units = optarg;
573 nunits++;
574 if (nunits >= unitv_size) {
575 unitv_size += 8;
576 unitv = realloc (unitv, unitv_size);
577 if (unitv == NULL)
578 terminate (STATE_UNKNOWN,
579 "Could not realloc() units [%d]\n", nunits);
580 }
581 unitv[nunits - 1] = optarg;
582 ptr = thisarg (optarg);
583 if (strstr (ptr, "'") == ptr)
584 unitv[nunits - 1] = ptr + 1;
585 else
586 unitv[nunits - 1] = ptr;
587 while (ptr && (ptr = nextarg (ptr))) {
588 if (nunits >= unitv_size) {
589 unitv_size += 8;
590 unitv = realloc (unitv, unitv_size);
591 if (units == NULL)
592 terminate (STATE_UNKNOWN, "Could not realloc() units\n");
593 }
594 nunits++;
595 ptr = thisarg (ptr);
596 if (strstr (ptr, "'") == ptr)
597 unitv[nunits - 1] = ptr + 1;
598 else
599 unitv[nunits - 1] = ptr;
600 }
601 break;
602 case 'p': /* TCP port number */
603 port = strscpy(port, optarg);
604 break;
606 }
607 }
609 if (server_address == NULL)
610 asprintf (&server_address, argv[optind]);
612 return validate_arguments ();
613 }
615 /******************************************************************************
617 @@-
618 <sect3>
619 <title>validate_arguments</title>
621 <para>&PROTO_validate_arguments;</para>
623 <para>Given a database name, this function returns TRUE if the string
624 is a valid PostgreSQL database name, and returns false if it is
625 not.</para>
627 <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
628 characters long and consist of letters, numbers, and underscores. The
629 first character cannot be a number, however.</para>
631 </sect3>
632 -@@
633 ******************************************************************************/
635 int
636 validate_arguments ()
637 {
639 if (community == NULL)
640 asprintf (&community, DEFAULT_COMMUNITY);
642 if (delimiter == NULL)
643 asprintf (&delimiter, DEFAULT_DELIMITER);
645 if (output_delim == NULL)
646 asprintf (&output_delim, DEFAULT_OUTPUT_DELIMITER);
648 if (label == NULL)
649 asprintf (&label, "SNMP");
651 if (units == NULL)
652 asprintf (&units, "");
654 return OK;
655 }
656 \f
659 void
660 print_help (void)
661 {
662 print_revision (PROGNAME, REVISION);
663 printf
664 ("Copyright (c) %s %s <%s>\n\n%s\n",
665 COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
666 print_usage ();
667 printf
668 ("\nOptions:\n" LONGOPTIONS "\n" DESCRIPTION "\n" NOTES "\n",
669 DEFAULT_COMMUNITY, DEFAULT_PORT, DEFAULT_DELIMITER, DEFAULT_TIMEOUT);
670 support ();
671 }
673 void
674 print_usage (void)
675 {
676 printf
677 ("Usage:\n" " %s %s\n"
678 " %s (-h | --help) for detailed help\n"
679 " %s (-V | --version) for version information\n",
680 PROGNAME, OPTIONS, PROGNAME, PROGNAME);
681 }
682 \f
685 char *
686 clarify_message (char *msg)
687 {
688 int i = 0;
689 int foo;
690 char tmpmsg_c[MAX_INPUT_BUFFER];
691 char *tmpmsg = (char *) &tmpmsg_c;
692 tmpmsg = strcpy (tmpmsg, msg);
693 if (!strncmp (tmpmsg, " Hex:", 5)) {
694 tmpmsg = strtok (tmpmsg, ":");
695 while ((tmpmsg = strtok (NULL, " "))) {
696 foo = strtol (tmpmsg, NULL, 16);
697 /* Translate chars that are not the same value in the printers
698 * character set.
699 */
700 switch (foo) {
701 case 208:
702 {
703 foo = 197;
704 break;
705 }
706 case 216:
707 {
708 foo = 196;
709 break;
710 }
711 }
712 msg[i] = foo;
713 i++;
714 }
715 msg[i] = 0;
716 }
717 return (msg);
718 }
721 int
722 check_num (int i)
723 {
724 int result;
725 result = STATE_OK;
726 if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
727 lower_warn_lim[i] > upper_warn_lim[i]) {
728 if (response_value[i] <= lower_warn_lim[i] &&
729 response_value[i] >= upper_warn_lim[i]) {
730 result = STATE_WARNING;
731 }
732 }
733 else if
734 ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
735 (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
736 (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
737 (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
738 (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
739 (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
740 result = STATE_WARNING;
741 }
743 if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
744 lower_warn_lim[i] > upper_warn_lim[i]) {
745 if (response_value[i] <= lower_crit_lim[i] &&
746 response_value[i] >= upper_crit_lim[i]) {
747 result = STATE_CRITICAL;
748 }
749 }
750 else if
751 ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
752 (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
753 (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
754 (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
755 (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
756 (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
757 result = STATE_CRITICAL;
758 }
760 return result;
761 }
764 int
765 lu_getll (unsigned long *ll, char *str)
766 {
767 char tmp[100];
768 if (strchr (str, ':') == NULL)
769 return 0;
770 if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
771 return 0;
772 if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
773 return 1;
774 return 0;
775 }
777 int
778 lu_getul (unsigned long *ul, char *str)
779 {
780 char tmp[100];
781 if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
782 return 1;
783 if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
784 return 1;
785 if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
786 return 1;
787 return 0;
788 }
795 /* trim leading whitespace
796 if there is a leading quote, make sure it balances */
798 char *
799 thisarg (char *str)
800 {
801 str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
802 if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
803 if (strlen (str) == 1 || !strstr (str + 1, "'"))
804 terminate (STATE_UNKNOWN, "Unbalanced quotes\n");
805 }
806 return str;
807 }
810 /* if there's a leading quote, advance to the trailing quote
811 set the trailing quote to '\x0'
812 if the string continues, advance beyond the comma */
814 char *
815 nextarg (char *str)
816 {
817 if (strstr (str, "'") == str) {
818 if (strlen (str) > 1) {
819 str = strstr (str + 1, "'");
820 str[0] = 0;
821 return (++str);
822 }
823 else {
824 str[0] = 0;
825 return NULL;
826 }
827 }
828 if (strstr (str, ",") == str) {
829 if (strlen (str) > 1) {
830 str[0] = 0;
831 return (++str);
832 }
833 else {
834 str[0] = 0;
835 return NULL;
836 }
837 }
838 if ((str = strstr (str, ",")) && strlen (str) > 1) {
839 str[0] = 0;
840 return (++str);
841 }
842 return NULL;
843 }