Code

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