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