Code

* bugfix: snprintf of timestamp truncated '\0'
[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 ******************************************************************************/
19 const char *progname = "check_snmp";
20 const char *revision = "$Revision$";
21 const char *copyright = "1999-2003";
22 const char *email = "nagiosplug-devel@lists.sourceforge.net";
24 #include "common.h"
25 #include "utils.h"
26 #include "popen.h"
28 #define DEFAULT_COMMUNITY "public"
29 #define DEFAULT_PORT "161"
30 #define DEFAULT_MIBLIST "ALL"
31 #define DEFAULT_PROTOCOL "1"
32 #define DEFAULT_AUTH_PROTOCOL "MD5"
33 #define DEFAULT_DELIMITER "="
34 #define DEFAULT_OUTPUT_DELIMITER " "
36 #define mark(a) ((a)!=0?"*":"")
38 #define CHECK_UNDEF 0
39 #define CRIT_PRESENT 1
40 #define CRIT_STRING 2
41 #define CRIT_REGEX 4
42 #define CRIT_GT 8
43 #define CRIT_LT 16
44 #define CRIT_GE 32
45 #define CRIT_LE 64
46 #define CRIT_EQ 128
47 #define CRIT_NE 256
48 #define CRIT_RANGE 512
49 #define WARN_PRESENT 1024
50 #define WARN_STRING 2048
51 #define WARN_REGEX 4096
52 #define WARN_GT 8192
53 #define WARN_LT 16384
54 #define WARN_GE 32768
55 #define WARN_LE 65536
56 #define WARN_EQ 131072
57 #define WARN_NE 262144
58 #define WARN_RANGE 524288
60 #define MAX_OIDS 8
61 #define MAX_DELIM_LENGTH 8
63 int process_arguments (int, char **);
64 int validate_arguments (void);
65 char *clarify_message (char *);
66 int check_num (int);
67 int lu_getll (unsigned long *, char *);
68 int lu_getul (unsigned long *, char *);
69 char *thisarg (char *str);
70 char *nextarg (char *str);
71 void print_usage (void);
72 void print_help (void);
74 #ifdef HAVE_REGEX_H
75 #include <regex.h>
76 char regex_expect[MAX_INPUT_BUFFER] = "";
77 regex_t preg;
78 regmatch_t pmatch[10];
79 char timestamp[10] = "";
80 char errbuf[MAX_INPUT_BUFFER];
81 int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
82 int eflags = 0;
83 int errcode, excode;
84 #endif
86 char *server_address = NULL;
87 char *community = NULL;
88 char *authpriv = NULL;
89 char *proto = NULL;
90 char *seclevel = NULL;
91 char *secname = NULL;
92 char *authproto = NULL;
93 char *authpasswd = NULL;
94 char *privpasswd = NULL;
95 char *oid;
96 char *label;
97 char *units;
98 char *port;
99 char string_value[MAX_INPUT_BUFFER] = "";
100 char **labels = NULL;
101 char **unitv = NULL;
102 size_t nlabels = 0;
103 size_t labels_size = 8;
104 size_t nunits = 0;
105 size_t unitv_size = 8;
106 int verbose = FALSE;
107 unsigned long lower_warn_lim[MAX_OIDS];
108 unsigned long upper_warn_lim[MAX_OIDS];
109 unsigned long lower_crit_lim[MAX_OIDS];
110 unsigned long upper_crit_lim[MAX_OIDS];
111 unsigned long response_value[MAX_OIDS];
112 int check_warning_value = FALSE;
113 int check_critical_value = FALSE;
114 int eval_method[MAX_OIDS];
115 char *delimiter;
116 char *output_delim;
117 char *miblist;
123 \f
124 int
125 main (int argc, char **argv)
127         int i = 0;
128         int iresult = STATE_UNKNOWN;
129         int found = 0;
130         int result = STATE_DEPENDENT;
131         char input_buffer[MAX_INPUT_BUFFER];
132         char *command_line = NULL;
133         char *response = NULL;
134         char *outbuff;
135         char *output;
136         char *ptr = NULL;
137         char *p2 = NULL;
138         char *show = NULL;
140         labels = malloc (labels_size);
141         unitv = malloc (unitv_size);
142         for (i = 0; i < MAX_OIDS; i++)
143                 eval_method[i] = CHECK_UNDEF;
144         i = 0;
146         oid = strdup ("");
147         label = strdup ("SNMP");
148         units = strdup ("");
149         port = strdup (DEFAULT_PORT);
150         outbuff = strdup ("");
151         output = strdup ("");
152         delimiter = strdup (DEFAULT_DELIMITER);
153         output_delim = strdup (DEFAULT_OUTPUT_DELIMITER);
154         miblist = strdup (DEFAULT_MIBLIST);
156         if (process_arguments (argc, argv) == ERROR)
157                 usage (_("Incorrect arguments supplied\n"));
159         /* create the command line to execute */
160         asprintf (&command_line, "%s -t 1 -r %d -m %s -v %s %s %s:%s %s",
161                   PATH_TO_SNMPGET, timeout_interval - 1, miblist, proto,
162                   authpriv, server_address, port, oid);
163         if (verbose)
164                 printf ("%s\n", command_line);
166         /* run the command */
167         child_process = spopen (command_line);
168         if (child_process == NULL) {
169                 printf (_("Could not open pipe: %s\n"), command_line);
170                 exit (STATE_UNKNOWN);
171         }
173         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
174         if (child_stderr == NULL) {
175                 printf (_("Could not open stderr for %s\n"), command_line);
176         }
178         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
179                 asprintf (&output, "%s%s", output, input_buffer);
181         if (verbose)
182                 printf ("%s\n", output);
184         ptr = output;
186         while (ptr) {
188                 ptr = strstr (ptr, delimiter);
189                 if (ptr == NULL)
190                         break;
192                 ptr += strlen (delimiter);
193                 ptr += strspn (ptr, " ");
195                 found++;
197                 if (ptr[0] == '"') {
198                         ptr++;
199                         response = strpcpy (response, ptr, "\"");
200                         ptr = strpbrk (ptr, "\"");
201                         ptr += strspn (ptr, "\"\n");
202                 }
203                 else {
204                         response = strpcpy (response, ptr, "\n");
205                         ptr = strpbrk (ptr, "\n");
206                         ptr += strspn (ptr, "\n");
207                         while
208                                 (strstr (ptr, delimiter) &&
209                                  strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter)) {
210                                 response = strpcat (response, ptr, "\n");
211                                 ptr = strpbrk (ptr, "\n");
212                         }
213                         if (ptr && strstr (ptr, delimiter) == NULL) {
214                                 asprintf (&response, "%s%s", response, ptr);
215                                 ptr = NULL;
216                         }
217                 }
219                 /* We strip out the datatype indicator for PHBs */
220                 if (strstr (response, "Gauge: "))
221                         show = strstr (response, "Gauge: ") + 7;
222                 else if (strstr (response, "Gauge32: "))
223                         show = strstr (response, "Gauge32: ") + 9;
224                 else if (strstr (response, "Counter32: "))
225                         show = strstr (response, "Counter32: ") + 11;
226                 else if (strstr (response, "INTEGER: "))
227                         show = strstr (response, "INTEGER: ") + 9;
228                 else if (strstr (response, "STRING: "))
229                         show = strstr (response, "STRING: ") + 8;
230                 else
231                         show = response;
232                 p2 = show;
234                 iresult = STATE_DEPENDENT;
236                 /* Process this block for integer comparisons */
237                 if (eval_method[i] & CRIT_GT ||
238                     eval_method[i] & CRIT_LT ||
239                     eval_method[i] & CRIT_GE ||
240                     eval_method[i] & CRIT_LE ||
241                     eval_method[i] & CRIT_EQ ||
242                     eval_method[i] & CRIT_NE ||
243                     eval_method[i] & WARN_GT ||
244                     eval_method[i] & WARN_LT ||
245                     eval_method[i] & WARN_GE ||
246                     eval_method[i] & WARN_LE ||
247                     eval_method[i] & WARN_EQ ||
248                     eval_method[i] & WARN_NE) {
249                         p2 = strpbrk (p2, "0123456789");
250                         if (p2 == NULL) 
251                                 die (STATE_UNKNOWN,"No valid data returned");
252                         response_value[i] = strtoul (p2, NULL, 10);
253                         iresult = check_num (i);
254                         asprintf (&show, "%lu", response_value[i]);
255                 }
257                 /* Process this block for string matching */
258                 else if (eval_method[i] & CRIT_STRING) {
259                         if (strcmp (response, string_value))
260                                 iresult = STATE_CRITICAL;
261                         else
262                                 iresult = STATE_OK;
263                 }
265                 /* Process this block for regex matching */
266                 else if (eval_method[i] & CRIT_REGEX) {
267 #ifdef HAVE_REGEX_H
268                         excode = regexec (&preg, response, 10, pmatch, eflags);
269                         if (excode == 0) {
270                                 iresult = STATE_OK;
271                         }
272                         else if (excode != REG_NOMATCH) {
273                                 regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
274                                 printf (_("Execute Error: %s\n"), errbuf);
275                                 exit (STATE_CRITICAL);
276                         }
277                         else {
278                                 iresult = STATE_CRITICAL;
279                         }
280 #else
281                         printf (_("%s UNKNOWN: call for regex which was not a compiled option"), label);
282                         exit (STATE_UNKNOWN);
283 #endif
284                 }
286                 /* Process this block for existence-nonexistence checks */
287                 else {
288                         if (eval_method[i] & CRIT_PRESENT)
289                                 iresult = STATE_CRITICAL;
290                         else if (eval_method[i] & WARN_PRESENT)
291                                 iresult = STATE_WARNING;
292                         else if (response && iresult == STATE_DEPENDENT) 
293                                 iresult = STATE_OK;
294                 }
296                 /* Result is the worst outcome of all the OIDs tested */
297                 result = max_state (result, iresult);
299                 /* Prepend a label for this OID if there is one */
300                 if (nlabels > (size_t)1 && (size_t)i < nlabels && labels[i] != NULL)
301                         asprintf (&outbuff, "%s%s%s %s%s%s", outbuff,
302                                   (i == 0) ? " " : output_delim,
303                                   labels[i], mark (iresult), show, mark (iresult));
304                 else
305                         asprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim,
306                                   mark (iresult), show, mark (iresult));
308                 /* Append a unit string for this OID if there is one */
309                 if (nunits > (size_t)0 && (size_t)i < nunits && unitv[i] != NULL)
310                         asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
312                 i++;
314         }                                                                                                                       /* end while (ptr) */
316         if (found == 0)
317                 die (STATE_UNKNOWN,
318                      _("%s problem - No data recieved from host\nCMD: %s\n"),
319                      label,
320                      command_line);
322         /* WARNING if output found on stderr */
323         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
324                 result = max_state (result, STATE_WARNING);
326         /* close stderr */
327         (void) fclose (child_stderr);
329         /* close the pipe */
330         if (spclose (child_process))
331                 result = max_state (result, STATE_WARNING);
333 /*      if (nunits == 1 || i == 1) */
334 /*              printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units); */
335 /*      else */
336         printf ("%s %s -%s\n", label, state_text (result), outbuff);
338         return result;
345 \f
346 /* process command-line arguments */
347 int
348 process_arguments (int argc, char **argv)
350         char *ptr;
351         int c = 1;
352         int j = 0, jj = 0, ii = 0;
354         int option = 0;
355         static struct option longopts[] = {
356                 STD_LONG_OPTS,
357                 {"community", required_argument, 0, 'C'},
358                 {"oid", required_argument, 0, 'o'},
359                 {"object", required_argument, 0, 'o'},
360                 {"delimiter", required_argument, 0, 'd'},
361                 {"output-delimiter", required_argument, 0, 'D'},
362                 {"string", required_argument, 0, 's'},
363                 {"regex", required_argument, 0, 'r'},
364                 {"ereg", required_argument, 0, 'r'},
365                 {"eregi", required_argument, 0, 'R'},
366                 {"label", required_argument, 0, 'l'},
367                 {"units", required_argument, 0, 'u'},
368                 {"port", required_argument, 0, 'p'},
369                 {"miblist", required_argument, 0, 'm'},
370                 {"protocol", required_argument, 0, 'P'},
371                 {"seclevel", required_argument, 0, 'L'},
372                 {"secname", required_argument, 0, 'U'},
373                 {"authproto", required_argument, 0, 'a'},
374                 {"authpasswd", required_argument, 0, 'A'},
375                 {"privpasswd", required_argument, 0, 'X'},
376                 {0, 0, 0, 0}
377         };
379         if (argc < 2)
380                 return ERROR;
382         /* reverse compatibility for very old non-POSIX usage forms */
383         for (c = 1; c < argc; c++) {
384                 if (strcmp ("-to", argv[c]) == 0)
385                         strcpy (argv[c], "-t");
386                 if (strcmp ("-wv", argv[c]) == 0)
387                         strcpy (argv[c], "-w");
388                 if (strcmp ("-cv", argv[c]) == 0)
389                         strcpy (argv[c], "-c");
390         }
392         while (1) {
393                 c = getopt_long (argc, argv, "hvVt:c:w:H:C:o:e:E:d:D:s:R:r:l:u:p:m:P:L:U:a:A:X:",
394                                                                          longopts, &option);
396                 if (c == -1 || c == EOF)
397                         break;
399                 switch (c) {
400                 case '?':       /* usage */
401                         usage3 ("Unknown argument", optopt);
402                 case 'h':       /* help */
403                         print_help ();
404                         exit (STATE_OK); 
405                 case 'V':       /* version */
406                         print_revision (progname, revision);
407                         exit (STATE_OK);
408                 case 'v': /* verbose */
409                         verbose = TRUE;
410                         break;
412         /* Connection info */
413                 case 'C':                                                                       /* group or community */
414                         community = optarg;
415                         break;
416                 case 'H':                                                                       /* Host or server */
417                         server_address = optarg;
418                         break;
419                 case 'p':       /* TCP port number */
420                         port = optarg;
421                         break;
422                 case 'm':      /* List of MIBS  */
423                         miblist = optarg;
424                         break;
425                 case 'P':     /* SNMP protocol version */
426                         proto = optarg;
427                         break;
428                 case 'L':     /* security level */
429                         seclevel = optarg;
430                         break;
431                 case 'U':     /* security username */
432                         secname = optarg;
433                         break;
434                 case 'a':     /* auth protocol */
435                         authproto = optarg;
436                         break;
437                 case 'A':     /* auth passwd */
438                         authpasswd = optarg;
439                         break;
440                 case 'X':     /* priv passwd */
441                         privpasswd = optarg;
442                         break;
443                 case 't':       /* timeout period */
444                         if (!is_integer (optarg))
445                                 usage2 (_("Timeout Interval must be an integer"), optarg);
446                         timeout_interval = atoi (optarg);
447                         break;
449         /* Test parameters */
450                 case 'c':                                                                       /* critical time threshold */
451                         if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
452                                 printf (_("Invalid critical threshold: %s\n"), optarg);
453                                 print_usage ();
454                                 exit (STATE_UNKNOWN);
455                         }
456                         for (ptr = optarg; ptr && jj < MAX_OIDS; jj++) {
457                                 if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
458                                         eval_method[jj] |= CRIT_LT;
459                                 if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
460                                         eval_method[jj] |= CRIT_GT;
461                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
462                         }
463                         break;
464                 case 'w':                                                                       /* warning time threshold */
465                         if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
466                                 printf (_("Invalid warning threshold: %s\n"), optarg);
467                                 print_usage ();
468                                 exit (STATE_UNKNOWN);
469                         }
470                         for (ptr = optarg; ptr && ii < MAX_OIDS; ii++) {
471                                 if (lu_getll (&lower_warn_lim[ii], ptr) == 1)
472                                         eval_method[ii] |= WARN_LT;
473                                 if (lu_getul (&upper_warn_lim[ii], ptr) == 1)
474                                         eval_method[ii] |= WARN_GT;
475                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
476                         }
477                         break;
478                 case 'o':                                                                       /* object identifier */
479                 case 'e': /* PRELIMINARY - may change */
480                 case 'E': /* PRELIMINARY - may change */
481                         for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
482                                 ptr[0] = ' '; /* relpace comma with space */
483                         for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
484                                 j++; /* count OIDs */
485                         asprintf (&oid, "%s %s", (oid?oid:""), optarg);
486                         if (c == 'E' || c == 'e') {
487                                 jj++;
488                                 ii++;
489                         }
490                         if (c == 'E') 
491                                 eval_method[j+1] |= WARN_PRESENT;
492                         else if (c == 'e')
493                                 eval_method[j+1] |= CRIT_PRESENT;
494                         break;
495                 case 's':                                                                       /* string or substring */
496                         strncpy (string_value, optarg, sizeof (string_value) - 1);
497                         string_value[sizeof (string_value) - 1] = 0;
498                         eval_method[jj++] = CRIT_STRING;
499                         ii++;
500                         break;
501                 case 'R':                                                                       /* regex */
502 #ifdef HAVE_REGEX_H
503                         cflags = REG_ICASE;
504 #endif
505                 case 'r':                                                                       /* regex */
506 #ifdef HAVE_REGEX_H
507                         cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
508                         strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
509                         regex_expect[sizeof (regex_expect) - 1] = 0;
510                         errcode = regcomp (&preg, regex_expect, cflags);
511                         if (errcode != 0) {
512                                 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
513                                 printf (_("Could Not Compile Regular Expression"));
514                                 return ERROR;
515                         }
516                         eval_method[jj++] = CRIT_REGEX;
517                         ii++;
518 #else
519                         printf (_("%s UNKNOWN: call for regex which was not a compiled option"), label);
520                         exit (STATE_UNKNOWN);
521 #endif
522                         break;
524         /* Format */
525                 case 'd':                                                                       /* delimiter */
526                         delimiter = strscpy (delimiter, optarg);
527                         break;
528                 case 'D':                                                                       /* output-delimiter */
529                         output_delim = strscpy (output_delim, optarg);
530                         break;
531                 case 'l':                                                                       /* label */
532                         label = optarg;
533                         nlabels++;
534                         if (nlabels >= labels_size) {
535                                 labels_size += 8;
536                                 labels = realloc (labels, labels_size);
537                                 if (labels == NULL)
538                                         die (STATE_UNKNOWN,
539                                                                                  _("Could not realloc() labels[%d]"), nlabels);
540                         }
541                         labels[nlabels - 1] = optarg;
542                         ptr = thisarg (optarg);
543                         if (strstr (ptr, "'") == ptr)
544                                 labels[nlabels - 1] = ptr + 1;
545                         else
546                                 labels[nlabels - 1] = ptr;
547                         while (ptr && (ptr = nextarg (ptr))) {
548                                 if (nlabels >= labels_size) {
549                                         labels_size += 8;
550                                         labels = realloc (labels, labels_size);
551                                         if (labels == NULL)
552                                                 die (STATE_UNKNOWN, _("Could not realloc() labels\n"));
553                                 }
554                                 labels++;
555                                 ptr = thisarg (ptr);
556                                 if (strstr (ptr, "'") == ptr)
557                                         labels[nlabels - 1] = ptr + 1;
558                                 else
559                                         labels[nlabels - 1] = ptr;
560                         }
561                         break;
562                 case 'u':                                                                       /* units */
563                         units = optarg;
564                         nunits++;
565                         if (nunits >= unitv_size) {
566                                 unitv_size += 8;
567                                 unitv = realloc (unitv, unitv_size);
568                                 if (unitv == NULL)
569                                         die (STATE_UNKNOWN,
570                                                                                  _("Could not realloc() units [%d]\n"), nunits);
571                         }
572                         unitv[nunits - 1] = optarg;
573                         ptr = thisarg (optarg);
574                         if (strstr (ptr, "'") == ptr)
575                                 unitv[nunits - 1] = ptr + 1;
576                         else
577                                 unitv[nunits - 1] = ptr;
578                         while (ptr && (ptr = nextarg (ptr))) {
579                                 if (nunits >= unitv_size) {
580                                         unitv_size += 8;
581                                         unitv = realloc (unitv, unitv_size);
582                                         if (units == NULL)
583                                                 die (STATE_UNKNOWN, _("Could not realloc() units\n"));
584                                 }
585                                 nunits++;
586                                 ptr = thisarg (ptr);
587                                 if (strstr (ptr, "'") == ptr)
588                                         unitv[nunits - 1] = ptr + 1;
589                                 else
590                                         unitv[nunits - 1] = ptr;
591                         }
592                         break;
594                 }
595         }
597         if (server_address == NULL)
598                 server_address = argv[optind];
600         if (community == NULL)
601                 community = strdup (DEFAULT_COMMUNITY);
603         return validate_arguments ();
609 /******************************************************************************
611 @@-
612 <sect3>
613 <title>validate_arguments</title>
615 <para>&PROTO_validate_arguments;</para>
617 <para>Given a database name, this function returns TRUE if the string
618 is a valid PostgreSQL database name, and returns false if it is
619 not.</para>
621 <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
622 characters long and consist of letters, numbers, and underscores. The
623 first character cannot be a number, however.</para>
625 </sect3>
626 -@@
627 ******************************************************************************/
629 int
630 validate_arguments ()
633         /* Need better checks to verify seclevel and authproto choices */
634         
635         if (seclevel == NULL) 
636                 asprintf (&seclevel, "noAuthNoPriv");
639         if (authproto == NULL ) 
640                 asprintf(&authproto, DEFAULT_AUTH_PROTOCOL);
641         
642          
643         
644         if (proto == NULL || (strcmp(proto,DEFAULT_PROTOCOL) == 0) ) {        /* default protocol version */
645                 asprintf(&proto, DEFAULT_PROTOCOL);
646                 asprintf(&authpriv, "%s%s", "-c ", community);
647         }
648         else if ( strcmp (proto, "3") == 0 ) {                 /* snmpv3 args */
649                 asprintf(&proto, "%s", "3");
650                 
651                 if ( (strcmp(seclevel, "noAuthNoPriv") == 0) || seclevel == NULL ) {
652                         asprintf(&authpriv, "%s", "-l noAuthNoPriv" );
653                 }
654                 else if ( strcmp(seclevel, "authNoPriv") == 0 ) {
655                         if ( secname == NULL || authpasswd == NULL) {
656                                 printf (_("Missing secname (%s) or authpassword (%s) ! \n)"),secname, authpasswd );
657                                 print_usage ();
658                                 exit (STATE_UNKNOWN);
659                         }
660                         asprintf(&authpriv, "-l authNoPriv -a %s -u %s -A %s ", authproto, secname, authpasswd);
661                 }
662                 else if ( strcmp(seclevel, "authPriv") == 0 ) {
663                         if ( secname == NULL || authpasswd == NULL || privpasswd == NULL ) {
664                                 printf (("Missing secname (%s), authpassword (%s), or privpasswd (%s)! \n"),secname, authpasswd,privpasswd );
665                                 print_usage ();
666                                 exit (STATE_UNKNOWN);
667                         }
668                         asprintf(&authpriv, "-l authPriv -a %s -u %s -A %s -x DES -X %s ", authproto, secname, authpasswd, privpasswd);
669                 }
670                 
671                                                                         
672         }
673         else {
674                 printf (_("Invalid SNMP version: %s\n"), proto);
675                 print_usage ();
676                 exit (STATE_UNKNOWN);                           
677         }
678                         
679         
680         
682         return OK;
689 \f
690 char *
691 clarify_message (char *msg)
693         int i = 0;
694         int foo;
695         char tmpmsg_c[MAX_INPUT_BUFFER];
696         char *tmpmsg = (char *) &tmpmsg_c;
697         tmpmsg = strcpy (tmpmsg, msg);
698         if (!strncmp (tmpmsg, " Hex:", 5)) {
699                 tmpmsg = strtok (tmpmsg, ":");
700                 while ((tmpmsg = strtok (NULL, " "))) {
701                         foo = strtol (tmpmsg, NULL, 16);
702                         /* Translate chars that are not the same value in the printers
703                          * character set.
704                          */
705                         switch (foo) {
706                         case 208:
707                                 {
708                                         foo = 197;
709                                         break;
710                                 }
711                         case 216:
712                                 {
713                                         foo = 196;
714                                         break;
715                                 }
716                         }
717                         msg[i] = foo;
718                         i++;
719                 }
720                 msg[i] = 0;
721         }
722         return (msg);
728 int
729 check_num (int i)
731         int result;
732         result = STATE_OK;
733         if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
734                         lower_warn_lim[i] > upper_warn_lim[i]) {
735                 if (response_value[i] <= lower_warn_lim[i] &&
736                                 response_value[i] >= upper_warn_lim[i]) {
737                         result = STATE_WARNING;
738                 }
739         }
740         else if
741                 ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
742                  (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
743                  (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
744                  (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
745                  (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
746                  (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
747                 result = STATE_WARNING;
748         }
750         if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
751                         lower_warn_lim[i] > upper_warn_lim[i]) {
752                 if (response_value[i] <= lower_crit_lim[i] &&
753                                 response_value[i] >= upper_crit_lim[i]) {
754                         result = STATE_CRITICAL;
755                 }
756         }
757         else if
758                 ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
759                  (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
760                  (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
761                  (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
762                  (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
763                  (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
764                 result = STATE_CRITICAL;
765         }
767         return result;
773 int
774 lu_getll (unsigned long *ll, char *str)
776         char tmp[100];
777         if (strchr (str, ':') == NULL)
778                 return 0;
779         if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
780                 return 0;
781         if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
782                 return 1;
783         return 0;
789 int
790 lu_getul (unsigned long *ul, char *str)
792         char tmp[100];
793         if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
794                 return 1;
795         if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
796                 return 1;
797         if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
798                 return 1;
799         return 0;
805 /* trim leading whitespace
806          if there is a leading quote, make sure it balances */
808 char *
809 thisarg (char *str)
811         str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
812         if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
813                 if (strlen (str) == 1 || !strstr (str + 1, "'"))
814                         die (STATE_UNKNOWN, "Unbalanced quotes\n");
815         }
816         return str;
822 /* if there's a leading quote, advance to the trailing quote
823          set the trailing quote to '\x0'
824          if the string continues, advance beyond the comma */
826 char *
827 nextarg (char *str)
829         if (strstr (str, "'") == str) {
830                 if (strlen (str) > 1) {
831                         str = strstr (str + 1, "'");
832                         str[0] = 0;
833                         return (++str);
834                 }
835                 else {
836                         str[0] = 0;
837                         return NULL;
838                 }
839         }
840         if (strstr (str, ",") == str) {
841                 if (strlen (str) > 1) {
842                         str[0] = 0;
843                         return (++str);
844                 }
845                 else {
846                         str[0] = 0;
847                         return NULL;
848                 }
849         }
850         if ((str = strstr (str, ",")) && strlen (str) > 1) {
851                 str[0] = 0;
852                 return (++str);
853         }
854         return NULL;
861 \f
862 void
863 print_help (void)
865         print_revision (progname, revision);
867         printf (_(COPYRIGHT), copyright, email);
869         printf (_("\
870 Check status of remote machines and obtain sustem information via SNMP\n\n"));
872         print_usage ();
874         printf (_(UT_HELP_VRSN));
876         printf (_(UT_HOST_PORT), 'p', DEFAULT_PORT);
878         /* SNMP and Authentication Protocol */
879         printf (_("\
880  -P, --protocol=[1|3]\n\
881     SNMP protocol version\n\
882  -L, --seclevel=[noAuthNoPriv|authNoPriv|authPriv]\n\
883     SNMPv3 securityLevel\n\
884  -a, --authproto=[MD5|SHA]\n\
885     SNMPv3 auth proto\n"));
887         /* Authentication Tokens*/
888         printf (_("\
889  -C, --community=STRING\n\
890     Optional community string for SNMP communication\n\
891     (default is \"%s\")\n\
892  -U, --secname=USERNAME\n\
893     SNMPv3 username\n\
894  -A, --authpassword=PASSWORD\n\
895     SNMPv3 authentication password\n\
896  -X, --privpasswd=PASSWORD\n\
897     SNMPv3 crypt passwd (DES)\n"), DEFAULT_COMMUNITY);
899         /* OID Stuff */
900         printf (_("\
901  -o, --oid=OID(s)\n\
902     Object identifier(s) whose value you wish to query\n\
903  -m, --miblist=STRING\n\
904     List of MIBS to be loaded (default = ALL)\n -d, --delimiter=STRING\n\
905     Delimiter to use when parsing returned data. Default is \"%s\"\n\
906     Any data on the right hand side of the delimiter is considered\n\
907     to be the data that should be used in the evaluation.\n"), DEFAULT_DELIMITER);
909         /* Tests Against Integers */
910         printf (_("\
911  -w, --warning=INTEGER_RANGE(s)\n\
912     Range(s) which will not result in a WARNING status\n\
913  -c, --critical=INTEGER_RANGE(s)\n\
914     Range(s) which will not result in a CRITICAL status\n"));
916         /* Tests Against Strings */
917         printf (_("\
918  -s, --string=STRING\n\
919     Return OK state (for that OID) if STRING is an exact match\n\
920  -r, --ereg=REGEX\n\
921     Return OK state (for that OID) if extended regular expression REGEX matches\n\
922  -R, --eregi=REGEX\n\
923     Return OK state (for that OID) if case-insensitive extended REGEX matches\n\
924  -l, --label=STRING\n\
925     Prefix label for output from plugin (default -s 'SNMP')\n"));
927         /* Output Formatting */
928         printf (_("\
929  -u, --units=STRING\n\
930     Units label(s) for output data (e.g., 'sec.').\n\
931  -D, --output-delimiter=STRING\n\
932     Separates output on multiple OID requests\n"));
934         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
936         printf (_(UT_VERBOSE));
938         printf (_("\n\
939 - This plugin uses the 'snmpget' command included with the NET-SNMP package.\n\
940   If you don't have the package installed, you will need to download it from\n\
941   http://net-snmp.sourceforge.net before you can use this plugin.\n"));
943         printf (_("\
944 - Multiple OIDs may be indicated by a comma- or space-delimited list (lists with\n\
945   internal spaces must be quoted) [max 8 OIDs]\n"));
947         printf (_("\
948 - Ranges are inclusive and are indicated with colons. When specified as\n\
949   'min:max' a STATE_OK will be returned if the result is within the indicated\n\
950   range or is equal to the upper or lower bound. A non-OK state will be\n\
951   returned if the result is outside the specified range.\n"));
953         printf (_("\
954 - If specified in the order 'max:min' a non-OK state will be returned if the\n\
955   result is within the (inclusive) range.\n"));
957         printf (_("\
958 - Upper or lower bounds may be omitted to skip checking the respective limit.\n\
959 - Bare integers are interpreted as upper limits.\n\
960 - When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'\n\
961 - Note that only one string and one regex may be checked at present\n\
962 - All evaluation methods other than PR, STR, and SUBSTR expect that the value\n\
963   returned from the SNMP query is an unsigned integer.\n"));
965         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] [-t timeout]\n\
974   [-l label] [-u units] [-p port-number] [-d delimiter]\n\
975   [-D output-delimiter] [-m miblist] [-P snmp version]\n\
976   [-L seclevel] [-U secname] [-a authproto] [-A authpasswd]\n\
977   [-X privpasswd]\n"), progname);
978         printf (_(UT_HLP_VRS), progname, progname);