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