Code

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