Code

check_snmp now only prints perfdata for non numeric values (Joerg Linge #1867716)
[nagiosplug.git] / plugins / check_snmp.c
1 /*****************************************************************************
2
3 * Nagios check_snmp plugin
4
5 * License: GPL
6 * Copyright (c) 1999-2007 Nagios Plugins Development 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 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
21
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 * GNU General Public License for more details.
26
27 * You should have received a copy of the GNU General Public License
28 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29
30 * $Id$
31
32 *****************************************************************************/
34 const char *progname = "check_snmp";
35 const char *revision = "$Revision$";
36 const char *copyright = "1999-2007";
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 llu_getll (unsigned long long *, char *);
85 int llu_getul (unsigned long 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 long lower_warn_lim[MAX_OIDS];
125 unsigned long long upper_warn_lim[MAX_OIDS];
126 unsigned long long lower_crit_lim[MAX_OIDS];
127 unsigned long long upper_crit_lim[MAX_OIDS];
128 unsigned long long response_value[MAX_OIDS];
129 int check_warning_value = FALSE;
130 int check_critical_value = FALSE;
131 int retries = 0;
132 unsigned long 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 *cl_hidden_auth = NULL;
149         char *response = NULL;
150         char *outbuff;
151         char *output;
152         char *ptr = NULL;
153         char *p2 = NULL;
154         char *show = NULL;
155         char type[8] = "";
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         /* Parse extra opts if any */
180         argv=np_extra_opts (&argc, argv, progname);
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         strncat(perfstr, "| ", sizeof(perfstr)-strlen(perfstr)-1);
230         while (ptr) {
231                 char *foo, *ptr2;
232                 unsigned int copylen;
234                 foo = strstr (ptr, delimiter);
235                 copylen = foo-ptr;
236                 if (copylen > sizeof(perfstr)-strlen(perfstr)-1)
237                         copylen = sizeof(perfstr)-strlen(perfstr)-1;
238                 ptr2 = ptr;
239                 ptr = foo; 
241                 if (ptr == NULL)
242                         break;
244                 ptr += strlen (delimiter);
245                 ptr += strspn (ptr, " ");
247                 found++;
249                 if (ptr[0] == '"') {
250                         ptr++;
251                         response = strpcpy (response, ptr, "\"");
252                         ptr = strpbrk (ptr, "\"");
253                         ptr += strspn (ptr, "\"\n");
254                 }
255                 else {
256                         response = strpcpy (response, ptr, "\n");
257                         ptr = strpbrk (ptr, "\n");
258                         ptr += strspn (ptr, "\n");
259                         while
260                                 (strstr (ptr, delimiter) &&
261                                  strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter)) {
262                                 response = strpcat (response, ptr, "\n");
263                                 ptr = strpbrk (ptr, "\n");
264                         }
265                         if (ptr && strstr (ptr, delimiter) == NULL) {
266                                 asprintf (&response, "%s%s", response, ptr);
267                                 ptr = NULL;
268                         }
269                 }
271                 /* We strip out the datatype indicator for PHBs */
273                 /* Clean up type array - Sol10 does not necessarily zero it out */
274                 bzero(type, sizeof(type));
276                 if (strstr (response, "Gauge: "))
277                         show = strstr (response, "Gauge: ") + 7;
278                 else if (strstr (response, "Gauge32: "))
279                         show = strstr (response, "Gauge32: ") + 9;
280                 else if (strstr (response, "Counter32: ")) {
281                         show = strstr (response, "Counter32: ") + 11;
282                         strcpy(type, "c");
283                 }
284                 else if (strstr (response, "Counter64: ")) {
285                         show = strstr (response, "Counter64: ") + 11;
286                         strcpy(type, "c");
287                 }
288                 else if (strstr (response, "INTEGER: "))
289                         show = strstr (response, "INTEGER: ") + 9;
290                 else if (strstr (response, "STRING: "))
291                         show = strstr (response, "STRING: ") + 8;
292                 else
293                         show = response;
294                 p2 = show;
296                 iresult = STATE_DEPENDENT;
298                 /* Process this block for integer comparisons */
299                 if (eval_method[i] & CRIT_GT ||
300                     eval_method[i] & CRIT_LT ||
301                     eval_method[i] & CRIT_GE ||
302                     eval_method[i] & CRIT_LE ||
303                     eval_method[i] & CRIT_EQ ||
304                     eval_method[i] & CRIT_NE ||
305                     eval_method[i] & WARN_GT ||
306                     eval_method[i] & WARN_LT ||
307                     eval_method[i] & WARN_GE ||
308                     eval_method[i] & WARN_LE ||
309                     eval_method[i] & WARN_EQ ||
310                     eval_method[i] & WARN_NE) {
311                         p2 = strpbrk (p2, "0123456789");
312                         if (p2 == NULL) 
313                                 die (STATE_UNKNOWN,_("No valid data returned"));
314                         response_value[i] = strtoul (p2, NULL, 10);
315                         iresult = check_num (i);
316                         asprintf (&show, "%llu", response_value[i]);
317                 }
319                 /* Process this block for string matching */
320                 else if (eval_method[i] & CRIT_STRING) {
321                         if (strcmp (show, string_value))
322                                 iresult = STATE_CRITICAL;
323                         else
324                                 iresult = STATE_OK;
325                 }
327                 /* Process this block for regex matching */
328                 else if (eval_method[i] & CRIT_REGEX) {
329                         excode = regexec (&preg, response, 10, pmatch, eflags);
330                         if (excode == 0) {
331                                 iresult = STATE_OK;
332                         }
333                         else if (excode != REG_NOMATCH) {
334                                 regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
335                                 printf (_("Execute Error: %s\n"), errbuf);
336                                 exit (STATE_CRITICAL);
337                         }
338                         else {
339                                 iresult = STATE_CRITICAL;
340                         }
341                 }
343                 /* Process this block for existence-nonexistence checks */
344                 else {
345                         if (eval_method[i] & CRIT_PRESENT)
346                                 iresult = STATE_CRITICAL;
347                         else if (eval_method[i] & WARN_PRESENT)
348                                 iresult = STATE_WARNING;
349                         else if (response && iresult == STATE_DEPENDENT) 
350                                 iresult = STATE_OK;
351                 }
353                 /* Result is the worst outcome of all the OIDs tested */
354                 result = max_state (result, iresult);
356                 /* Prepend a label for this OID if there is one */
357                 if (nlabels > (size_t)1 && (size_t)i < nlabels && labels[i] != NULL)
358                         asprintf (&outbuff, "%s%s%s %s%s%s", outbuff,
359                                 (i == 0) ? " " : output_delim,
360                                 labels[i], mark (iresult), show, mark (iresult));
361                 else
362                         asprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim,
363                                 mark (iresult), show, mark (iresult));
365                 /* Append a unit string for this OID if there is one */
366                 if (nunits > (size_t)0 && (size_t)i < nunits && unitv[i] != NULL)
367                         asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
369                 i++;
371                 if (is_numeric(show)) {
372                         strncat(perfstr, ptr2, copylen);
373                         strncat(perfstr, "=", sizeof(perfstr)-strlen(perfstr)-1);
374                         strncat(perfstr, show, sizeof(perfstr)-strlen(perfstr)-1);
376                         if (type)
377                                 strncat(perfstr, type, sizeof(perfstr)-strlen(perfstr)-1);
378                         strncat(perfstr, " ", sizeof(perfstr)-strlen(perfstr)-1);
379                 }
381         }       /* end while (ptr) */
383         if (found == 0)
384                 die (STATE_UNKNOWN,
385                         _("%s problem - No data received from host\nCMD: %s\n"),
386                         label,
387                         cl_hidden_auth);
389 #if 0           /* Removed May 29, 2007 */
390         /* WARNING if output found on stderr */
391         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
392                 result = max_state (result, STATE_WARNING);
394         /* close stderr */
395         (void) fclose (child_stderr);
396 #endif
398         /* close the pipe */
399         if (spclose (child_process)) {
400                 if (result == STATE_OK)
401                         result = STATE_UNKNOWN;
402                 asprintf (&outbuff, "%s (%s)", outbuff, _("snmpget returned an error status"));
403         }
405 /*      if (nunits == 1 || i == 1) */
406 /*              printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units); */
407 /*      else */
408         printf ("%s %s -%s %s \n", label, state_text (result), outbuff, perfstr);
410         return result;
415 /* process command-line arguments */
416 int
417 process_arguments (int argc, char **argv)
419         char *ptr;
420         int c = 1;
421         int j = 0, jj = 0, ii = 0;
423         int option = 0;
424         static struct option longopts[] = {
425                 STD_LONG_OPTS,
426                 {"community", required_argument, 0, 'C'},
427                 {"oid", required_argument, 0, 'o'},
428                 {"object", required_argument, 0, 'o'},
429                 {"delimiter", required_argument, 0, 'd'},
430                 {"output-delimiter", required_argument, 0, 'D'},
431                 {"string", required_argument, 0, 's'},
432                 {"timeout", required_argument, 0, 't'},
433                 {"regex", required_argument, 0, 'r'},
434                 {"ereg", required_argument, 0, 'r'},
435                 {"eregi", required_argument, 0, 'R'},
436                 {"label", required_argument, 0, 'l'},
437                 {"units", required_argument, 0, 'u'},
438                 {"port", required_argument, 0, 'p'},
439                 {"retries", required_argument, 0, 'e'},
440                 {"miblist", required_argument, 0, 'm'},
441                 {"protocol", required_argument, 0, 'P'},
442                 {"seclevel", required_argument, 0, 'L'},
443                 {"secname", required_argument, 0, 'U'},
444                 {"authproto", required_argument, 0, 'a'},
445                 {"authpasswd", required_argument, 0, 'A'},
446                 {"privpasswd", required_argument, 0, 'X'},
447                 {"next", no_argument, 0, 'n'},
448                 {0, 0, 0, 0}
449         };
451         if (argc < 2)
452                 return ERROR;
454         /* reverse compatibility for very old non-POSIX usage forms */
455         for (c = 1; c < argc; c++) {
456                 if (strcmp ("-to", argv[c]) == 0)
457                         strcpy (argv[c], "-t");
458                 if (strcmp ("-wv", argv[c]) == 0)
459                         strcpy (argv[c], "-w");
460                 if (strcmp ("-cv", argv[c]) == 0)
461                         strcpy (argv[c], "-c");
462         }
464         while (1) {
465                 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:",
466                                                                          longopts, &option);
468                 if (c == -1 || c == EOF)
469                         break;
471                 switch (c) {
472                 case '?':       /* usage */
473                         usage5 ();
474                 case 'h':       /* help */
475                         print_help ();
476                         exit (STATE_OK); 
477                 case 'V':       /* version */
478                         print_revision (progname, revision);
479                         exit (STATE_OK);
480                 case 'v': /* verbose */
481                         verbose = TRUE;
482                         break;
484         /* Connection info */
485                 case 'C':                                                                       /* group or community */
486                         community = optarg;
487                         break;
488                 case 'H':                                                                       /* Host or server */
489                         server_address = optarg;
490                         break;
491                 case 'p':       /* TCP port number */
492                         port = optarg;
493                         break;
494                 case 'm':       /* List of MIBS  */
495                         miblist = optarg;
496                         break;
497                 case 'n':       /* usesnmpgetnext */
498                         usesnmpgetnext = TRUE;
499                         break;
500                 case 'P':       /* SNMP protocol version */
501                         proto = optarg;
502                         break;
503                 case 'L':       /* security level */
504                         seclevel = optarg;
505                         break;
506                 case 'U':       /* security username */
507                         secname = optarg;
508                         break;
509                 case 'a':       /* auth protocol */
510                         authproto = optarg;
511                         break;
512                 case 'A':       /* auth passwd */
513                         authpasswd = optarg;
514                         break;
515                 case 'X':       /* priv passwd */
516                         privpasswd = optarg;
517                         break;
518                 case 't':       /* timeout period */
519                         if (!is_integer (optarg))
520                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
521                         else
522                                 timeout_interval = atoi (optarg);
523                         break;
525         /* Test parameters */
526                 case 'c':                                                                       /* critical time threshold */
527                         if (strspn (optarg, "0123456789:,") < strlen (optarg))
528                                 usage2 (_("Invalid critical threshold"), optarg);
529                         for (ptr = optarg; ptr && jj < MAX_OIDS; jj++) {
530                                 if (llu_getll (&lower_crit_lim[jj], ptr) == 1)
531                                         eval_method[jj] |= CRIT_LT;
532                                 if (llu_getul (&upper_crit_lim[jj], ptr) == 1)
533                                         eval_method[jj] |= CRIT_GT;
534                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
535                         }
536                         break;
537                 case 'w':                                                                       /* warning time threshold */
538                         if (strspn (optarg, "0123456789:,") < strlen (optarg))
539                                 usage2 (_("Invalid warning threshold"), optarg);
540                         for (ptr = optarg; ptr && ii < MAX_OIDS; ii++) {
541                                 if (llu_getll (&lower_warn_lim[ii], ptr) == 1)
542                                         eval_method[ii] |= WARN_LT;
543                                 if (llu_getul (&upper_warn_lim[ii], ptr) == 1)
544                                         eval_method[ii] |= WARN_GT;
545                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
546                         }
547                         break;
548                 case 'e': /* PRELIMINARY - may change */
549                 case 'E': /* PRELIMINARY - may change */
550                         if (!is_integer (optarg))
551                                 usage2 (_("Retries interval must be a positive integer"), optarg);
552                         else
553                                 retries = atoi(optarg);
554                         break;
555                 case 'o':                                                                       /* object identifier */
556                         if ( strspn( optarg, "0123456789.," ) != strlen( optarg ) ) {
557                                         /*
558                                          * we have something other than digits, periods and comas,
559                                          * so we have a mib variable, rather than just an SNMP OID,
560                                          * so we have to actually read the mib files
561                                          */
562                                         needmibs = TRUE;
563                         }
565                         for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
566                                 ptr[0] = ' '; /* relpace comma with space */
567                         for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
568                                 j++; /* count OIDs */
569                         asprintf (&oid, "%s %s", (oid?oid:""), optarg);
570                         if (c == 'E' || c == 'e') {
571                                 jj++;
572                                 ii++;
573                         }
574                         if (c == 'E') 
575                                 eval_method[j+1] |= WARN_PRESENT;
576                         else if (c == 'e')
577                                 eval_method[j+1] |= CRIT_PRESENT;
578                         break;
579                 case 's':                                                                       /* string or substring */
580                         strncpy (string_value, optarg, sizeof (string_value) - 1);
581                         string_value[sizeof (string_value) - 1] = 0;
582                         eval_method[jj++] = CRIT_STRING;
583                         ii++;
584                         break;
585                 case 'R':                                                                       /* regex */
586                         cflags = REG_ICASE;
587                 case 'r':                                                                       /* regex */
588                         cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
589                         strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
590                         regex_expect[sizeof (regex_expect) - 1] = 0;
591                         errcode = regcomp (&preg, regex_expect, cflags);
592                         if (errcode != 0) {
593                                 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
594                                 printf (_("Could Not Compile Regular Expression"));
595                                 return ERROR;
596                         }
597                         eval_method[jj++] = CRIT_REGEX;
598                         ii++;
599                         break;
601         /* Format */
602                 case 'd':                                                                       /* delimiter */
603                         delimiter = strscpy (delimiter, optarg);
604                         break;
605                 case 'D':                                                                       /* output-delimiter */
606                         output_delim = strscpy (output_delim, optarg);
607                         break;
608                 case 'l':                                                                       /* label */
609                         label = optarg;
610                         nlabels++;
611                         if (nlabels >= labels_size) {
612                                 labels_size += 8;
613                                 labels = realloc (labels, labels_size);
614                                 if (labels == NULL)
615                                         die (STATE_UNKNOWN, _("Could not reallocate labels[%d]"), (int)nlabels);
616                         }
617                         labels[nlabels - 1] = optarg;
618                         ptr = thisarg (optarg);
619                         labels[nlabels - 1] = ptr;
620                         if (strstr (ptr, "'") == ptr)
621                                 labels[nlabels - 1] = ptr + 1;
622                         while (ptr && (ptr = nextarg (ptr))) {
623                                 if (nlabels >= labels_size) {
624                                         labels_size += 8;
625                                         labels = realloc (labels, labels_size);
626                                         if (labels == NULL)
627                                                 die (STATE_UNKNOWN, _("Could not reallocate labels\n"));
628                                 }
629                                 labels++;
630                                 ptr = thisarg (ptr);
631                                 if (strstr (ptr, "'") == ptr)
632                                         labels[nlabels - 1] = ptr + 1;
633                                 else
634                                         labels[nlabels - 1] = ptr;
635                         }
636                         break;
637                 case 'u':                                                                       /* units */
638                         units = optarg;
639                         nunits++;
640                         if (nunits >= unitv_size) {
641                                 unitv_size += 8;
642                                 unitv = realloc (unitv, unitv_size);
643                                 if (unitv == NULL)
644                                         die (STATE_UNKNOWN, _("Could not reallocate units [%d]\n"), (int)nunits);
645                         }
646                         unitv[nunits - 1] = optarg;
647                         ptr = thisarg (optarg);
648                         unitv[nunits - 1] = ptr;
649                         if (strstr (ptr, "'") == ptr)
650                                 unitv[nunits - 1] = ptr + 1;
651                         while (ptr && (ptr = nextarg (ptr))) {
652                                 if (nunits >= unitv_size) {
653                                         unitv_size += 8;
654                                         unitv = realloc (unitv, unitv_size);
655                                         if (units == NULL)
656                                                 die (STATE_UNKNOWN, _("Could not realloc() units\n"));
657                                 }
658                                 nunits++;
659                                 ptr = thisarg (ptr);
660                                 if (strstr (ptr, "'") == ptr)
661                                         unitv[nunits - 1] = ptr + 1;
662                                 else
663                                         unitv[nunits - 1] = ptr;
664                         }
665                         break;
667                 }
668         }
670         if (server_address == NULL)
671                 server_address = argv[optind];
673         if (community == NULL)
674                 community = strdup (DEFAULT_COMMUNITY);
675         
678         return validate_arguments ();
682 /******************************************************************************
684 @@-
685 <sect3>
686 <title>validate_arguments</title>
688 <para>&PROTO_validate_arguments;</para>
690 <para>Checks to see if the default miblist needs to be loaded. Also verifies 
691 the authentication and authorization combinations based on protocol version 
692 selected.</para>
694 <para></para>
696 </sect3>
697 -@@
698 ******************************************************************************/
702 int
703 validate_arguments ()
705         /* check whether to load locally installed MIBS (CPU/disk intensive) */
706         if (miblist == NULL) {
707                 if ( needmibs  == TRUE ) {
708                         miblist = strdup (DEFAULT_MIBLIST);
709                 }else{
710                         miblist = "''";                 /* don't read any mib files for numeric oids */
711                 }
712         }
715         /* Need better checks to verify seclevel and authproto choices */
716         
717         if (seclevel == NULL) 
718                 asprintf (&seclevel, "noAuthNoPriv");
721         if (authproto == NULL ) 
722                 asprintf(&authproto, DEFAULT_AUTH_PROTOCOL);
723         
724          
725         
726         if (proto == NULL || (strcmp(proto,DEFAULT_PROTOCOL) == 0) ) {  /* default protocol version */
727                 asprintf(&proto, DEFAULT_PROTOCOL);
728                 asprintf(&authpriv, "%s%s", "-c ", community);
729         }
730         else if ( strcmp (proto, "2c") == 0 ) {         /* snmpv2c args */
731                 asprintf(&authpriv, "%s%s", "-c ", community);
732         }
733         else if ( strcmp (proto, "3") == 0 ) {          /* snmpv3 args */
734                 asprintf(&proto, "%s", "3");
735                 
736                 if ( (strcmp(seclevel, "noAuthNoPriv") == 0) || seclevel == NULL ) {
737                         asprintf(&authpriv, "%s", "-l noAuthNoPriv" );
738                 }
739                 else if ( strcmp(seclevel, "authNoPriv") == 0 ) {
740                         if ( secname == NULL || authpasswd == NULL) {
741                                 printf (_("Missing secname (%s) or authpassword (%s) ! \n"),secname, authpasswd );
742                                 print_usage ();
743                                 exit (STATE_UNKNOWN);
744                         }
745                         asprintf(&authpriv, "-l authNoPriv -a %s -u %s -A %s ", authproto, secname, authpasswd);
746                 }
747                 else if ( strcmp(seclevel, "authPriv") == 0 ) {
748                         if ( secname == NULL || authpasswd == NULL || privpasswd == NULL ) {
749                                 printf (_("Missing secname (%s), authpassword (%s), or privpasswd (%s)! \n"),secname, authpasswd,privpasswd );
750                                 print_usage ();
751                                 exit (STATE_UNKNOWN);
752                         }
753                         asprintf(&authpriv, "-l authPriv -a %s -u %s -A %s -x DES -X %s ", authproto, secname, authpasswd, privpasswd);
754                 }
755                 
756         }
757         else {
758                 usage2 (_("Invalid SNMP version"), proto);
759         }
760                         
761         return OK;
766 char *
767 clarify_message (char *msg)
769         int i = 0;
770         int foo;
771         char tmpmsg_c[MAX_INPUT_BUFFER];
772         char *tmpmsg = (char *) &tmpmsg_c;
773         tmpmsg = strcpy (tmpmsg, msg);
774         if (!strncmp (tmpmsg, " Hex:", 5)) {
775                 tmpmsg = strtok (tmpmsg, ":");
776                 while ((tmpmsg = strtok (NULL, " "))) {
777                         foo = strtol (tmpmsg, NULL, 16);
778                         /* Translate chars that are not the same value in the printers
779                          * character set.
780                          */
781                         switch (foo) {
782                         case 208:
783                                 {
784                                         foo = 197;
785                                         break;
786                                 }
787                         case 216:
788                                 {
789                                         foo = 196;
790                                         break;
791                                 }
792                         }
793                         msg[i] = foo;
794                         i++;
795                 }
796                 msg[i] = 0;
797         }
798         return (msg);
803 int
804 check_num (int i)
806         int result;
807         result = STATE_OK;
808         if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
809                         lower_warn_lim[i] > upper_warn_lim[i]) {
810                 if (response_value[i] <= lower_warn_lim[i] &&
811                                 response_value[i] >= upper_warn_lim[i]) {
812                         result = STATE_WARNING;
813                 }
814         }
815         else if
816                 ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
817                  (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
818                  (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
819                  (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
820                  (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
821                  (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
822                 result = STATE_WARNING;
823         }
825         if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
826                         lower_crit_lim[i] > upper_crit_lim[i]) {
827                 if (response_value[i] <= lower_crit_lim[i] &&
828                                 response_value[i] >= upper_crit_lim[i]) {
829                         result = STATE_CRITICAL;
830                 }
831         }
832         else if
833                 ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
834                  (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
835                  (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
836                  (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
837                  (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
838                  (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
839                 result = STATE_CRITICAL;
840         }
842         return result;
847 int
848 llu_getll (unsigned long long *ll, char *str)
850         char tmp[100];
851         if (strchr (str, ':') == NULL)
852                 return 0;
853         if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
854                 return 0;
855         if (sscanf (str, "%llu%[:]", ll, tmp) == 2)
856                 return 1;
857         return 0;
862 int
863 llu_getul (unsigned long long *ul, char *str)
865         char tmp[100];
866         if (sscanf (str, "%llu%[^,]", ul, tmp) == 1)
867                 return 1;
868         if (sscanf (str, ":%llu%[^,]", ul, tmp) == 1)
869                 return 1;
870         if (sscanf (str, "%*u:%llu%[^,]", ul, tmp) == 1)
871                 return 1;
872         return 0;
877 /* trim leading whitespace
878          if there is a leading quote, make sure it balances */
880 char *
881 thisarg (char *str)
883         str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
884         if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
885                 if (strlen (str) == 1 || !strstr (str + 1, "'"))
886                         die (STATE_UNKNOWN, _("Unbalanced quotes\n"));
887         }
888         return str;
893 /* if there's a leading quote, advance to the trailing quote
894          set the trailing quote to '\x0'
895          if the string continues, advance beyond the comma */
897 char *
898 nextarg (char *str)
900         if (strstr (str, "'") == str) {
901                 str[0] = 0;
902                 if (strlen (str) > 1) {
903                         str = strstr (str + 1, "'");
904                         return (++str);
905                 }
906                 else {
907                         return NULL;
908                 }
909         }
910         if (strstr (str, ",") == str) {
911                 str[0] = 0;
912                 if (strlen (str) > 1) {
913                         return (++str);
914                 }
915                 else {
916                         return NULL;
917                 }
918         }
919         if ((str = strstr (str, ",")) && strlen (str) > 1) {
920                 str[0] = 0;
921                 return (++str);
922         }
923         return NULL;
928 void
929 print_help (void)
931         print_revision (progname, revision);
933         printf (COPYRIGHT, copyright, email);
935         printf ("%s\n", _("Check status of remote machines and obtain sustem information via SNMP"));
937   printf ("\n\n");
939         print_usage ();
941         printf (_(UT_HELP_VRSN));
942         printf (_(UT_EXTRA_OPTS));
944         printf (_(UT_HOST_PORT), 'p', DEFAULT_PORT);
946         /* SNMP and Authentication Protocol */
947         printf (" %s\n", "-n, --next");
948   printf ("    %s\n", _("Use SNMP GETNEXT instead of SNMP GET"));
949   printf (" %s\n", "-P, --protocol=[1|2c|3]");
950   printf ("    %s\n", _("SNMP protocol version"));
951   printf (" %s\n", "-L, --seclevel=[noAuthNoPriv|authNoPriv|authPriv]");
952   printf ("    %s\n", _("SNMPv3 securityLevel"));
953   printf (" %s\n", "-a, --authproto=[MD5|SHA]");
954   printf ("    %s\n", _("SNMPv3 auth proto"));
956         /* Authentication Tokens*/
957         printf (" %s\n", "-C, --community=STRING");
958   printf ("    %s ", _("Optional community string for SNMP communication"));
959   printf ("(%s \"%s\")\n", _("default is") ,DEFAULT_COMMUNITY);
960   printf (" %s\n", "-U, --secname=USERNAME");
961   printf ("    %s\n", _("SNMPv3 username"));
962   printf (" %s\n", "-A, --authpassword=PASSWORD");
963   printf ("    %s\n", _("SNMPv3 authentication password"));
964   printf (" %s\n", "-X, --privpasswd=PASSWORD");
965   printf ("    %s\n", _("SNMPv3 privacy password"));
967         /* OID Stuff */
968         printf (" %s\n", "-o, --oid=OID(s)");
969   printf ("    %s\n", _("Object identifier(s) or SNMP variables whose value you wish to query"));
970   printf (" %s\n", "-m, --miblist=STRING");
971   printf ("    %s\n", _("List of MIBS to be loaded (default = none if using numeric oids or 'ALL'"));
972   printf ("    %s\n", _("for symbolic oids.)"));
973   printf (" %s\n", "-d, --delimiter=STRING");
974   printf (_("    Delimiter to use when parsing returned data. Default is \"%s\""), DEFAULT_DELIMITER);
975   printf ("    %s\n", _("Any data on the right hand side of the delimiter is considered"));
976   printf ("    %s\n", _("to be the data that should be used in the evaluation."));
978         /* Tests Against Integers */
979         printf (" %s\n", "-w, --warning=INTEGER_RANGE(s)");
980   printf ("    %s\n", _("Range(s) which will not result in a WARNING status"));
981   printf (" %s\n", "-c, --critical=INTEGER_RANGE(s)");
982   printf ("    %s\n", _("Range(s) which will not result in a CRITICAL status"));
984         /* Tests Against Strings */
985         printf (" %s\n", "-s, --string=STRING");
986   printf ("    %s\n", _("Return OK state (for that OID) if STRING is an exact match"));
987   printf (" %s\n", "-r, --ereg=REGEX");
988   printf ("    %s\n", _("Return OK state (for that OID) if extended regular expression REGEX matches"));
989   printf (" %s\n", "-R, --eregi=REGEX");
990   printf ("    %s\n", _("Return OK state (for that OID) if case-insensitive extended REGEX matches"));
991   printf (" %s\n", "-l, --label=STRING");
992   printf ("    %s\n", _("Prefix label for output from plugin (default -s 'SNMP')"));
994         /* Output Formatting */
995         printf (" %s\n", "-u, --units=STRING");
996   printf ("    %s\n", _("Units label(s) for output data (e.g., 'sec.')."));
997   printf (" %s\n", "-D, --output-delimiter=STRING");
998   printf ("    %s\n", _("Separates output on multiple OID requests"));
1000         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
1002         printf (_(UT_VERBOSE));
1004   printf ("\n");
1005   printf ("%s\n", _("This plugin uses the 'snmpget' command included with the NET-SNMP package."));
1006   printf ("%s\n", _("if you don't have the package installed, you will need to download it from"));
1007   printf ("%s\n", _("http://net-snmp.sourceforge.net before you can use this plugin."));
1009   printf ("\n");
1010   printf ("%s\n", _("Notes:"));
1011   printf (" %s\n", _("- Multiple OIDs may be indicated by a comma- or space-delimited list (lists with"));
1012   printf ("   %s\n", _("internal spaces must be quoted) [max 8 OIDs]"));
1014   printf (" %s\n", _("- Ranges are inclusive and are indicated with colons. When specified as"));
1015   printf ("   %s\n", _("'min:max' a STATE_OK will be returned if the result is within the indicated"));
1016   printf ("   %s\n", _("range or is equal to the upper or lower bound. A non-OK state will be"));
1017   printf ("   %s\n", _("returned if the result is outside the specified range."));
1019   printf (" %s\n", _("- If specified in the order 'max:min' a non-OK state will be returned if the"));
1020   printf ("   %s\n", _("result is within the (inclusive) range."));
1022   printf (" %s\n", _("- Upper or lower bounds may be omitted to skip checking the respective limit."));
1023   printf (" %s\n", _("- Bare integers are interpreted as upper limits."));
1024   printf (" %s\n", _("- When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'"));
1025   printf (" %s\n", _("- Note that only one string and one regex may be checked at present"));
1026   printf (" %s\n", _("- All evaluation methods other than PR, STR, and SUBSTR expect that the value"));
1027   printf ("   %s\n", _("returned from the SNMP query is an unsigned integer."));
1028 #ifdef NP_EXTRA_OPTS
1029   printf (" -%s", _(UT_EXTRA_OPTS_NOTES));
1030 #endif
1032         printf (_(UT_SUPPORT));
1037 void
1038 print_usage (void)
1040   printf (_("Usage:"));
1041         printf ("%s -H <ip_address> -o <OID> [-w warn_range] [-c crit_range]\n",progname);
1042   printf ("[-C community] [-s string] [-r regex] [-R regexi] [-t timeout] [-e retries]\n");
1043   printf ("[-l label] [-u units] [-p port-number] [-d delimiter] [-D output-delimiter]\n");
1044   printf ("[-m miblist] [-P snmp version] [-L seclevel] [-U secname] [-a authproto]\n");
1045   printf ("[-A authpasswd] [-X privpasswd]\n");