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