Code

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