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"
73 #define NOTES "\
74 - This plugin uses the 'snmpget' command included with the UCD-SNMP package.\n\
75 If you don't have the package installed, you will need to download it from\n\
76 http://ucd-snmp.ucdavis.edu before you can use this plugin.\n\
77 - Multiple OIDs may be indicated by a comma- or space-delimited list (lists with\n\
78 internal spaces must be quoted)\n\
79 - Ranges are inclusive and are indicated with colons. When specified as\n\
80 'min:max' a STATE_OK will be returned if the result is within the indicated\n\
81 range or is equal to the upper or lower bound. A non-OK state will be\n\
82 returned if the result is outside the specified range.\n\
83 - If spcified in the order 'max:min' a non-OK state will be returned if the\n\
84 result is within the (inclusive) range.\n\
85 - Upper or lower bounds may be omitted to skip checking the respective limit.\n\
86 - Bare integers are interpreted as upper limits.\n\
87 - When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'\n\
88 - Note that only one string and one regex may be checked at present\n\
89 - All evaluation methods other than PR, STR, and SUBSTR expect that the value\n\
90 returned from the SNMP query is an unsigned integer.\n"
92 #define DESCRIPTION "\
93 This plugin gets system information on a remote server via snmp.\n"
95 #define DEFAULT_COMMUNITY "public"
96 #define DEFAULT_PORT "161"
97 #define DEFAULT_TIMEOUT 10
99 #include "common.h"
100 #include "utils.h"
101 #include "popen.h"
103 #define mark(a) ((a)!=0?"*":"")
105 #define CHECK_UNDEF 0
106 #define CRIT_PRESENT 1
107 #define CRIT_STRING 2
108 #define CRIT_REGEX 4
109 #define CRIT_GT 8
110 #define CRIT_LT 16
111 #define CRIT_GE 32
112 #define CRIT_LE 64
113 #define CRIT_EQ 128
114 #define CRIT_NE 256
115 #define CRIT_RANGE 512
116 #define WARN_PRESENT 1024
117 #define WARN_STRING 2048
118 #define WARN_REGEX 4096
119 #define WARN_GT 8192
120 #define WARN_LT 16384
121 #define WARN_GE 32768
122 #define WARN_LE 65536
123 #define WARN_EQ 131072
124 #define WARN_NE 262144
125 #define WARN_RANGE 524288
127 #define MAX_OIDS 8
128 #define MAX_DELIM_LENGTH 8
129 #define DEFAULT_DELIMITER "="
130 #define DEFAULT_OUTPUT_DELIMITER " "
132 void print_usage (void);
133 void print_help (void);
134 int process_arguments (int, char **);
135 int validate_arguments (void);
136 int check_num (int);
137 char *clarify_message (char *);
138 int lu_getll (unsigned long *, char *);
139 int lu_getul (unsigned long *, char *);
140 char *thisarg (char *str);
141 char *nextarg (char *str);
143 #ifdef HAVE_REGEX_H
144 #include <regex.h>
145 char regex_expect[MAX_INPUT_BUFFER] = "";
146 regex_t preg;
147 regmatch_t pmatch[10];
148 char timestamp[10] = "";
149 char regex[MAX_INPUT_BUFFER];
150 char errbuf[MAX_INPUT_BUFFER];
151 int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
152 int eflags = 0;
153 int errcode, excode;
154 #endif
156 char *server_address = NULL;
157 char *community = NULL;
158 char *oid = "";
159 char *label = NULL;
160 char *units = NULL;
161 char *port = NULL;
162 char string_value[MAX_INPUT_BUFFER] = "";
163 char **labels = NULL;
164 char **unitv = NULL;
165 int nlabels = 0;
166 int labels_size = 8;
167 int nunits = 0;
168 int unitv_size = 8;
169 int verbose = FALSE;
170 unsigned long lower_warn_lim[MAX_OIDS];
171 unsigned long upper_warn_lim[MAX_OIDS];
172 unsigned long lower_crit_lim[MAX_OIDS];
173 unsigned long upper_crit_lim[MAX_OIDS];
174 unsigned long response_value[MAX_OIDS];
175 int check_warning_value = FALSE;
176 int check_critical_value = FALSE;
177 int eval_method[MAX_OIDS];
178 char *delimiter = NULL;
179 char *output_delim = NULL;
182 int
183 main (int argc, char **argv)
184 {
185 int i = 0;
186 int iresult = STATE_UNKNOWN;
187 int found = 0;
188 int result = STATE_DEPENDENT;
189 char input_buffer[MAX_INPUT_BUFFER];
190 char *command_line = NULL;
191 char *response = NULL;
192 char *outbuff = "";
193 char *output = NULL;
194 char *ptr = NULL;
195 char *p2 = NULL;
196 char *show = NULL;
198 labels = malloc (labels_size);
199 unitv = malloc (unitv_size);
200 for (i = 0; i < MAX_OIDS; i++)
201 eval_method[i] = CHECK_UNDEF;
202 i = 0;
204 if (process_arguments (argc, argv) == ERROR)
205 usage ("Incorrect arguments supplied\n");
207 /* create the command line to execute */
208 asprintf (&command_line, "%s -m ALL -v 1 -c %s %s:%s %s",
209 PATH_TO_SNMPGET, community, server_address, port, oid);
210 if (verbose)
211 printf ("%s\n", command_line);
213 /* run the command */
214 child_process = spopen (command_line);
215 if (child_process == NULL) {
216 printf ("Could not open pipe: %s\n", command_line);
217 exit (STATE_UNKNOWN);
218 }
220 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
221 if (child_stderr == NULL) {
222 printf ("Could not open stderr for %s\n", command_line);
223 }
225 asprintf (&output, "");
226 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
227 asprintf (&output, "%s%s", output, input_buffer);
229 if (verbose)
230 printf ("%s\n", output);
232 ptr = output;
234 while (ptr) {
236 ptr = strstr (ptr, delimiter);
237 if (ptr == NULL)
238 break;
240 ptr += strlen (delimiter);
241 ptr += strspn (ptr, " ");
243 found++;
245 if (ptr[0] == '"') {
246 ptr++;
247 response = strpcpy (response, ptr, "\"");
248 ptr = strpbrk (ptr, "\"");
249 ptr += strspn (ptr, "\"\n");
250 }
251 else {
252 response = strpcpy (response, ptr, "\n");
253 ptr = strpbrk (ptr, "\n");
254 ptr += strspn (ptr, "\n");
255 while
256 (strstr (ptr, delimiter) &&
257 strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter)) {
258 response = strpcat (response, ptr, "\n");
259 ptr = strpbrk (ptr, "\n");
260 }
261 if (ptr && strstr (ptr, delimiter) == NULL) {
262 response = strscat (response, ptr);
263 ptr = NULL;
264 }
265 }
267 if (strstr (response, "Gauge: "))
268 show = strstr (response, "Gauge: ") + 7;
269 else if (strstr (response, "Gauge32: "))
270 show = strstr (response, "Gauge32: ") + 9;
271 else
272 show = response;
273 p2 = show;
275 iresult = STATE_DEPENDENT;
277 if (eval_method[i] & CRIT_PRESENT) {
278 iresult = STATE_CRITICAL;
279 } else if (eval_method[i] & WARN_PRESENT) {
280 iresult = STATE_WARNING;
281 }
283 if (eval_method[i] & CRIT_GT ||
284 eval_method[i] & CRIT_LT ||
285 eval_method[i] & CRIT_GE ||
286 eval_method[i] & CRIT_LE ||
287 eval_method[i] & CRIT_EQ ||
288 eval_method[i] & CRIT_NE ||
289 eval_method[i] & WARN_GT ||
290 eval_method[i] & WARN_LT ||
291 eval_method[i] & WARN_GE ||
292 eval_method[i] & WARN_LE ||
293 eval_method[i] & WARN_EQ || eval_method[i] & WARN_NE) {
294 p2 = strpbrk (p2, "0123456789");
295 response_value[i] = strtoul (p2, NULL, 10);
296 iresult = check_num (i);
297 asprintf (&show, "%lu", response_value[i]);
298 /*asprintf (&show, "%s", response); */
299 }
301 else if (eval_method[i] & CRIT_STRING) {
302 if (strcmp (response, string_value))
303 iresult = STATE_CRITICAL;
304 else
305 iresult = STATE_OK;
306 }
308 else if (eval_method[i] & CRIT_REGEX) {
309 #ifdef HAVE_REGEX_H
310 excode = regexec (&preg, response, 10, pmatch, eflags);
311 if (excode == 0) {
312 iresult = STATE_OK;
313 }
314 else if (excode != REG_NOMATCH) {
315 regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
316 printf ("Execute Error: %s\n", errbuf);
317 exit (STATE_CRITICAL);
318 }
319 else {
320 iresult = STATE_CRITICAL;
321 }
322 #else
323 printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
324 exit (STATE_UNKNOWN);
325 #endif
326 }
328 if (response && iresult == STATE_DEPENDENT)
329 iresult = STATE_OK;
330 else if (eval_method[i] & CRIT_PRESENT)
331 iresult = STATE_CRITICAL;
332 else
333 iresult = STATE_WARNING;
335 result = max_state (result, iresult);
337 if (nlabels > 1 && i < nlabels && labels[i] != NULL)
338 asprintf (&outbuff, "%s%s%s %s%s%s", outbuff,
339 (i == 0) ? " " : output_delim,
340 labels[i], mark (iresult), show, mark (iresult));
341 else
342 asprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim,
343 mark (iresult), show, mark (iresult));
345 if (nunits > 0 && i < nunits)
346 asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
348 i++;
350 } /* end while */
352 if (found == 0)
353 terminate
354 (STATE_UNKNOWN,
355 "%s problem - No data recieved from host\nCMD: %s\n",
356 label, command_line);
358 /* WARNING if output found on stderr */
359 if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
360 result = max_state (result, STATE_WARNING);
362 /* close stderr */
363 (void) fclose (child_stderr);
365 /* close the pipe */
366 if (spclose (child_process))
367 result = max_state (result, STATE_WARNING);
369 if (nunits > 0)
370 printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units);
371 else
372 printf ("%s %s -%s\n", label, state_text (result), outbuff);
374 return result;
375 }
377 /* process command-line arguments */
378 int
379 process_arguments (int argc, char **argv)
380 {
381 char *ptr;
382 int c = 1;
383 int j = 0, jj = 0;
385 #ifdef HAVE_GETOPT_H
386 int option_index = 0;
387 static struct option long_options[] = {
388 STD_LONG_OPTS,
389 {"community", required_argument, 0, 'C'},
390 {"oid", required_argument, 0, 'o'},
391 {"object", required_argument, 0, 'o'},
392 {"delimiter", required_argument, 0, 'd'},
393 {"output-delimiter", required_argument, 0, 'D'},
394 {"string", required_argument, 0, 's'},
395 {"regex", required_argument, 0, 'r'},
396 {"ereg", required_argument, 0, 'r'},
397 {"eregi", required_argument, 0, 'R'},
398 {"label", required_argument, 0, 'l'},
399 {"units", required_argument, 0, 'u'},
400 {"port", required_argument, 0, 'p'},
401 {0, 0, 0, 0}
402 };
403 #endif
405 if (argc < 2)
406 return ERROR;
408 /* reverse compatibility for very old non-POSIX usage forms */
409 for (c = 1; c < argc; c++) {
410 if (strcmp ("-to", argv[c]) == 0)
411 strcpy (argv[c], "-t");
412 if (strcmp ("-wv", argv[c]) == 0)
413 strcpy (argv[c], "-w");
414 if (strcmp ("-cv", argv[c]) == 0)
415 strcpy (argv[c], "-c");
416 }
418 while (1) {
419 #ifdef HAVE_GETOPT_H
420 c =
421 getopt_long (argc, argv, "+?hVt:c:w:H:C:o:e:E:d:D:s:R:r:l:u:p:",
422 long_options, &option_index);
423 #else
424 c = getopt (argc, argv, "+?hVt:c:w:H:C:o:e:E:d:D:s:R:r:l:u:p:");
425 #endif
427 if (c == -1 || c == EOF)
428 break;
430 switch (c) {
431 case '?': /* usage */
432 usage2 ("Unknown argument", optarg);
433 case 'h': /* help */
434 print_help ();
435 exit (STATE_OK);
436 case 'V': /* version */
437 print_revision (PROGNAME, REVISION);
438 exit (STATE_OK);
439 case 'v': /* verbose */
440 verbose = TRUE;
441 break;
442 case 't': /* timeout period */
443 if (!is_integer (optarg))
444 usage2 ("Timeout Interval must be an integer", optarg);
445 timeout_interval = atoi (optarg);
446 break;
447 case 'e': /* PRELIMINARY - may change */
448 eval_method[j] |= WARN_PRESENT;
449 for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
450 ptr[0] = ' '; /* relpace comma with space */
451 for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
452 eval_method[++j] |= WARN_PRESENT;
453 asprintf (&oid, "%s %s", (oid?oid:""), optarg);
454 break;
455 case 'E': /* PRELIMINARY - may change */
456 eval_method[j] |= WARN_PRESENT;
457 for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
458 ptr[0] = ' '; /* relpace comma with space */
459 for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
460 eval_method[++j] |= CRIT_PRESENT;
461 asprintf (&oid, "%s %s", (oid?oid:""), optarg);
462 break;
463 case 'c': /* critical time threshold */
464 if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
465 printf ("Invalid critical threshold: %s\n", optarg);
466 print_usage ();
467 exit (STATE_UNKNOWN);
468 }
469 for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
470 if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
471 eval_method[jj] |= CRIT_LT;
472 if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
473 eval_method[jj] |= CRIT_GT;
474 (ptr = index (ptr, ',')) ? ptr++ : ptr;
475 }
476 break;
477 case 'w': /* warning time threshold */
478 if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
479 printf ("Invalid warning threshold: %s\n", optarg);
480 print_usage ();
481 exit (STATE_UNKNOWN);
482 }
483 for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
484 if (lu_getll (&lower_warn_lim[jj], ptr) == 1)
485 eval_method[jj] |= WARN_LT;
486 if (lu_getul (&upper_warn_lim[jj], ptr) == 1)
487 eval_method[jj] |= WARN_GT;
488 (ptr = index (ptr, ',')) ? ptr++ : ptr;
489 }
490 break;
491 case 'H': /* Host or server */
492 server_address = strscpy (server_address, optarg);
493 break;
494 case 'C': /* group or community */
495 community = strscpy (community, optarg);
496 break;
497 case 'o': /* object identifier */
498 for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
499 ptr[0] = ' '; /* relpace comma with space */
500 for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
501 j++; /* count OIDs */
502 asprintf (&oid, "%s %s", (oid?oid:""), optarg);
503 break;
504 case 'd': /* delimiter */
505 delimiter = strscpy (delimiter, optarg);
506 break;
507 case 'D': /* output-delimiter */
508 output_delim = strscpy (output_delim, optarg);
509 break;
510 case 's': /* string or substring */
511 strncpy (string_value, optarg, sizeof (string_value) - 1);
512 string_value[sizeof (string_value) - 1] = 0;
513 eval_method[jj++] = CRIT_STRING;
514 break;
515 case 'R': /* regex */
516 #ifdef HAVE_REGEX_H
517 cflags = REG_ICASE;
518 #endif
519 case 'r': /* regex */
520 #ifdef HAVE_REGEX_H
521 cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
522 strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
523 regex_expect[sizeof (regex_expect) - 1] = 0;
524 errcode = regcomp (&preg, regex_expect, cflags);
525 if (errcode != 0) {
526 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
527 printf ("Could Not Compile Regular Expression");
528 return ERROR;
529 }
530 eval_method[jj++] = CRIT_REGEX;
531 #else
532 printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
533 exit (STATE_UNKNOWN);
534 #endif
535 break;
536 case 'l': /* label */
537 label = optarg;
538 nlabels++;
539 if (nlabels >= labels_size) {
540 labels_size += 8;
541 labels = realloc (labels, labels_size);
542 if (labels == NULL)
543 terminate (STATE_UNKNOWN,
544 "Could not realloc() labels[%d]", nlabels);
545 }
546 labels[nlabels - 1] = optarg;
547 ptr = thisarg (optarg);
548 if (strstr (ptr, "'") == ptr)
549 labels[nlabels - 1] = ptr + 1;
550 else
551 labels[nlabels - 1] = ptr;
552 while (ptr && (ptr = nextarg (ptr))) {
553 if (nlabels >= labels_size) {
554 labels_size += 8;
555 labels = realloc (labels, labels_size);
556 if (labels == NULL)
557 terminate (STATE_UNKNOWN, "Could not realloc() labels\n");
558 }
559 labels++;
560 ptr = thisarg (ptr);
561 if (strstr (ptr, "'") == ptr)
562 labels[nlabels - 1] = ptr + 1;
563 else
564 labels[nlabels - 1] = ptr;
565 }
566 break;
567 case 'u': /* units */
568 units = optarg;
569 nunits++;
570 if (nunits >= unitv_size) {
571 unitv_size += 8;
572 unitv = realloc (unitv, unitv_size);
573 if (unitv == NULL)
574 terminate (STATE_UNKNOWN,
575 "Could not realloc() units [%d]\n", nunits);
576 }
577 unitv[nunits - 1] = optarg;
578 ptr = thisarg (optarg);
579 if (strstr (ptr, "'") == ptr)
580 unitv[nunits - 1] = ptr + 1;
581 else
582 unitv[nunits - 1] = ptr;
583 while (ptr && (ptr = nextarg (ptr))) {
584 if (nunits >= unitv_size) {
585 unitv_size += 8;
586 unitv = realloc (unitv, unitv_size);
587 if (units == NULL)
588 terminate (STATE_UNKNOWN, "Could not realloc() units\n");
589 }
590 nunits++;
591 ptr = thisarg (ptr);
592 if (strstr (ptr, "'") == ptr)
593 unitv[nunits - 1] = ptr + 1;
594 else
595 unitv[nunits - 1] = ptr;
596 }
597 break;
598 case 'p': /* TCP port number */
599 port = strscpy(port, optarg);
600 break;
602 }
603 }
605 if (server_address == NULL)
606 asprintf (&server_address, argv[optind]);
608 return validate_arguments ();
609 }
611 /******************************************************************************
613 @@-
614 <sect3>
615 <title>validate_arguments</title>
617 <para>&PROTO_validate_arguments;</para>
619 <para>Given a database name, this function returns TRUE if the string
620 is a valid PostgreSQL database name, and returns false if it is
621 not.</para>
623 <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
624 characters long and consist of letters, numbers, and underscores. The
625 first character cannot be a number, however.</para>
627 </sect3>
628 -@@
629 ******************************************************************************/
631 int
632 validate_arguments ()
633 {
635 if (community == NULL)
636 asprintf (&community, DEFAULT_COMMUNITY);
638 if (delimiter == NULL)
639 asprintf (&delimiter, DEFAULT_DELIMITER);
641 if (output_delim == NULL)
642 asprintf (&output_delim, DEFAULT_OUTPUT_DELIMITER);
644 if (label == NULL)
645 asprintf (&label, "SNMP");
647 if (units == NULL)
648 asprintf (&units, "");
650 if (port == NULL)
651 asprintf (&port, DEFAULT_PORT);
653 return OK;
654 }
655 \f
658 void
659 print_help (void)
660 {
661 print_revision (PROGNAME, REVISION);
662 printf
663 ("Copyright (c) %s %s <%s>\n\n%s\n",
664 COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
665 print_usage ();
666 printf
667 ("\nOptions:\n" LONGOPTIONS "\n" DESCRIPTION "\n" NOTES "\n",
668 DEFAULT_COMMUNITY, DEFAULT_PORT, DEFAULT_DELIMITER, DEFAULT_TIMEOUT);
669 support ();
670 }
672 void
673 print_usage (void)
674 {
675 printf
676 ("Usage:\n" " %s %s\n"
677 " %s (-h | --help) for detailed help\n"
678 " %s (-V | --version) for version information\n",
679 PROGNAME, OPTIONS, PROGNAME, PROGNAME);
680 }
681 \f
684 char *
685 clarify_message (char *msg)
686 {
687 int i = 0;
688 int foo;
689 char tmpmsg_c[MAX_INPUT_BUFFER];
690 char *tmpmsg = (char *) &tmpmsg_c;
691 tmpmsg = strcpy (tmpmsg, msg);
692 if (!strncmp (tmpmsg, " Hex:", 5)) {
693 tmpmsg = strtok (tmpmsg, ":");
694 while ((tmpmsg = strtok (NULL, " "))) {
695 foo = strtol (tmpmsg, NULL, 16);
696 /* Translate chars that are not the same value in the printers
697 * character set.
698 */
699 switch (foo) {
700 case 208:
701 {
702 foo = 197;
703 break;
704 }
705 case 216:
706 {
707 foo = 196;
708 break;
709 }
710 }
711 msg[i] = foo;
712 i++;
713 }
714 msg[i] = 0;
715 }
716 return (msg);
717 }
720 int
721 check_num (int i)
722 {
723 int result;
724 result = STATE_OK;
725 if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
726 lower_warn_lim[i] > upper_warn_lim[i]) {
727 if (response_value[i] <= lower_warn_lim[i] &&
728 response_value[i] >= upper_warn_lim[i]) {
729 result = STATE_WARNING;
730 }
731 }
732 else if
733 ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
734 (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
735 (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
736 (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
737 (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
738 (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
739 result = STATE_WARNING;
740 }
742 if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
743 lower_warn_lim[i] > upper_warn_lim[i]) {
744 if (response_value[i] <= lower_crit_lim[i] &&
745 response_value[i] >= upper_crit_lim[i]) {
746 result = STATE_CRITICAL;
747 }
748 }
749 else if
750 ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
751 (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
752 (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
753 (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
754 (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
755 (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
756 result = STATE_CRITICAL;
757 }
759 return result;
760 }
763 int
764 lu_getll (unsigned long *ll, char *str)
765 {
766 char tmp[100];
767 if (strchr (str, ':') == NULL)
768 return 0;
769 if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
770 return 0;
771 if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
772 return 1;
773 return 0;
774 }
776 int
777 lu_getul (unsigned long *ul, char *str)
778 {
779 char tmp[100];
780 if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
781 return 1;
782 if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
783 return 1;
784 if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
785 return 1;
786 return 0;
787 }
794 /* trim leading whitespace
795 if there is a leading quote, make sure it balances */
797 char *
798 thisarg (char *str)
799 {
800 str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
801 if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
802 if (strlen (str) == 1 || !strstr (str + 1, "'"))
803 terminate (STATE_UNKNOWN, "Unbalanced quotes\n");
804 }
805 return str;
806 }
809 /* if there's a leading quote, advance to the trailing quote
810 set the trailing quote to '\x0'
811 if the string continues, advance beyond the comma */
813 char *
814 nextarg (char *str)
815 {
816 if (strstr (str, "'") == str) {
817 if (strlen (str) > 1) {
818 str = strstr (str + 1, "'");
819 str[0] = 0;
820 return (++str);
821 }
822 else {
823 str[0] = 0;
824 return NULL;
825 }
826 }
827 if (strstr (str, ",") == str) {
828 if (strlen (str) > 1) {
829 str[0] = 0;
830 return (++str);
831 }
832 else {
833 str[0] = 0;
834 return NULL;
835 }
836 }
837 if ((str = strstr (str, ",")) && strlen (str) > 1) {
838 str[0] = 0;
839 return (++str);
840 }
841 return NULL;
842 }