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