Code

v2c support - 1155754
[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 #ifdef HAVE_REGEX_H
79 #include <regex.h>
80 char regex_expect[MAX_INPUT_BUFFER] = "";
81 regex_t preg;
82 regmatch_t pmatch[10];
83 char timestamp[10] = "";
84 char errbuf[MAX_INPUT_BUFFER];
85 char perfstr[MAX_INPUT_BUFFER];
86 int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
87 int eflags = 0;
88 int errcode, excode;
89 #endif
91 char *server_address = NULL;
92 char *community = NULL;
93 char *authpriv = NULL;
94 char *proto = NULL;
95 char *seclevel = NULL;
96 char *secname = NULL;
97 char *authproto = NULL;
98 char *authpasswd = NULL;
99 char *privpasswd = NULL;
100 char *oid;
101 char *label;
102 char *units;
103 char *port;
104 char string_value[MAX_INPUT_BUFFER] = "";
105 char **labels = NULL;
106 char **unitv = NULL;
107 size_t nlabels = 0;
108 size_t labels_size = 8;
109 size_t nunits = 0;
110 size_t unitv_size = 8;
111 int verbose = FALSE;
112 unsigned long lower_warn_lim[MAX_OIDS];
113 unsigned long upper_warn_lim[MAX_OIDS];
114 unsigned long lower_crit_lim[MAX_OIDS];
115 unsigned long upper_crit_lim[MAX_OIDS];
116 unsigned long response_value[MAX_OIDS];
117 int check_warning_value = FALSE;
118 int check_critical_value = FALSE;
119 int retries = 0;
120 unsigned long eval_method[MAX_OIDS];
121 char *delimiter;
122 char *output_delim;
123 char *miblist;
125 int
126 main (int argc, char **argv)
128         int i = 0;
129         int iresult = STATE_UNKNOWN;
130         int found = 0;
131         int result = STATE_DEPENDENT;
132         char input_buffer[MAX_INPUT_BUFFER];
133         char *command_line = NULL;
134         char *response = NULL;
135         char *outbuff;
136         char *output;
137         char *ptr = NULL;
138         char *p2 = NULL;
139         char *show = NULL;
140         char type[8];
142         setlocale (LC_ALL, "");
143         bindtextdomain (PACKAGE, LOCALEDIR);
144         textdomain (PACKAGE);
146         labels = malloc (labels_size);
147         unitv = malloc (unitv_size);
148         for (i = 0; i < MAX_OIDS; i++)
149                 eval_method[i] = CHECK_UNDEF;
150         i = 0;
152         oid = strdup ("");
153         label = strdup ("SNMP");
154         units = strdup ("");
155         port = strdup (DEFAULT_PORT);
156         outbuff = strdup ("");
157         output = strdup ("");
158         delimiter = strdup (" = ");
159         output_delim = strdup (DEFAULT_OUTPUT_DELIMITER);
160         miblist = strdup (DEFAULT_MIBLIST);
161         timeout_interval = DEFAULT_TIMEOUT;
162         retries = DEFAULT_RETRIES;
164         if (process_arguments (argc, argv) == ERROR)
165                 usage4 (_("Could not parse arguments"));
167         /* create the command line to execute */
168         asprintf (&command_line, "%s -t %d -r %d -m %s -v %s %s %s:%s %s",
169                   PATH_TO_SNMPGET, timeout_interval, retries, miblist, proto,
170                   authpriv, server_address, port, oid);
171         if (verbose)
172                 printf ("%s\n", command_line);
174         /* run the command */
175         child_process = spopen (command_line);
176         if (child_process == NULL) {
177                 printf (_("Could not open pipe: %s\n"), command_line);
178                 exit (STATE_UNKNOWN);
179         }
181         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
182         if (child_stderr == NULL) {
183                 printf (_("Could not open stderr for %s\n"), command_line);
184         }
186         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
187                 asprintf (&output, "%s%s", output, input_buffer);
189         if (verbose)
190                 printf ("%s\n", output);
192         ptr = output;
194         strcat(perfstr, "| ");
195         while (ptr) {
196                 char *foo;
198                 foo = strstr (ptr, delimiter);
199                 strncat(perfstr, ptr, foo-ptr);
200                 ptr = foo; 
202                 if (ptr == NULL)
203                         break;
205                 ptr += strlen (delimiter);
206                 ptr += strspn (ptr, " ");
208                 found++;
210                 if (ptr[0] == '"') {
211                         ptr++;
212                         response = strpcpy (response, ptr, "\"");
213                         ptr = strpbrk (ptr, "\"");
214                         ptr += strspn (ptr, "\"\n");
215                 }
216                 else {
217                         response = strpcpy (response, ptr, "\n");
218                         ptr = strpbrk (ptr, "\n");
219                         ptr += strspn (ptr, "\n");
220                         while
221                                 (strstr (ptr, delimiter) &&
222                                  strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter)) {
223                                 response = strpcat (response, ptr, "\n");
224                                 ptr = strpbrk (ptr, "\n");
225                         }
226                         if (ptr && strstr (ptr, delimiter) == NULL) {
227                                 asprintf (&response, "%s%s", response, ptr);
228                                 ptr = NULL;
229                         }
230                 }
232                 /* We strip out the datatype indicator for PHBs */
233                 if (strstr (response, "Gauge: "))
234                         show = strstr (response, "Gauge: ") + 7;
235                 else if (strstr (response, "Gauge32: "))
236                         show = strstr (response, "Gauge32: ") + 9;
237                 else if (strstr (response, "Counter32: ")) {
238                         show = strstr (response, "Counter32: ") + 11;
239                         strcpy(type, "c");
240                 }
241                 else if (strstr (response, "INTEGER: "))
242                         show = strstr (response, "INTEGER: ") + 9;
243                 else if (strstr (response, "STRING: "))
244                         show = strstr (response, "STRING: ") + 8;
245                 else
246                         show = response;
247                 p2 = show;
249                 iresult = STATE_DEPENDENT;
251                 /* Process this block for integer comparisons */
252                 if (eval_method[i] & CRIT_GT ||
253                     eval_method[i] & CRIT_LT ||
254                     eval_method[i] & CRIT_GE ||
255                     eval_method[i] & CRIT_LE ||
256                     eval_method[i] & CRIT_EQ ||
257                     eval_method[i] & CRIT_NE ||
258                     eval_method[i] & WARN_GT ||
259                     eval_method[i] & WARN_LT ||
260                     eval_method[i] & WARN_GE ||
261                     eval_method[i] & WARN_LE ||
262                     eval_method[i] & WARN_EQ ||
263                     eval_method[i] & WARN_NE) {
264                         p2 = strpbrk (p2, "0123456789");
265                         if (p2 == NULL) 
266                                 die (STATE_UNKNOWN,_("No valid data returned"));
267                         response_value[i] = strtoul (p2, NULL, 10);
268                         iresult = check_num (i);
269                         asprintf (&show, "%lu", response_value[i]);
270                 }
272                 /* Process this block for string matching */
273                 else if (eval_method[i] & CRIT_STRING) {
274                         if (strcmp (show, string_value))
275                                 iresult = STATE_CRITICAL;
276                         else
277                                 iresult = STATE_OK;
278                 }
280                 /* Process this block for regex matching */
281                 else if (eval_method[i] & CRIT_REGEX) {
282 #ifdef HAVE_REGEX_H
283                         excode = regexec (&preg, response, 10, pmatch, eflags);
284                         if (excode == 0) {
285                                 iresult = STATE_OK;
286                         }
287                         else if (excode != REG_NOMATCH) {
288                                 regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
289                                 printf (_("Execute Error: %s\n"), errbuf);
290                                 exit (STATE_CRITICAL);
291                         }
292                         else {
293                                 iresult = STATE_CRITICAL;
294                         }
295 #else
296                         printf (_("Call for regex which was not a compiled option"));
297                         exit (STATE_UNKNOWN);
298 #endif
299                 }
301                 /* Process this block for existence-nonexistence checks */
302                 else {
303                         if (eval_method[i] & CRIT_PRESENT)
304                                 iresult = STATE_CRITICAL;
305                         else if (eval_method[i] & WARN_PRESENT)
306                                 iresult = STATE_WARNING;
307                         else if (response && iresult == STATE_DEPENDENT) 
308                                 iresult = STATE_OK;
309                 }
311                 /* Result is the worst outcome of all the OIDs tested */
312                 result = max_state (result, iresult);
314                 /* Prepend a label for this OID if there is one */
315                 if (nlabels > (size_t)1 && (size_t)i < nlabels && labels[i] != NULL)
316                         asprintf (&outbuff, "%s%s%s %s%s%s", outbuff,
317                                   (i == 0) ? " " : output_delim,
318                                   labels[i], mark (iresult), show, mark (iresult));
319                 else
320                         asprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim,
321                                   mark (iresult), show, mark (iresult));
323                 /* Append a unit string for this OID if there is one */
324                 if (nunits > (size_t)0 && (size_t)i < nunits && unitv[i] != NULL)
325                         asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
327                 i++;
329                 char *str[MAX_INPUT_BUFFER];
330                 asprintf(str, "=%s%s;;;; ", show, type ? type : "");
331                 strcat(perfstr, *str);
333         }       /* end while (ptr) */
335         if (found == 0)
336                 die (STATE_UNKNOWN,
337                      _("%s problem - No data received from host\nCMD: %s\n"),
338                      label,
339                      command_line);
341         /* WARNING if output found on stderr */
342         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
343                 result = max_state (result, STATE_WARNING);
345         /* close stderr */
346         (void) fclose (child_stderr);
348         /* close the pipe */
349         if (spclose (child_process))
350                 result = max_state (result, STATE_WARNING);
352 /*      if (nunits == 1 || i == 1) */
353 /*              printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units); */
354 /*      else */
355         printf ("%s %s -%s %s \n", label, state_text (result), outbuff, perfstr);
357         return result;
362 /* process command-line arguments */
363 int
364 process_arguments (int argc, char **argv)
366         char *ptr;
367         int c = 1;
368         int j = 0, jj = 0, ii = 0;
370         int option = 0;
371         static struct option longopts[] = {
372                 STD_LONG_OPTS,
373                 {"community", required_argument, 0, 'C'},
374                 {"oid", required_argument, 0, 'o'},
375                 {"object", required_argument, 0, 'o'},
376                 {"delimiter", required_argument, 0, 'd'},
377                 {"output-delimiter", required_argument, 0, 'D'},
378                 {"string", required_argument, 0, 's'},
379                 {"timeout", required_argument, 0, 't'},
380                 {"regex", required_argument, 0, 'r'},
381                 {"ereg", required_argument, 0, 'r'},
382                 {"eregi", required_argument, 0, 'R'},
383                 {"label", required_argument, 0, 'l'},
384                 {"units", required_argument, 0, 'u'},
385                 {"port", required_argument, 0, 'p'},
386                 {"retries", required_argument, 0, 'e'},
387                 {"miblist", required_argument, 0, 'm'},
388                 {"protocol", required_argument, 0, 'P'},
389                 {"seclevel", required_argument, 0, 'L'},
390                 {"secname", required_argument, 0, 'U'},
391                 {"authproto", required_argument, 0, 'a'},
392                 {"authpasswd", required_argument, 0, 'A'},
393                 {"privpasswd", required_argument, 0, 'X'},
394                 {0, 0, 0, 0}
395         };
397         if (argc < 2)
398                 return ERROR;
400         /* reverse compatibility for very old non-POSIX usage forms */
401         for (c = 1; c < argc; c++) {
402                 if (strcmp ("-to", argv[c]) == 0)
403                         strcpy (argv[c], "-t");
404                 if (strcmp ("-wv", argv[c]) == 0)
405                         strcpy (argv[c], "-w");
406                 if (strcmp ("-cv", argv[c]) == 0)
407                         strcpy (argv[c], "-c");
408         }
410         while (1) {
411                 c = getopt_long (argc, argv, "hvVt:c:w:H:C:o:e:E:d:D:s:t:R:r:l:u:p:m:P:L:U:a:A:X:",
412                                                                          longopts, &option);
414                 if (c == -1 || c == EOF)
415                         break;
417                 switch (c) {
418                 case '?':       /* usage */
419                         usage2 (_("Unknown argument"), optarg);
420                 case 'h':       /* help */
421                         print_help ();
422                         exit (STATE_OK); 
423                 case 'V':       /* version */
424                         print_revision (progname, revision);
425                         exit (STATE_OK);
426                 case 'v': /* verbose */
427                         verbose = TRUE;
428                         break;
430         /* Connection info */
431                 case 'C':                                                                       /* group or community */
432                         community = optarg;
433                         break;
434                 case 'H':                                                                       /* Host or server */
435                         server_address = optarg;
436                         break;
437                 case 'p':       /* TCP port number */
438                         port = optarg;
439                         break;
440                 case 'm':      /* List of MIBS  */
441                         miblist = optarg;
442                         break;
443                 case 'P':     /* SNMP protocol version */
444                         proto = optarg;
445                         break;
446                 case 'L':     /* security level */
447                         seclevel = optarg;
448                         break;
449                 case 'U':     /* security username */
450                         secname = optarg;
451                         break;
452                 case 'a':     /* auth protocol */
453                         authproto = optarg;
454                         break;
455                 case 'A':     /* auth passwd */
456                         authpasswd = optarg;
457                         break;
458                 case 'X':     /* priv passwd */
459                         privpasswd = optarg;
460                         break;
461                 case 't':       /* timeout period */
462                         if (!is_integer (optarg))
463                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
464                         else
465                                 timeout_interval = atoi (optarg);
466                         break;
468         /* Test parameters */
469                 case 'c':                                                                       /* critical time threshold */
470                         if (strspn (optarg, "0123456789:,") < strlen (optarg))
471                                 usage2 (_("Invalid critical threshold: %s\n"), optarg);
472                         for (ptr = optarg; ptr && jj < MAX_OIDS; jj++) {
473                                 if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
474                                         eval_method[jj] |= CRIT_LT;
475                                 if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
476                                         eval_method[jj] |= CRIT_GT;
477                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
478                         }
479                         break;
480                 case 'w':                                                                       /* warning time threshold */
481                         if (strspn (optarg, "0123456789:,") < strlen (optarg))
482                                 usage2 (_("Invalid warning threshold: %s\n"), optarg);
483                         for (ptr = optarg; ptr && ii < MAX_OIDS; ii++) {
484                                 if (lu_getll (&lower_warn_lim[ii], ptr) == 1)
485                                         eval_method[ii] |= WARN_LT;
486                                 if (lu_getul (&upper_warn_lim[ii], ptr) == 1)
487                                         eval_method[ii] |= WARN_GT;
488                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
489                         }
490                         break;
491                 case 'e': /* PRELIMINARY - may change */
492                 case 'E': /* PRELIMINARY - may change */
493                         if (!is_integer (optarg))
494                                 usage2 (_("Retries interval must be a positive integer"), optarg);
495                         else
496                                 retries = atoi(optarg);
497                         break;
498                 case 'o':                                                                       /* object identifier */
499                         for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
500                                 ptr[0] = ' '; /* relpace comma with space */
501                         for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
502                                 j++; /* count OIDs */
503                         asprintf (&oid, "%s %s", (oid?oid:""), optarg);
504                         if (c == 'E' || c == 'e') {
505                                 jj++;
506                                 ii++;
507                         }
508                         if (c == 'E') 
509                                 eval_method[j+1] |= WARN_PRESENT;
510                         else if (c == 'e')
511                                 eval_method[j+1] |= CRIT_PRESENT;
512                         break;
513                 case 's':                                                                       /* string or substring */
514                         strncpy (string_value, optarg, sizeof (string_value) - 1);
515                         string_value[sizeof (string_value) - 1] = 0;
516                         eval_method[jj++] = CRIT_STRING;
517                         ii++;
518                         break;
519                 case 'R':                                                                       /* regex */
520 #ifdef HAVE_REGEX_H
521                         cflags = REG_ICASE;
522 #endif
523                 case 'r':                                                                       /* regex */
524 #ifdef HAVE_REGEX_H
525                         cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
526                         strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
527                         regex_expect[sizeof (regex_expect) - 1] = 0;
528                         errcode = regcomp (&preg, regex_expect, cflags);
529                         if (errcode != 0) {
530                                 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
531                                 printf (_("Could Not Compile Regular Expression"));
532                                 return ERROR;
533                         }
534                         eval_method[jj++] = CRIT_REGEX;
535                         ii++;
536 #else
537                         printf (_("call for regex which was not a compiled option"));
538                         exit (STATE_UNKNOWN);
539 #endif
540                         break;
542         /* Format */
543                 case 'd':                                                                       /* delimiter */
544                         delimiter = strscpy (delimiter, optarg);
545                         break;
546                 case 'D':                                                                       /* output-delimiter */
547                         output_delim = strscpy (output_delim, optarg);
548                         break;
549                 case 'l':                                                                       /* label */
550                         label = optarg;
551                         nlabels++;
552                         if (nlabels >= labels_size) {
553                                 labels_size += 8;
554                                 labels = realloc (labels, labels_size);
555                                 if (labels == NULL)
556                                         die (STATE_UNKNOWN, _("Could not reallocate labels[%d]"), nlabels);
557                         }
558                         labels[nlabels - 1] = optarg;
559                         ptr = thisarg (optarg);
560                         labels[nlabels - 1] = ptr;
561                         if (strstr (ptr, "'") == ptr)
562                                 labels[nlabels - 1] = ptr + 1;
563                         while (ptr && (ptr = nextarg (ptr))) {
564                                 if (nlabels >= labels_size) {
565                                         labels_size += 8;
566                                         labels = realloc (labels, labels_size);
567                                         if (labels == NULL)
568                                                 die (STATE_UNKNOWN, _("Could not reallocate labels\n"));
569                                 }
570                                 labels++;
571                                 ptr = thisarg (ptr);
572                                 if (strstr (ptr, "'") == ptr)
573                                         labels[nlabels - 1] = ptr + 1;
574                                 else
575                                         labels[nlabels - 1] = ptr;
576                         }
577                         break;
578                 case 'u':                                                                       /* units */
579                         units = optarg;
580                         nunits++;
581                         if (nunits >= unitv_size) {
582                                 unitv_size += 8;
583                                 unitv = realloc (unitv, unitv_size);
584                                 if (unitv == NULL)
585                                         die (STATE_UNKNOWN, _("Could not reallocate units [%d]\n"), nunits);
586                         }
587                         unitv[nunits - 1] = optarg;
588                         ptr = thisarg (optarg);
589                         unitv[nunits - 1] = ptr;
590                         if (strstr (ptr, "'") == ptr)
591                                 unitv[nunits - 1] = ptr + 1;
592                         while (ptr && (ptr = nextarg (ptr))) {
593                                 if (nunits >= unitv_size) {
594                                         unitv_size += 8;
595                                         unitv = realloc (unitv, unitv_size);
596                                         if (units == NULL)
597                                                 die (STATE_UNKNOWN, _("Could not realloc() units\n"));
598                                 }
599                                 nunits++;
600                                 ptr = thisarg (ptr);
601                                 if (strstr (ptr, "'") == ptr)
602                                         unitv[nunits - 1] = ptr + 1;
603                                 else
604                                         unitv[nunits - 1] = ptr;
605                         }
606                         break;
608                 }
609         }
611         if (server_address == NULL)
612                 server_address = argv[optind];
614         if (community == NULL)
615                 community = strdup (DEFAULT_COMMUNITY);
617         return validate_arguments ();
621 /******************************************************************************
623 @@-
624 <sect3>
625 <title>validate_arguments</title>
627 <para>&PROTO_validate_arguments;</para>
629 <para>Given a database name, this function returns TRUE if the string
630 is a valid PostgreSQL database name, and returns false if it is
631 not.</para>
633 <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
634 characters long and consist of letters, numbers, and underscores. The
635 first character cannot be a number, however.</para>
637 </sect3>
638 -@@
639 ******************************************************************************/
643 int
644 validate_arguments ()
647         /* Need better checks to verify seclevel and authproto choices */
648         
649         if (seclevel == NULL) 
650                 asprintf (&seclevel, "noAuthNoPriv");
653         if (authproto == NULL ) 
654                 asprintf(&authproto, DEFAULT_AUTH_PROTOCOL);
655         
656          
657         
658         if (proto == NULL || (strcmp(proto,DEFAULT_PROTOCOL) == 0) ) {        /* default protocol version */
659                 asprintf(&proto, DEFAULT_PROTOCOL);
660                 asprintf(&authpriv, "%s%s", "-c ", community);
661         }
662         else if ( strcmp (proto, "2c") == 0 ) {                 /* snmpv2c args */
663                 asprintf(&authpriv, "%s%s", "-c ", community);
664         }
665         else if ( strcmp (proto, "3") == 0 ) {                 /* snmpv3 args */
666                 asprintf(&proto, "%s", "3");
667                 
668                 if ( (strcmp(seclevel, "noAuthNoPriv") == 0) || seclevel == NULL ) {
669                         asprintf(&authpriv, "%s", "-l noAuthNoPriv" );
670                 }
671                 else if ( strcmp(seclevel, "authNoPriv") == 0 ) {
672                         if ( secname == NULL || authpasswd == NULL) {
673                                 printf (_("Missing secname (%s) or authpassword (%s) ! \n"),secname, authpasswd );
674                                 print_usage ();
675                                 exit (STATE_UNKNOWN);
676                         }
677                         asprintf(&authpriv, "-l authNoPriv -a %s -u %s -A %s ", authproto, secname, authpasswd);
678                 }
679                 else if ( strcmp(seclevel, "authPriv") == 0 ) {
680                         if ( secname == NULL || authpasswd == NULL || privpasswd == NULL ) {
681                                 printf (_("Missing secname (%s), authpassword (%s), or privpasswd (%s)! \n"),secname, authpasswd,privpasswd );
682                                 print_usage ();
683                                 exit (STATE_UNKNOWN);
684                         }
685                         asprintf(&authpriv, "-l authPriv -a %s -u %s -A %s -x DES -X %s ", authproto, secname, authpasswd, privpasswd);
686                 }
687                 
688         }
689         else {
690                 usage2 (_("Invalid SNMP version"), proto);
691         }
692                         
693         return OK;
698 char *
699 clarify_message (char *msg)
701         int i = 0;
702         int foo;
703         char tmpmsg_c[MAX_INPUT_BUFFER];
704         char *tmpmsg = (char *) &tmpmsg_c;
705         tmpmsg = strcpy (tmpmsg, msg);
706         if (!strncmp (tmpmsg, " Hex:", 5)) {
707                 tmpmsg = strtok (tmpmsg, ":");
708                 while ((tmpmsg = strtok (NULL, " "))) {
709                         foo = strtol (tmpmsg, NULL, 16);
710                         /* Translate chars that are not the same value in the printers
711                          * character set.
712                          */
713                         switch (foo) {
714                         case 208:
715                                 {
716                                         foo = 197;
717                                         break;
718                                 }
719                         case 216:
720                                 {
721                                         foo = 196;
722                                         break;
723                                 }
724                         }
725                         msg[i] = foo;
726                         i++;
727                 }
728                 msg[i] = 0;
729         }
730         return (msg);
735 int
736 check_num (int i)
738         int result;
739         result = STATE_OK;
740         if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
741                         lower_warn_lim[i] > upper_warn_lim[i]) {
742                 if (response_value[i] <= lower_warn_lim[i] &&
743                                 response_value[i] >= upper_warn_lim[i]) {
744                         result = STATE_WARNING;
745                 }
746         }
747         else if
748                 ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
749                  (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
750                  (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
751                  (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
752                  (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
753                  (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
754                 result = STATE_WARNING;
755         }
757         if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
758                         lower_crit_lim[i] > upper_crit_lim[i]) {
759                 if (response_value[i] <= lower_crit_lim[i] &&
760                                 response_value[i] >= upper_crit_lim[i]) {
761                         result = STATE_CRITICAL;
762                 }
763         }
764         else if
765                 ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
766                  (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
767                  (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
768                  (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
769                  (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
770                  (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
771                 result = STATE_CRITICAL;
772         }
774         return result;
779 int
780 lu_getll (unsigned long *ll, char *str)
782         char tmp[100];
783         if (strchr (str, ':') == NULL)
784                 return 0;
785         if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
786                 return 0;
787         if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
788                 return 1;
789         return 0;
794 int
795 lu_getul (unsigned long *ul, char *str)
797         char tmp[100];
798         if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
799                 return 1;
800         if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
801                 return 1;
802         if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
803                 return 1;
804         return 0;
809 /* trim leading whitespace
810          if there is a leading quote, make sure it balances */
812 char *
813 thisarg (char *str)
815         str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
816         if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
817                 if (strlen (str) == 1 || !strstr (str + 1, "'"))
818                         die (STATE_UNKNOWN, _("Unbalanced quotes\n"));
819         }
820         return str;
825 /* if there's a leading quote, advance to the trailing quote
826          set the trailing quote to '\x0'
827          if the string continues, advance beyond the comma */
829 char *
830 nextarg (char *str)
832         if (strstr (str, "'") == str) {
833                 str[0] = 0;
834                 if (strlen (str) > 1) {
835                         str = strstr (str + 1, "'");
836                         return (++str);
837                 }
838                 else {
839                         return NULL;
840                 }
841         }
842         if (strstr (str, ",") == str) {
843                 str[0] = 0;
844                 if (strlen (str) > 1) {
845                         return (++str);
846                 }
847                 else {
848                         return NULL;
849                 }
850         }
851         if ((str = strstr (str, ",")) && strlen (str) > 1) {
852                 str[0] = 0;
853                 return (++str);
854         }
855         return NULL;
860 void
861 print_help (void)
863         print_revision (progname, revision);
865         printf (COPYRIGHT, copyright, email);
867         printf (_("\
868 Check status of remote machines and obtain sustem information via SNMP\n\n"));
870         print_usage ();
872         printf (_(UT_HELP_VRSN));
874         printf (_(UT_HOST_PORT), 'p', DEFAULT_PORT);
876         /* SNMP and Authentication Protocol */
877         printf (_("\
878  -P, --protocol=[1|2c|3]\n\
879     SNMP protocol version\n\
880  -L, --seclevel=[noAuthNoPriv|authNoPriv|authPriv]\n\
881     SNMPv3 securityLevel\n\
882  -a, --authproto=[MD5|SHA]\n\
883     SNMPv3 auth proto\n"));
885         /* Authentication Tokens*/
886         printf (_("\
887  -C, --community=STRING\n\
888     Optional community string for SNMP communication\n\
889     (default is \"%s\")\n\
890  -U, --secname=USERNAME\n\
891     SNMPv3 username\n\
892  -A, --authpassword=PASSWORD\n\
893     SNMPv3 authentication password\n\
894  -X, --privpasswd=PASSWORD\n\
895     SNMPv3 crypt passwd (DES)\n"), DEFAULT_COMMUNITY);
897         /* OID Stuff */
898         printf (_("\
899  -o, --oid=OID(s)\n\
900     Object identifier(s) whose value you wish to query\n\
901  -m, --miblist=STRING\n\
902     List of MIBS to be loaded (default = ALL)\n -d, --delimiter=STRING\n\
903     Delimiter to use when parsing returned data. Default is \"%s\"\n\
904     Any data on the right hand side of the delimiter is considered\n\
905     to be the data that should be used in the evaluation.\n"), DEFAULT_DELIMITER);
907         /* Tests Against Integers */
908         printf (_("\
909  -w, --warning=INTEGER_RANGE(s)\n\
910     Range(s) which will not result in a WARNING status\n\
911  -c, --critical=INTEGER_RANGE(s)\n\
912     Range(s) which will not result in a CRITICAL status\n"));
914         /* Tests Against Strings */
915         printf (_("\
916  -s, --string=STRING\n\
917     Return OK state (for that OID) if STRING is an exact match\n\
918  -r, --ereg=REGEX\n\
919     Return OK state (for that OID) if extended regular expression REGEX matches\n\
920  -R, --eregi=REGEX\n\
921     Return OK state (for that OID) if case-insensitive extended REGEX matches\n\
922  -l, --label=STRING\n\
923     Prefix label for output from plugin (default -s 'SNMP')\n"));
925         /* Output Formatting */
926         printf (_("\
927  -u, --units=STRING\n\
928     Units label(s) for output data (e.g., 'sec.').\n\
929  -D, --output-delimiter=STRING\n\
930     Separates output on multiple OID requests\n"));
932         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
934         printf (_(UT_VERBOSE));
936         printf (_("\n\
937 - This plugin uses the 'snmpget' command included with the NET-SNMP package.\n\
938   If you don't have the package installed, you will need to download it from\n\
939   http://net-snmp.sourceforge.net before you can use this plugin.\n"));
941         printf (_("\
942 - Multiple OIDs may be indicated by a comma- or space-delimited list (lists with\n\
943   internal spaces must be quoted) [max 8 OIDs]\n"));
945         printf (_("\
946 - Ranges are inclusive and are indicated with colons. When specified as\n\
947   'min:max' a STATE_OK will be returned if the result is within the indicated\n\
948   range or is equal to the upper or lower bound. A non-OK state will be\n\
949   returned if the result is outside the specified range.\n"));
951         printf (_("\
952 - If specified in the order 'max:min' a non-OK state will be returned if the\n\
953   result is within the (inclusive) range.\n"));
955         printf (_("\
956 - Upper or lower bounds may be omitted to skip checking the respective limit.\n\
957 - Bare integers are interpreted as upper limits.\n\
958 - When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'\n\
959 - Note that only one string and one regex may be checked at present\n\
960 - All evaluation methods other than PR, STR, and SUBSTR expect that the value\n\
961   returned from the SNMP query is an unsigned integer.\n"));
963         printf (_(UT_SUPPORT));
968 void
969 print_usage (void)
971         printf ("\
972 Usage: %s -H <ip_address> -o <OID> [-w warn_range] [-c crit_range] \n\
973                   [-C community] [-s string] [-r regex] [-R regexi]\n\
974                   [-t timeout] [-e retries]\n\
975                   [-l label] [-u units] [-p port-number] [-d delimiter]\n\
976                   [-D output-delimiter] [-m miblist] [-P snmp version]\n\
977                   [-L seclevel] [-U secname] [-a authproto] [-A authpasswd]\n\
978                   [-X privpasswd]\n", progname);