d66d834e145fa0183f6bd6915b33ca37fb101de3
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 unsigned long lower_warn_lim[MAX_OIDS];
113 unsigned long upper_warn_lim[MAX_OIDS];
114 unsigned long lower_crit_lim[MAX_OIDS];
115 unsigned long upper_crit_lim[MAX_OIDS];
116 unsigned long response_value[MAX_OIDS];
117 int check_warning_value = FALSE;
118 int check_critical_value = FALSE;
119 int retries = 0;
120 unsigned long eval_method[MAX_OIDS];
121 char *delimiter;
122 char *output_delim;
123 char *miblist;
125 int
126 main (int argc, char **argv)
127 {
128 int i = 0;
129 int iresult = STATE_UNKNOWN;
130 int found = 0;
131 int result = STATE_DEPENDENT;
132 char input_buffer[MAX_INPUT_BUFFER];
133 char *command_line = NULL;
134 char *response = NULL;
135 char *outbuff;
136 char *output;
137 char *ptr = NULL;
138 char *p2 = NULL;
139 char *show = NULL;
140 char type[8];
142 setlocale (LC_ALL, "");
143 bindtextdomain (PACKAGE, LOCALEDIR);
144 textdomain (PACKAGE);
146 labels = malloc (labels_size);
147 unitv = malloc (unitv_size);
148 for (i = 0; i < MAX_OIDS; i++)
149 eval_method[i] = CHECK_UNDEF;
150 i = 0;
152 oid = strdup ("");
153 label = strdup ("SNMP");
154 units = strdup ("");
155 port = strdup (DEFAULT_PORT);
156 outbuff = strdup ("");
157 output = strdup ("");
158 delimiter = strdup (" = ");
159 output_delim = strdup (DEFAULT_OUTPUT_DELIMITER);
160 miblist = strdup (DEFAULT_MIBLIST);
161 timeout_interval = DEFAULT_TIMEOUT;
162 retries = DEFAULT_RETRIES;
164 if (process_arguments (argc, argv) == ERROR)
165 usage4 (_("Could not parse arguments"));
167 /* create the command line to execute */
168 asprintf (&command_line, "%s -t %d -r %d -m %s -v %s %s %s:%s %s",
169 PATH_TO_SNMPGET, timeout_interval, retries, miblist, proto,
170 authpriv, server_address, port, oid);
171 if (verbose)
172 printf ("%s\n", command_line);
174 /* run the command */
175 child_process = spopen (command_line);
176 if (child_process == NULL) {
177 printf (_("Could not open pipe: %s\n"), command_line);
178 exit (STATE_UNKNOWN);
179 }
181 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
182 if (child_stderr == NULL) {
183 printf (_("Could not open stderr for %s\n"), command_line);
184 }
186 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
187 asprintf (&output, "%s%s", output, input_buffer);
189 if (verbose)
190 printf ("%s\n", output);
192 ptr = output;
194 strcat(perfstr, "| ");
195 while (ptr) {
196 char *foo;
198 foo = strstr (ptr, delimiter);
199 strncat(perfstr, ptr, foo-ptr);
200 ptr = foo;
202 if (ptr == NULL)
203 break;
205 ptr += strlen (delimiter);
206 ptr += strspn (ptr, " ");
208 found++;
210 if (ptr[0] == '"') {
211 ptr++;
212 response = strpcpy (response, ptr, "\"");
213 ptr = strpbrk (ptr, "\"");
214 ptr += strspn (ptr, "\"\n");
215 }
216 else {
217 response = strpcpy (response, ptr, "\n");
218 ptr = strpbrk (ptr, "\n");
219 ptr += strspn (ptr, "\n");
220 while
221 (strstr (ptr, delimiter) &&
222 strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter)) {
223 response = strpcat (response, ptr, "\n");
224 ptr = strpbrk (ptr, "\n");
225 }
226 if (ptr && strstr (ptr, delimiter) == NULL) {
227 asprintf (&response, "%s%s", response, ptr);
228 ptr = NULL;
229 }
230 }
232 /* We strip out the datatype indicator for PHBs */
233 if (strstr (response, "Gauge: "))
234 show = strstr (response, "Gauge: ") + 7;
235 else if (strstr (response, "Gauge32: "))
236 show = strstr (response, "Gauge32: ") + 9;
237 else if (strstr (response, "Counter32: ")) {
238 show = strstr (response, "Counter32: ") + 11;
239 strcpy(type, "c");
240 }
241 else if (strstr (response, "INTEGER: "))
242 show = strstr (response, "INTEGER: ") + 9;
243 else if (strstr (response, "STRING: "))
244 show = strstr (response, "STRING: ") + 8;
245 else
246 show = response;
247 p2 = show;
249 iresult = STATE_DEPENDENT;
251 /* Process this block for integer comparisons */
252 if (eval_method[i] & CRIT_GT ||
253 eval_method[i] & CRIT_LT ||
254 eval_method[i] & CRIT_GE ||
255 eval_method[i] & CRIT_LE ||
256 eval_method[i] & CRIT_EQ ||
257 eval_method[i] & CRIT_NE ||
258 eval_method[i] & WARN_GT ||
259 eval_method[i] & WARN_LT ||
260 eval_method[i] & WARN_GE ||
261 eval_method[i] & WARN_LE ||
262 eval_method[i] & WARN_EQ ||
263 eval_method[i] & WARN_NE) {
264 p2 = strpbrk (p2, "0123456789");
265 if (p2 == NULL)
266 die (STATE_UNKNOWN,_("No valid data returned"));
267 response_value[i] = strtoul (p2, NULL, 10);
268 iresult = check_num (i);
269 asprintf (&show, "%lu", response_value[i]);
270 }
272 /* Process this block for string matching */
273 else if (eval_method[i] & CRIT_STRING) {
274 if (strcmp (show, string_value))
275 iresult = STATE_CRITICAL;
276 else
277 iresult = STATE_OK;
278 }
280 /* Process this block for regex matching */
281 else if (eval_method[i] & CRIT_REGEX) {
282 #ifdef HAVE_REGEX_H
283 excode = regexec (&preg, response, 10, pmatch, eflags);
284 if (excode == 0) {
285 iresult = STATE_OK;
286 }
287 else if (excode != REG_NOMATCH) {
288 regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
289 printf (_("Execute Error: %s\n"), errbuf);
290 exit (STATE_CRITICAL);
291 }
292 else {
293 iresult = STATE_CRITICAL;
294 }
295 #else
296 printf (_("Call for regex which was not a compiled option"));
297 exit (STATE_UNKNOWN);
298 #endif
299 }
301 /* Process this block for existence-nonexistence checks */
302 else {
303 if (eval_method[i] & CRIT_PRESENT)
304 iresult = STATE_CRITICAL;
305 else if (eval_method[i] & WARN_PRESENT)
306 iresult = STATE_WARNING;
307 else if (response && iresult == STATE_DEPENDENT)
308 iresult = STATE_OK;
309 }
311 /* Result is the worst outcome of all the OIDs tested */
312 result = max_state (result, iresult);
314 /* Prepend a label for this OID if there is one */
315 if (nlabels > (size_t)1 && (size_t)i < nlabels && labels[i] != NULL)
316 asprintf (&outbuff, "%s%s%s %s%s%s", outbuff,
317 (i == 0) ? " " : output_delim,
318 labels[i], mark (iresult), show, mark (iresult));
319 else
320 asprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim,
321 mark (iresult), show, mark (iresult));
323 /* Append a unit string for this OID if there is one */
324 if (nunits > (size_t)0 && (size_t)i < nunits && unitv[i] != NULL)
325 asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
327 i++;
329 char *str[MAX_INPUT_BUFFER];
330 asprintf(str, "=%s%s;;;; ", show, type ? type : "");
331 strcat(perfstr, *str);
333 } /* end while (ptr) */
335 if (found == 0)
336 die (STATE_UNKNOWN,
337 _("%s problem - No data received from host\nCMD: %s\n"),
338 label,
339 command_line);
341 /* WARNING if output found on stderr */
342 if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
343 result = max_state (result, STATE_WARNING);
345 /* close stderr */
346 (void) fclose (child_stderr);
348 /* close the pipe */
349 if (spclose (child_process))
350 result = max_state (result, STATE_WARNING);
352 /* if (nunits == 1 || i == 1) */
353 /* printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units); */
354 /* else */
355 printf ("%s %s -%s %s \n", label, state_text (result), outbuff, perfstr);
357 return result;
358 }
362 /* process command-line arguments */
363 int
364 process_arguments (int argc, char **argv)
365 {
366 char *ptr;
367 int c = 1;
368 int j = 0, jj = 0, ii = 0;
370 int option = 0;
371 static struct option longopts[] = {
372 STD_LONG_OPTS,
373 {"community", required_argument, 0, 'C'},
374 {"oid", required_argument, 0, 'o'},
375 {"object", required_argument, 0, 'o'},
376 {"delimiter", required_argument, 0, 'd'},
377 {"output-delimiter", required_argument, 0, 'D'},
378 {"string", required_argument, 0, 's'},
379 {"timeout", required_argument, 0, 't'},
380 {"regex", required_argument, 0, 'r'},
381 {"ereg", required_argument, 0, 'r'},
382 {"eregi", required_argument, 0, 'R'},
383 {"label", required_argument, 0, 'l'},
384 {"units", required_argument, 0, 'u'},
385 {"port", required_argument, 0, 'p'},
386 {"retries", required_argument, 0, 'e'},
387 {"miblist", required_argument, 0, 'm'},
388 {"protocol", required_argument, 0, 'P'},
389 {"seclevel", required_argument, 0, 'L'},
390 {"secname", required_argument, 0, 'U'},
391 {"authproto", required_argument, 0, 'a'},
392 {"authpasswd", required_argument, 0, 'A'},
393 {"privpasswd", required_argument, 0, 'X'},
394 {0, 0, 0, 0}
395 };
397 if (argc < 2)
398 return ERROR;
400 /* reverse compatibility for very old non-POSIX usage forms */
401 for (c = 1; c < argc; c++) {
402 if (strcmp ("-to", argv[c]) == 0)
403 strcpy (argv[c], "-t");
404 if (strcmp ("-wv", argv[c]) == 0)
405 strcpy (argv[c], "-w");
406 if (strcmp ("-cv", argv[c]) == 0)
407 strcpy (argv[c], "-c");
408 }
410 while (1) {
411 c = getopt_long (argc, argv, "hvVt:c:w:H:C:o:e:E:d:D:s:t:R:r:l:u:p:m:P:L:U:a:A:X:",
412 longopts, &option);
414 if (c == -1 || c == EOF)
415 break;
417 switch (c) {
418 case '?': /* usage */
419 usage2 (_("Unknown argument"), optarg);
420 case 'h': /* help */
421 print_help ();
422 exit (STATE_OK);
423 case 'V': /* version */
424 print_revision (progname, revision);
425 exit (STATE_OK);
426 case 'v': /* verbose */
427 verbose = TRUE;
428 break;
430 /* Connection info */
431 case 'C': /* group or community */
432 community = optarg;
433 break;
434 case 'H': /* Host or server */
435 server_address = optarg;
436 break;
437 case 'p': /* TCP port number */
438 port = optarg;
439 break;
440 case 'm': /* List of MIBS */
441 miblist = optarg;
442 break;
443 case 'P': /* SNMP protocol version */
444 proto = optarg;
445 break;
446 case 'L': /* security level */
447 seclevel = optarg;
448 break;
449 case 'U': /* security username */
450 secname = optarg;
451 break;
452 case 'a': /* auth protocol */
453 authproto = optarg;
454 break;
455 case 'A': /* auth passwd */
456 authpasswd = optarg;
457 break;
458 case 'X': /* priv passwd */
459 privpasswd = optarg;
460 break;
461 case 't': /* timeout period */
462 if (!is_integer (optarg))
463 usage2 (_("Timeout interval must be a positive integer"), optarg);
464 else
465 timeout_interval = atoi (optarg);
466 break;
468 /* Test parameters */
469 case 'c': /* critical time threshold */
470 if (strspn (optarg, "0123456789:,") < strlen (optarg))
471 usage2 (_("Invalid critical threshold: %s\n"), optarg);
472 for (ptr = optarg; ptr && jj < MAX_OIDS; jj++) {
473 if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
474 eval_method[jj] |= CRIT_LT;
475 if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
476 eval_method[jj] |= CRIT_GT;
477 (ptr = index (ptr, ',')) ? ptr++ : ptr;
478 }
479 break;
480 case 'w': /* warning time threshold */
481 if (strspn (optarg, "0123456789:,") < strlen (optarg))
482 usage2 (_("Invalid warning threshold: %s\n"), optarg);
483 for (ptr = optarg; ptr && ii < MAX_OIDS; ii++) {
484 if (lu_getll (&lower_warn_lim[ii], ptr) == 1)
485 eval_method[ii] |= WARN_LT;
486 if (lu_getul (&upper_warn_lim[ii], ptr) == 1)
487 eval_method[ii] |= WARN_GT;
488 (ptr = index (ptr, ',')) ? ptr++ : ptr;
489 }
490 break;
491 case 'e': /* PRELIMINARY - may change */
492 case 'E': /* PRELIMINARY - may change */
493 if (!is_integer (optarg))
494 usage2 (_("Retries interval must be a positive integer"), optarg);
495 else
496 retries = atoi(optarg);
497 break;
498 case 'o': /* object identifier */
499 for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
500 ptr[0] = ' '; /* relpace comma with space */
501 for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
502 j++; /* count OIDs */
503 asprintf (&oid, "%s %s", (oid?oid:""), optarg);
504 if (c == 'E' || c == 'e') {
505 jj++;
506 ii++;
507 }
508 if (c == 'E')
509 eval_method[j+1] |= WARN_PRESENT;
510 else if (c == 'e')
511 eval_method[j+1] |= CRIT_PRESENT;
512 break;
513 case 's': /* string or substring */
514 strncpy (string_value, optarg, sizeof (string_value) - 1);
515 string_value[sizeof (string_value) - 1] = 0;
516 eval_method[jj++] = CRIT_STRING;
517 ii++;
518 break;
519 case 'R': /* regex */
520 #ifdef HAVE_REGEX_H
521 cflags = REG_ICASE;
522 #endif
523 case 'r': /* regex */
524 #ifdef HAVE_REGEX_H
525 cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
526 strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
527 regex_expect[sizeof (regex_expect) - 1] = 0;
528 errcode = regcomp (&preg, regex_expect, cflags);
529 if (errcode != 0) {
530 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
531 printf (_("Could Not Compile Regular Expression"));
532 return ERROR;
533 }
534 eval_method[jj++] = CRIT_REGEX;
535 ii++;
536 #else
537 printf (_("call for regex which was not a compiled option"));
538 exit (STATE_UNKNOWN);
539 #endif
540 break;
542 /* Format */
543 case 'd': /* delimiter */
544 delimiter = strscpy (delimiter, optarg);
545 break;
546 case 'D': /* output-delimiter */
547 output_delim = strscpy (output_delim, optarg);
548 break;
549 case 'l': /* label */
550 label = optarg;
551 nlabels++;
552 if (nlabels >= labels_size) {
553 labels_size += 8;
554 labels = realloc (labels, labels_size);
555 if (labels == NULL)
556 die (STATE_UNKNOWN, _("Could not reallocate labels[%d]"), nlabels);
557 }
558 labels[nlabels - 1] = optarg;
559 ptr = thisarg (optarg);
560 labels[nlabels - 1] = ptr;
561 if (strstr (ptr, "'") == ptr)
562 labels[nlabels - 1] = ptr + 1;
563 while (ptr && (ptr = nextarg (ptr))) {
564 if (nlabels >= labels_size) {
565 labels_size += 8;
566 labels = realloc (labels, labels_size);
567 if (labels == NULL)
568 die (STATE_UNKNOWN, _("Could not reallocate labels\n"));
569 }
570 labels++;
571 ptr = thisarg (ptr);
572 if (strstr (ptr, "'") == ptr)
573 labels[nlabels - 1] = ptr + 1;
574 else
575 labels[nlabels - 1] = ptr;
576 }
577 break;
578 case 'u': /* units */
579 units = optarg;
580 nunits++;
581 if (nunits >= unitv_size) {
582 unitv_size += 8;
583 unitv = realloc (unitv, unitv_size);
584 if (unitv == NULL)
585 die (STATE_UNKNOWN, _("Could not reallocate units [%d]\n"), nunits);
586 }
587 unitv[nunits - 1] = optarg;
588 ptr = thisarg (optarg);
589 unitv[nunits - 1] = ptr;
590 if (strstr (ptr, "'") == ptr)
591 unitv[nunits - 1] = ptr + 1;
592 while (ptr && (ptr = nextarg (ptr))) {
593 if (nunits >= unitv_size) {
594 unitv_size += 8;
595 unitv = realloc (unitv, unitv_size);
596 if (units == NULL)
597 die (STATE_UNKNOWN, _("Could not realloc() units\n"));
598 }
599 nunits++;
600 ptr = thisarg (ptr);
601 if (strstr (ptr, "'") == ptr)
602 unitv[nunits - 1] = ptr + 1;
603 else
604 unitv[nunits - 1] = ptr;
605 }
606 break;
608 }
609 }
611 if (server_address == NULL)
612 server_address = argv[optind];
614 if (community == NULL)
615 community = strdup (DEFAULT_COMMUNITY);
617 return validate_arguments ();
618 }
621 /******************************************************************************
623 @@-
624 <sect3>
625 <title>validate_arguments</title>
627 <para>&PROTO_validate_arguments;</para>
629 <para>Given a database name, this function returns TRUE if the string
630 is a valid PostgreSQL database name, and returns false if it is
631 not.</para>
633 <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
634 characters long and consist of letters, numbers, and underscores. The
635 first character cannot be a number, however.</para>
637 </sect3>
638 -@@
639 ******************************************************************************/
643 int
644 validate_arguments ()
645 {
647 /* Need better checks to verify seclevel and authproto choices */
649 if (seclevel == NULL)
650 asprintf (&seclevel, "noAuthNoPriv");
653 if (authproto == NULL )
654 asprintf(&authproto, DEFAULT_AUTH_PROTOCOL);
658 if (proto == NULL || (strcmp(proto,DEFAULT_PROTOCOL) == 0) ) { /* default protocol version */
659 asprintf(&proto, DEFAULT_PROTOCOL);
660 asprintf(&authpriv, "%s%s", "-c ", community);
661 }
662 else if ( strcmp (proto, "3") == 0 ) { /* snmpv3 args */
663 asprintf(&proto, "%s", "3");
665 if ( (strcmp(seclevel, "noAuthNoPriv") == 0) || seclevel == NULL ) {
666 asprintf(&authpriv, "%s", "-l noAuthNoPriv" );
667 }
668 else if ( strcmp(seclevel, "authNoPriv") == 0 ) {
669 if ( secname == NULL || authpasswd == NULL) {
670 printf (_("Missing secname (%s) or authpassword (%s) ! \n"),secname, authpasswd );
671 print_usage ();
672 exit (STATE_UNKNOWN);
673 }
674 asprintf(&authpriv, "-l authNoPriv -a %s -u %s -A %s ", authproto, secname, authpasswd);
675 }
676 else if ( strcmp(seclevel, "authPriv") == 0 ) {
677 if ( secname == NULL || authpasswd == NULL || privpasswd == NULL ) {
678 printf (_("Missing secname (%s), authpassword (%s), or privpasswd (%s)! \n"),secname, authpasswd,privpasswd );
679 print_usage ();
680 exit (STATE_UNKNOWN);
681 }
682 asprintf(&authpriv, "-l authPriv -a %s -u %s -A %s -x DES -X %s ", authproto, secname, authpasswd, privpasswd);
683 }
685 }
686 else {
687 usage2 (_("Invalid SNMP version"), proto);
688 }
690 return OK;
691 }
695 char *
696 clarify_message (char *msg)
697 {
698 int i = 0;
699 int foo;
700 char tmpmsg_c[MAX_INPUT_BUFFER];
701 char *tmpmsg = (char *) &tmpmsg_c;
702 tmpmsg = strcpy (tmpmsg, msg);
703 if (!strncmp (tmpmsg, " Hex:", 5)) {
704 tmpmsg = strtok (tmpmsg, ":");
705 while ((tmpmsg = strtok (NULL, " "))) {
706 foo = strtol (tmpmsg, NULL, 16);
707 /* Translate chars that are not the same value in the printers
708 * character set.
709 */
710 switch (foo) {
711 case 208:
712 {
713 foo = 197;
714 break;
715 }
716 case 216:
717 {
718 foo = 196;
719 break;
720 }
721 }
722 msg[i] = foo;
723 i++;
724 }
725 msg[i] = 0;
726 }
727 return (msg);
728 }
732 int
733 check_num (int i)
734 {
735 int result;
736 result = STATE_OK;
737 if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
738 lower_warn_lim[i] > upper_warn_lim[i]) {
739 if (response_value[i] <= lower_warn_lim[i] &&
740 response_value[i] >= upper_warn_lim[i]) {
741 result = STATE_WARNING;
742 }
743 }
744 else if
745 ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
746 (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
747 (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
748 (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
749 (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
750 (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
751 result = STATE_WARNING;
752 }
754 if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
755 lower_crit_lim[i] > upper_crit_lim[i]) {
756 if (response_value[i] <= lower_crit_lim[i] &&
757 response_value[i] >= upper_crit_lim[i]) {
758 result = STATE_CRITICAL;
759 }
760 }
761 else if
762 ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
763 (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
764 (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
765 (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
766 (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
767 (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
768 result = STATE_CRITICAL;
769 }
771 return result;
772 }
776 int
777 lu_getll (unsigned long *ll, char *str)
778 {
779 char tmp[100];
780 if (strchr (str, ':') == NULL)
781 return 0;
782 if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
783 return 0;
784 if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
785 return 1;
786 return 0;
787 }
791 int
792 lu_getul (unsigned long *ul, char *str)
793 {
794 char tmp[100];
795 if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
796 return 1;
797 if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
798 return 1;
799 if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
800 return 1;
801 return 0;
802 }
806 /* trim leading whitespace
807 if there is a leading quote, make sure it balances */
809 char *
810 thisarg (char *str)
811 {
812 str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
813 if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
814 if (strlen (str) == 1 || !strstr (str + 1, "'"))
815 die (STATE_UNKNOWN, _("Unbalanced quotes\n"));
816 }
817 return str;
818 }
822 /* if there's a leading quote, advance to the trailing quote
823 set the trailing quote to '\x0'
824 if the string continues, advance beyond the comma */
826 char *
827 nextarg (char *str)
828 {
829 if (strstr (str, "'") == str) {
830 str[0] = 0;
831 if (strlen (str) > 1) {
832 str = strstr (str + 1, "'");
833 return (++str);
834 }
835 else {
836 return NULL;
837 }
838 }
839 if (strstr (str, ",") == str) {
840 str[0] = 0;
841 if (strlen (str) > 1) {
842 return (++str);
843 }
844 else {
845 return NULL;
846 }
847 }
848 if ((str = strstr (str, ",")) && strlen (str) > 1) {
849 str[0] = 0;
850 return (++str);
851 }
852 return NULL;
853 }
857 void
858 print_help (void)
859 {
860 print_revision (progname, revision);
862 printf (COPYRIGHT, copyright, email);
864 printf (_("\
865 Check status of remote machines and obtain sustem information via SNMP\n\n"));
867 print_usage ();
869 printf (_(UT_HELP_VRSN));
871 printf (_(UT_HOST_PORT), 'p', DEFAULT_PORT);
873 /* SNMP and Authentication Protocol */
874 printf (_("\
875 -P, --protocol=[1|3]\n\
876 SNMP protocol version\n\
877 -L, --seclevel=[noAuthNoPriv|authNoPriv|authPriv]\n\
878 SNMPv3 securityLevel\n\
879 -a, --authproto=[MD5|SHA]\n\
880 SNMPv3 auth proto\n"));
882 /* Authentication Tokens*/
883 printf (_("\
884 -C, --community=STRING\n\
885 Optional community string for SNMP communication\n\
886 (default is \"%s\")\n\
887 -U, --secname=USERNAME\n\
888 SNMPv3 username\n\
889 -A, --authpassword=PASSWORD\n\
890 SNMPv3 authentication password\n\
891 -X, --privpasswd=PASSWORD\n\
892 SNMPv3 crypt passwd (DES)\n"), DEFAULT_COMMUNITY);
894 /* OID Stuff */
895 printf (_("\
896 -o, --oid=OID(s)\n\
897 Object identifier(s) whose value you wish to query\n\
898 -m, --miblist=STRING\n\
899 List of MIBS to be loaded (default = ALL)\n -d, --delimiter=STRING\n\
900 Delimiter to use when parsing returned data. Default is \"%s\"\n\
901 Any data on the right hand side of the delimiter is considered\n\
902 to be the data that should be used in the evaluation.\n"), DEFAULT_DELIMITER);
904 /* Tests Against Integers */
905 printf (_("\
906 -w, --warning=INTEGER_RANGE(s)\n\
907 Range(s) which will not result in a WARNING status\n\
908 -c, --critical=INTEGER_RANGE(s)\n\
909 Range(s) which will not result in a CRITICAL status\n"));
911 /* Tests Against Strings */
912 printf (_("\
913 -s, --string=STRING\n\
914 Return OK state (for that OID) if STRING is an exact match\n\
915 -r, --ereg=REGEX\n\
916 Return OK state (for that OID) if extended regular expression REGEX matches\n\
917 -R, --eregi=REGEX\n\
918 Return OK state (for that OID) if case-insensitive extended REGEX matches\n\
919 -l, --label=STRING\n\
920 Prefix label for output from plugin (default -s 'SNMP')\n"));
922 /* Output Formatting */
923 printf (_("\
924 -u, --units=STRING\n\
925 Units label(s) for output data (e.g., 'sec.').\n\
926 -D, --output-delimiter=STRING\n\
927 Separates output on multiple OID requests\n"));
929 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
931 printf (_(UT_VERBOSE));
933 printf (_("\n\
934 - This plugin uses the 'snmpget' command included with the NET-SNMP package.\n\
935 If you don't have the package installed, you will need to download it from\n\
936 http://net-snmp.sourceforge.net before you can use this plugin.\n"));
938 printf (_("\
939 - Multiple OIDs may be indicated by a comma- or space-delimited list (lists with\n\
940 internal spaces must be quoted) [max 8 OIDs]\n"));
942 printf (_("\
943 - Ranges are inclusive and are indicated with colons. When specified as\n\
944 'min:max' a STATE_OK will be returned if the result is within the indicated\n\
945 range or is equal to the upper or lower bound. A non-OK state will be\n\
946 returned if the result is outside the specified range.\n"));
948 printf (_("\
949 - If specified in the order 'max:min' a non-OK state will be returned if the\n\
950 result is within the (inclusive) range.\n"));
952 printf (_("\
953 - Upper or lower bounds may be omitted to skip checking the respective limit.\n\
954 - Bare integers are interpreted as upper limits.\n\
955 - When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'\n\
956 - Note that only one string and one regex may be checked at present\n\
957 - All evaluation methods other than PR, STR, and SUBSTR expect that the value\n\
958 returned from the SNMP query is an unsigned integer.\n"));
960 printf (_(UT_SUPPORT));
961 }
965 void
966 print_usage (void)
967 {
968 printf ("\
969 Usage: %s -H <ip_address> -o <OID> [-w warn_range] [-c crit_range] \n\
970 [-C community] [-s string] [-r regex] [-R regexi]\n\
971 [-t timeout] [-e retries]\n\
972 [-l label] [-u units] [-p port-number] [-d delimiter]\n\
973 [-D output-delimiter] [-m miblist] [-P snmp version]\n\
974 [-L seclevel] [-U secname] [-a authproto] [-A authpasswd]\n\
975 [-X privpasswd]\n", progname);
976 }