Code

c43f1efbcbe315c2490dada0f17d6cd43c5a846b
[nagiosplug.git] / plugins / check_snmp.c
1 /******************************************************************************
2 *
3 * Nagios check_snmp plugin
4 *
5 * License: GPL
6 * Copyright (c) 1999-2006 nagios-plugins team
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
11 *
12 * This file contains the check_snmp plugin
13 *
14 *  Check status of remote machines and obtain sustem 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-2006";
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 lu_getll (unsigned long *, char *);
88 int lu_getul (unsigned 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 lower_warn_lim[MAX_OIDS];
128 unsigned long upper_warn_lim[MAX_OIDS];
129 unsigned long lower_crit_lim[MAX_OIDS];
130 unsigned long upper_crit_lim[MAX_OIDS];
131 unsigned long response_value[MAX_OIDS];
132 int check_warning_value = FALSE;
133 int check_critical_value = FALSE;
134 int retries = 0;
135 unsigned 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, "INTEGER: "))
272                         show = strstr (response, "INTEGER: ") + 9;
273                 else if (strstr (response, "STRING: "))
274                         show = strstr (response, "STRING: ") + 8;
275                 else
276                         show = response;
277                 p2 = show;
279                 iresult = STATE_DEPENDENT;
281                 /* Process this block for integer comparisons */
282                 if (eval_method[i] & CRIT_GT ||
283                     eval_method[i] & CRIT_LT ||
284                     eval_method[i] & CRIT_GE ||
285                     eval_method[i] & CRIT_LE ||
286                     eval_method[i] & CRIT_EQ ||
287                     eval_method[i] & CRIT_NE ||
288                     eval_method[i] & WARN_GT ||
289                     eval_method[i] & WARN_LT ||
290                     eval_method[i] & WARN_GE ||
291                     eval_method[i] & WARN_LE ||
292                     eval_method[i] & WARN_EQ ||
293                     eval_method[i] & WARN_NE) {
294                         p2 = strpbrk (p2, "0123456789");
295                         if (p2 == NULL) 
296                                 die (STATE_UNKNOWN,_("No valid data returned"));
297                         response_value[i] = strtoul (p2, NULL, 10);
298                         iresult = check_num (i);
299                         asprintf (&show, "%lu", response_value[i]);
300                 }
302                 /* Process this block for string matching */
303                 else if (eval_method[i] & CRIT_STRING) {
304                         if (strcmp (show, string_value))
305                                 iresult = STATE_CRITICAL;
306                         else
307                                 iresult = STATE_OK;
308                 }
310                 /* Process this block for regex matching */
311                 else if (eval_method[i] & CRIT_REGEX) {
312                         excode = regexec (&preg, response, 10, pmatch, eflags);
313                         if (excode == 0) {
314                                 iresult = STATE_OK;
315                         }
316                         else if (excode != REG_NOMATCH) {
317                                 regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
318                                 printf (_("Execute Error: %s\n"), errbuf);
319                                 exit (STATE_CRITICAL);
320                         }
321                         else {
322                                 iresult = STATE_CRITICAL;
323                         }
324                 }
326                 /* Process this block for existence-nonexistence checks */
327                 else {
328                         if (eval_method[i] & CRIT_PRESENT)
329                                 iresult = STATE_CRITICAL;
330                         else if (eval_method[i] & WARN_PRESENT)
331                                 iresult = STATE_WARNING;
332                         else if (response && iresult == STATE_DEPENDENT) 
333                                 iresult = STATE_OK;
334                 }
336                 /* Result is the worst outcome of all the OIDs tested */
337                 result = max_state (result, iresult);
339                 /* Prepend a label for this OID if there is one */
340                 if (nlabels > (size_t)1 && (size_t)i < nlabels && labels[i] != NULL)
341                         asprintf (&outbuff, "%s%s%s %s%s%s", outbuff,
342                                   (i == 0) ? " " : output_delim,
343                                   labels[i], mark (iresult), show, mark (iresult));
344                 else
345                         asprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim,
346                                   mark (iresult), show, mark (iresult));
348                 /* Append a unit string for this OID if there is one */
349                 if (nunits > (size_t)0 && (size_t)i < nunits && unitv[i] != NULL)
350                         asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
352                 i++;
354                 strcat(perfstr, "=");
355                 strcat(perfstr, show);
356                 if (type)
357                         strcat(perfstr, type);
358                 strcat(perfstr, " ");
360         }       /* end while (ptr) */
362         if (found == 0)
363                 die (STATE_UNKNOWN,
364                      _("%s problem - No data received from host\nCMD: %s\n"),
365                      label,
366                      command_line);
368         /* WARNING if output found on stderr */
369         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
370                 result = max_state (result, STATE_WARNING);
372         /* close stderr */
373         (void) fclose (child_stderr);
375         /* close the pipe */
376         if (spclose (child_process))
377                 result = max_state (result, STATE_WARNING);
379 /*      if (nunits == 1 || i == 1) */
380 /*              printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units); */
381 /*      else */
382         printf ("%s %s -%s %s \n", label, state_text (result), outbuff, perfstr);
384         return result;
389 /* process command-line arguments */
390 int
391 process_arguments (int argc, char **argv)
393         char *ptr;
394         int c = 1;
395         int j = 0, jj = 0, ii = 0;
397         int option = 0;
398         static struct option longopts[] = {
399                 STD_LONG_OPTS,
400                 {"community", required_argument, 0, 'C'},
401                 {"oid", required_argument, 0, 'o'},
402                 {"object", required_argument, 0, 'o'},
403                 {"delimiter", required_argument, 0, 'd'},
404                 {"output-delimiter", required_argument, 0, 'D'},
405                 {"string", required_argument, 0, 's'},
406                 {"timeout", required_argument, 0, 't'},
407                 {"regex", required_argument, 0, 'r'},
408                 {"ereg", required_argument, 0, 'r'},
409                 {"eregi", required_argument, 0, 'R'},
410                 {"label", required_argument, 0, 'l'},
411                 {"units", required_argument, 0, 'u'},
412                 {"port", required_argument, 0, 'p'},
413                 {"retries", required_argument, 0, 'e'},
414                 {"miblist", required_argument, 0, 'm'},
415                 {"protocol", required_argument, 0, 'P'},
416                 {"seclevel", required_argument, 0, 'L'},
417                 {"secname", required_argument, 0, 'U'},
418                 {"authproto", required_argument, 0, 'a'},
419                 {"authpasswd", required_argument, 0, 'A'},
420                 {"privpasswd", required_argument, 0, 'X'},
421                 {"next", no_argument, 0, 'n'},
422                 {0, 0, 0, 0}
423         };
425         if (argc < 2)
426                 return ERROR;
428         /* reverse compatibility for very old non-POSIX usage forms */
429         for (c = 1; c < argc; c++) {
430                 if (strcmp ("-to", argv[c]) == 0)
431                         strcpy (argv[c], "-t");
432                 if (strcmp ("-wv", argv[c]) == 0)
433                         strcpy (argv[c], "-w");
434                 if (strcmp ("-cv", argv[c]) == 0)
435                         strcpy (argv[c], "-c");
436         }
438         while (1) {
439                 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:",
440                                                                          longopts, &option);
442                 if (c == -1 || c == EOF)
443                         break;
445                 switch (c) {
446                 case '?':       /* usage */
447                         usage5 ();
448                 case 'h':       /* help */
449                         print_help ();
450                         exit (STATE_OK); 
451                 case 'V':       /* version */
452                         print_revision (progname, revision);
453                         exit (STATE_OK);
454                 case 'v': /* verbose */
455                         verbose = TRUE;
456                         break;
458         /* Connection info */
459                 case 'C':                                                                       /* group or community */
460                         community = optarg;
461                         break;
462                 case 'H':                                                                       /* Host or server */
463                         server_address = optarg;
464                         break;
465                 case 'p':       /* TCP port number */
466                         port = optarg;
467                         break;
468                 case 'm':      /* List of MIBS  */
469                         miblist = optarg;
470                         break;
471                 case 'n':     /* usesnmpgetnext */
472                         usesnmpgetnext = TRUE;
473                         break;
474                 case 'P':     /* SNMP protocol version */
475                         proto = optarg;
476                         break;
477                 case 'L':     /* security level */
478                         seclevel = optarg;
479                         break;
480                 case 'U':     /* security username */
481                         secname = optarg;
482                         break;
483                 case 'a':     /* auth protocol */
484                         authproto = optarg;
485                         break;
486                 case 'A':     /* auth passwd */
487                         authpasswd = optarg;
488                         break;
489                 case 'X':     /* priv passwd */
490                         privpasswd = optarg;
491                         break;
492                 case 't':       /* timeout period */
493                         if (!is_integer (optarg))
494                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
495                         else
496                                 timeout_interval = atoi (optarg);
497                         break;
499         /* Test parameters */
500                 case 'c':                                                                       /* critical time threshold */
501                         if (strspn (optarg, "0123456789:,") < strlen (optarg))
502                                 usage2 (_("Invalid critical threshold: %s\n"), optarg);
503                         for (ptr = optarg; ptr && jj < MAX_OIDS; jj++) {
504                                 if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
505                                         eval_method[jj] |= CRIT_LT;
506                                 if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
507                                         eval_method[jj] |= CRIT_GT;
508                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
509                         }
510                         break;
511                 case 'w':                                                                       /* warning time threshold */
512                         if (strspn (optarg, "0123456789:,") < strlen (optarg))
513                                 usage2 (_("Invalid warning threshold: %s\n"), optarg);
514                         for (ptr = optarg; ptr && ii < MAX_OIDS; ii++) {
515                                 if (lu_getll (&lower_warn_lim[ii], ptr) == 1)
516                                         eval_method[ii] |= WARN_LT;
517                                 if (lu_getul (&upper_warn_lim[ii], ptr) == 1)
518                                         eval_method[ii] |= WARN_GT;
519                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
520                         }
521                         break;
522                 case 'e': /* PRELIMINARY - may change */
523                 case 'E': /* PRELIMINARY - may change */
524                         if (!is_integer (optarg))
525                                 usage2 (_("Retries interval must be a positive integer"), optarg);
526                         else
527                                 retries = atoi(optarg);
528                         break;
529                 case 'o':                                                                       /* object identifier */
530                         if ( strspn( optarg, "0123456789.," ) != strlen( optarg ) ) {
531                                         /*
532                                          * we have something other than digits, periods and comas,
533                                          * so we have a mib variable, rather than just an SNMP OID,
534                                          * so we have to actually read the mib files
535                                          */
536                                         needmibs = TRUE;
537                         }
539                         for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
540                                 ptr[0] = ' '; /* relpace comma with space */
541                         for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
542                                 j++; /* count OIDs */
543                         asprintf (&oid, "%s %s", (oid?oid:""), optarg);
544                         if (c == 'E' || c == 'e') {
545                                 jj++;
546                                 ii++;
547                         }
548                         if (c == 'E') 
549                                 eval_method[j+1] |= WARN_PRESENT;
550                         else if (c == 'e')
551                                 eval_method[j+1] |= CRIT_PRESENT;
552                         break;
553                 case 's':                                                                       /* string or substring */
554                         strncpy (string_value, optarg, sizeof (string_value) - 1);
555                         string_value[sizeof (string_value) - 1] = 0;
556                         eval_method[jj++] = CRIT_STRING;
557                         ii++;
558                         break;
559                 case 'R':                                                                       /* regex */
560                         cflags = REG_ICASE;
561                 case 'r':                                                                       /* regex */
562                         cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
563                         strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
564                         regex_expect[sizeof (regex_expect) - 1] = 0;
565                         errcode = regcomp (&preg, regex_expect, cflags);
566                         if (errcode != 0) {
567                                 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
568                                 printf (_("Could Not Compile Regular Expression"));
569                                 return ERROR;
570                         }
571                         eval_method[jj++] = CRIT_REGEX;
572                         ii++;
573                         break;
575         /* Format */
576                 case 'd':                                                                       /* delimiter */
577                         delimiter = strscpy (delimiter, optarg);
578                         break;
579                 case 'D':                                                                       /* output-delimiter */
580                         output_delim = strscpy (output_delim, optarg);
581                         break;
582                 case 'l':                                                                       /* label */
583                         label = optarg;
584                         nlabels++;
585                         if (nlabels >= labels_size) {
586                                 labels_size += 8;
587                                 labels = realloc (labels, labels_size);
588                                 if (labels == NULL)
589                                         die (STATE_UNKNOWN, _("Could not reallocate labels[%d]"), (int)nlabels);
590                         }
591                         labels[nlabels - 1] = optarg;
592                         ptr = thisarg (optarg);
593                         labels[nlabels - 1] = ptr;
594                         if (strstr (ptr, "'") == ptr)
595                                 labels[nlabels - 1] = ptr + 1;
596                         while (ptr && (ptr = nextarg (ptr))) {
597                                 if (nlabels >= labels_size) {
598                                         labels_size += 8;
599                                         labels = realloc (labels, labels_size);
600                                         if (labels == NULL)
601                                                 die (STATE_UNKNOWN, _("Could not reallocate labels\n"));
602                                 }
603                                 labels++;
604                                 ptr = thisarg (ptr);
605                                 if (strstr (ptr, "'") == ptr)
606                                         labels[nlabels - 1] = ptr + 1;
607                                 else
608                                         labels[nlabels - 1] = ptr;
609                         }
610                         break;
611                 case 'u':                                                                       /* units */
612                         units = optarg;
613                         nunits++;
614                         if (nunits >= unitv_size) {
615                                 unitv_size += 8;
616                                 unitv = realloc (unitv, unitv_size);
617                                 if (unitv == NULL)
618                                         die (STATE_UNKNOWN, _("Could not reallocate units [%d]\n"), (int)nunits);
619                         }
620                         unitv[nunits - 1] = optarg;
621                         ptr = thisarg (optarg);
622                         unitv[nunits - 1] = ptr;
623                         if (strstr (ptr, "'") == ptr)
624                                 unitv[nunits - 1] = ptr + 1;
625                         while (ptr && (ptr = nextarg (ptr))) {
626                                 if (nunits >= unitv_size) {
627                                         unitv_size += 8;
628                                         unitv = realloc (unitv, unitv_size);
629                                         if (units == NULL)
630                                                 die (STATE_UNKNOWN, _("Could not realloc() units\n"));
631                                 }
632                                 nunits++;
633                                 ptr = thisarg (ptr);
634                                 if (strstr (ptr, "'") == ptr)
635                                         unitv[nunits - 1] = ptr + 1;
636                                 else
637                                         unitv[nunits - 1] = ptr;
638                         }
639                         break;
641                 }
642         }
644         if (server_address == NULL)
645                 server_address = argv[optind];
647         if (community == NULL)
648                 community = strdup (DEFAULT_COMMUNITY);
649         
652         return validate_arguments ();
656 /******************************************************************************
658 @@-
659 <sect3>
660 <title>validate_arguments</title>
662 <para>&PROTO_validate_arguments;</para>
664 <para>Checks to see if the default miblist needs to be loaded. Also verifies 
665 the authentication and authorization combinations based on protocol version 
666 selected.</para>
668 <para></para>
670 </sect3>
671 -@@
672 ******************************************************************************/
676 int
677 validate_arguments ()
679         /* check whether to load locally installed MIBS (CPU/disk intensive) */
680         if (miblist == NULL) {
681                 if ( needmibs  == TRUE ) {
682                         miblist = strdup (DEFAULT_MIBLIST);
683                 }else{
684                         miblist = "''";                 /* don't read any mib files for numeric oids */
685                 }
686         }
689         /* Need better checks to verify seclevel and authproto choices */
690         
691         if (seclevel == NULL) 
692                 asprintf (&seclevel, "noAuthNoPriv");
695         if (authproto == NULL ) 
696                 asprintf(&authproto, DEFAULT_AUTH_PROTOCOL);
697         
698          
699         
700         if (proto == NULL || (strcmp(proto,DEFAULT_PROTOCOL) == 0) ) {        /* default protocol version */
701                 asprintf(&proto, DEFAULT_PROTOCOL);
702                 asprintf(&authpriv, "%s%s", "-c ", community);
703         }
704         else if ( strcmp (proto, "2c") == 0 ) {                 /* snmpv2c args */
705                 asprintf(&authpriv, "%s%s", "-c ", community);
706         }
707         else if ( strcmp (proto, "3") == 0 ) {                 /* snmpv3 args */
708                 asprintf(&proto, "%s", "3");
709                 
710                 if ( (strcmp(seclevel, "noAuthNoPriv") == 0) || seclevel == NULL ) {
711                         asprintf(&authpriv, "%s", "-l noAuthNoPriv" );
712                 }
713                 else if ( strcmp(seclevel, "authNoPriv") == 0 ) {
714                         if ( secname == NULL || authpasswd == NULL) {
715                                 printf (_("Missing secname (%s) or authpassword (%s) ! \n"),secname, authpasswd );
716                                 print_usage ();
717                                 exit (STATE_UNKNOWN);
718                         }
719                         asprintf(&authpriv, "-l authNoPriv -a %s -u %s -A %s ", authproto, secname, authpasswd);
720                 }
721                 else if ( strcmp(seclevel, "authPriv") == 0 ) {
722                         if ( secname == NULL || authpasswd == NULL || privpasswd == NULL ) {
723                                 printf (_("Missing secname (%s), authpassword (%s), or privpasswd (%s)! \n"),secname, authpasswd,privpasswd );
724                                 print_usage ();
725                                 exit (STATE_UNKNOWN);
726                         }
727                         asprintf(&authpriv, "-l authPriv -a %s -u %s -A %s -x DES -X %s ", authproto, secname, authpasswd, privpasswd);
728                 }
729                 
730         }
731         else {
732                 usage2 (_("Invalid SNMP version"), proto);
733         }
734                         
735         return OK;
740 char *
741 clarify_message (char *msg)
743         int i = 0;
744         int foo;
745         char tmpmsg_c[MAX_INPUT_BUFFER];
746         char *tmpmsg = (char *) &tmpmsg_c;
747         tmpmsg = strcpy (tmpmsg, msg);
748         if (!strncmp (tmpmsg, " Hex:", 5)) {
749                 tmpmsg = strtok (tmpmsg, ":");
750                 while ((tmpmsg = strtok (NULL, " "))) {
751                         foo = strtol (tmpmsg, NULL, 16);
752                         /* Translate chars that are not the same value in the printers
753                          * character set.
754                          */
755                         switch (foo) {
756                         case 208:
757                                 {
758                                         foo = 197;
759                                         break;
760                                 }
761                         case 216:
762                                 {
763                                         foo = 196;
764                                         break;
765                                 }
766                         }
767                         msg[i] = foo;
768                         i++;
769                 }
770                 msg[i] = 0;
771         }
772         return (msg);
777 int
778 check_num (int i)
780         int result;
781         result = STATE_OK;
782         if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
783                         lower_warn_lim[i] > upper_warn_lim[i]) {
784                 if (response_value[i] <= lower_warn_lim[i] &&
785                                 response_value[i] >= upper_warn_lim[i]) {
786                         result = STATE_WARNING;
787                 }
788         }
789         else if
790                 ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
791                  (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
792                  (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
793                  (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
794                  (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
795                  (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
796                 result = STATE_WARNING;
797         }
799         if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
800                         lower_crit_lim[i] > upper_crit_lim[i]) {
801                 if (response_value[i] <= lower_crit_lim[i] &&
802                                 response_value[i] >= upper_crit_lim[i]) {
803                         result = STATE_CRITICAL;
804                 }
805         }
806         else if
807                 ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
808                  (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
809                  (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
810                  (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
811                  (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
812                  (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
813                 result = STATE_CRITICAL;
814         }
816         return result;
821 int
822 lu_getll (unsigned long *ll, char *str)
824         char tmp[100];
825         if (strchr (str, ':') == NULL)
826                 return 0;
827         if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
828                 return 0;
829         if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
830                 return 1;
831         return 0;
836 int
837 lu_getul (unsigned long *ul, char *str)
839         char tmp[100];
840         if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
841                 return 1;
842         if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
843                 return 1;
844         if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
845                 return 1;
846         return 0;
851 /* trim leading whitespace
852          if there is a leading quote, make sure it balances */
854 char *
855 thisarg (char *str)
857         str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
858         if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
859                 if (strlen (str) == 1 || !strstr (str + 1, "'"))
860                         die (STATE_UNKNOWN, _("Unbalanced quotes\n"));
861         }
862         return str;
867 /* if there's a leading quote, advance to the trailing quote
868          set the trailing quote to '\x0'
869          if the string continues, advance beyond the comma */
871 char *
872 nextarg (char *str)
874         if (strstr (str, "'") == str) {
875                 str[0] = 0;
876                 if (strlen (str) > 1) {
877                         str = strstr (str + 1, "'");
878                         return (++str);
879                 }
880                 else {
881                         return NULL;
882                 }
883         }
884         if (strstr (str, ",") == str) {
885                 str[0] = 0;
886                 if (strlen (str) > 1) {
887                         return (++str);
888                 }
889                 else {
890                         return NULL;
891                 }
892         }
893         if ((str = strstr (str, ",")) && strlen (str) > 1) {
894                 str[0] = 0;
895                 return (++str);
896         }
897         return NULL;
902 void
903 print_help (void)
905         print_revision (progname, revision);
907         printf (COPYRIGHT, copyright, email);
909         printf ("%s\n", _("Check status of remote machines and obtain sustem information via SNMP"));
911   printf ("\n\n");
913         print_usage ();
915         printf (_(UT_HELP_VRSN));
917         printf (_(UT_HOST_PORT), 'p', DEFAULT_PORT);
919         /* SNMP and Authentication Protocol */
920         printf (" %s\n", "-n, --next");
921   printf ("    %s\n", _("Use SNMP GETNEXT instead of SNMP GET"));
922   printf (" %s\n", "-P, --protocol=[1|2c|3]");
923   printf ("    %s\n", _("SNMP protocol version"));
924   printf (" %s\n", "-L, --seclevel=[noAuthNoPriv|authNoPriv|authPriv]");
925   printf ("    %s\n", _("SNMPv3 securityLevel"));
926   printf (" %s\n", "-a, --authproto=[MD5|SHA]");
927   printf ("    %s\n", _("SNMPv3 auth proto"));
929         /* Authentication Tokens*/
930         printf (" %s\n", "-C, --community=STRING");
931   printf ("    %s\n", _("Optional community string for SNMP communication"));
932   printf (_("(default is \"%s\")"),DEFAULT_COMMUNITY);
933   printf (" %s\n", "-U, --secname=USERNAME");
934   printf ("    %s\n", _("SNMPv3 username"));
935   printf (" %s\n", "-A, --authpassword=PASSWORD");
936   printf ("    %s\n", _("SNMPv3 authentication password"));
937   printf (" %s\n", "-X, --privpasswd=PASSWORD");
938   printf ("    %s\n", _("SNMPv3 crypt passwd (DES)"));
940         /* OID Stuff */
941         printf (" %s\n", "-o, --oid=OID(s)");
942   printf ("    %s\n", _("Object identifier(s) or SNMP variables whose value you wish to query"));
943   printf (" %s\n", "-m, --miblist=STRING");
944   printf ("    %s\n", _("List of MIBS to be loaded (default = none if using numeric oids or 'ALL'"));
945   printf ("    %s\n", _("for symbolic oids.)"));
946   printf (" %s\n", "-d, --delimiter=STRING");
947   printf (_("    Delimiter to use when parsing returned data. Default is \"%s\""), DEFAULT_DELIMITER);
948   printf ("    %s\n", _("Any data on the right hand side of the delimiter is considered"));
949   printf ("    %s\n", _("to be the data that should be used in the evaluation."));
951         /* Tests Against Integers */
952         printf (" %s\n", "-w, --warning=INTEGER_RANGE(s)");
953   printf ("    %s\n", _("Range(s) which will not result in a WARNING status"));
954   printf (" %s\n", "-c, --critical=INTEGER_RANGE(s)");
955   printf ("    %s\n", _("Range(s) which will not result in a CRITICAL status"));
957         /* Tests Against Strings */
958         printf (" %s\n", "-s, --string=STRING");
959   printf ("    %s\n", _("Return OK state (for that OID) if STRING is an exact match"));
960   printf (" %s\n", "-r, --ereg=REGEX");
961   printf ("    %s\n", _("Return OK state (for that OID) if extended regular expression REGEX matches"));
962   printf (" %s\n", "-R, --eregi=REGEX");
963   printf ("    %s\n", _("Return OK state (for that OID) if case-insensitive extended REGEX matches"));
964   printf (" %s\n", "-l, --label=STRING");
965   printf ("    %s\n", _("Prefix label for output from plugin (default -s 'SNMP')"));
967         /* Output Formatting */
968         printf (" %s\n", "-u, --units=STRING");
969   printf ("    %s\n", _("Units label(s) for output data (e.g., 'sec.')."));
970   printf (" %s\n", "-D, --output-delimiter=STRING");
971   printf ("    %s\n", _("Separates output on multiple OID requests"));
973         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
975         printf (_(UT_VERBOSE));
977         printf ("%s\n", _("This plugin uses the 'snmpget' command included with the NET-SNMP package."));
978   printf ("%s\n", _("if you don't have the package installed, you will need to download it from"));
979   printf ("%s\n", _("http://net-snmp.sourceforge.net before you can use this plugin."));
981         printf ("%s\n", _("- Multiple OIDs may be indicated by a comma- or space-delimited list (lists with"));
982   printf ("%s\n", _(" internal spaces must be quoted) [max 8 OIDs]"));
984         printf ("%s\n", _("- Ranges are inclusive and are indicated with colons. When specified as"));
985   printf ("%s\n", _(" 'min:max' a STATE_OK will be returned if the result is within the indicated"));
986   printf ("%s\n", _(" range or is equal to the upper or lower bound. A non-OK state will be"));
987   printf ("%s\n", _(" returned if the result is outside the specified range."));
989         printf ("%s\n", _("- If specified in the order 'max:min' a non-OK state will be returned if the"));
990   printf ("%s\n", _(" result is within the (inclusive) range."));
992         printf ("%s\n", _("- Upper or lower bounds may be omitted to skip checking the respective limit."));
993   printf ("%s\n", _("- Bare integers are interpreted as upper limits."));
994   printf ("%s\n", _("- When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'"));
995   printf ("%s\n", _("- Note that only one string and one regex may be checked at present"));
996   printf ("%s\n", _("- All evaluation methods other than PR, STR, and SUBSTR expect that the value"));
997   printf ("%s\n", _(" returned from the SNMP query is an unsigned integer."));
999         printf (_(UT_SUPPORT));
1004 void
1005 print_usage (void)
1007   printf (_("Usage:"));
1008         printf ("%s -H <ip_address> -o <OID> [-w warn_range] [-c crit_range]\n",progname);
1009   printf ("[-C community] [-s string] [-r regex] [-R regexi] [-t timeout] [-e retries]\n");
1010   printf ("[-l label] [-u units] [-p port-number] [-d delimiter] [-D output-delimiter]\n");
1011   printf ("[-m miblist] [-P snmp version] [-L seclevel] [-U secname] [-a authproto]\n");
1012   printf ("[-A authpasswd] [-X privpasswd]\n");