Code

Making messages more consistent
[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 unsigned long 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         setlocale (LC_ALL, "");
141         bindtextdomain (PACKAGE, LOCALEDIR);
142         textdomain (PACKAGE);
144         labels = malloc (labels_size);
145         unitv = malloc (unitv_size);
146         for (i = 0; i < MAX_OIDS; i++)
147                 eval_method[i] = CHECK_UNDEF;
148         i = 0;
150         oid = strdup ("");
151         label = strdup ("SNMP");
152         units = strdup ("");
153         port = strdup (DEFAULT_PORT);
154         outbuff = strdup ("");
155         output = strdup ("");
156         delimiter = strdup (DEFAULT_DELIMITER);
157         output_delim = strdup (DEFAULT_OUTPUT_DELIMITER);
158         miblist = strdup (DEFAULT_MIBLIST);
160         if (process_arguments (argc, argv) == ERROR)
161                 usage (_("Incorrect arguments supplied\n"));
163         /* create the command line to execute */
164         asprintf (&command_line, "%s -t 1 -r %d -m %s -v %s %s %s:%s %s",
165                   PATH_TO_SNMPGET, timeout_interval - 1, miblist, proto,
166                   authpriv, server_address, port, oid);
167         if (verbose)
168                 printf ("%s\n", command_line);
170         /* run the command */
171         child_process = spopen (command_line);
172         if (child_process == NULL) {
173                 printf (_("Could not open pipe: %s\n"), command_line);
174                 exit (STATE_UNKNOWN);
175         }
177         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
178         if (child_stderr == NULL) {
179                 printf (_("Could not open stderr for %s\n"), command_line);
180         }
182         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
183                 asprintf (&output, "%s%s", output, input_buffer);
185         if (verbose)
186                 printf ("%s\n", output);
188         ptr = output;
190         while (ptr) {
192                 ptr = strstr (ptr, delimiter);
193                 if (ptr == NULL)
194                         break;
196                 ptr += strlen (delimiter);
197                 ptr += strspn (ptr, " ");
199                 found++;
201                 if (ptr[0] == '"') {
202                         ptr++;
203                         response = strpcpy (response, ptr, "\"");
204                         ptr = strpbrk (ptr, "\"");
205                         ptr += strspn (ptr, "\"\n");
206                 }
207                 else {
208                         response = strpcpy (response, ptr, "\n");
209                         ptr = strpbrk (ptr, "\n");
210                         ptr += strspn (ptr, "\n");
211                         while
212                                 (strstr (ptr, delimiter) &&
213                                  strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter)) {
214                                 response = strpcat (response, ptr, "\n");
215                                 ptr = strpbrk (ptr, "\n");
216                         }
217                         if (ptr && strstr (ptr, delimiter) == NULL) {
218                                 asprintf (&response, "%s%s", response, ptr);
219                                 ptr = NULL;
220                         }
221                 }
223                 /* We strip out the datatype indicator for PHBs */
224                 if (strstr (response, "Gauge: "))
225                         show = strstr (response, "Gauge: ") + 7;
226                 else if (strstr (response, "Gauge32: "))
227                         show = strstr (response, "Gauge32: ") + 9;
228                 else if (strstr (response, "Counter32: "))
229                         show = strstr (response, "Counter32: ") + 11;
230                 else if (strstr (response, "INTEGER: "))
231                         show = strstr (response, "INTEGER: ") + 9;
232                 else if (strstr (response, "STRING: "))
233                         show = strstr (response, "STRING: ") + 8;
234                 else
235                         show = response;
236                 p2 = show;
238                 iresult = STATE_DEPENDENT;
240                 /* Process this block for integer comparisons */
241                 if (eval_method[i] & CRIT_GT ||
242                     eval_method[i] & CRIT_LT ||
243                     eval_method[i] & CRIT_GE ||
244                     eval_method[i] & CRIT_LE ||
245                     eval_method[i] & CRIT_EQ ||
246                     eval_method[i] & CRIT_NE ||
247                     eval_method[i] & WARN_GT ||
248                     eval_method[i] & WARN_LT ||
249                     eval_method[i] & WARN_GE ||
250                     eval_method[i] & WARN_LE ||
251                     eval_method[i] & WARN_EQ ||
252                     eval_method[i] & WARN_NE) {
253                         p2 = strpbrk (p2, "0123456789");
254                         if (p2 == NULL) 
255                                 die (STATE_UNKNOWN,"No valid data returned");
256                         response_value[i] = strtoul (p2, NULL, 10);
257                         iresult = check_num (i);
258                         asprintf (&show, "%lu", response_value[i]);
259                 }
261                 /* Process this block for string matching */
262                 else if (eval_method[i] & CRIT_STRING) {
263                         if (strcmp (response, string_value))
264                                 iresult = STATE_CRITICAL;
265                         else
266                                 iresult = STATE_OK;
267                 }
269                 /* Process this block for regex matching */
270                 else if (eval_method[i] & CRIT_REGEX) {
271 #ifdef HAVE_REGEX_H
272                         excode = regexec (&preg, response, 10, pmatch, eflags);
273                         if (excode == 0) {
274                                 iresult = STATE_OK;
275                         }
276                         else if (excode != REG_NOMATCH) {
277                                 regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
278                                 printf (_("Execute Error: %s\n"), errbuf);
279                                 exit (STATE_CRITICAL);
280                         }
281                         else {
282                                 iresult = STATE_CRITICAL;
283                         }
284 #else
285                         printf (_("%s UNKNOWN: call for regex which was not a compiled option"), label);
286                         exit (STATE_UNKNOWN);
287 #endif
288                 }
290                 /* Process this block for existence-nonexistence checks */
291                 else {
292                         if (eval_method[i] & CRIT_PRESENT)
293                                 iresult = STATE_CRITICAL;
294                         else if (eval_method[i] & WARN_PRESENT)
295                                 iresult = STATE_WARNING;
296                         else if (response && iresult == STATE_DEPENDENT) 
297                                 iresult = STATE_OK;
298                 }
300                 /* Result is the worst outcome of all the OIDs tested */
301                 result = max_state (result, iresult);
303                 /* Prepend a label for this OID if there is one */
304                 if (nlabels > (size_t)1 && (size_t)i < nlabels && labels[i] != NULL)
305                         asprintf (&outbuff, "%s%s%s %s%s%s", outbuff,
306                                   (i == 0) ? " " : output_delim,
307                                   labels[i], mark (iresult), show, mark (iresult));
308                 else
309                         asprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim,
310                                   mark (iresult), show, mark (iresult));
312                 /* Append a unit string for this OID if there is one */
313                 if (nunits > (size_t)0 && (size_t)i < nunits && unitv[i] != NULL)
314                         asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
316                 i++;
318         }                                                                                                                       /* end while (ptr) */
320         if (found == 0)
321                 die (STATE_UNKNOWN,
322                      _("%s problem - No data received from host\nCMD: %s\n"),
323                      label,
324                      command_line);
326         /* WARNING if output found on stderr */
327         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
328                 result = max_state (result, STATE_WARNING);
330         /* close stderr */
331         (void) fclose (child_stderr);
333         /* close the pipe */
334         if (spclose (child_process))
335                 result = max_state (result, STATE_WARNING);
337 /*      if (nunits == 1 || i == 1) */
338 /*              printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units); */
339 /*      else */
340         printf ("%s %s -%s\n", label, state_text (result), outbuff);
342         return result;
349 \f
350 /* process command-line arguments */
351 int
352 process_arguments (int argc, char **argv)
354         char *ptr;
355         int c = 1;
356         int j = 0, jj = 0, ii = 0;
358         int option = 0;
359         static struct option longopts[] = {
360                 STD_LONG_OPTS,
361                 {"community", required_argument, 0, 'C'},
362                 {"oid", required_argument, 0, 'o'},
363                 {"object", required_argument, 0, 'o'},
364                 {"delimiter", required_argument, 0, 'd'},
365                 {"output-delimiter", required_argument, 0, 'D'},
366                 {"string", required_argument, 0, 's'},
367                 {"regex", required_argument, 0, 'r'},
368                 {"ereg", required_argument, 0, 'r'},
369                 {"eregi", required_argument, 0, 'R'},
370                 {"label", required_argument, 0, 'l'},
371                 {"units", required_argument, 0, 'u'},
372                 {"port", required_argument, 0, 'p'},
373                 {"miblist", required_argument, 0, 'm'},
374                 {"protocol", required_argument, 0, 'P'},
375                 {"seclevel", required_argument, 0, 'L'},
376                 {"secname", required_argument, 0, 'U'},
377                 {"authproto", required_argument, 0, 'a'},
378                 {"authpasswd", required_argument, 0, 'A'},
379                 {"privpasswd", required_argument, 0, 'X'},
380                 {0, 0, 0, 0}
381         };
383         if (argc < 2)
384                 return ERROR;
386         /* reverse compatibility for very old non-POSIX usage forms */
387         for (c = 1; c < argc; c++) {
388                 if (strcmp ("-to", argv[c]) == 0)
389                         strcpy (argv[c], "-t");
390                 if (strcmp ("-wv", argv[c]) == 0)
391                         strcpy (argv[c], "-w");
392                 if (strcmp ("-cv", argv[c]) == 0)
393                         strcpy (argv[c], "-c");
394         }
396         while (1) {
397                 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:",
398                                                                          longopts, &option);
400                 if (c == -1 || c == EOF)
401                         break;
403                 switch (c) {
404                 case '?':       /* usage */
405                         usage3 ("Unknown argument", optopt);
406                 case 'h':       /* help */
407                         print_help ();
408                         exit (STATE_OK); 
409                 case 'V':       /* version */
410                         print_revision (progname, revision);
411                         exit (STATE_OK);
412                 case 'v': /* verbose */
413                         verbose = TRUE;
414                         break;
416         /* Connection info */
417                 case 'C':                                                                       /* group or community */
418                         community = optarg;
419                         break;
420                 case 'H':                                                                       /* Host or server */
421                         server_address = optarg;
422                         break;
423                 case 'p':       /* TCP port number */
424                         port = optarg;
425                         break;
426                 case 'm':      /* List of MIBS  */
427                         miblist = optarg;
428                         break;
429                 case 'P':     /* SNMP protocol version */
430                         proto = optarg;
431                         break;
432                 case 'L':     /* security level */
433                         seclevel = optarg;
434                         break;
435                 case 'U':     /* security username */
436                         secname = optarg;
437                         break;
438                 case 'a':     /* auth protocol */
439                         authproto = optarg;
440                         break;
441                 case 'A':     /* auth passwd */
442                         authpasswd = optarg;
443                         break;
444                 case 'X':     /* priv passwd */
445                         privpasswd = optarg;
446                         break;
447                 case 't':       /* timeout period */
448                         if (!is_integer (optarg))
449                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
450                         else
451                                 timeout_interval = atoi (optarg);
452                         break;
454         /* Test parameters */
455                 case 'c':                                                                       /* critical time threshold */
456                         if (strspn (optarg, "0123456789:,") < strlen (optarg))
457                                 usage2 (_("Invalid critical threshold: %s\n"), optarg);
458                         for (ptr = optarg; ptr && jj < MAX_OIDS; jj++) {
459                                 if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
460                                         eval_method[jj] |= CRIT_LT;
461                                 if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
462                                         eval_method[jj] |= CRIT_GT;
463                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
464                         }
465                         break;
466                 case 'w':                                                                       /* warning time threshold */
467                         if (strspn (optarg, "0123456789:,") < strlen (optarg))
468                                 usage2 (_("Invalid warning threshold: %s\n"), optarg);
469                         for (ptr = optarg; ptr && ii < MAX_OIDS; ii++) {
470                                 if (lu_getll (&lower_warn_lim[ii], ptr) == 1)
471                                         eval_method[ii] |= WARN_LT;
472                                 if (lu_getul (&upper_warn_lim[ii], ptr) == 1)
473                                         eval_method[ii] |= WARN_GT;
474                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
475                         }
476                         break;
477                 case 'o':                                                                       /* object identifier */
478                 case 'e': /* PRELIMINARY - may change */
479                 case 'E': /* PRELIMINARY - may change */
480                         for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
481                                 ptr[0] = ' '; /* relpace comma with space */
482                         for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
483                                 j++; /* count OIDs */
484                         asprintf (&oid, "%s %s", (oid?oid:""), optarg);
485                         if (c == 'E' || c == 'e') {
486                                 jj++;
487                                 ii++;
488                         }
489                         if (c == 'E') 
490                                 eval_method[j+1] |= WARN_PRESENT;
491                         else if (c == 'e')
492                                 eval_method[j+1] |= CRIT_PRESENT;
493                         break;
494                 case 's':                                                                       /* string or substring */
495                         strncpy (string_value, optarg, sizeof (string_value) - 1);
496                         string_value[sizeof (string_value) - 1] = 0;
497                         eval_method[jj++] = CRIT_STRING;
498                         ii++;
499                         break;
500                 case 'R':                                                                       /* regex */
501 #ifdef HAVE_REGEX_H
502                         cflags = REG_ICASE;
503 #endif
504                 case 'r':                                                                       /* regex */
505 #ifdef HAVE_REGEX_H
506                         cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
507                         strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
508                         regex_expect[sizeof (regex_expect) - 1] = 0;
509                         errcode = regcomp (&preg, regex_expect, cflags);
510                         if (errcode != 0) {
511                                 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
512                                 printf (_("Could Not Compile Regular Expression"));
513                                 return ERROR;
514                         }
515                         eval_method[jj++] = CRIT_REGEX;
516                         ii++;
517 #else
518                         printf (_("%s UNKNOWN: call for regex which was not a compiled option"), label);
519                         exit (STATE_UNKNOWN);
520 #endif
521                         break;
523         /* Format */
524                 case 'd':                                                                       /* delimiter */
525                         delimiter = strscpy (delimiter, optarg);
526                         break;
527                 case 'D':                                                                       /* output-delimiter */
528                         output_delim = strscpy (output_delim, optarg);
529                         break;
530                 case 'l':                                                                       /* label */
531                         label = optarg;
532                         nlabels++;
533                         if (nlabels >= labels_size) {
534                                 labels_size += 8;
535                                 labels = realloc (labels, labels_size);
536                                 if (labels == NULL)
537                                         die (STATE_UNKNOWN,
538                                                                                  _("Could not realloc() labels[%d]"), nlabels);
539                         }
540                         labels[nlabels - 1] = optarg;
541                         ptr = thisarg (optarg);
542                         labels[nlabels - 1] = ptr;
543                         if (strstr (ptr, "'") == ptr)
544                                 labels[nlabels - 1] = ptr + 1;
545                         while (ptr && (ptr = nextarg (ptr))) {
546                                 if (nlabels >= labels_size) {
547                                         labels_size += 8;
548                                         labels = realloc (labels, labels_size);
549                                         if (labels == NULL)
550                                                 die (STATE_UNKNOWN, _("Could not realloc() labels\n"));
551                                 }
552                                 labels++;
553                                 ptr = thisarg (ptr);
554                                 if (strstr (ptr, "'") == ptr)
555                                         labels[nlabels - 1] = ptr + 1;
556                                 else
557                                         labels[nlabels - 1] = ptr;
558                         }
559                         break;
560                 case 'u':                                                                       /* units */
561                         units = optarg;
562                         nunits++;
563                         if (nunits >= unitv_size) {
564                                 unitv_size += 8;
565                                 unitv = realloc (unitv, unitv_size);
566                                 if (unitv == NULL)
567                                         die (STATE_UNKNOWN,
568                                                                                  _("Could not realloc() units [%d]\n"), nunits);
569                         }
570                         unitv[nunits - 1] = optarg;
571                         ptr = thisarg (optarg);
572                         unitv[nunits - 1] = ptr;
573                         if (strstr (ptr, "'") == ptr)
574                                 unitv[nunits - 1] = ptr + 1;
575                         while (ptr && (ptr = nextarg (ptr))) {
576                                 if (nunits >= unitv_size) {
577                                         unitv_size += 8;
578                                         unitv = realloc (unitv, unitv_size);
579                                         if (units == NULL)
580                                                 die (STATE_UNKNOWN, _("Could not realloc() units\n"));
581                                 }
582                                 nunits++;
583                                 ptr = thisarg (ptr);
584                                 if (strstr (ptr, "'") == ptr)
585                                         unitv[nunits - 1] = ptr + 1;
586                                 else
587                                         unitv[nunits - 1] = ptr;
588                         }
589                         break;
591                 }
592         }
594         if (server_address == NULL)
595                 server_address = argv[optind];
597         if (community == NULL)
598                 community = strdup (DEFAULT_COMMUNITY);
600         return validate_arguments ();
606 /******************************************************************************
608 @@-
609 <sect3>
610 <title>validate_arguments</title>
612 <para>&PROTO_validate_arguments;</para>
614 <para>Given a database name, this function returns TRUE if the string
615 is a valid PostgreSQL database name, and returns false if it is
616 not.</para>
618 <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
619 characters long and consist of letters, numbers, and underscores. The
620 first character cannot be a number, however.</para>
622 </sect3>
623 -@@
624 ******************************************************************************/
626 int
627 validate_arguments ()
630         /* Need better checks to verify seclevel and authproto choices */
631         
632         if (seclevel == NULL) 
633                 asprintf (&seclevel, "noAuthNoPriv");
636         if (authproto == NULL ) 
637                 asprintf(&authproto, DEFAULT_AUTH_PROTOCOL);
638         
639          
640         
641         if (proto == NULL || (strcmp(proto,DEFAULT_PROTOCOL) == 0) ) {        /* default protocol version */
642                 asprintf(&proto, DEFAULT_PROTOCOL);
643                 asprintf(&authpriv, "%s%s", "-c ", community);
644         }
645         else if ( strcmp (proto, "3") == 0 ) {                 /* snmpv3 args */
646                 asprintf(&proto, "%s", "3");
647                 
648                 if ( (strcmp(seclevel, "noAuthNoPriv") == 0) || seclevel == NULL ) {
649                         asprintf(&authpriv, "%s", "-l noAuthNoPriv" );
650                 }
651                 else if ( strcmp(seclevel, "authNoPriv") == 0 ) {
652                         if ( secname == NULL || authpasswd == NULL) {
653                                 printf (_("Missing secname (%s) or authpassword (%s) ! \n)"),secname, authpasswd );
654                                 print_usage ();
655                                 exit (STATE_UNKNOWN);
656                         }
657                         asprintf(&authpriv, "-l authNoPriv -a %s -u %s -A %s ", authproto, secname, authpasswd);
658                 }
659                 else if ( strcmp(seclevel, "authPriv") == 0 ) {
660                         if ( secname == NULL || authpasswd == NULL || privpasswd == NULL ) {
661                                 printf (("Missing secname (%s), authpassword (%s), or privpasswd (%s)! \n"),secname, authpasswd,privpasswd );
662                                 print_usage ();
663                                 exit (STATE_UNKNOWN);
664                         }
665                         asprintf(&authpriv, "-l authPriv -a %s -u %s -A %s -x DES -X %s ", authproto, secname, authpasswd, privpasswd);
666                 }
667                 
668                                                                         
669         }
670         else {
671                 printf (_("Invalid SNMP version: %s\n"), proto);
672                 print_usage ();
673                 exit (STATE_UNKNOWN);                           
674         }
675                         
676         
677         
679         return OK;
686 \f
687 char *
688 clarify_message (char *msg)
690         int i = 0;
691         int foo;
692         char tmpmsg_c[MAX_INPUT_BUFFER];
693         char *tmpmsg = (char *) &tmpmsg_c;
694         tmpmsg = strcpy (tmpmsg, msg);
695         if (!strncmp (tmpmsg, " Hex:", 5)) {
696                 tmpmsg = strtok (tmpmsg, ":");
697                 while ((tmpmsg = strtok (NULL, " "))) {
698                         foo = strtol (tmpmsg, NULL, 16);
699                         /* Translate chars that are not the same value in the printers
700                          * character set.
701                          */
702                         switch (foo) {
703                         case 208:
704                                 {
705                                         foo = 197;
706                                         break;
707                                 }
708                         case 216:
709                                 {
710                                         foo = 196;
711                                         break;
712                                 }
713                         }
714                         msg[i] = foo;
715                         i++;
716                 }
717                 msg[i] = 0;
718         }
719         return (msg);
725 int
726 check_num (int i)
728         int result;
729         result = STATE_OK;
730         if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
731                         lower_warn_lim[i] > upper_warn_lim[i]) {
732                 if (response_value[i] <= lower_warn_lim[i] &&
733                                 response_value[i] >= upper_warn_lim[i]) {
734                         result = STATE_WARNING;
735                 }
736         }
737         else if
738                 ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
739                  (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
740                  (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
741                  (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
742                  (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
743                  (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
744                 result = STATE_WARNING;
745         }
747         if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
748                         lower_crit_lim[i] > upper_crit_lim[i]) {
749                 if (response_value[i] <= lower_crit_lim[i] &&
750                                 response_value[i] >= upper_crit_lim[i]) {
751                         result = STATE_CRITICAL;
752                 }
753         }
754         else if
755                 ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
756                  (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
757                  (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
758                  (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
759                  (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
760                  (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
761                 result = STATE_CRITICAL;
762         }
764         return result;
770 int
771 lu_getll (unsigned long *ll, char *str)
773         char tmp[100];
774         if (strchr (str, ':') == NULL)
775                 return 0;
776         if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
777                 return 0;
778         if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
779                 return 1;
780         return 0;
786 int
787 lu_getul (unsigned long *ul, char *str)
789         char tmp[100];
790         if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
791                 return 1;
792         if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
793                 return 1;
794         if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
795                 return 1;
796         return 0;
802 /* trim leading whitespace
803          if there is a leading quote, make sure it balances */
805 char *
806 thisarg (char *str)
808         str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
809         if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
810                 if (strlen (str) == 1 || !strstr (str + 1, "'"))
811                         die (STATE_UNKNOWN, "Unbalanced quotes\n");
812         }
813         return str;
819 /* if there's a leading quote, advance to the trailing quote
820          set the trailing quote to '\x0'
821          if the string continues, advance beyond the comma */
823 char *
824 nextarg (char *str)
826         if (strstr (str, "'") == str) {
827                 str[0] = 0;
828                 if (strlen (str) > 1) {
829                         str = strstr (str + 1, "'");
830                         return (++str);
831                 }
832                 else {
833                         return NULL;
834                 }
835         }
836         if (strstr (str, ",") == str) {
837                 str[0] = 0;
838                 if (strlen (str) > 1) {
839                         return (++str);
840                 }
841                 else {
842                         return NULL;
843                 }
844         }
845         if ((str = strstr (str, ",")) && strlen (str) > 1) {
846                 str[0] = 0;
847                 return (++str);
848         }
849         return NULL;
856 \f
857 void
858 print_help (void)
860         print_revision (progname, revision);
862         printf (_(COPYRIGHT), copyright, email);
864         printf (_("\
865 Check status of remote machines and obtain sustem information via SNMP\n\n"));
867         print_usage ();
869         printf (_(UT_HELP_VRSN));
871         printf (_(UT_HOST_PORT), 'p', DEFAULT_PORT);
873         /* SNMP and Authentication Protocol */
874         printf (_("\
875  -P, --protocol=[1|3]\n\
876     SNMP protocol version\n\
877  -L, --seclevel=[noAuthNoPriv|authNoPriv|authPriv]\n\
878     SNMPv3 securityLevel\n\
879  -a, --authproto=[MD5|SHA]\n\
880     SNMPv3 auth proto\n"));
882         /* Authentication Tokens*/
883         printf (_("\
884  -C, --community=STRING\n\
885     Optional community string for SNMP communication\n\
886     (default is \"%s\")\n\
887  -U, --secname=USERNAME\n\
888     SNMPv3 username\n\
889  -A, --authpassword=PASSWORD\n\
890     SNMPv3 authentication password\n\
891  -X, --privpasswd=PASSWORD\n\
892     SNMPv3 crypt passwd (DES)\n"), DEFAULT_COMMUNITY);
894         /* OID Stuff */
895         printf (_("\
896  -o, --oid=OID(s)\n\
897     Object identifier(s) whose value you wish to query\n\
898  -m, --miblist=STRING\n\
899     List of MIBS to be loaded (default = ALL)\n -d, --delimiter=STRING\n\
900     Delimiter to use when parsing returned data. Default is \"%s\"\n\
901     Any data on the right hand side of the delimiter is considered\n\
902     to be the data that should be used in the evaluation.\n"), DEFAULT_DELIMITER);
904         /* Tests Against Integers */
905         printf (_("\
906  -w, --warning=INTEGER_RANGE(s)\n\
907     Range(s) which will not result in a WARNING status\n\
908  -c, --critical=INTEGER_RANGE(s)\n\
909     Range(s) which will not result in a CRITICAL status\n"));
911         /* Tests Against Strings */
912         printf (_("\
913  -s, --string=STRING\n\
914     Return OK state (for that OID) if STRING is an exact match\n\
915  -r, --ereg=REGEX\n\
916     Return OK state (for that OID) if extended regular expression REGEX matches\n\
917  -R, --eregi=REGEX\n\
918     Return OK state (for that OID) if case-insensitive extended REGEX matches\n\
919  -l, --label=STRING\n\
920     Prefix label for output from plugin (default -s 'SNMP')\n"));
922         /* Output Formatting */
923         printf (_("\
924  -u, --units=STRING\n\
925     Units label(s) for output data (e.g., 'sec.').\n\
926  -D, --output-delimiter=STRING\n\
927     Separates output on multiple OID requests\n"));
929         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
931         printf (_(UT_VERBOSE));
933         printf (_("\n\
934 - This plugin uses the 'snmpget' command included with the NET-SNMP package.\n\
935   If you don't have the package installed, you will need to download it from\n\
936   http://net-snmp.sourceforge.net before you can use this plugin.\n"));
938         printf (_("\
939 - Multiple OIDs may be indicated by a comma- or space-delimited list (lists with\n\
940   internal spaces must be quoted) [max 8 OIDs]\n"));
942         printf (_("\
943 - Ranges are inclusive and are indicated with colons. When specified as\n\
944   'min:max' a STATE_OK will be returned if the result is within the indicated\n\
945   range or is equal to the upper or lower bound. A non-OK state will be\n\
946   returned if the result is outside the specified range.\n"));
948         printf (_("\
949 - If specified in the order 'max:min' a non-OK state will be returned if the\n\
950   result is within the (inclusive) range.\n"));
952         printf (_("\
953 - Upper or lower bounds may be omitted to skip checking the respective limit.\n\
954 - Bare integers are interpreted as upper limits.\n\
955 - When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'\n\
956 - Note that only one string and one regex may be checked at present\n\
957 - All evaluation methods other than PR, STR, and SUBSTR expect that the value\n\
958   returned from the SNMP query is an unsigned integer.\n"));
960         printf (_(UT_SUPPORT));
963 void
964 print_usage (void)
966         printf (_("\
967 Usage: %s -H <ip_address> -o <OID> [-w warn_range] [-c crit_range] \n\
968   [-C community] [-s string] [-r regex] [-R regexi] [-t timeout]\n\
969   [-l label] [-u units] [-p port-number] [-d delimiter]\n\
970   [-D output-delimiter] [-m miblist] [-P snmp version]\n\
971   [-L seclevel] [-U secname] [-a authproto] [-A authpasswd]\n\
972   [-X privpasswd]\n"), progname);
973         printf (_(UT_HLP_VRS), progname, progname);