--help output cleanup (plus removal of spaces on blank lines)
[monitoring-plugins.git] / plugins / check_snmp.c
blobb9435651707f42f4e8acf5a247e01fb772b8b769
1 /*****************************************************************************
2 *
3 * Nagios check_snmp plugin
4 *
5 * License: GPL
6 * Copyright (c) 1999-2007 Nagios Plugins Development Team
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
12 * This file contains the check_snmp plugin
14 * Check status of remote machines and obtain system information via SNMP
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 * $Id$
32 *****************************************************************************/
34 const char *progname = "check_snmp";
35 const char *revision = "$Revision$";
36 const char *copyright = "1999-2007";
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 llu_getll (unsigned long long *, char *);
85 int llu_getul (unsigned long 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 long lower_warn_lim[MAX_OIDS];
125 unsigned long long upper_warn_lim[MAX_OIDS];
126 unsigned long long lower_crit_lim[MAX_OIDS];
127 unsigned long long upper_crit_lim[MAX_OIDS];
128 unsigned long long response_value[MAX_OIDS];
129 int check_warning_value = FALSE;
130 int check_critical_value = FALSE;
131 int retries = 0;
132 unsigned long long eval_method[MAX_OIDS];
133 char *delimiter;
134 char *output_delim;
135 char *miblist = NULL;
136 int needmibs = FALSE;
140 main (int argc, char **argv)
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 *cl_hidden_auth = NULL;
149 char *response = NULL;
150 char *outbuff;
151 char *output;
152 char *ptr = NULL;
153 char *p2 = NULL;
154 char *show = NULL;
155 char type[8] = "";
157 setlocale (LC_ALL, "");
158 bindtextdomain (PACKAGE, LOCALEDIR);
159 textdomain (PACKAGE);
161 labels = malloc (labels_size);
162 unitv = malloc (unitv_size);
163 for (i = 0; i < MAX_OIDS; i++)
164 eval_method[i] = CHECK_UNDEF;
165 i = 0;
167 oid = strdup ("");
168 label = strdup ("SNMP");
169 units = strdup ("");
170 port = strdup (DEFAULT_PORT);
171 outbuff = strdup ("");
172 output = strdup ("");
173 delimiter = strdup (" = ");
174 output_delim = strdup (DEFAULT_OUTPUT_DELIMITER);
175 /* miblist = strdup (DEFAULT_MIBLIST); */
176 timeout_interval = DEFAULT_TIMEOUT;
177 retries = DEFAULT_RETRIES;
179 if (process_arguments (argc, argv) == ERROR)
180 usage4 (_("Could not parse arguments"));
182 /* create the command line to execute */
183 if(usesnmpgetnext == TRUE) {
184 asprintf(&command_line, "%s -t %d -r %d -m %s -v %s %s %s:%s %s",
185 PATH_TO_SNMPGETNEXT, timeout_interval, retries, miblist, proto,
186 authpriv, server_address, port, oid);
187 asprintf(&cl_hidden_auth, "%s -t %d -r %d -m %s -v %s %s %s:%s %s",
188 PATH_TO_SNMPGETNEXT, timeout_interval, retries, miblist, proto,
189 "[authpriv]", server_address, port, oid);
190 }else{
192 asprintf (&command_line, "%s -t %d -r %d -m %s -v %s %s %s:%s %s",
193 PATH_TO_SNMPGET, timeout_interval, retries, miblist, proto,
194 authpriv, server_address, port, oid);
195 asprintf(&cl_hidden_auth, "%s -t %d -r %d -m %s -v %s %s %s:%s %s",
196 PATH_TO_SNMPGET, timeout_interval, retries, miblist, proto,
197 "[authpriv]", server_address, port, oid);
200 if (verbose)
201 printf ("%s\n", command_line);
204 /* run the command */
205 child_process = spopen (command_line);
206 if (child_process == NULL) {
207 printf (_("Could not open pipe: %s\n"), cl_hidden_auth);
208 exit (STATE_UNKNOWN);
211 #if 0 /* Removed May 29, 2007 */
212 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
213 if (child_stderr == NULL) {
214 printf (_("Could not open stderr for %s\n"), cl_hidden_auth);
216 #endif
218 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
219 asprintf (&output, "%s%s", output, input_buffer);
221 if (verbose)
222 printf ("%s\n", output);
224 ptr = output;
226 strncat(perfstr, "| ", sizeof(perfstr)-strlen(perfstr)-1);
227 while (ptr) {
228 char *foo;
229 unsigned int copylen;
231 foo = strstr (ptr, delimiter);
232 copylen = foo-ptr;
233 if (copylen > sizeof(perfstr)-strlen(perfstr)-1)
234 copylen = sizeof(perfstr)-strlen(perfstr)-1;
235 strncat(perfstr, ptr, copylen);
236 ptr = foo;
238 if (ptr == NULL)
239 break;
241 ptr += strlen (delimiter);
242 ptr += strspn (ptr, " ");
244 found++;
246 if (ptr[0] == '"') {
247 ptr++;
248 response = strpcpy (response, ptr, "\"");
249 ptr = strpbrk (ptr, "\"");
250 ptr += strspn (ptr, "\"\n");
252 else {
253 response = strpcpy (response, ptr, "\n");
254 ptr = strpbrk (ptr, "\n");
255 ptr += strspn (ptr, "\n");
256 while
257 (strstr (ptr, delimiter) &&
258 strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter)) {
259 response = strpcat (response, ptr, "\n");
260 ptr = strpbrk (ptr, "\n");
262 if (ptr && strstr (ptr, delimiter) == NULL) {
263 asprintf (&response, "%s%s", response, ptr);
264 ptr = NULL;
268 /* We strip out the datatype indicator for PHBs */
270 /* Clean up type array - Sol10 does not necessarily zero it out */
271 bzero(type, sizeof(type));
273 if (strstr (response, "Gauge: "))
274 show = strstr (response, "Gauge: ") + 7;
275 else if (strstr (response, "Gauge32: "))
276 show = strstr (response, "Gauge32: ") + 9;
277 else if (strstr (response, "Counter32: ")) {
278 show = strstr (response, "Counter32: ") + 11;
279 strcpy(type, "c");
281 else if (strstr (response, "Counter64: ")) {
282 show = strstr (response, "Counter64: ") + 11;
283 strcpy(type, "c");
285 else if (strstr (response, "INTEGER: "))
286 show = strstr (response, "INTEGER: ") + 9;
287 else if (strstr (response, "STRING: "))
288 show = strstr (response, "STRING: ") + 8;
289 else
290 show = response;
291 p2 = show;
293 iresult = STATE_DEPENDENT;
295 /* Process this block for integer comparisons */
296 if (eval_method[i] & CRIT_GT ||
297 eval_method[i] & CRIT_LT ||
298 eval_method[i] & CRIT_GE ||
299 eval_method[i] & CRIT_LE ||
300 eval_method[i] & CRIT_EQ ||
301 eval_method[i] & CRIT_NE ||
302 eval_method[i] & WARN_GT ||
303 eval_method[i] & WARN_LT ||
304 eval_method[i] & WARN_GE ||
305 eval_method[i] & WARN_LE ||
306 eval_method[i] & WARN_EQ ||
307 eval_method[i] & WARN_NE) {
308 p2 = strpbrk (p2, "0123456789");
309 if (p2 == NULL)
310 die (STATE_UNKNOWN,_("No valid data returned"));
311 response_value[i] = strtoul (p2, NULL, 10);
312 iresult = check_num (i);
313 asprintf (&show, "%llu", response_value[i]);
316 /* Process this block for string matching */
317 else if (eval_method[i] & CRIT_STRING) {
318 if (strcmp (show, string_value))
319 iresult = STATE_CRITICAL;
320 else
321 iresult = STATE_OK;
324 /* Process this block for regex matching */
325 else if (eval_method[i] & CRIT_REGEX) {
326 excode = regexec (&preg, response, 10, pmatch, eflags);
327 if (excode == 0) {
328 iresult = STATE_OK;
330 else if (excode != REG_NOMATCH) {
331 regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
332 printf (_("Execute Error: %s\n"), errbuf);
333 exit (STATE_CRITICAL);
335 else {
336 iresult = STATE_CRITICAL;
340 /* Process this block for existence-nonexistence checks */
341 else {
342 if (eval_method[i] & CRIT_PRESENT)
343 iresult = STATE_CRITICAL;
344 else if (eval_method[i] & WARN_PRESENT)
345 iresult = STATE_WARNING;
346 else if (response && iresult == STATE_DEPENDENT)
347 iresult = STATE_OK;
350 /* Result is the worst outcome of all the OIDs tested */
351 result = max_state (result, iresult);
353 /* Prepend a label for this OID if there is one */
354 if (nlabels > (size_t)1 && (size_t)i < nlabels && labels[i] != NULL)
355 asprintf (&outbuff, "%s%s%s %s%s%s", outbuff,
356 (i == 0) ? " " : output_delim,
357 labels[i], mark (iresult), show, mark (iresult));
358 else
359 asprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim,
360 mark (iresult), show, mark (iresult));
362 /* Append a unit string for this OID if there is one */
363 if (nunits > (size_t)0 && (size_t)i < nunits && unitv[i] != NULL)
364 asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
366 i++;
368 strncat(perfstr, "=", sizeof(perfstr)-strlen(perfstr)-1);
369 strncat(perfstr, show, sizeof(perfstr)-strlen(perfstr)-1);
370 if (type)
371 strncat(perfstr, type, sizeof(perfstr)-strlen(perfstr)-1);
372 strncat(perfstr, " ", sizeof(perfstr)-strlen(perfstr)-1);
374 } /* end while (ptr) */
376 if (found == 0)
377 die (STATE_UNKNOWN,
378 _("%s problem - No data received from host\nCMD: %s\n"),
379 label,
380 cl_hidden_auth);
382 #if 0 /* Removed May 29, 2007 */
383 /* WARNING if output found on stderr */
384 if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
385 result = max_state (result, STATE_WARNING);
387 /* close stderr */
388 (void) fclose (child_stderr);
389 #endif
391 /* close the pipe */
392 if (spclose (child_process)) {
393 if (result == STATE_OK)
394 result = STATE_UNKNOWN;
395 asprintf (&outbuff, "%s (%s)", outbuff, _("snmpget returned an error status"));
398 /* if (nunits == 1 || i == 1) */
399 /* printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units); */
400 /* else */
401 printf ("%s %s -%s %s \n", label, state_text (result), outbuff, perfstr);
403 return result;
408 /* process command-line arguments */
410 process_arguments (int argc, char **argv)
412 char *ptr;
413 int c = 1;
414 int j = 0, jj = 0, ii = 0;
416 int option = 0;
417 static struct option longopts[] = {
418 STD_LONG_OPTS,
419 {"community", required_argument, 0, 'C'},
420 {"oid", required_argument, 0, 'o'},
421 {"object", required_argument, 0, 'o'},
422 {"delimiter", required_argument, 0, 'd'},
423 {"output-delimiter", required_argument, 0, 'D'},
424 {"string", required_argument, 0, 's'},
425 {"timeout", required_argument, 0, 't'},
426 {"regex", required_argument, 0, 'r'},
427 {"ereg", required_argument, 0, 'r'},
428 {"eregi", required_argument, 0, 'R'},
429 {"label", required_argument, 0, 'l'},
430 {"units", required_argument, 0, 'u'},
431 {"port", required_argument, 0, 'p'},
432 {"retries", required_argument, 0, 'e'},
433 {"miblist", required_argument, 0, 'm'},
434 {"protocol", required_argument, 0, 'P'},
435 {"seclevel", required_argument, 0, 'L'},
436 {"secname", required_argument, 0, 'U'},
437 {"authproto", required_argument, 0, 'a'},
438 {"authpasswd", required_argument, 0, 'A'},
439 {"privpasswd", required_argument, 0, 'X'},
440 {"next", no_argument, 0, 'n'},
441 {0, 0, 0, 0}
444 if (argc < 2)
445 return ERROR;
447 /* reverse compatibility for very old non-POSIX usage forms */
448 for (c = 1; c < argc; c++) {
449 if (strcmp ("-to", argv[c]) == 0)
450 strcpy (argv[c], "-t");
451 if (strcmp ("-wv", argv[c]) == 0)
452 strcpy (argv[c], "-w");
453 if (strcmp ("-cv", argv[c]) == 0)
454 strcpy (argv[c], "-c");
457 while (1) {
458 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:",
459 longopts, &option);
461 if (c == -1 || c == EOF)
462 break;
464 switch (c) {
465 case '?': /* usage */
466 usage5 ();
467 case 'h': /* help */
468 print_help ();
469 exit (STATE_OK);
470 case 'V': /* version */
471 print_revision (progname, revision);
472 exit (STATE_OK);
473 case 'v': /* verbose */
474 verbose = TRUE;
475 break;
477 /* Connection info */
478 case 'C': /* group or community */
479 community = optarg;
480 break;
481 case 'H': /* Host or server */
482 server_address = optarg;
483 break;
484 case 'p': /* TCP port number */
485 port = optarg;
486 break;
487 case 'm': /* List of MIBS */
488 miblist = optarg;
489 break;
490 case 'n': /* usesnmpgetnext */
491 usesnmpgetnext = TRUE;
492 break;
493 case 'P': /* SNMP protocol version */
494 proto = optarg;
495 break;
496 case 'L': /* security level */
497 seclevel = optarg;
498 break;
499 case 'U': /* security username */
500 secname = optarg;
501 break;
502 case 'a': /* auth protocol */
503 authproto = optarg;
504 break;
505 case 'A': /* auth passwd */
506 authpasswd = optarg;
507 break;
508 case 'X': /* priv passwd */
509 privpasswd = optarg;
510 break;
511 case 't': /* timeout period */
512 if (!is_integer (optarg))
513 usage2 (_("Timeout interval must be a positive integer"), optarg);
514 else
515 timeout_interval = atoi (optarg);
516 break;
518 /* Test parameters */
519 case 'c': /* critical time threshold */
520 if (strspn (optarg, "0123456789:,") < strlen (optarg))
521 usage2 (_("Invalid critical threshold"), optarg);
522 for (ptr = optarg; ptr && jj < MAX_OIDS; jj++) {
523 if (llu_getll (&lower_crit_lim[jj], ptr) == 1)
524 eval_method[jj] |= CRIT_LT;
525 if (llu_getul (&upper_crit_lim[jj], ptr) == 1)
526 eval_method[jj] |= CRIT_GT;
527 (ptr = index (ptr, ',')) ? ptr++ : ptr;
529 break;
530 case 'w': /* warning time threshold */
531 if (strspn (optarg, "0123456789:,") < strlen (optarg))
532 usage2 (_("Invalid warning threshold"), optarg);
533 for (ptr = optarg; ptr && ii < MAX_OIDS; ii++) {
534 if (llu_getll (&lower_warn_lim[ii], ptr) == 1)
535 eval_method[ii] |= WARN_LT;
536 if (llu_getul (&upper_warn_lim[ii], ptr) == 1)
537 eval_method[ii] |= WARN_GT;
538 (ptr = index (ptr, ',')) ? ptr++ : ptr;
540 break;
541 case 'e': /* PRELIMINARY - may change */
542 case 'E': /* PRELIMINARY - may change */
543 if (!is_integer (optarg))
544 usage2 (_("Retries interval must be a positive integer"), optarg);
545 else
546 retries = atoi(optarg);
547 break;
548 case 'o': /* object identifier */
549 if ( strspn( optarg, "0123456789.," ) != strlen( optarg ) ) {
551 * we have something other than digits, periods and comas,
552 * so we have a mib variable, rather than just an SNMP OID,
553 * so we have to actually read the mib files
555 needmibs = TRUE;
558 for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
559 ptr[0] = ' '; /* relpace comma with space */
560 for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
561 j++; /* count OIDs */
562 asprintf (&oid, "%s %s", (oid?oid:""), optarg);
563 if (c == 'E' || c == 'e') {
564 jj++;
565 ii++;
567 if (c == 'E')
568 eval_method[j+1] |= WARN_PRESENT;
569 else if (c == 'e')
570 eval_method[j+1] |= CRIT_PRESENT;
571 break;
572 case 's': /* string or substring */
573 strncpy (string_value, optarg, sizeof (string_value) - 1);
574 string_value[sizeof (string_value) - 1] = 0;
575 eval_method[jj++] = CRIT_STRING;
576 ii++;
577 break;
578 case 'R': /* regex */
579 cflags = REG_ICASE;
580 case 'r': /* regex */
581 cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
582 strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
583 regex_expect[sizeof (regex_expect) - 1] = 0;
584 errcode = regcomp (&preg, regex_expect, cflags);
585 if (errcode != 0) {
586 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
587 printf (_("Could Not Compile Regular Expression"));
588 return ERROR;
590 eval_method[jj++] = CRIT_REGEX;
591 ii++;
592 break;
594 /* Format */
595 case 'd': /* delimiter */
596 delimiter = strscpy (delimiter, optarg);
597 break;
598 case 'D': /* output-delimiter */
599 output_delim = strscpy (output_delim, optarg);
600 break;
601 case 'l': /* label */
602 label = optarg;
603 nlabels++;
604 if (nlabels >= labels_size) {
605 labels_size += 8;
606 labels = realloc (labels, labels_size);
607 if (labels == NULL)
608 die (STATE_UNKNOWN, _("Could not reallocate labels[%d]"), (int)nlabels);
610 labels[nlabels - 1] = optarg;
611 ptr = thisarg (optarg);
612 labels[nlabels - 1] = ptr;
613 if (strstr (ptr, "'") == ptr)
614 labels[nlabels - 1] = ptr + 1;
615 while (ptr && (ptr = nextarg (ptr))) {
616 if (nlabels >= labels_size) {
617 labels_size += 8;
618 labels = realloc (labels, labels_size);
619 if (labels == NULL)
620 die (STATE_UNKNOWN, _("Could not reallocate labels\n"));
622 labels++;
623 ptr = thisarg (ptr);
624 if (strstr (ptr, "'") == ptr)
625 labels[nlabels - 1] = ptr + 1;
626 else
627 labels[nlabels - 1] = ptr;
629 break;
630 case 'u': /* units */
631 units = optarg;
632 nunits++;
633 if (nunits >= unitv_size) {
634 unitv_size += 8;
635 unitv = realloc (unitv, unitv_size);
636 if (unitv == NULL)
637 die (STATE_UNKNOWN, _("Could not reallocate units [%d]\n"), (int)nunits);
639 unitv[nunits - 1] = optarg;
640 ptr = thisarg (optarg);
641 unitv[nunits - 1] = ptr;
642 if (strstr (ptr, "'") == ptr)
643 unitv[nunits - 1] = ptr + 1;
644 while (ptr && (ptr = nextarg (ptr))) {
645 if (nunits >= unitv_size) {
646 unitv_size += 8;
647 unitv = realloc (unitv, unitv_size);
648 if (units == NULL)
649 die (STATE_UNKNOWN, _("Could not realloc() units\n"));
651 nunits++;
652 ptr = thisarg (ptr);
653 if (strstr (ptr, "'") == ptr)
654 unitv[nunits - 1] = ptr + 1;
655 else
656 unitv[nunits - 1] = ptr;
658 break;
663 if (server_address == NULL)
664 server_address = argv[optind];
666 if (community == NULL)
667 community = strdup (DEFAULT_COMMUNITY);
671 return validate_arguments ();
675 /******************************************************************************
678 <sect3>
679 <title>validate_arguments</title>
681 <para>&PROTO_validate_arguments;</para>
683 <para>Checks to see if the default miblist needs to be loaded. Also verifies
684 the authentication and authorization combinations based on protocol version
685 selected.</para>
687 <para></para>
689 </sect3>
691 ******************************************************************************/
696 validate_arguments ()
698 /* check whether to load locally installed MIBS (CPU/disk intensive) */
699 if (miblist == NULL) {
700 if ( needmibs == TRUE ) {
701 miblist = strdup (DEFAULT_MIBLIST);
702 }else{
703 miblist = "''"; /* don't read any mib files for numeric oids */
708 /* Need better checks to verify seclevel and authproto choices */
710 if (seclevel == NULL)
711 asprintf (&seclevel, "noAuthNoPriv");
714 if (authproto == NULL )
715 asprintf(&authproto, DEFAULT_AUTH_PROTOCOL);
719 if (proto == NULL || (strcmp(proto,DEFAULT_PROTOCOL) == 0) ) { /* default protocol version */
720 asprintf(&proto, DEFAULT_PROTOCOL);
721 asprintf(&authpriv, "%s%s", "-c ", community);
723 else if ( strcmp (proto, "2c") == 0 ) { /* snmpv2c args */
724 asprintf(&authpriv, "%s%s", "-c ", community);
726 else if ( strcmp (proto, "3") == 0 ) { /* snmpv3 args */
727 asprintf(&proto, "%s", "3");
729 if ( (strcmp(seclevel, "noAuthNoPriv") == 0) || seclevel == NULL ) {
730 asprintf(&authpriv, "%s", "-l noAuthNoPriv" );
732 else if ( strcmp(seclevel, "authNoPriv") == 0 ) {
733 if ( secname == NULL || authpasswd == NULL) {
734 printf (_("Missing secname (%s) or authpassword (%s) ! \n"),secname, authpasswd );
735 print_usage ();
736 exit (STATE_UNKNOWN);
738 asprintf(&authpriv, "-l authNoPriv -a %s -u %s -A %s ", authproto, secname, authpasswd);
740 else if ( strcmp(seclevel, "authPriv") == 0 ) {
741 if ( secname == NULL || authpasswd == NULL || privpasswd == NULL ) {
742 printf (_("Missing secname (%s), authpassword (%s), or privpasswd (%s)! \n"),secname, authpasswd,privpasswd );
743 print_usage ();
744 exit (STATE_UNKNOWN);
746 asprintf(&authpriv, "-l authPriv -a %s -u %s -A %s -x DES -X %s ", authproto, secname, authpasswd, privpasswd);
750 else {
751 usage2 (_("Invalid SNMP version"), proto);
754 return OK;
759 char *
760 clarify_message (char *msg)
762 int i = 0;
763 int foo;
764 char tmpmsg_c[MAX_INPUT_BUFFER];
765 char *tmpmsg = (char *) &tmpmsg_c;
766 tmpmsg = strcpy (tmpmsg, msg);
767 if (!strncmp (tmpmsg, " Hex:", 5)) {
768 tmpmsg = strtok (tmpmsg, ":");
769 while ((tmpmsg = strtok (NULL, " "))) {
770 foo = strtol (tmpmsg, NULL, 16);
771 /* Translate chars that are not the same value in the printers
772 * character set.
774 switch (foo) {
775 case 208:
777 foo = 197;
778 break;
780 case 216:
782 foo = 196;
783 break;
786 msg[i] = foo;
787 i++;
789 msg[i] = 0;
791 return (msg);
797 check_num (int i)
799 int result;
800 result = STATE_OK;
801 if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
802 lower_warn_lim[i] > upper_warn_lim[i]) {
803 if (response_value[i] <= lower_warn_lim[i] &&
804 response_value[i] >= upper_warn_lim[i]) {
805 result = STATE_WARNING;
808 else if
809 ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
810 (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
811 (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
812 (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
813 (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
814 (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
815 result = STATE_WARNING;
818 if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
819 lower_crit_lim[i] > upper_crit_lim[i]) {
820 if (response_value[i] <= lower_crit_lim[i] &&
821 response_value[i] >= upper_crit_lim[i]) {
822 result = STATE_CRITICAL;
825 else if
826 ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
827 (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
828 (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
829 (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
830 (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
831 (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
832 result = STATE_CRITICAL;
835 return result;
841 llu_getll (unsigned long long *ll, char *str)
843 char tmp[100];
844 if (strchr (str, ':') == NULL)
845 return 0;
846 if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
847 return 0;
848 if (sscanf (str, "%llu%[:]", ll, tmp) == 2)
849 return 1;
850 return 0;
856 llu_getul (unsigned long long *ul, char *str)
858 char tmp[100];
859 if (sscanf (str, "%llu%[^,]", ul, tmp) == 1)
860 return 1;
861 if (sscanf (str, ":%llu%[^,]", ul, tmp) == 1)
862 return 1;
863 if (sscanf (str, "%*u:%llu%[^,]", ul, tmp) == 1)
864 return 1;
865 return 0;
870 /* trim leading whitespace
871 if there is a leading quote, make sure it balances */
873 char *
874 thisarg (char *str)
876 str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
877 if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
878 if (strlen (str) == 1 || !strstr (str + 1, "'"))
879 die (STATE_UNKNOWN, _("Unbalanced quotes\n"));
881 return str;
886 /* if there's a leading quote, advance to the trailing quote
887 set the trailing quote to '\x0'
888 if the string continues, advance beyond the comma */
890 char *
891 nextarg (char *str)
893 if (strstr (str, "'") == str) {
894 str[0] = 0;
895 if (strlen (str) > 1) {
896 str = strstr (str + 1, "'");
897 return (++str);
899 else {
900 return NULL;
903 if (strstr (str, ",") == str) {
904 str[0] = 0;
905 if (strlen (str) > 1) {
906 return (++str);
908 else {
909 return NULL;
912 if ((str = strstr (str, ",")) && strlen (str) > 1) {
913 str[0] = 0;
914 return (++str);
916 return NULL;
921 void
922 print_help (void)
924 print_revision (progname, revision);
926 printf (COPYRIGHT, copyright, email);
928 printf ("%s\n", _("Check status of remote machines and obtain sustem information via SNMP"));
930 printf ("\n\n");
932 print_usage ();
934 printf (_(UT_HELP_VRSN));
936 printf (_(UT_HOST_PORT), 'p', DEFAULT_PORT);
938 /* SNMP and Authentication Protocol */
939 printf (" %s\n", "-n, --next");
940 printf (" %s\n", _("Use SNMP GETNEXT instead of SNMP GET"));
941 printf (" %s\n", "-P, --protocol=[1|2c|3]");
942 printf (" %s\n", _("SNMP protocol version"));
943 printf (" %s\n", "-L, --seclevel=[noAuthNoPriv|authNoPriv|authPriv]");
944 printf (" %s\n", _("SNMPv3 securityLevel"));
945 printf (" %s\n", "-a, --authproto=[MD5|SHA]");
946 printf (" %s\n", _("SNMPv3 auth proto"));
948 /* Authentication Tokens*/
949 printf (" %s\n", "-C, --community=STRING");
950 printf (" %s ", _("Optional community string for SNMP communication"));
951 printf ("(%s \"%s\")\n", _("default is") ,DEFAULT_COMMUNITY);
952 printf (" %s\n", "-U, --secname=USERNAME");
953 printf (" %s\n", _("SNMPv3 username"));
954 printf (" %s\n", "-A, --authpassword=PASSWORD");
955 printf (" %s\n", _("SNMPv3 authentication password"));
956 printf (" %s\n", "-X, --privpasswd=PASSWORD");
957 printf (" %s\n", _("SNMPv3 privacy password"));
959 /* OID Stuff */
960 printf (" %s\n", "-o, --oid=OID(s)");
961 printf (" %s\n", _("Object identifier(s) or SNMP variables whose value you wish to query"));
962 printf (" %s\n", "-m, --miblist=STRING");
963 printf (" %s\n", _("List of MIBS to be loaded (default = none if using numeric oids or 'ALL'"));
964 printf (" %s\n", _("for symbolic oids.)"));
965 printf (" %s\n", "-d, --delimiter=STRING");
966 printf (_(" Delimiter to use when parsing returned data. Default is \"%s\""), DEFAULT_DELIMITER);
967 printf (" %s\n", _("Any data on the right hand side of the delimiter is considered"));
968 printf (" %s\n", _("to be the data that should be used in the evaluation."));
970 /* Tests Against Integers */
971 printf (" %s\n", "-w, --warning=INTEGER_RANGE(s)");
972 printf (" %s\n", _("Range(s) which will not result in a WARNING status"));
973 printf (" %s\n", "-c, --critical=INTEGER_RANGE(s)");
974 printf (" %s\n", _("Range(s) which will not result in a CRITICAL status"));
976 /* Tests Against Strings */
977 printf (" %s\n", "-s, --string=STRING");
978 printf (" %s\n", _("Return OK state (for that OID) if STRING is an exact match"));
979 printf (" %s\n", "-r, --ereg=REGEX");
980 printf (" %s\n", _("Return OK state (for that OID) if extended regular expression REGEX matches"));
981 printf (" %s\n", "-R, --eregi=REGEX");
982 printf (" %s\n", _("Return OK state (for that OID) if case-insensitive extended REGEX matches"));
983 printf (" %s\n", "-l, --label=STRING");
984 printf (" %s\n", _("Prefix label for output from plugin (default -s 'SNMP')"));
986 /* Output Formatting */
987 printf (" %s\n", "-u, --units=STRING");
988 printf (" %s\n", _("Units label(s) for output data (e.g., 'sec.')."));
989 printf (" %s\n", "-D, --output-delimiter=STRING");
990 printf (" %s\n", _("Separates output on multiple OID requests"));
992 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
994 printf (_(UT_VERBOSE));
996 printf ("\n");
997 printf ("%s\n", _("This plugin uses the 'snmpget' command included with the NET-SNMP package."));
998 printf ("%s\n", _("if you don't have the package installed, you will need to download it from"));
999 printf ("%s\n", _("http://net-snmp.sourceforge.net before you can use this plugin."));
1001 printf ("\n");
1002 printf ("%s\n", _("Notes:"));
1003 printf (" %s\n", _("- Multiple OIDs may be indicated by a comma- or space-delimited list (lists with"));
1004 printf (" %s\n", _("internal spaces must be quoted) [max 8 OIDs]"));
1006 printf (" %s\n", _("- Ranges are inclusive and are indicated with colons. When specified as"));
1007 printf (" %s\n", _("'min:max' a STATE_OK will be returned if the result is within the indicated"));
1008 printf (" %s\n", _("range or is equal to the upper or lower bound. A non-OK state will be"));
1009 printf (" %s\n", _("returned if the result is outside the specified range."));
1011 printf (" %s\n", _("- If specified in the order 'max:min' a non-OK state will be returned if the"));
1012 printf (" %s\n", _("result is within the (inclusive) range."));
1014 printf (" %s\n", _("- Upper or lower bounds may be omitted to skip checking the respective limit."));
1015 printf (" %s\n", _("- Bare integers are interpreted as upper limits."));
1016 printf (" %s\n", _("- When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'"));
1017 printf (" %s\n", _("- Note that only one string and one regex may be checked at present"));
1018 printf (" %s\n", _("- All evaluation methods other than PR, STR, and SUBSTR expect that the value"));
1019 printf (" %s\n", _("returned from the SNMP query is an unsigned integer."));
1021 printf (_(UT_SUPPORT));
1026 void
1027 print_usage (void)
1029 printf (_("Usage:"));
1030 printf ("%s -H <ip_address> -o <OID> [-w warn_range] [-c crit_range]\n",progname);
1031 printf ("[-C community] [-s string] [-r regex] [-R regexi] [-t timeout] [-e retries]\n");
1032 printf ("[-l label] [-u units] [-p port-number] [-d delimiter] [-D output-delimiter]\n");
1033 printf ("[-m miblist] [-P snmp version] [-L seclevel] [-U secname] [-a authproto]\n");
1034 printf ("[-A authpasswd] [-X privpasswd]\n");