1 /******************************************************************************
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.
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.
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.
17 $Id$
19 ******************************************************************************/
21 const char *progname = "check_snmp";
22 const char *revision = "$Revision$";
23 const char *copyright = "1999-2004";
24 const char *email = "nagiosplug-devel@lists.sourceforge.net";
26 #include "common.h"
27 #include "utils.h"
28 #include "popen.h"
30 #define DEFAULT_COMMUNITY "public"
31 #define DEFAULT_PORT "161"
32 #define DEFAULT_MIBLIST "ALL"
33 #define DEFAULT_PROTOCOL "1"
34 #define DEFAULT_AUTH_PROTOCOL "MD5"
35 #define DEFAULT_DELIMITER "="
36 #define DEFAULT_OUTPUT_DELIMITER " "
38 #define mark(a) ((a)!=0?"*":"")
40 #define CHECK_UNDEF 0
41 #define CRIT_PRESENT 1
42 #define CRIT_STRING 2
43 #define CRIT_REGEX 4
44 #define CRIT_GT 8
45 #define CRIT_LT 16
46 #define CRIT_GE 32
47 #define CRIT_LE 64
48 #define CRIT_EQ 128
49 #define CRIT_NE 256
50 #define CRIT_RANGE 512
51 #define WARN_PRESENT 1024
52 #define WARN_STRING 2048
53 #define WARN_REGEX 4096
54 #define WARN_GT 8192
55 #define WARN_LT 16384
56 #define WARN_GE 32768
57 #define WARN_LE 65536
58 #define WARN_EQ 131072
59 #define WARN_NE 262144
60 #define WARN_RANGE 524288
62 #define MAX_OIDS 8
63 #define MAX_DELIM_LENGTH 8
65 int process_arguments (int, char **);
66 int validate_arguments (void);
67 char *clarify_message (char *);
68 int check_num (int);
69 int lu_getll (unsigned long *, char *);
70 int lu_getul (unsigned long *, char *);
71 char *thisarg (char *str);
72 char *nextarg (char *str);
73 void print_usage (void);
74 void print_help (void);
76 #ifdef HAVE_REGEX_H
77 #include <regex.h>
78 char regex_expect[MAX_INPUT_BUFFER] = "";
79 regex_t preg;
80 regmatch_t pmatch[10];
81 char timestamp[10] = "";
82 char errbuf[MAX_INPUT_BUFFER];
83 int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
84 int eflags = 0;
85 int errcode, excode;
86 #endif
88 char *server_address = NULL;
89 char *community = NULL;
90 char *authpriv = NULL;
91 char *proto = NULL;
92 char *seclevel = NULL;
93 char *secname = NULL;
94 char *authproto = NULL;
95 char *authpasswd = NULL;
96 char *privpasswd = NULL;
97 char *oid;
98 char *label;
99 char *units;
100 char *port;
101 char string_value[MAX_INPUT_BUFFER] = "";
102 char **labels = NULL;
103 char **unitv = NULL;
104 size_t nlabels = 0;
105 size_t labels_size = 8;
106 size_t nunits = 0;
107 size_t unitv_size = 8;
108 int verbose = FALSE;
109 unsigned long lower_warn_lim[MAX_OIDS];
110 unsigned long upper_warn_lim[MAX_OIDS];
111 unsigned long lower_crit_lim[MAX_OIDS];
112 unsigned long upper_crit_lim[MAX_OIDS];
113 unsigned long response_value[MAX_OIDS];
114 int check_warning_value = FALSE;
115 int check_critical_value = FALSE;
116 unsigned long eval_method[MAX_OIDS];
117 char *delimiter;
118 char *output_delim;
119 char *miblist;
123 int
124 main (int argc, char **argv)
125 {
126 int i = 0;
127 int iresult = STATE_UNKNOWN;
128 int found = 0;
129 int result = STATE_DEPENDENT;
130 char input_buffer[MAX_INPUT_BUFFER];
131 char *command_line = NULL;
132 char *response = NULL;
133 char *outbuff;
134 char *output;
135 char *ptr = NULL;
136 char *p2 = NULL;
137 char *show = NULL;
139 setlocale (LC_ALL, "");
140 bindtextdomain (PACKAGE, LOCALEDIR);
141 textdomain (PACKAGE);
143 labels = malloc (labels_size);
144 unitv = malloc (unitv_size);
145 for (i = 0; i < MAX_OIDS; i++)
146 eval_method[i] = CHECK_UNDEF;
147 i = 0;
149 oid = strdup ("");
150 label = strdup ("SNMP");
151 units = strdup ("");
152 port = strdup (DEFAULT_PORT);
153 outbuff = strdup ("");
154 output = strdup ("");
155 delimiter = strdup (DEFAULT_DELIMITER);
156 output_delim = strdup (DEFAULT_OUTPUT_DELIMITER);
157 miblist = strdup (DEFAULT_MIBLIST);
159 if (process_arguments (argc, argv) == ERROR)
160 usage4 (_("Could not parse arguments"));
162 /* create the command line to execute */
163 asprintf (&command_line, "%s -t 1 -r %d -m %s -v %s %s %s:%s %s",
164 PATH_TO_SNMPGET, timeout_interval - 1, miblist, proto,
165 authpriv, server_address, port, oid);
166 if (verbose)
167 printf ("%s\n", command_line);
169 /* run the command */
170 child_process = spopen (command_line);
171 if (child_process == NULL) {
172 printf (_("Could not open pipe: %s\n"), command_line);
173 exit (STATE_UNKNOWN);
174 }
176 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
177 if (child_stderr == NULL) {
178 printf (_("Could not open stderr for %s\n"), command_line);
179 }
181 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
182 asprintf (&output, "%s%s", output, input_buffer);
184 if (verbose)
185 printf ("%s\n", output);
187 ptr = output;
189 while (ptr) {
191 ptr = strstr (ptr, delimiter);
192 if (ptr == NULL)
193 break;
195 ptr += strlen (delimiter);
196 ptr += strspn (ptr, " ");
198 found++;
200 if (ptr[0] == '"') {
201 ptr++;
202 response = strpcpy (response, ptr, "\"");
203 ptr = strpbrk (ptr, "\"");
204 ptr += strspn (ptr, "\"\n");
205 }
206 else {
207 response = strpcpy (response, ptr, "\n");
208 ptr = strpbrk (ptr, "\n");
209 ptr += strspn (ptr, "\n");
210 while
211 (strstr (ptr, delimiter) &&
212 strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter)) {
213 response = strpcat (response, ptr, "\n");
214 ptr = strpbrk (ptr, "\n");
215 }
216 if (ptr && strstr (ptr, delimiter) == NULL) {
217 asprintf (&response, "%s%s", response, ptr);
218 ptr = NULL;
219 }
220 }
222 /* We strip out the datatype indicator for PHBs */
223 if (strstr (response, "Gauge: "))
224 show = strstr (response, "Gauge: ") + 7;
225 else if (strstr (response, "Gauge32: "))
226 show = strstr (response, "Gauge32: ") + 9;
227 else if (strstr (response, "Counter32: "))
228 show = strstr (response, "Counter32: ") + 11;
229 else if (strstr (response, "INTEGER: "))
230 show = strstr (response, "INTEGER: ") + 9;
231 else if (strstr (response, "STRING: "))
232 show = strstr (response, "STRING: ") + 8;
233 else
234 show = response;
235 p2 = show;
237 iresult = STATE_DEPENDENT;
239 /* Process this block for integer comparisons */
240 if (eval_method[i] & CRIT_GT ||
241 eval_method[i] & CRIT_LT ||
242 eval_method[i] & CRIT_GE ||
243 eval_method[i] & CRIT_LE ||
244 eval_method[i] & CRIT_EQ ||
245 eval_method[i] & CRIT_NE ||
246 eval_method[i] & WARN_GT ||
247 eval_method[i] & WARN_LT ||
248 eval_method[i] & WARN_GE ||
249 eval_method[i] & WARN_LE ||
250 eval_method[i] & WARN_EQ ||
251 eval_method[i] & WARN_NE) {
252 p2 = strpbrk (p2, "0123456789");
253 if (p2 == NULL)
254 die (STATE_UNKNOWN,_("No valid data returned"));
255 response_value[i] = strtoul (p2, NULL, 10);
256 iresult = check_num (i);
257 asprintf (&show, "%lu", response_value[i]);
258 }
260 /* Process this block for string matching */
261 else if (eval_method[i] & CRIT_STRING) {
262 if (strcmp (show, string_value))
263 iresult = STATE_CRITICAL;
264 else
265 iresult = STATE_OK;
266 }
268 /* Process this block for regex matching */
269 else if (eval_method[i] & CRIT_REGEX) {
270 #ifdef HAVE_REGEX_H
271 excode = regexec (&preg, response, 10, pmatch, eflags);
272 if (excode == 0) {
273 iresult = STATE_OK;
274 }
275 else if (excode != REG_NOMATCH) {
276 regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
277 printf (_("Execute Error: %s\n"), errbuf);
278 exit (STATE_CRITICAL);
279 }
280 else {
281 iresult = STATE_CRITICAL;
282 }
283 #else
284 printf (_("%s UNKNOWN: call for regex which was not a compiled option"), label);
285 exit (STATE_UNKNOWN);
286 #endif
287 }
289 /* Process this block for existence-nonexistence checks */
290 else {
291 if (eval_method[i] & CRIT_PRESENT)
292 iresult = STATE_CRITICAL;
293 else if (eval_method[i] & WARN_PRESENT)
294 iresult = STATE_WARNING;
295 else if (response && iresult == STATE_DEPENDENT)
296 iresult = STATE_OK;
297 }
299 /* Result is the worst outcome of all the OIDs tested */
300 result = max_state (result, iresult);
302 /* Prepend a label for this OID if there is one */
303 if (nlabels > (size_t)1 && (size_t)i < nlabels && labels[i] != NULL)
304 asprintf (&outbuff, "%s%s%s %s%s%s", outbuff,
305 (i == 0) ? " " : output_delim,
306 labels[i], mark (iresult), show, mark (iresult));
307 else
308 asprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim,
309 mark (iresult), show, mark (iresult));
311 /* Append a unit string for this OID if there is one */
312 if (nunits > (size_t)0 && (size_t)i < nunits && unitv[i] != NULL)
313 asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
315 i++;
317 } /* end while (ptr) */
319 if (found == 0)
320 die (STATE_UNKNOWN,
321 _("%s problem - No data received from host\nCMD: %s\n"),
322 label,
323 command_line);
325 /* WARNING if output found on stderr */
326 if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
327 result = max_state (result, STATE_WARNING);
329 /* close stderr */
330 (void) fclose (child_stderr);
332 /* close the pipe */
333 if (spclose (child_process))
334 result = max_state (result, STATE_WARNING);
336 /* if (nunits == 1 || i == 1) */
337 /* printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units); */
338 /* else */
339 printf ("%s %s -%s\n", label, state_text (result), outbuff);
341 return result;
342 }
346 /* process command-line arguments */
347 int
348 process_arguments (int argc, char **argv)
349 {
350 char *ptr;
351 int c = 1;
352 int j = 0, jj = 0, ii = 0;
354 int option = 0;
355 static struct option longopts[] = {
356 STD_LONG_OPTS,
357 {"community", required_argument, 0, 'C'},
358 {"oid", required_argument, 0, 'o'},
359 {"object", required_argument, 0, 'o'},
360 {"delimiter", required_argument, 0, 'd'},
361 {"output-delimiter", required_argument, 0, 'D'},
362 {"string", required_argument, 0, 's'},
363 {"regex", required_argument, 0, 'r'},
364 {"ereg", required_argument, 0, 'r'},
365 {"eregi", required_argument, 0, 'R'},
366 {"label", required_argument, 0, 'l'},
367 {"units", required_argument, 0, 'u'},
368 {"port", required_argument, 0, 'p'},
369 {"miblist", required_argument, 0, 'm'},
370 {"protocol", required_argument, 0, 'P'},
371 {"seclevel", required_argument, 0, 'L'},
372 {"secname", required_argument, 0, 'U'},
373 {"authproto", required_argument, 0, 'a'},
374 {"authpasswd", required_argument, 0, 'A'},
375 {"privpasswd", required_argument, 0, 'X'},
376 {0, 0, 0, 0}
377 };
379 if (argc < 2)
380 return ERROR;
382 /* reverse compatibility for very old non-POSIX usage forms */
383 for (c = 1; c < argc; c++) {
384 if (strcmp ("-to", argv[c]) == 0)
385 strcpy (argv[c], "-t");
386 if (strcmp ("-wv", argv[c]) == 0)
387 strcpy (argv[c], "-w");
388 if (strcmp ("-cv", argv[c]) == 0)
389 strcpy (argv[c], "-c");
390 }
392 while (1) {
393 c = getopt_long (argc, argv, "hvVt:c:w:H:C:o:e:E:d:D:s:R:r:l:u:p:m:P:L:U:a:A:X:",
394 longopts, &option);
396 if (c == -1 || c == EOF)
397 break;
399 switch (c) {
400 case '?': /* usage */
401 usage2 (_("Unknown argument"), optarg);
402 case 'h': /* help */
403 print_help ();
404 exit (STATE_OK);
405 case 'V': /* version */
406 print_revision (progname, revision);
407 exit (STATE_OK);
408 case 'v': /* verbose */
409 verbose = TRUE;
410 break;
412 /* Connection info */
413 case 'C': /* group or community */
414 community = optarg;
415 break;
416 case 'H': /* Host or server */
417 server_address = optarg;
418 break;
419 case 'p': /* TCP port number */
420 port = optarg;
421 break;
422 case 'm': /* List of MIBS */
423 miblist = optarg;
424 break;
425 case 'P': /* SNMP protocol version */
426 proto = optarg;
427 break;
428 case 'L': /* security level */
429 seclevel = optarg;
430 break;
431 case 'U': /* security username */
432 secname = optarg;
433 break;
434 case 'a': /* auth protocol */
435 authproto = optarg;
436 break;
437 case 'A': /* auth passwd */
438 authpasswd = optarg;
439 break;
440 case 'X': /* priv passwd */
441 privpasswd = optarg;
442 break;
443 case 't': /* timeout period */
444 if (!is_integer (optarg))
445 usage2 (_("Timeout interval must be a positive integer"), optarg);
446 else
447 timeout_interval = atoi (optarg);
448 break;
450 /* Test parameters */
451 case 'c': /* critical time threshold */
452 if (strspn (optarg, "0123456789:,") < strlen (optarg))
453 usage2 (_("Invalid critical threshold: %s\n"), optarg);
454 for (ptr = optarg; ptr && jj < MAX_OIDS; jj++) {
455 if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
456 eval_method[jj] |= CRIT_LT;
457 if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
458 eval_method[jj] |= CRIT_GT;
459 (ptr = index (ptr, ',')) ? ptr++ : ptr;
460 }
461 break;
462 case 'w': /* warning time threshold */
463 if (strspn (optarg, "0123456789:,") < strlen (optarg))
464 usage2 (_("Invalid warning threshold: %s\n"), optarg);
465 for (ptr = optarg; ptr && ii < MAX_OIDS; ii++) {
466 if (lu_getll (&lower_warn_lim[ii], ptr) == 1)
467 eval_method[ii] |= WARN_LT;
468 if (lu_getul (&upper_warn_lim[ii], ptr) == 1)
469 eval_method[ii] |= WARN_GT;
470 (ptr = index (ptr, ',')) ? ptr++ : ptr;
471 }
472 break;
473 case 'o': /* object identifier */
474 case 'e': /* PRELIMINARY - may change */
475 case 'E': /* PRELIMINARY - may change */
476 for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
477 ptr[0] = ' '; /* relpace comma with space */
478 for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
479 j++; /* count OIDs */
480 asprintf (&oid, "%s %s", (oid?oid:""), optarg);
481 if (c == 'E' || c == 'e') {
482 jj++;
483 ii++;
484 }
485 if (c == 'E')
486 eval_method[j+1] |= WARN_PRESENT;
487 else if (c == 'e')
488 eval_method[j+1] |= CRIT_PRESENT;
489 break;
490 case 's': /* string or substring */
491 strncpy (string_value, optarg, sizeof (string_value) - 1);
492 string_value[sizeof (string_value) - 1] = 0;
493 eval_method[jj++] = CRIT_STRING;
494 ii++;
495 break;
496 case 'R': /* regex */
497 #ifdef HAVE_REGEX_H
498 cflags = REG_ICASE;
499 #endif
500 case 'r': /* regex */
501 #ifdef HAVE_REGEX_H
502 cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
503 strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
504 regex_expect[sizeof (regex_expect) - 1] = 0;
505 errcode = regcomp (&preg, regex_expect, cflags);
506 if (errcode != 0) {
507 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
508 printf (_("Could Not Compile Regular Expression"));
509 return ERROR;
510 }
511 eval_method[jj++] = CRIT_REGEX;
512 ii++;
513 #else
514 printf (_("%s UNKNOWN: call for regex which was not a compiled option"), label);
515 exit (STATE_UNKNOWN);
516 #endif
517 break;
519 /* Format */
520 case 'd': /* delimiter */
521 delimiter = strscpy (delimiter, optarg);
522 break;
523 case 'D': /* output-delimiter */
524 output_delim = strscpy (output_delim, optarg);
525 break;
526 case 'l': /* label */
527 label = optarg;
528 nlabels++;
529 if (nlabels >= labels_size) {
530 labels_size += 8;
531 labels = realloc (labels, labels_size);
532 if (labels == NULL)
533 die (STATE_UNKNOWN,
534 _("Could not realloc() labels[%d]"), nlabels);
535 }
536 labels[nlabels - 1] = optarg;
537 ptr = thisarg (optarg);
538 labels[nlabels - 1] = ptr;
539 if (strstr (ptr, "'") == ptr)
540 labels[nlabels - 1] = ptr + 1;
541 while (ptr && (ptr = nextarg (ptr))) {
542 if (nlabels >= labels_size) {
543 labels_size += 8;
544 labels = realloc (labels, labels_size);
545 if (labels == NULL)
546 die (STATE_UNKNOWN, _("Could not realloc() labels\n"));
547 }
548 labels++;
549 ptr = thisarg (ptr);
550 if (strstr (ptr, "'") == ptr)
551 labels[nlabels - 1] = ptr + 1;
552 else
553 labels[nlabels - 1] = ptr;
554 }
555 break;
556 case 'u': /* units */
557 units = optarg;
558 nunits++;
559 if (nunits >= unitv_size) {
560 unitv_size += 8;
561 unitv = realloc (unitv, unitv_size);
562 if (unitv == NULL)
563 die (STATE_UNKNOWN,
564 _("Could not realloc() units [%d]\n"), nunits);
565 }
566 unitv[nunits - 1] = optarg;
567 ptr = thisarg (optarg);
568 unitv[nunits - 1] = ptr;
569 if (strstr (ptr, "'") == ptr)
570 unitv[nunits - 1] = ptr + 1;
571 while (ptr && (ptr = nextarg (ptr))) {
572 if (nunits >= unitv_size) {
573 unitv_size += 8;
574 unitv = realloc (unitv, unitv_size);
575 if (units == NULL)
576 die (STATE_UNKNOWN, _("Could not realloc() units\n"));
577 }
578 nunits++;
579 ptr = thisarg (ptr);
580 if (strstr (ptr, "'") == ptr)
581 unitv[nunits - 1] = ptr + 1;
582 else
583 unitv[nunits - 1] = ptr;
584 }
585 break;
587 }
588 }
590 if (server_address == NULL)
591 server_address = argv[optind];
593 if (community == NULL)
594 community = strdup (DEFAULT_COMMUNITY);
596 return validate_arguments ();
597 }
600 /******************************************************************************
602 @@-
603 <sect3>
604 <title>validate_arguments</title>
606 <para>&PROTO_validate_arguments;</para>
608 <para>Given a database name, this function returns TRUE if the string
609 is a valid PostgreSQL database name, and returns false if it is
610 not.</para>
612 <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
613 characters long and consist of letters, numbers, and underscores. The
614 first character cannot be a number, however.</para>
616 </sect3>
617 -@@
618 ******************************************************************************/
622 int
623 validate_arguments ()
624 {
626 /* Need better checks to verify seclevel and authproto choices */
628 if (seclevel == NULL)
629 asprintf (&seclevel, "noAuthNoPriv");
632 if (authproto == NULL )
633 asprintf(&authproto, DEFAULT_AUTH_PROTOCOL);
637 if (proto == NULL || (strcmp(proto,DEFAULT_PROTOCOL) == 0) ) { /* default protocol version */
638 asprintf(&proto, DEFAULT_PROTOCOL);
639 asprintf(&authpriv, "%s%s", "-c ", community);
640 }
641 else if ( strcmp (proto, "3") == 0 ) { /* snmpv3 args */
642 asprintf(&proto, "%s", "3");
644 if ( (strcmp(seclevel, "noAuthNoPriv") == 0) || seclevel == NULL ) {
645 asprintf(&authpriv, "%s", "-l noAuthNoPriv" );
646 }
647 else if ( strcmp(seclevel, "authNoPriv") == 0 ) {
648 if ( secname == NULL || authpasswd == NULL) {
649 printf (_("Missing secname (%s) or authpassword (%s) ! \n)"),secname, authpasswd );
650 print_usage ();
651 exit (STATE_UNKNOWN);
652 }
653 asprintf(&authpriv, "-l authNoPriv -a %s -u %s -A %s ", authproto, secname, authpasswd);
654 }
655 else if ( strcmp(seclevel, "authPriv") == 0 ) {
656 if ( secname == NULL || authpasswd == NULL || privpasswd == NULL ) {
657 printf (_("Missing secname (%s), authpassword (%s), or privpasswd (%s)! \n"),secname, authpasswd,privpasswd );
658 print_usage ();
659 exit (STATE_UNKNOWN);
660 }
661 asprintf(&authpriv, "-l authPriv -a %s -u %s -A %s -x DES -X %s ", authproto, secname, authpasswd, privpasswd);
662 }
664 }
665 else {
666 printf (_("Invalid SNMP version: %s\n"), proto);
667 print_usage ();
668 exit (STATE_UNKNOWN);
669 }
671 return OK;
672 }
676 char *
677 clarify_message (char *msg)
678 {
679 int i = 0;
680 int foo;
681 char tmpmsg_c[MAX_INPUT_BUFFER];
682 char *tmpmsg = (char *) &tmpmsg_c;
683 tmpmsg = strcpy (tmpmsg, msg);
684 if (!strncmp (tmpmsg, " Hex:", 5)) {
685 tmpmsg = strtok (tmpmsg, ":");
686 while ((tmpmsg = strtok (NULL, " "))) {
687 foo = strtol (tmpmsg, NULL, 16);
688 /* Translate chars that are not the same value in the printers
689 * character set.
690 */
691 switch (foo) {
692 case 208:
693 {
694 foo = 197;
695 break;
696 }
697 case 216:
698 {
699 foo = 196;
700 break;
701 }
702 }
703 msg[i] = foo;
704 i++;
705 }
706 msg[i] = 0;
707 }
708 return (msg);
709 }
713 int
714 check_num (int i)
715 {
716 int result;
717 result = STATE_OK;
718 if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
719 lower_warn_lim[i] > upper_warn_lim[i]) {
720 if (response_value[i] <= lower_warn_lim[i] &&
721 response_value[i] >= upper_warn_lim[i]) {
722 result = STATE_WARNING;
723 }
724 }
725 else if
726 ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
727 (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
728 (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
729 (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
730 (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
731 (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
732 result = STATE_WARNING;
733 }
735 if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
736 lower_crit_lim[i] > upper_crit_lim[i]) {
737 if (response_value[i] <= lower_crit_lim[i] &&
738 response_value[i] >= upper_crit_lim[i]) {
739 result = STATE_CRITICAL;
740 }
741 }
742 else if
743 ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
744 (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
745 (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
746 (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
747 (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
748 (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
749 result = STATE_CRITICAL;
750 }
752 return result;
753 }
757 int
758 lu_getll (unsigned long *ll, char *str)
759 {
760 char tmp[100];
761 if (strchr (str, ':') == NULL)
762 return 0;
763 if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
764 return 0;
765 if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
766 return 1;
767 return 0;
768 }
772 int
773 lu_getul (unsigned long *ul, char *str)
774 {
775 char tmp[100];
776 if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
777 return 1;
778 if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
779 return 1;
780 if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
781 return 1;
782 return 0;
783 }
787 /* trim leading whitespace
788 if there is a leading quote, make sure it balances */
790 char *
791 thisarg (char *str)
792 {
793 str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
794 if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
795 if (strlen (str) == 1 || !strstr (str + 1, "'"))
796 die (STATE_UNKNOWN, _("Unbalanced quotes\n"));
797 }
798 return str;
799 }
803 /* if there's a leading quote, advance to the trailing quote
804 set the trailing quote to '\x0'
805 if the string continues, advance beyond the comma */
807 char *
808 nextarg (char *str)
809 {
810 if (strstr (str, "'") == str) {
811 str[0] = 0;
812 if (strlen (str) > 1) {
813 str = strstr (str + 1, "'");
814 return (++str);
815 }
816 else {
817 return NULL;
818 }
819 }
820 if (strstr (str, ",") == str) {
821 str[0] = 0;
822 if (strlen (str) > 1) {
823 return (++str);
824 }
825 else {
826 return NULL;
827 }
828 }
829 if ((str = strstr (str, ",")) && strlen (str) > 1) {
830 str[0] = 0;
831 return (++str);
832 }
833 return NULL;
834 }
838 void
839 print_help (void)
840 {
841 print_revision (progname, revision);
843 printf (COPYRIGHT, copyright, email);
845 printf (_("\
846 Check status of remote machines and obtain sustem information via SNMP\n\n"));
848 print_usage ();
850 printf (_(UT_HELP_VRSN));
852 printf (_(UT_HOST_PORT), 'p', DEFAULT_PORT);
854 /* SNMP and Authentication Protocol */
855 printf (_("\
856 -P, --protocol=[1|3]\n\
857 SNMP protocol version\n\
858 -L, --seclevel=[noAuthNoPriv|authNoPriv|authPriv]\n\
859 SNMPv3 securityLevel\n\
860 -a, --authproto=[MD5|SHA]\n\
861 SNMPv3 auth proto\n"));
863 /* Authentication Tokens*/
864 printf (_("\
865 -C, --community=STRING\n\
866 Optional community string for SNMP communication\n\
867 (default is \"%s\")\n\
868 -U, --secname=USERNAME\n\
869 SNMPv3 username\n\
870 -A, --authpassword=PASSWORD\n\
871 SNMPv3 authentication password\n\
872 -X, --privpasswd=PASSWORD\n\
873 SNMPv3 crypt passwd (DES)\n"), DEFAULT_COMMUNITY);
875 /* OID Stuff */
876 printf (_("\
877 -o, --oid=OID(s)\n\
878 Object identifier(s) whose value you wish to query\n\
879 -m, --miblist=STRING\n\
880 List of MIBS to be loaded (default = ALL)\n -d, --delimiter=STRING\n\
881 Delimiter to use when parsing returned data. Default is \"%s\"\n\
882 Any data on the right hand side of the delimiter is considered\n\
883 to be the data that should be used in the evaluation.\n"), DEFAULT_DELIMITER);
885 /* Tests Against Integers */
886 printf (_("\
887 -w, --warning=INTEGER_RANGE(s)\n\
888 Range(s) which will not result in a WARNING status\n\
889 -c, --critical=INTEGER_RANGE(s)\n\
890 Range(s) which will not result in a CRITICAL status\n"));
892 /* Tests Against Strings */
893 printf (_("\
894 -s, --string=STRING\n\
895 Return OK state (for that OID) if STRING is an exact match\n\
896 -r, --ereg=REGEX\n\
897 Return OK state (for that OID) if extended regular expression REGEX matches\n\
898 -R, --eregi=REGEX\n\
899 Return OK state (for that OID) if case-insensitive extended REGEX matches\n\
900 -l, --label=STRING\n\
901 Prefix label for output from plugin (default -s 'SNMP')\n"));
903 /* Output Formatting */
904 printf (_("\
905 -u, --units=STRING\n\
906 Units label(s) for output data (e.g., 'sec.').\n\
907 -D, --output-delimiter=STRING\n\
908 Separates output on multiple OID requests\n"));
910 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
912 printf (_(UT_VERBOSE));
914 printf (_("\n\
915 - This plugin uses the 'snmpget' command included with the NET-SNMP package.\n\
916 If you don't have the package installed, you will need to download it from\n\
917 http://net-snmp.sourceforge.net before you can use this plugin.\n"));
919 printf (_("\
920 - Multiple OIDs may be indicated by a comma- or space-delimited list (lists with\n\
921 internal spaces must be quoted) [max 8 OIDs]\n"));
923 printf (_("\
924 - Ranges are inclusive and are indicated with colons. When specified as\n\
925 'min:max' a STATE_OK will be returned if the result is within the indicated\n\
926 range or is equal to the upper or lower bound. A non-OK state will be\n\
927 returned if the result is outside the specified range.\n"));
929 printf (_("\
930 - If specified in the order 'max:min' a non-OK state will be returned if the\n\
931 result is within the (inclusive) range.\n"));
933 printf (_("\
934 - Upper or lower bounds may be omitted to skip checking the respective limit.\n\
935 - Bare integers are interpreted as upper limits.\n\
936 - When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'\n\
937 - Note that only one string and one regex may be checked at present\n\
938 - All evaluation methods other than PR, STR, and SUBSTR expect that the value\n\
939 returned from the SNMP query is an unsigned integer.\n"));
941 printf (_(UT_SUPPORT));
942 }
946 void
947 print_usage (void)
948 {
949 printf ("\
950 Usage: %s -H <ip_address> -o <OID> [-w warn_range] [-c crit_range] \n\
951 [-C community] [-s string] [-r regex] [-R regexi] [-t timeout]\n\
952 [-l label] [-u units] [-p port-number] [-d delimiter]\n\
953 [-D output-delimiter] [-m miblist] [-P snmp version]\n\
954 [-L seclevel] [-U secname] [-a authproto] [-A authpasswd]\n\
955 [-X privpasswd]\n", progname);
956 }