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_TIMEOUT 1
35 #define DEFAULT_RETRIES 5
36 #define DEFAULT_AUTH_PROTOCOL "MD5"
37 #define DEFAULT_DELIMITER "="
38 #define DEFAULT_OUTPUT_DELIMITER " "
40 #define mark(a) ((a)!=0?"*":"")
42 #define CHECK_UNDEF 0
43 #define CRIT_PRESENT 1
44 #define CRIT_STRING 2
45 #define CRIT_REGEX 4
46 #define CRIT_GT 8
47 #define CRIT_LT 16
48 #define CRIT_GE 32
49 #define CRIT_LE 64
50 #define CRIT_EQ 128
51 #define CRIT_NE 256
52 #define CRIT_RANGE 512
53 #define WARN_PRESENT 1024
54 #define WARN_STRING 2048
55 #define WARN_REGEX 4096
56 #define WARN_GT 8192
57 #define WARN_LT 16384
58 #define WARN_GE 32768
59 #define WARN_LE 65536
60 #define WARN_EQ 131072
61 #define WARN_NE 262144
62 #define WARN_RANGE 524288
64 #define MAX_OIDS 8
65 #define MAX_DELIM_LENGTH 8
67 int process_arguments (int, char **);
68 int validate_arguments (void);
69 char *clarify_message (char *);
70 int check_num (int);
71 int lu_getll (unsigned long *, char *);
72 int lu_getul (unsigned long *, char *);
73 char *thisarg (char *str);
74 char *nextarg (char *str);
75 void print_usage (void);
76 void print_help (void);
78 #ifdef HAVE_REGEX_H
79 #include <regex.h>
80 char regex_expect[MAX_INPUT_BUFFER] = "";
81 regex_t preg;
82 regmatch_t pmatch[10];
83 char timestamp[10] = "";
84 char errbuf[MAX_INPUT_BUFFER];
85 char perfstr[MAX_INPUT_BUFFER];
86 int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
87 int eflags = 0;
88 int errcode, excode;
89 #endif
91 char *server_address = NULL;
92 char *community = NULL;
93 char *authpriv = NULL;
94 char *proto = NULL;
95 char *seclevel = NULL;
96 char *secname = NULL;
97 char *authproto = NULL;
98 char *authpasswd = NULL;
99 char *privpasswd = NULL;
100 char *oid;
101 char *label;
102 char *units;
103 char *port;
104 char string_value[MAX_INPUT_BUFFER] = "";
105 char **labels = NULL;
106 char **unitv = NULL;
107 size_t nlabels = 0;
108 size_t labels_size = 8;
109 size_t nunits = 0;
110 size_t unitv_size = 8;
111 int verbose = FALSE;
112 int usesnmpgetnext = FALSE;
113 unsigned long lower_warn_lim[MAX_OIDS];
114 unsigned long upper_warn_lim[MAX_OIDS];
115 unsigned long lower_crit_lim[MAX_OIDS];
116 unsigned long upper_crit_lim[MAX_OIDS];
117 unsigned long response_value[MAX_OIDS];
118 int check_warning_value = FALSE;
119 int check_critical_value = FALSE;
120 int retries = 0;
121 unsigned long eval_method[MAX_OIDS];
122 char *delimiter;
123 char *output_delim;
124 char *miblist = NULL;
125 int needmibs = FALSE;
128 int
129 main (int argc, char **argv)
130 {
131 int i = 0;
132 int iresult = STATE_UNKNOWN;
133 int found = 0;
134 int result = STATE_DEPENDENT;
135 char input_buffer[MAX_INPUT_BUFFER];
136 char *command_line = NULL;
137 char *response = NULL;
138 char *outbuff;
139 char *output;
140 char *ptr = NULL;
141 char *p2 = NULL;
142 char *show = NULL;
143 char type[8];
145 setlocale (LC_ALL, "");
146 bindtextdomain (PACKAGE, LOCALEDIR);
147 textdomain (PACKAGE);
149 labels = malloc (labels_size);
150 unitv = malloc (unitv_size);
151 for (i = 0; i < MAX_OIDS; i++)
152 eval_method[i] = CHECK_UNDEF;
153 i = 0;
155 oid = strdup ("");
156 label = strdup ("SNMP");
157 units = strdup ("");
158 port = strdup (DEFAULT_PORT);
159 outbuff = strdup ("");
160 output = strdup ("");
161 delimiter = strdup (" = ");
162 output_delim = strdup (DEFAULT_OUTPUT_DELIMITER);
163 /* miblist = strdup (DEFAULT_MIBLIST); */
164 timeout_interval = DEFAULT_TIMEOUT;
165 retries = DEFAULT_RETRIES;
167 if (process_arguments (argc, argv) == ERROR)
168 usage4 (_("Could not parse arguments"));
170 /* create the command line to execute */
171 if(usesnmpgetnext == TRUE) {
172 asprintf(&command_line, "%s -t %d -r %d -m %s -v %s %s %s:%s %s",
173 PATH_TO_SNMPGETNEXT, timeout_interval, retries, miblist, proto,
174 authpriv, server_address, port, oid);
175 }else{
177 asprintf (&command_line, "%s -t %d -r %d -m %s -v %s %s %s:%s %s",
178 PATH_TO_SNMPGET, timeout_interval, retries, miblist, proto,
179 authpriv, server_address, port, oid);
180 }
182 if (verbose)
183 printf ("%s\n", command_line);
186 /* run the command */
187 child_process = spopen (command_line);
188 if (child_process == NULL) {
189 printf (_("Could not open pipe: %s\n"), command_line);
190 exit (STATE_UNKNOWN);
191 }
193 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
194 if (child_stderr == NULL) {
195 printf (_("Could not open stderr for %s\n"), command_line);
196 }
198 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
199 asprintf (&output, "%s%s", output, input_buffer);
201 if (verbose)
202 printf ("%s\n", output);
204 ptr = output;
206 strcat(perfstr, "| ");
207 while (ptr) {
208 char *foo;
210 foo = strstr (ptr, delimiter);
211 strncat(perfstr, ptr, foo-ptr);
212 ptr = foo;
214 if (ptr == NULL)
215 break;
217 ptr += strlen (delimiter);
218 ptr += strspn (ptr, " ");
220 found++;
222 if (ptr[0] == '"') {
223 ptr++;
224 response = strpcpy (response, ptr, "\"");
225 ptr = strpbrk (ptr, "\"");
226 ptr += strspn (ptr, "\"\n");
227 }
228 else {
229 response = strpcpy (response, ptr, "\n");
230 ptr = strpbrk (ptr, "\n");
231 ptr += strspn (ptr, "\n");
232 while
233 (strstr (ptr, delimiter) &&
234 strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter)) {
235 response = strpcat (response, ptr, "\n");
236 ptr = strpbrk (ptr, "\n");
237 }
238 if (ptr && strstr (ptr, delimiter) == NULL) {
239 asprintf (&response, "%s%s", response, ptr);
240 ptr = NULL;
241 }
242 }
244 /* We strip out the datatype indicator for PHBs */
245 if (strstr (response, "Gauge: "))
246 show = strstr (response, "Gauge: ") + 7;
247 else if (strstr (response, "Gauge32: "))
248 show = strstr (response, "Gauge32: ") + 9;
249 else if (strstr (response, "Counter32: ")) {
250 show = strstr (response, "Counter32: ") + 11;
251 strcpy(type, "c");
252 }
253 else if (strstr (response, "INTEGER: "))
254 show = strstr (response, "INTEGER: ") + 9;
255 else if (strstr (response, "STRING: "))
256 show = strstr (response, "STRING: ") + 8;
257 else
258 show = response;
259 p2 = show;
261 iresult = STATE_DEPENDENT;
263 /* Process this block for integer comparisons */
264 if (eval_method[i] & CRIT_GT ||
265 eval_method[i] & CRIT_LT ||
266 eval_method[i] & CRIT_GE ||
267 eval_method[i] & CRIT_LE ||
268 eval_method[i] & CRIT_EQ ||
269 eval_method[i] & CRIT_NE ||
270 eval_method[i] & WARN_GT ||
271 eval_method[i] & WARN_LT ||
272 eval_method[i] & WARN_GE ||
273 eval_method[i] & WARN_LE ||
274 eval_method[i] & WARN_EQ ||
275 eval_method[i] & WARN_NE) {
276 p2 = strpbrk (p2, "0123456789");
277 if (p2 == NULL)
278 die (STATE_UNKNOWN,_("No valid data returned"));
279 response_value[i] = strtoul (p2, NULL, 10);
280 iresult = check_num (i);
281 asprintf (&show, "%lu", response_value[i]);
282 }
284 /* Process this block for string matching */
285 else if (eval_method[i] & CRIT_STRING) {
286 if (strcmp (show, string_value))
287 iresult = STATE_CRITICAL;
288 else
289 iresult = STATE_OK;
290 }
292 /* Process this block for regex matching */
293 else if (eval_method[i] & CRIT_REGEX) {
294 #ifdef HAVE_REGEX_H
295 excode = regexec (&preg, response, 10, pmatch, eflags);
296 if (excode == 0) {
297 iresult = STATE_OK;
298 }
299 else if (excode != REG_NOMATCH) {
300 regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
301 printf (_("Execute Error: %s\n"), errbuf);
302 exit (STATE_CRITICAL);
303 }
304 else {
305 iresult = STATE_CRITICAL;
306 }
307 #else
308 printf (_("Call for regex which was not a compiled option"));
309 exit (STATE_UNKNOWN);
310 #endif
311 }
313 /* Process this block for existence-nonexistence checks */
314 else {
315 if (eval_method[i] & CRIT_PRESENT)
316 iresult = STATE_CRITICAL;
317 else if (eval_method[i] & WARN_PRESENT)
318 iresult = STATE_WARNING;
319 else if (response && iresult == STATE_DEPENDENT)
320 iresult = STATE_OK;
321 }
323 /* Result is the worst outcome of all the OIDs tested */
324 result = max_state (result, iresult);
326 /* Prepend a label for this OID if there is one */
327 if (nlabels > (size_t)1 && (size_t)i < nlabels && labels[i] != NULL)
328 asprintf (&outbuff, "%s%s%s %s%s%s", outbuff,
329 (i == 0) ? " " : output_delim,
330 labels[i], mark (iresult), show, mark (iresult));
331 else
332 asprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim,
333 mark (iresult), show, mark (iresult));
335 /* Append a unit string for this OID if there is one */
336 if (nunits > (size_t)0 && (size_t)i < nunits && unitv[i] != NULL)
337 asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
339 i++;
341 char *str[MAX_INPUT_BUFFER];
342 asprintf(str, "=%s%s;;;; ", show, type ? type : "");
343 strcat(perfstr, *str);
345 } /* end while (ptr) */
347 if (found == 0)
348 die (STATE_UNKNOWN,
349 _("%s problem - No data received from host\nCMD: %s\n"),
350 label,
351 command_line);
353 /* WARNING if output found on stderr */
354 if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
355 result = max_state (result, STATE_WARNING);
357 /* close stderr */
358 (void) fclose (child_stderr);
360 /* close the pipe */
361 if (spclose (child_process))
362 result = max_state (result, STATE_WARNING);
364 /* if (nunits == 1 || i == 1) */
365 /* printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units); */
366 /* else */
367 printf ("%s %s -%s %s \n", label, state_text (result), outbuff, perfstr);
369 return result;
370 }
374 /* process command-line arguments */
375 int
376 process_arguments (int argc, char **argv)
377 {
378 char *ptr;
379 int c = 1;
380 int j = 0, jj = 0, ii = 0;
382 int option = 0;
383 static struct option longopts[] = {
384 STD_LONG_OPTS,
385 {"community", required_argument, 0, 'C'},
386 {"oid", required_argument, 0, 'o'},
387 {"object", required_argument, 0, 'o'},
388 {"delimiter", required_argument, 0, 'd'},
389 {"output-delimiter", required_argument, 0, 'D'},
390 {"string", required_argument, 0, 's'},
391 {"timeout", required_argument, 0, 't'},
392 {"regex", required_argument, 0, 'r'},
393 {"ereg", required_argument, 0, 'r'},
394 {"eregi", required_argument, 0, 'R'},
395 {"label", required_argument, 0, 'l'},
396 {"units", required_argument, 0, 'u'},
397 {"port", required_argument, 0, 'p'},
398 {"retries", required_argument, 0, 'e'},
399 {"miblist", required_argument, 0, 'm'},
400 {"protocol", required_argument, 0, 'P'},
401 {"seclevel", required_argument, 0, 'L'},
402 {"secname", required_argument, 0, 'U'},
403 {"authproto", required_argument, 0, 'a'},
404 {"authpasswd", required_argument, 0, 'A'},
405 {"privpasswd", required_argument, 0, 'X'},
406 {"next", no_argument, 0, 'n'},
407 {0, 0, 0, 0}
408 };
410 if (argc < 2)
411 return ERROR;
413 /* reverse compatibility for very old non-POSIX usage forms */
414 for (c = 1; c < argc; c++) {
415 if (strcmp ("-to", argv[c]) == 0)
416 strcpy (argv[c], "-t");
417 if (strcmp ("-wv", argv[c]) == 0)
418 strcpy (argv[c], "-w");
419 if (strcmp ("-cv", argv[c]) == 0)
420 strcpy (argv[c], "-c");
421 }
423 while (1) {
424 c = getopt_long (argc, argv, "nhvVt:c:w:H:C:o:e:E:d:D:s:t:R:r:l:u:p:m:P:L:U:a:A:X:",
425 longopts, &option);
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;
443 /* Connection info */
444 case 'C': /* group or community */
445 community = optarg;
446 break;
447 case 'H': /* Host or server */
448 server_address = optarg;
449 break;
450 case 'p': /* TCP port number */
451 port = optarg;
452 break;
453 case 'm': /* List of MIBS */
454 miblist = optarg;
455 break;
456 case 'n': /* usesnmpgetnext */
457 usesnmpgetnext = TRUE;
458 break;
459 case 'P': /* SNMP protocol version */
460 proto = optarg;
461 break;
462 case 'L': /* security level */
463 seclevel = optarg;
464 break;
465 case 'U': /* security username */
466 secname = optarg;
467 break;
468 case 'a': /* auth protocol */
469 authproto = optarg;
470 break;
471 case 'A': /* auth passwd */
472 authpasswd = optarg;
473 break;
474 case 'X': /* priv passwd */
475 privpasswd = optarg;
476 break;
477 case 't': /* timeout period */
478 if (!is_integer (optarg))
479 usage2 (_("Timeout interval must be a positive integer"), optarg);
480 else
481 timeout_interval = atoi (optarg);
482 break;
484 /* Test parameters */
485 case 'c': /* critical time threshold */
486 if (strspn (optarg, "0123456789:,") < strlen (optarg))
487 usage2 (_("Invalid critical threshold: %s\n"), optarg);
488 for (ptr = optarg; ptr && jj < MAX_OIDS; jj++) {
489 if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
490 eval_method[jj] |= CRIT_LT;
491 if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
492 eval_method[jj] |= CRIT_GT;
493 (ptr = index (ptr, ',')) ? ptr++ : ptr;
494 }
495 break;
496 case 'w': /* warning time threshold */
497 if (strspn (optarg, "0123456789:,") < strlen (optarg))
498 usage2 (_("Invalid warning threshold: %s\n"), optarg);
499 for (ptr = optarg; ptr && ii < MAX_OIDS; ii++) {
500 if (lu_getll (&lower_warn_lim[ii], ptr) == 1)
501 eval_method[ii] |= WARN_LT;
502 if (lu_getul (&upper_warn_lim[ii], ptr) == 1)
503 eval_method[ii] |= WARN_GT;
504 (ptr = index (ptr, ',')) ? ptr++ : ptr;
505 }
506 break;
507 case 'e': /* PRELIMINARY - may change */
508 case 'E': /* PRELIMINARY - may change */
509 if (!is_integer (optarg))
510 usage2 (_("Retries interval must be a positive integer"), optarg);
511 else
512 retries = atoi(optarg);
513 break;
514 case 'o': /* object identifier */
515 if ( strspn( optarg, "0123456789." ) != strlen( optarg ) ) {
516 /*
517 * we have something other than digits and periods, so we
518 * have a mib variable, rather than just an SNMP OID, so
519 * we have to actually read the mib files
520 */
521 needmibs = TRUE;
522 }
524 for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
525 ptr[0] = ' '; /* relpace comma with space */
526 for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
527 j++; /* count OIDs */
528 asprintf (&oid, "%s %s", (oid?oid:""), optarg);
529 if (c == 'E' || c == 'e') {
530 jj++;
531 ii++;
532 }
533 if (c == 'E')
534 eval_method[j+1] |= WARN_PRESENT;
535 else if (c == 'e')
536 eval_method[j+1] |= CRIT_PRESENT;
537 break;
538 case 's': /* string or substring */
539 strncpy (string_value, optarg, sizeof (string_value) - 1);
540 string_value[sizeof (string_value) - 1] = 0;
541 eval_method[jj++] = CRIT_STRING;
542 ii++;
543 break;
544 case 'R': /* regex */
545 #ifdef HAVE_REGEX_H
546 cflags = REG_ICASE;
547 #endif
548 case 'r': /* regex */
549 #ifdef HAVE_REGEX_H
550 cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
551 strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
552 regex_expect[sizeof (regex_expect) - 1] = 0;
553 errcode = regcomp (&preg, regex_expect, cflags);
554 if (errcode != 0) {
555 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
556 printf (_("Could Not Compile Regular Expression"));
557 return ERROR;
558 }
559 eval_method[jj++] = CRIT_REGEX;
560 ii++;
561 #else
562 printf (_("call for regex which was not a compiled option"));
563 exit (STATE_UNKNOWN);
564 #endif
565 break;
567 /* Format */
568 case 'd': /* delimiter */
569 delimiter = strscpy (delimiter, optarg);
570 break;
571 case 'D': /* output-delimiter */
572 output_delim = strscpy (output_delim, optarg);
573 break;
574 case 'l': /* label */
575 label = optarg;
576 nlabels++;
577 if (nlabels >= labels_size) {
578 labels_size += 8;
579 labels = realloc (labels, labels_size);
580 if (labels == NULL)
581 die (STATE_UNKNOWN, _("Could not reallocate labels[%d]"), nlabels);
582 }
583 labels[nlabels - 1] = optarg;
584 ptr = thisarg (optarg);
585 labels[nlabels - 1] = ptr;
586 if (strstr (ptr, "'") == ptr)
587 labels[nlabels - 1] = ptr + 1;
588 while (ptr && (ptr = nextarg (ptr))) {
589 if (nlabels >= labels_size) {
590 labels_size += 8;
591 labels = realloc (labels, labels_size);
592 if (labels == NULL)
593 die (STATE_UNKNOWN, _("Could not reallocate labels\n"));
594 }
595 labels++;
596 ptr = thisarg (ptr);
597 if (strstr (ptr, "'") == ptr)
598 labels[nlabels - 1] = ptr + 1;
599 else
600 labels[nlabels - 1] = ptr;
601 }
602 break;
603 case 'u': /* units */
604 units = optarg;
605 nunits++;
606 if (nunits >= unitv_size) {
607 unitv_size += 8;
608 unitv = realloc (unitv, unitv_size);
609 if (unitv == NULL)
610 die (STATE_UNKNOWN, _("Could not reallocate units [%d]\n"), nunits);
611 }
612 unitv[nunits - 1] = optarg;
613 ptr = thisarg (optarg);
614 unitv[nunits - 1] = ptr;
615 if (strstr (ptr, "'") == ptr)
616 unitv[nunits - 1] = ptr + 1;
617 while (ptr && (ptr = nextarg (ptr))) {
618 if (nunits >= unitv_size) {
619 unitv_size += 8;
620 unitv = realloc (unitv, unitv_size);
621 if (units == NULL)
622 die (STATE_UNKNOWN, _("Could not realloc() units\n"));
623 }
624 nunits++;
625 ptr = thisarg (ptr);
626 if (strstr (ptr, "'") == ptr)
627 unitv[nunits - 1] = ptr + 1;
628 else
629 unitv[nunits - 1] = ptr;
630 }
631 break;
633 }
634 }
636 if (server_address == NULL)
637 server_address = argv[optind];
639 if (community == NULL)
640 community = strdup (DEFAULT_COMMUNITY);
644 return validate_arguments ();
645 }
648 /******************************************************************************
650 @@-
651 <sect3>
652 <title>validate_arguments</title>
654 <para>&PROTO_validate_arguments;</para>
656 <para>Checks to see if the default miblist needs to be loaded. Also verifies
657 the authentication and authorization combinations based on protocol version
658 selected.</para>
660 <para></para>
662 </sect3>
663 -@@
664 ******************************************************************************/
668 int
669 validate_arguments ()
670 {
671 /* check whether to load locally installed MIBS (CPU/disk intensive) */
672 if (miblist == NULL) {
673 if ( needmibs == TRUE ) {
674 miblist = strdup (DEFAULT_MIBLIST);
675 }else{
676 miblist = "''"; /* don't read any mib files for numeric oids */
677 }
678 }
681 /* Need better checks to verify seclevel and authproto choices */
683 if (seclevel == NULL)
684 asprintf (&seclevel, "noAuthNoPriv");
687 if (authproto == NULL )
688 asprintf(&authproto, DEFAULT_AUTH_PROTOCOL);
692 if (proto == NULL || (strcmp(proto,DEFAULT_PROTOCOL) == 0) ) { /* default protocol version */
693 asprintf(&proto, DEFAULT_PROTOCOL);
694 asprintf(&authpriv, "%s%s", "-c ", community);
695 }
696 else if ( strcmp (proto, "2c") == 0 ) { /* snmpv2c args */
697 asprintf(&authpriv, "%s%s", "-c ", community);
698 }
699 else if ( strcmp (proto, "3") == 0 ) { /* snmpv3 args */
700 asprintf(&proto, "%s", "3");
702 if ( (strcmp(seclevel, "noAuthNoPriv") == 0) || seclevel == NULL ) {
703 asprintf(&authpriv, "%s", "-l noAuthNoPriv" );
704 }
705 else if ( strcmp(seclevel, "authNoPriv") == 0 ) {
706 if ( secname == NULL || authpasswd == NULL) {
707 printf (_("Missing secname (%s) or authpassword (%s) ! \n"),secname, authpasswd );
708 print_usage ();
709 exit (STATE_UNKNOWN);
710 }
711 asprintf(&authpriv, "-l authNoPriv -a %s -u %s -A %s ", authproto, secname, authpasswd);
712 }
713 else if ( strcmp(seclevel, "authPriv") == 0 ) {
714 if ( secname == NULL || authpasswd == NULL || privpasswd == NULL ) {
715 printf (_("Missing secname (%s), authpassword (%s), or privpasswd (%s)! \n"),secname, authpasswd,privpasswd );
716 print_usage ();
717 exit (STATE_UNKNOWN);
718 }
719 asprintf(&authpriv, "-l authPriv -a %s -u %s -A %s -x DES -X %s ", authproto, secname, authpasswd, privpasswd);
720 }
722 }
723 else {
724 usage2 (_("Invalid SNMP version"), proto);
725 }
727 return OK;
728 }
732 char *
733 clarify_message (char *msg)
734 {
735 int i = 0;
736 int foo;
737 char tmpmsg_c[MAX_INPUT_BUFFER];
738 char *tmpmsg = (char *) &tmpmsg_c;
739 tmpmsg = strcpy (tmpmsg, msg);
740 if (!strncmp (tmpmsg, " Hex:", 5)) {
741 tmpmsg = strtok (tmpmsg, ":");
742 while ((tmpmsg = strtok (NULL, " "))) {
743 foo = strtol (tmpmsg, NULL, 16);
744 /* Translate chars that are not the same value in the printers
745 * character set.
746 */
747 switch (foo) {
748 case 208:
749 {
750 foo = 197;
751 break;
752 }
753 case 216:
754 {
755 foo = 196;
756 break;
757 }
758 }
759 msg[i] = foo;
760 i++;
761 }
762 msg[i] = 0;
763 }
764 return (msg);
765 }
769 int
770 check_num (int i)
771 {
772 int result;
773 result = STATE_OK;
774 if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
775 lower_warn_lim[i] > upper_warn_lim[i]) {
776 if (response_value[i] <= lower_warn_lim[i] &&
777 response_value[i] >= upper_warn_lim[i]) {
778 result = STATE_WARNING;
779 }
780 }
781 else if
782 ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
783 (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
784 (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
785 (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
786 (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
787 (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
788 result = STATE_WARNING;
789 }
791 if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
792 lower_crit_lim[i] > upper_crit_lim[i]) {
793 if (response_value[i] <= lower_crit_lim[i] &&
794 response_value[i] >= upper_crit_lim[i]) {
795 result = STATE_CRITICAL;
796 }
797 }
798 else if
799 ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
800 (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
801 (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
802 (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
803 (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
804 (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
805 result = STATE_CRITICAL;
806 }
808 return result;
809 }
813 int
814 lu_getll (unsigned long *ll, char *str)
815 {
816 char tmp[100];
817 if (strchr (str, ':') == NULL)
818 return 0;
819 if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
820 return 0;
821 if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
822 return 1;
823 return 0;
824 }
828 int
829 lu_getul (unsigned long *ul, char *str)
830 {
831 char tmp[100];
832 if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
833 return 1;
834 if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
835 return 1;
836 if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
837 return 1;
838 return 0;
839 }
843 /* trim leading whitespace
844 if there is a leading quote, make sure it balances */
846 char *
847 thisarg (char *str)
848 {
849 str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
850 if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
851 if (strlen (str) == 1 || !strstr (str + 1, "'"))
852 die (STATE_UNKNOWN, _("Unbalanced quotes\n"));
853 }
854 return str;
855 }
859 /* if there's a leading quote, advance to the trailing quote
860 set the trailing quote to '\x0'
861 if the string continues, advance beyond the comma */
863 char *
864 nextarg (char *str)
865 {
866 if (strstr (str, "'") == str) {
867 str[0] = 0;
868 if (strlen (str) > 1) {
869 str = strstr (str + 1, "'");
870 return (++str);
871 }
872 else {
873 return NULL;
874 }
875 }
876 if (strstr (str, ",") == str) {
877 str[0] = 0;
878 if (strlen (str) > 1) {
879 return (++str);
880 }
881 else {
882 return NULL;
883 }
884 }
885 if ((str = strstr (str, ",")) && strlen (str) > 1) {
886 str[0] = 0;
887 return (++str);
888 }
889 return NULL;
890 }
894 void
895 print_help (void)
896 {
897 print_revision (progname, revision);
899 printf (COPYRIGHT, copyright, email);
901 printf (_("\
902 Check status of remote machines and obtain sustem information via SNMP\n\n"));
904 print_usage ();
906 printf (_(UT_HELP_VRSN));
908 printf (_(UT_HOST_PORT), 'p', DEFAULT_PORT);
910 /* SNMP and Authentication Protocol */
911 printf (_("\
912 -n, --next\n\
913 Use SNMP GETNEXT instead of SNMP GET\n\
914 -P, --protocol=[1|2c|3]\n\
915 SNMP protocol version\n\
916 -L, --seclevel=[noAuthNoPriv|authNoPriv|authPriv]\n\
917 SNMPv3 securityLevel\n\
918 -a, --authproto=[MD5|SHA]\n\
919 SNMPv3 auth proto\n"));
921 /* Authentication Tokens*/
922 printf (_("\
923 -C, --community=STRING\n\
924 Optional community string for SNMP communication\n\
925 (default is \"%s\")\n\
926 -U, --secname=USERNAME\n\
927 SNMPv3 username\n\
928 -A, --authpassword=PASSWORD\n\
929 SNMPv3 authentication password\n\
930 -X, --privpasswd=PASSWORD\n\
931 SNMPv3 crypt passwd (DES)\n"), DEFAULT_COMMUNITY);
933 /* OID Stuff */
934 printf (_("\
935 -o, --oid=OID(s)\n\
936 Object identifier(s) or SNMP variables whose value you wish to query\n\
937 -m, --miblist=STRING\n\
938 List of MIBS to be loaded (default = none if using numeric oids or 'ALL'\n\
939 for symbolic oids.)\n\
940 -d, --delimiter=STRING\n\
941 Delimiter to use when parsing returned data. Default is \"%s\"\n\
942 Any data on the right hand side of the delimiter is considered\n\
943 to be the data that should be used in the evaluation.\n"), DEFAULT_DELIMITER);
945 /* Tests Against Integers */
946 printf (_("\
947 -w, --warning=INTEGER_RANGE(s)\n\
948 Range(s) which will not result in a WARNING status\n\
949 -c, --critical=INTEGER_RANGE(s)\n\
950 Range(s) which will not result in a CRITICAL status\n"));
952 /* Tests Against Strings */
953 printf (_("\
954 -s, --string=STRING\n\
955 Return OK state (for that OID) if STRING is an exact match\n\
956 -r, --ereg=REGEX\n\
957 Return OK state (for that OID) if extended regular expression REGEX matches\n\
958 -R, --eregi=REGEX\n\
959 Return OK state (for that OID) if case-insensitive extended REGEX matches\n\
960 -l, --label=STRING\n\
961 Prefix label for output from plugin (default -s 'SNMP')\n"));
963 /* Output Formatting */
964 printf (_("\
965 -u, --units=STRING\n\
966 Units label(s) for output data (e.g., 'sec.').\n\
967 -D, --output-delimiter=STRING\n\
968 Separates output on multiple OID requests\n"));
970 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
972 printf (_(UT_VERBOSE));
974 printf (_("\n\
975 - This plugin uses the 'snmpget' command included with the NET-SNMP package.\n\
976 If you don't have the package installed, you will need to download it from\n\
977 http://net-snmp.sourceforge.net before you can use this plugin.\n"));
979 printf (_("\
980 - Multiple OIDs may be indicated by a comma- or space-delimited list (lists with\n\
981 internal spaces must be quoted) [max 8 OIDs]\n"));
983 printf (_("\
984 - Ranges are inclusive and are indicated with colons. When specified as\n\
985 'min:max' a STATE_OK will be returned if the result is within the indicated\n\
986 range or is equal to the upper or lower bound. A non-OK state will be\n\
987 returned if the result is outside the specified range.\n"));
989 printf (_("\
990 - If specified in the order 'max:min' a non-OK state will be returned if the\n\
991 result is within the (inclusive) range.\n"));
993 printf (_("\
994 - Upper or lower bounds may be omitted to skip checking the respective limit.\n\
995 - Bare integers are interpreted as upper limits.\n\
996 - When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'\n\
997 - Note that only one string and one regex may be checked at present\n\
998 - All evaluation methods other than PR, STR, and SUBSTR expect that the value\n\
999 returned from the SNMP query is an unsigned integer.\n"));
1001 printf (_(UT_SUPPORT));
1002 }
1006 void
1007 print_usage (void)
1008 {
1009 printf ("\
1010 Usage: %s -H <ip_address> -o <OID> [-w warn_range] [-c crit_range] \n\
1011 [-C community] [-s string] [-r regex] [-R regexi]\n\
1012 [-t timeout] [-e retries]\n\
1013 [-l label] [-u units] [-p port-number] [-d delimiter]\n\
1014 [-D output-delimiter] [-m miblist] [-P snmp version]\n\
1015 [-L seclevel] [-U secname] [-a authproto] [-A authpasswd]\n\
1016 [-X privpasswd]\n", progname);
1017 }