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