Code

- typo fixes
[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         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 recieved 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 an 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                                 printf (_("Invalid critical threshold: %s\n"), optarg);
458                                 print_usage ();
459                                 exit (STATE_UNKNOWN);
460                         }
461                         for (ptr = optarg; ptr && jj < MAX_OIDS; jj++) {
462                                 if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
463                                         eval_method[jj] |= CRIT_LT;
464                                 if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
465                                         eval_method[jj] |= CRIT_GT;
466                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
467                         }
468                         break;
469                 case 'w':                                                                       /* warning time threshold */
470                         if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
471                                 printf (_("Invalid warning threshold: %s\n"), optarg);
472                                 print_usage ();
473                                 exit (STATE_UNKNOWN);
474                         }
475                         for (ptr = optarg; ptr && ii < MAX_OIDS; ii++) {
476                                 if (lu_getll (&lower_warn_lim[ii], ptr) == 1)
477                                         eval_method[ii] |= WARN_LT;
478                                 if (lu_getul (&upper_warn_lim[ii], ptr) == 1)
479                                         eval_method[ii] |= WARN_GT;
480                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
481                         }
482                         break;
483                 case 'o':                                                                       /* object identifier */
484                 case 'e': /* PRELIMINARY - may change */
485                 case 'E': /* PRELIMINARY - may change */
486                         for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
487                                 ptr[0] = ' '; /* relpace comma with space */
488                         for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
489                                 j++; /* count OIDs */
490                         asprintf (&oid, "%s %s", (oid?oid:""), optarg);
491                         if (c == 'E' || c == 'e') {
492                                 jj++;
493                                 ii++;
494                         }
495                         if (c == 'E') 
496                                 eval_method[j+1] |= WARN_PRESENT;
497                         else if (c == 'e')
498                                 eval_method[j+1] |= CRIT_PRESENT;
499                         break;
500                 case 's':                                                                       /* string or substring */
501                         strncpy (string_value, optarg, sizeof (string_value) - 1);
502                         string_value[sizeof (string_value) - 1] = 0;
503                         eval_method[jj++] = CRIT_STRING;
504                         ii++;
505                         break;
506                 case 'R':                                                                       /* regex */
507 #ifdef HAVE_REGEX_H
508                         cflags = REG_ICASE;
509 #endif
510                 case 'r':                                                                       /* regex */
511 #ifdef HAVE_REGEX_H
512                         cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
513                         strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
514                         regex_expect[sizeof (regex_expect) - 1] = 0;
515                         errcode = regcomp (&preg, regex_expect, cflags);
516                         if (errcode != 0) {
517                                 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
518                                 printf (_("Could Not Compile Regular Expression"));
519                                 return ERROR;
520                         }
521                         eval_method[jj++] = CRIT_REGEX;
522                         ii++;
523 #else
524                         printf (_("%s UNKNOWN: call for regex which was not a compiled option"), label);
525                         exit (STATE_UNKNOWN);
526 #endif
527                         break;
529         /* Format */
530                 case 'd':                                                                       /* delimiter */
531                         delimiter = strscpy (delimiter, optarg);
532                         break;
533                 case 'D':                                                                       /* output-delimiter */
534                         output_delim = strscpy (output_delim, optarg);
535                         break;
536                 case 'l':                                                                       /* label */
537                         label = optarg;
538                         nlabels++;
539                         if (nlabels >= labels_size) {
540                                 labels_size += 8;
541                                 labels = realloc (labels, labels_size);
542                                 if (labels == NULL)
543                                         die (STATE_UNKNOWN,
544                                                                                  _("Could not realloc() labels[%d]"), nlabels);
545                         }
546                         labels[nlabels - 1] = optarg;
547                         ptr = thisarg (optarg);
548                         if (strstr (ptr, "'") == ptr)
549                                 labels[nlabels - 1] = ptr + 1;
550                         else
551                                 labels[nlabels - 1] = ptr;
552                         while (ptr && (ptr = nextarg (ptr))) {
553                                 if (nlabels >= labels_size) {
554                                         labels_size += 8;
555                                         labels = realloc (labels, labels_size);
556                                         if (labels == NULL)
557                                                 die (STATE_UNKNOWN, _("Could not realloc() labels\n"));
558                                 }
559                                 labels++;
560                                 ptr = thisarg (ptr);
561                                 if (strstr (ptr, "'") == ptr)
562                                         labels[nlabels - 1] = ptr + 1;
563                                 else
564                                         labels[nlabels - 1] = ptr;
565                         }
566                         break;
567                 case 'u':                                                                       /* units */
568                         units = optarg;
569                         nunits++;
570                         if (nunits >= unitv_size) {
571                                 unitv_size += 8;
572                                 unitv = realloc (unitv, unitv_size);
573                                 if (unitv == NULL)
574                                         die (STATE_UNKNOWN,
575                                                                                  _("Could not realloc() units [%d]\n"), nunits);
576                         }
577                         unitv[nunits - 1] = optarg;
578                         ptr = thisarg (optarg);
579                         if (strstr (ptr, "'") == ptr)
580                                 unitv[nunits - 1] = ptr + 1;
581                         else
582                                 unitv[nunits - 1] = ptr;
583                         while (ptr && (ptr = nextarg (ptr))) {
584                                 if (nunits >= unitv_size) {
585                                         unitv_size += 8;
586                                         unitv = realloc (unitv, unitv_size);
587                                         if (units == NULL)
588                                                 die (STATE_UNKNOWN, _("Could not realloc() units\n"));
589                                 }
590                                 nunits++;
591                                 ptr = thisarg (ptr);
592                                 if (strstr (ptr, "'") == ptr)
593                                         unitv[nunits - 1] = ptr + 1;
594                                 else
595                                         unitv[nunits - 1] = ptr;
596                         }
597                         break;
599                 }
600         }
602         if (server_address == NULL)
603                 server_address = argv[optind];
605         if (community == NULL)
606                 community = strdup (DEFAULT_COMMUNITY);
608         return validate_arguments ();
614 /******************************************************************************
616 @@-
617 <sect3>
618 <title>validate_arguments</title>
620 <para>&PROTO_validate_arguments;</para>
622 <para>Given a database name, this function returns TRUE if the string
623 is a valid PostgreSQL database name, and returns false if it is
624 not.</para>
626 <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
627 characters long and consist of letters, numbers, and underscores. The
628 first character cannot be a number, however.</para>
630 </sect3>
631 -@@
632 ******************************************************************************/
634 int
635 validate_arguments ()
638         /* Need better checks to verify seclevel and authproto choices */
639         
640         if (seclevel == NULL) 
641                 asprintf (&seclevel, "noAuthNoPriv");
644         if (authproto == NULL ) 
645                 asprintf(&authproto, DEFAULT_AUTH_PROTOCOL);
646         
647          
648         
649         if (proto == NULL || (strcmp(proto,DEFAULT_PROTOCOL) == 0) ) {        /* default protocol version */
650                 asprintf(&proto, DEFAULT_PROTOCOL);
651                 asprintf(&authpriv, "%s%s", "-c ", community);
652         }
653         else if ( strcmp (proto, "3") == 0 ) {                 /* snmpv3 args */
654                 asprintf(&proto, "%s", "3");
655                 
656                 if ( (strcmp(seclevel, "noAuthNoPriv") == 0) || seclevel == NULL ) {
657                         asprintf(&authpriv, "%s", "-l noAuthNoPriv" );
658                 }
659                 else if ( strcmp(seclevel, "authNoPriv") == 0 ) {
660                         if ( secname == NULL || authpasswd == NULL) {
661                                 printf (_("Missing secname (%s) or authpassword (%s) ! \n)"),secname, authpasswd );
662                                 print_usage ();
663                                 exit (STATE_UNKNOWN);
664                         }
665                         asprintf(&authpriv, "-l authNoPriv -a %s -u %s -A %s ", authproto, secname, authpasswd);
666                 }
667                 else if ( strcmp(seclevel, "authPriv") == 0 ) {
668                         if ( secname == NULL || authpasswd == NULL || privpasswd == NULL ) {
669                                 printf (("Missing secname (%s), authpassword (%s), or privpasswd (%s)! \n"),secname, authpasswd,privpasswd );
670                                 print_usage ();
671                                 exit (STATE_UNKNOWN);
672                         }
673                         asprintf(&authpriv, "-l authPriv -a %s -u %s -A %s -x DES -X %s ", authproto, secname, authpasswd, privpasswd);
674                 }
675                 
676                                                                         
677         }
678         else {
679                 printf (_("Invalid SNMP version: %s\n"), proto);
680                 print_usage ();
681                 exit (STATE_UNKNOWN);                           
682         }
683                         
684         
685         
687         return OK;
694 \f
695 char *
696 clarify_message (char *msg)
698         int i = 0;
699         int foo;
700         char tmpmsg_c[MAX_INPUT_BUFFER];
701         char *tmpmsg = (char *) &tmpmsg_c;
702         tmpmsg = strcpy (tmpmsg, msg);
703         if (!strncmp (tmpmsg, " Hex:", 5)) {
704                 tmpmsg = strtok (tmpmsg, ":");
705                 while ((tmpmsg = strtok (NULL, " "))) {
706                         foo = strtol (tmpmsg, NULL, 16);
707                         /* Translate chars that are not the same value in the printers
708                          * character set.
709                          */
710                         switch (foo) {
711                         case 208:
712                                 {
713                                         foo = 197;
714                                         break;
715                                 }
716                         case 216:
717                                 {
718                                         foo = 196;
719                                         break;
720                                 }
721                         }
722                         msg[i] = foo;
723                         i++;
724                 }
725                 msg[i] = 0;
726         }
727         return (msg);
733 int
734 check_num (int i)
736         int result;
737         result = STATE_OK;
738         if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
739                         lower_warn_lim[i] > upper_warn_lim[i]) {
740                 if (response_value[i] <= lower_warn_lim[i] &&
741                                 response_value[i] >= upper_warn_lim[i]) {
742                         result = STATE_WARNING;
743                 }
744         }
745         else if
746                 ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
747                  (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
748                  (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
749                  (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
750                  (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
751                  (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
752                 result = STATE_WARNING;
753         }
755         if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
756                         lower_warn_lim[i] > upper_warn_lim[i]) {
757                 if (response_value[i] <= lower_crit_lim[i] &&
758                                 response_value[i] >= upper_crit_lim[i]) {
759                         result = STATE_CRITICAL;
760                 }
761         }
762         else if
763                 ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
764                  (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
765                  (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
766                  (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
767                  (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
768                  (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
769                 result = STATE_CRITICAL;
770         }
772         return result;
778 int
779 lu_getll (unsigned long *ll, char *str)
781         char tmp[100];
782         if (strchr (str, ':') == NULL)
783                 return 0;
784         if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
785                 return 0;
786         if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
787                 return 1;
788         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;
810 /* trim leading whitespace
811          if there is a leading quote, make sure it balances */
813 char *
814 thisarg (char *str)
816         str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
817         if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
818                 if (strlen (str) == 1 || !strstr (str + 1, "'"))
819                         die (STATE_UNKNOWN, "Unbalanced quotes\n");
820         }
821         return str;
827 /* if there's a leading quote, advance to the trailing quote
828          set the trailing quote to '\x0'
829          if the string continues, advance beyond the comma */
831 char *
832 nextarg (char *str)
834         if (strstr (str, "'") == str) {
835                 if (strlen (str) > 1) {
836                         str = strstr (str + 1, "'");
837                         str[0] = 0;
838                         return (++str);
839                 }
840                 else {
841                         str[0] = 0;
842                         return NULL;
843                 }
844         }
845         if (strstr (str, ",") == str) {
846                 if (strlen (str) > 1) {
847                         str[0] = 0;
848                         return (++str);
849                 }
850                 else {
851                         str[0] = 0;
852                         return NULL;
853                 }
854         }
855         if ((str = strstr (str, ",")) && strlen (str) > 1) {
856                 str[0] = 0;
857                 return (++str);
858         }
859         return NULL;
866 \f
867 void
868 print_help (void)
870         print_revision (progname, revision);
872         printf (_(COPYRIGHT), copyright, email);
874         printf (_("\
875 Check status of remote machines and obtain sustem information via SNMP\n\n"));
877         print_usage ();
879         printf (_(UT_HELP_VRSN));
881         printf (_(UT_HOST_PORT), 'p', DEFAULT_PORT);
883         /* SNMP and Authentication Protocol */
884         printf (_("\
885  -P, --protocol=[1|3]\n\
886     SNMP protocol version\n\
887  -L, --seclevel=[noAuthNoPriv|authNoPriv|authPriv]\n\
888     SNMPv3 securityLevel\n\
889  -a, --authproto=[MD5|SHA]\n\
890     SNMPv3 auth proto\n"));
892         /* Authentication Tokens*/
893         printf (_("\
894  -C, --community=STRING\n\
895     Optional community string for SNMP communication\n\
896     (default is \"%s\")\n\
897  -U, --secname=USERNAME\n\
898     SNMPv3 username\n\
899  -A, --authpassword=PASSWORD\n\
900     SNMPv3 authentication password\n\
901  -X, --privpasswd=PASSWORD\n\
902     SNMPv3 crypt passwd (DES)\n"), DEFAULT_COMMUNITY);
904         /* OID Stuff */
905         printf (_("\
906  -o, --oid=OID(s)\n\
907     Object identifier(s) whose value you wish to query\n\
908  -m, --miblist=STRING\n\
909     List of MIBS to be loaded (default = ALL)\n -d, --delimiter=STRING\n\
910     Delimiter to use when parsing returned data. Default is \"%s\"\n\
911     Any data on the right hand side of the delimiter is considered\n\
912     to be the data that should be used in the evaluation.\n"), DEFAULT_DELIMITER);
914         /* Tests Against Integers */
915         printf (_("\
916  -w, --warning=INTEGER_RANGE(s)\n\
917     Range(s) which will not result in a WARNING status\n\
918  -c, --critical=INTEGER_RANGE(s)\n\
919     Range(s) which will not result in a CRITICAL status\n"));
921         /* Tests Against Strings */
922         printf (_("\
923  -s, --string=STRING\n\
924     Return OK state (for that OID) if STRING is an exact match\n\
925  -r, --ereg=REGEX\n\
926     Return OK state (for that OID) if extended regular expression REGEX matches\n\
927  -R, --eregi=REGEX\n\
928     Return OK state (for that OID) if case-insensitive extended REGEX matches\n\
929  -l, --label=STRING\n\
930     Prefix label for output from plugin (default -s 'SNMP')\n"));
932         /* Output Formatting */
933         printf (_("\
934  -u, --units=STRING\n\
935     Units label(s) for output data (e.g., 'sec.').\n\
936  -D, --output-delimiter=STRING\n\
937     Separates output on multiple OID requests\n"));
939         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
941         printf (_(UT_VERBOSE));
943         printf (_("\n\
944 - This plugin uses the 'snmpget' command included with the NET-SNMP package.\n\
945   If you don't have the package installed, you will need to download it from\n\
946   http://net-snmp.sourceforge.net before you can use this plugin.\n"));
948         printf (_("\
949 - Multiple OIDs may be indicated by a comma- or space-delimited list (lists with\n\
950   internal spaces must be quoted) [max 8 OIDs]\n"));
952         printf (_("\
953 - Ranges are inclusive and are indicated with colons. When specified as\n\
954   'min:max' a STATE_OK will be returned if the result is within the indicated\n\
955   range or is equal to the upper or lower bound. A non-OK state will be\n\
956   returned if the result is outside the specified range.\n"));
958         printf (_("\
959 - If specified in the order 'max:min' a non-OK state will be returned if the\n\
960   result is within the (inclusive) range.\n"));
962         printf (_("\
963 - Upper or lower bounds may be omitted to skip checking the respective limit.\n\
964 - Bare integers are interpreted as upper limits.\n\
965 - When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'\n\
966 - Note that only one string and one regex may be checked at present\n\
967 - All evaluation methods other than PR, STR, and SUBSTR expect that the value\n\
968   returned from the SNMP query is an unsigned integer.\n"));
970         printf (_(UT_SUPPORT));
973 void
974 print_usage (void)
976         printf (_("\
977 Usage: %s -H <ip_address> -o <OID> [-w warn_range] [-c crit_range] \n\
978   [-C community] [-s string] [-r regex] [-R regexi] [-t timeout]\n\
979   [-l label] [-u units] [-p port-number] [-d delimiter]\n\
980   [-D output-delimiter] [-m miblist] [-P snmp version]\n\
981   [-L seclevel] [-U secname] [-a authproto] [-A authpasswd]\n\
982   [-X privpasswd]\n"), progname);
983         printf (_(UT_HLP_VRS), progname, progname);