Code

Plenty of french translations (and a few fixes BTW)
[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         strncat(perfstr, "| ", sizeof(perfstr)-strlen(perfstr)-1);
230         while (ptr) {
231                 char *foo;
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                 strncat(perfstr, ptr, copylen);
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                 strncat(perfstr, "=", sizeof(perfstr)-strlen(perfstr)-1);
372                 strncat(perfstr, show, sizeof(perfstr)-strlen(perfstr)-1);
373                 if (type)
374                         strncat(perfstr, type, sizeof(perfstr)-strlen(perfstr)-1);
375                 strncat(perfstr, " ", sizeof(perfstr)-strlen(perfstr)-1);
377         }       /* end while (ptr) */
379         if (found == 0)
380                 die (STATE_UNKNOWN,
381                         _("%s problem - No data received from host\nCMD: %s\n"),
382                         label,
383                         cl_hidden_auth);
385 #if 0           /* Removed May 29, 2007 */
386         /* WARNING if output found on stderr */
387         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
388                 result = max_state (result, STATE_WARNING);
390         /* close stderr */
391         (void) fclose (child_stderr);
392 #endif
394         /* close the pipe */
395         if (spclose (child_process)) {
396                 if (result == STATE_OK)
397                         result = STATE_UNKNOWN;
398                 asprintf (&outbuff, "%s (%s)", outbuff, _("snmpget returned an error status"));
399         }
401 /*      if (nunits == 1 || i == 1) */
402 /*              printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units); */
403 /*      else */
404         printf ("%s %s -%s %s \n", label, state_text (result), outbuff, perfstr);
406         return result;
411 /* process command-line arguments */
412 int
413 process_arguments (int argc, char **argv)
415         char *ptr;
416         int c = 1;
417         int j = 0, jj = 0, ii = 0;
419         int option = 0;
420         static struct option longopts[] = {
421                 STD_LONG_OPTS,
422                 {"community", required_argument, 0, 'C'},
423                 {"oid", required_argument, 0, 'o'},
424                 {"object", required_argument, 0, 'o'},
425                 {"delimiter", required_argument, 0, 'd'},
426                 {"output-delimiter", required_argument, 0, 'D'},
427                 {"string", required_argument, 0, 's'},
428                 {"timeout", required_argument, 0, 't'},
429                 {"regex", required_argument, 0, 'r'},
430                 {"ereg", required_argument, 0, 'r'},
431                 {"eregi", required_argument, 0, 'R'},
432                 {"label", required_argument, 0, 'l'},
433                 {"units", required_argument, 0, 'u'},
434                 {"port", required_argument, 0, 'p'},
435                 {"retries", required_argument, 0, 'e'},
436                 {"miblist", required_argument, 0, 'm'},
437                 {"protocol", required_argument, 0, 'P'},
438                 {"seclevel", required_argument, 0, 'L'},
439                 {"secname", required_argument, 0, 'U'},
440                 {"authproto", required_argument, 0, 'a'},
441                 {"authpasswd", required_argument, 0, 'A'},
442                 {"privpasswd", required_argument, 0, 'X'},
443                 {"next", no_argument, 0, 'n'},
444                 {0, 0, 0, 0}
445         };
447         if (argc < 2)
448                 return ERROR;
450         /* reverse compatibility for very old non-POSIX usage forms */
451         for (c = 1; c < argc; c++) {
452                 if (strcmp ("-to", argv[c]) == 0)
453                         strcpy (argv[c], "-t");
454                 if (strcmp ("-wv", argv[c]) == 0)
455                         strcpy (argv[c], "-w");
456                 if (strcmp ("-cv", argv[c]) == 0)
457                         strcpy (argv[c], "-c");
458         }
460         while (1) {
461                 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:",
462                                                                          longopts, &option);
464                 if (c == -1 || c == EOF)
465                         break;
467                 switch (c) {
468                 case '?':       /* usage */
469                         usage5 ();
470                 case 'h':       /* help */
471                         print_help ();
472                         exit (STATE_OK); 
473                 case 'V':       /* version */
474                         print_revision (progname, revision);
475                         exit (STATE_OK);
476                 case 'v': /* verbose */
477                         verbose = TRUE;
478                         break;
480         /* Connection info */
481                 case 'C':                                                                       /* group or community */
482                         community = optarg;
483                         break;
484                 case 'H':                                                                       /* Host or server */
485                         server_address = optarg;
486                         break;
487                 case 'p':       /* TCP port number */
488                         port = optarg;
489                         break;
490                 case 'm':       /* List of MIBS  */
491                         miblist = optarg;
492                         break;
493                 case 'n':       /* usesnmpgetnext */
494                         usesnmpgetnext = TRUE;
495                         break;
496                 case 'P':       /* SNMP protocol version */
497                         proto = optarg;
498                         break;
499                 case 'L':       /* security level */
500                         seclevel = optarg;
501                         break;
502                 case 'U':       /* security username */
503                         secname = optarg;
504                         break;
505                 case 'a':       /* auth protocol */
506                         authproto = optarg;
507                         break;
508                 case 'A':       /* auth passwd */
509                         authpasswd = optarg;
510                         break;
511                 case 'X':       /* priv passwd */
512                         privpasswd = optarg;
513                         break;
514                 case 't':       /* timeout period */
515                         if (!is_integer (optarg))
516                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
517                         else
518                                 timeout_interval = atoi (optarg);
519                         break;
521         /* Test parameters */
522                 case 'c':                                                                       /* critical time threshold */
523                         if (strspn (optarg, "0123456789:,") < strlen (optarg))
524                                 usage2 (_("Invalid critical threshold"), optarg);
525                         for (ptr = optarg; ptr && jj < MAX_OIDS; jj++) {
526                                 if (llu_getll (&lower_crit_lim[jj], ptr) == 1)
527                                         eval_method[jj] |= CRIT_LT;
528                                 if (llu_getul (&upper_crit_lim[jj], ptr) == 1)
529                                         eval_method[jj] |= CRIT_GT;
530                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
531                         }
532                         break;
533                 case 'w':                                                                       /* warning time threshold */
534                         if (strspn (optarg, "0123456789:,") < strlen (optarg))
535                                 usage2 (_("Invalid warning threshold"), optarg);
536                         for (ptr = optarg; ptr && ii < MAX_OIDS; ii++) {
537                                 if (llu_getll (&lower_warn_lim[ii], ptr) == 1)
538                                         eval_method[ii] |= WARN_LT;
539                                 if (llu_getul (&upper_warn_lim[ii], ptr) == 1)
540                                         eval_method[ii] |= WARN_GT;
541                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
542                         }
543                         break;
544                 case 'e': /* PRELIMINARY - may change */
545                 case 'E': /* PRELIMINARY - may change */
546                         if (!is_integer (optarg))
547                                 usage2 (_("Retries interval must be a positive integer"), optarg);
548                         else
549                                 retries = atoi(optarg);
550                         break;
551                 case 'o':                                                                       /* object identifier */
552                         if ( strspn( optarg, "0123456789.," ) != strlen( optarg ) ) {
553                                         /*
554                                          * we have something other than digits, periods and comas,
555                                          * so we have a mib variable, rather than just an SNMP OID,
556                                          * so we have to actually read the mib files
557                                          */
558                                         needmibs = TRUE;
559                         }
561                         for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
562                                 ptr[0] = ' '; /* relpace comma with space */
563                         for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
564                                 j++; /* count OIDs */
565                         asprintf (&oid, "%s %s", (oid?oid:""), optarg);
566                         if (c == 'E' || c == 'e') {
567                                 jj++;
568                                 ii++;
569                         }
570                         if (c == 'E') 
571                                 eval_method[j+1] |= WARN_PRESENT;
572                         else if (c == 'e')
573                                 eval_method[j+1] |= CRIT_PRESENT;
574                         break;
575                 case 's':                                                                       /* string or substring */
576                         strncpy (string_value, optarg, sizeof (string_value) - 1);
577                         string_value[sizeof (string_value) - 1] = 0;
578                         eval_method[jj++] = CRIT_STRING;
579                         ii++;
580                         break;
581                 case 'R':                                                                       /* regex */
582                         cflags = REG_ICASE;
583                 case 'r':                                                                       /* regex */
584                         cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
585                         strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
586                         regex_expect[sizeof (regex_expect) - 1] = 0;
587                         errcode = regcomp (&preg, regex_expect, cflags);
588                         if (errcode != 0) {
589                                 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
590                                 printf (_("Could Not Compile Regular Expression"));
591                                 return ERROR;
592                         }
593                         eval_method[jj++] = CRIT_REGEX;
594                         ii++;
595                         break;
597         /* Format */
598                 case 'd':                                                                       /* delimiter */
599                         delimiter = strscpy (delimiter, optarg);
600                         break;
601                 case 'D':                                                                       /* output-delimiter */
602                         output_delim = strscpy (output_delim, optarg);
603                         break;
604                 case 'l':                                                                       /* label */
605                         label = optarg;
606                         nlabels++;
607                         if (nlabels >= labels_size) {
608                                 labels_size += 8;
609                                 labels = realloc (labels, labels_size);
610                                 if (labels == NULL)
611                                         die (STATE_UNKNOWN, _("Could not reallocate labels[%d]"), (int)nlabels);
612                         }
613                         labels[nlabels - 1] = optarg;
614                         ptr = thisarg (optarg);
615                         labels[nlabels - 1] = ptr;
616                         if (strstr (ptr, "'") == ptr)
617                                 labels[nlabels - 1] = ptr + 1;
618                         while (ptr && (ptr = nextarg (ptr))) {
619                                 if (nlabels >= labels_size) {
620                                         labels_size += 8;
621                                         labels = realloc (labels, labels_size);
622                                         if (labels == NULL)
623                                                 die (STATE_UNKNOWN, _("Could not reallocate labels\n"));
624                                 }
625                                 labels++;
626                                 ptr = thisarg (ptr);
627                                 if (strstr (ptr, "'") == ptr)
628                                         labels[nlabels - 1] = ptr + 1;
629                                 else
630                                         labels[nlabels - 1] = ptr;
631                         }
632                         break;
633                 case 'u':                                                                       /* units */
634                         units = optarg;
635                         nunits++;
636                         if (nunits >= unitv_size) {
637                                 unitv_size += 8;
638                                 unitv = realloc (unitv, unitv_size);
639                                 if (unitv == NULL)
640                                         die (STATE_UNKNOWN, _("Could not reallocate units [%d]\n"), (int)nunits);
641                         }
642                         unitv[nunits - 1] = optarg;
643                         ptr = thisarg (optarg);
644                         unitv[nunits - 1] = ptr;
645                         if (strstr (ptr, "'") == ptr)
646                                 unitv[nunits - 1] = ptr + 1;
647                         while (ptr && (ptr = nextarg (ptr))) {
648                                 if (nunits >= unitv_size) {
649                                         unitv_size += 8;
650                                         unitv = realloc (unitv, unitv_size);
651                                         if (units == NULL)
652                                                 die (STATE_UNKNOWN, _("Could not realloc() units\n"));
653                                 }
654                                 nunits++;
655                                 ptr = thisarg (ptr);
656                                 if (strstr (ptr, "'") == ptr)
657                                         unitv[nunits - 1] = ptr + 1;
658                                 else
659                                         unitv[nunits - 1] = ptr;
660                         }
661                         break;
663                 }
664         }
666         if (server_address == NULL)
667                 server_address = argv[optind];
669         if (community == NULL)
670                 community = strdup (DEFAULT_COMMUNITY);
671         
674         return validate_arguments ();
678 /******************************************************************************
680 @@-
681 <sect3>
682 <title>validate_arguments</title>
684 <para>&PROTO_validate_arguments;</para>
686 <para>Checks to see if the default miblist needs to be loaded. Also verifies 
687 the authentication and authorization combinations based on protocol version 
688 selected.</para>
690 <para></para>
692 </sect3>
693 -@@
694 ******************************************************************************/
698 int
699 validate_arguments ()
701         /* check whether to load locally installed MIBS (CPU/disk intensive) */
702         if (miblist == NULL) {
703                 if ( needmibs  == TRUE ) {
704                         miblist = strdup (DEFAULT_MIBLIST);
705                 }else{
706                         miblist = "''";                 /* don't read any mib files for numeric oids */
707                 }
708         }
711         /* Need better checks to verify seclevel and authproto choices */
712         
713         if (seclevel == NULL) 
714                 asprintf (&seclevel, "noAuthNoPriv");
717         if (authproto == NULL ) 
718                 asprintf(&authproto, DEFAULT_AUTH_PROTOCOL);
719         
720          
721         
722         if (proto == NULL || (strcmp(proto,DEFAULT_PROTOCOL) == 0) ) {  /* default protocol version */
723                 asprintf(&proto, DEFAULT_PROTOCOL);
724                 asprintf(&authpriv, "%s%s", "-c ", community);
725         }
726         else if ( strcmp (proto, "2c") == 0 ) {         /* snmpv2c args */
727                 asprintf(&authpriv, "%s%s", "-c ", community);
728         }
729         else if ( strcmp (proto, "3") == 0 ) {          /* snmpv3 args */
730                 asprintf(&proto, "%s", "3");
731                 
732                 if ( (strcmp(seclevel, "noAuthNoPriv") == 0) || seclevel == NULL ) {
733                         asprintf(&authpriv, "%s", "-l noAuthNoPriv" );
734                 }
735                 else if ( strcmp(seclevel, "authNoPriv") == 0 ) {
736                         if ( secname == NULL || authpasswd == NULL) {
737                                 printf (_("Missing secname (%s) or authpassword (%s) ! \n"),secname, authpasswd );
738                                 print_usage ();
739                                 exit (STATE_UNKNOWN);
740                         }
741                         asprintf(&authpriv, "-l authNoPriv -a %s -u %s -A %s ", authproto, secname, authpasswd);
742                 }
743                 else if ( strcmp(seclevel, "authPriv") == 0 ) {
744                         if ( secname == NULL || authpasswd == NULL || privpasswd == NULL ) {
745                                 printf (_("Missing secname (%s), authpassword (%s), or privpasswd (%s)! \n"),secname, authpasswd,privpasswd );
746                                 print_usage ();
747                                 exit (STATE_UNKNOWN);
748                         }
749                         asprintf(&authpriv, "-l authPriv -a %s -u %s -A %s -x DES -X %s ", authproto, secname, authpasswd, privpasswd);
750                 }
751                 
752         }
753         else {
754                 usage2 (_("Invalid SNMP version"), proto);
755         }
756                         
757         return OK;
762 char *
763 clarify_message (char *msg)
765         int i = 0;
766         int foo;
767         char tmpmsg_c[MAX_INPUT_BUFFER];
768         char *tmpmsg = (char *) &tmpmsg_c;
769         tmpmsg = strcpy (tmpmsg, msg);
770         if (!strncmp (tmpmsg, " Hex:", 5)) {
771                 tmpmsg = strtok (tmpmsg, ":");
772                 while ((tmpmsg = strtok (NULL, " "))) {
773                         foo = strtol (tmpmsg, NULL, 16);
774                         /* Translate chars that are not the same value in the printers
775                          * character set.
776                          */
777                         switch (foo) {
778                         case 208:
779                                 {
780                                         foo = 197;
781                                         break;
782                                 }
783                         case 216:
784                                 {
785                                         foo = 196;
786                                         break;
787                                 }
788                         }
789                         msg[i] = foo;
790                         i++;
791                 }
792                 msg[i] = 0;
793         }
794         return (msg);
799 int
800 check_num (int i)
802         int result;
803         result = STATE_OK;
804         if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
805                         lower_warn_lim[i] > upper_warn_lim[i]) {
806                 if (response_value[i] <= lower_warn_lim[i] &&
807                                 response_value[i] >= upper_warn_lim[i]) {
808                         result = STATE_WARNING;
809                 }
810         }
811         else if
812                 ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
813                  (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
814                  (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
815                  (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
816                  (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
817                  (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
818                 result = STATE_WARNING;
819         }
821         if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
822                         lower_crit_lim[i] > upper_crit_lim[i]) {
823                 if (response_value[i] <= lower_crit_lim[i] &&
824                                 response_value[i] >= upper_crit_lim[i]) {
825                         result = STATE_CRITICAL;
826                 }
827         }
828         else if
829                 ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
830                  (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
831                  (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
832                  (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
833                  (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
834                  (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
835                 result = STATE_CRITICAL;
836         }
838         return result;
843 int
844 llu_getll (unsigned long long *ll, char *str)
846         char tmp[100];
847         if (strchr (str, ':') == NULL)
848                 return 0;
849         if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
850                 return 0;
851         if (sscanf (str, "%llu%[:]", ll, tmp) == 2)
852                 return 1;
853         return 0;
858 int
859 llu_getul (unsigned long long *ul, char *str)
861         char tmp[100];
862         if (sscanf (str, "%llu%[^,]", ul, tmp) == 1)
863                 return 1;
864         if (sscanf (str, ":%llu%[^,]", ul, tmp) == 1)
865                 return 1;
866         if (sscanf (str, "%*u:%llu%[^,]", ul, tmp) == 1)
867                 return 1;
868         return 0;
873 /* trim leading whitespace
874          if there is a leading quote, make sure it balances */
876 char *
877 thisarg (char *str)
879         str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
880         if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
881                 if (strlen (str) == 1 || !strstr (str + 1, "'"))
882                         die (STATE_UNKNOWN, _("Unbalanced quotes\n"));
883         }
884         return str;
889 /* if there's a leading quote, advance to the trailing quote
890          set the trailing quote to '\x0'
891          if the string continues, advance beyond the comma */
893 char *
894 nextarg (char *str)
896         if (strstr (str, "'") == str) {
897                 str[0] = 0;
898                 if (strlen (str) > 1) {
899                         str = strstr (str + 1, "'");
900                         return (++str);
901                 }
902                 else {
903                         return NULL;
904                 }
905         }
906         if (strstr (str, ",") == str) {
907                 str[0] = 0;
908                 if (strlen (str) > 1) {
909                         return (++str);
910                 }
911                 else {
912                         return NULL;
913                 }
914         }
915         if ((str = strstr (str, ",")) && strlen (str) > 1) {
916                 str[0] = 0;
917                 return (++str);
918         }
919         return NULL;
924 void
925 print_help (void)
927         print_revision (progname, revision);
929         printf (COPYRIGHT, copyright, email);
931         printf ("%s\n", _("Check status of remote machines and obtain sustem information via SNMP"));
933   printf ("\n\n");
935         print_usage ();
937         printf (_(UT_HELP_VRSN));
939         printf (_(UT_HOST_PORT), 'p', DEFAULT_PORT);
941         /* SNMP and Authentication Protocol */
942         printf (" %s\n", "-n, --next");
943   printf ("    %s\n", _("Use SNMP GETNEXT instead of SNMP GET"));
944   printf (" %s\n", "-P, --protocol=[1|2c|3]");
945   printf ("    %s\n", _("SNMP protocol version"));
946   printf (" %s\n", "-L, --seclevel=[noAuthNoPriv|authNoPriv|authPriv]");
947   printf ("    %s\n", _("SNMPv3 securityLevel"));
948   printf (" %s\n", "-a, --authproto=[MD5|SHA]");
949   printf ("    %s\n", _("SNMPv3 auth proto"));
951         /* Authentication Tokens*/
952         printf (" %s\n", "-C, --community=STRING");
953   printf ("    %s ", _("Optional community string for SNMP communication"));
954   printf ("(%s \"%s\")\n", _("default is") ,DEFAULT_COMMUNITY);
955   printf (" %s\n", "-U, --secname=USERNAME");
956   printf ("    %s\n", _("SNMPv3 username"));
957   printf (" %s\n", "-A, --authpassword=PASSWORD");
958   printf ("    %s\n", _("SNMPv3 authentication password"));
959   printf (" %s\n", "-X, --privpasswd=PASSWORD");
960   printf ("    %s\n", _("SNMPv3 privacy password"));
962         /* OID Stuff */
963         printf (" %s\n", "-o, --oid=OID(s)");
964   printf ("    %s\n", _("Object identifier(s) or SNMP variables whose value you wish to query"));
965   printf (" %s\n", "-m, --miblist=STRING");
966   printf ("    %s\n", _("List of MIBS to be loaded (default = none if using numeric oids or 'ALL'"));
967   printf ("    %s\n", _("for symbolic oids.)"));
968   printf (" %s\n", "-d, --delimiter=STRING");
969   printf (_("    Delimiter to use when parsing returned data. Default is \"%s\""), DEFAULT_DELIMITER);
970   printf ("    %s\n", _("Any data on the right hand side of the delimiter is considered"));
971   printf ("    %s\n", _("to be the data that should be used in the evaluation."));
973         /* Tests Against Integers */
974         printf (" %s\n", "-w, --warning=INTEGER_RANGE(s)");
975   printf ("    %s\n", _("Range(s) which will not result in a WARNING status"));
976   printf (" %s\n", "-c, --critical=INTEGER_RANGE(s)");
977   printf ("    %s\n", _("Range(s) which will not result in a CRITICAL status"));
979         /* Tests Against Strings */
980         printf (" %s\n", "-s, --string=STRING");
981   printf ("    %s\n", _("Return OK state (for that OID) if STRING is an exact match"));
982   printf (" %s\n", "-r, --ereg=REGEX");
983   printf ("    %s\n", _("Return OK state (for that OID) if extended regular expression REGEX matches"));
984   printf (" %s\n", "-R, --eregi=REGEX");
985   printf ("    %s\n", _("Return OK state (for that OID) if case-insensitive extended REGEX matches"));
986   printf (" %s\n", "-l, --label=STRING");
987   printf ("    %s\n", _("Prefix label for output from plugin (default -s 'SNMP')"));
989         /* Output Formatting */
990         printf (" %s\n", "-u, --units=STRING");
991   printf ("    %s\n", _("Units label(s) for output data (e.g., 'sec.')."));
992   printf (" %s\n", "-D, --output-delimiter=STRING");
993   printf ("    %s\n", _("Separates output on multiple OID requests"));
995         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
997         printf (_(UT_VERBOSE));
999         printf ("%s\n", _("This plugin uses the 'snmpget' command included with the NET-SNMP package."));
1000   printf ("%s\n", _("if you don't have the package installed, you will need to download it from"));
1001   printf ("%s\n", _("http://net-snmp.sourceforge.net before you can use this plugin."));
1003         printf ("%s\n", _("- Multiple OIDs may be indicated by a comma- or space-delimited list (lists with"));
1004   printf ("%s\n", _(" internal spaces must be quoted) [max 8 OIDs]"));
1006         printf ("%s\n", _("- Ranges are inclusive and are indicated with colons. When specified as"));
1007   printf ("%s\n", _(" 'min:max' a STATE_OK will be returned if the result is within the indicated"));
1008   printf ("%s\n", _(" range or is equal to the upper or lower bound. A non-OK state will be"));
1009   printf ("%s\n", _(" returned if the result is outside the specified range."));
1011         printf ("%s\n", _("- If specified in the order 'max:min' a non-OK state will be returned if the"));
1012   printf ("%s\n", _(" result is within the (inclusive) range."));
1014         printf ("%s\n", _("- Upper or lower bounds may be omitted to skip checking the respective limit."));
1015   printf ("%s\n", _("- Bare integers are interpreted as upper limits."));
1016   printf ("%s\n", _("- When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'"));
1017   printf ("%s\n", _("- Note that only one string and one regex may be checked at present"));
1018   printf ("%s\n", _("- All evaluation methods other than PR, STR, and SUBSTR expect that the value"));
1019   printf ("%s\n", _(" returned from the SNMP query is an unsigned integer."));
1021         printf (_(UT_SUPPORT));
1026 void
1027 print_usage (void)
1029   printf (_("Usage:"));
1030         printf ("%s -H <ip_address> -o <OID> [-w warn_range] [-c crit_range]\n",progname);
1031   printf ("[-C community] [-s string] [-r regex] [-R regexi] [-t timeout] [-e retries]\n");
1032   printf ("[-l label] [-u units] [-p port-number] [-d delimiter] [-D output-delimiter]\n");
1033   printf ("[-m miblist] [-P snmp version] [-L seclevel] [-U secname] [-a authproto]\n");
1034   printf ("[-A authpasswd] [-X privpasswd]\n");