Code

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