Code

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