Code

More internationalization work
[nagiosplug.git] / plugins / check_snmp.c
1 /******************************************************************************
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; either version 2 of the License, or
6  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  GNU General Public License for more details.
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  $Id$
18  
19 ******************************************************************************/
21 const char *progname = "check_snmp";
22 const char *revision = "$Revision$";
23 const char *copyright = "1999-2003";
24 const char *email = "nagiosplug-devel@lists.sourceforge.net";
26 #include "common.h"
27 #include "utils.h"
28 #include "popen.h"
30 #define DEFAULT_COMMUNITY "public"
31 #define DEFAULT_PORT "161"
32 #define DEFAULT_MIBLIST "ALL"
33 #define DEFAULT_PROTOCOL "1"
34 #define DEFAULT_AUTH_PROTOCOL "MD5"
35 #define DEFAULT_DELIMITER "="
36 #define DEFAULT_OUTPUT_DELIMITER " "
38 #define mark(a) ((a)!=0?"*":"")
40 #define CHECK_UNDEF 0
41 #define CRIT_PRESENT 1
42 #define CRIT_STRING 2
43 #define CRIT_REGEX 4
44 #define CRIT_GT 8
45 #define CRIT_LT 16
46 #define CRIT_GE 32
47 #define CRIT_LE 64
48 #define CRIT_EQ 128
49 #define CRIT_NE 256
50 #define CRIT_RANGE 512
51 #define WARN_PRESENT 1024
52 #define WARN_STRING 2048
53 #define WARN_REGEX 4096
54 #define WARN_GT 8192
55 #define WARN_LT 16384
56 #define WARN_GE 32768
57 #define WARN_LE 65536
58 #define WARN_EQ 131072
59 #define WARN_NE 262144
60 #define WARN_RANGE 524288
62 #define MAX_OIDS 8
63 #define MAX_DELIM_LENGTH 8
65 int process_arguments (int, char **);
66 int validate_arguments (void);
67 char *clarify_message (char *);
68 int check_num (int);
69 int lu_getll (unsigned long *, char *);
70 int lu_getul (unsigned long *, char *);
71 char *thisarg (char *str);
72 char *nextarg (char *str);
73 void print_usage (void);
74 void print_help (void);
76 #ifdef HAVE_REGEX_H
77 #include <regex.h>
78 char regex_expect[MAX_INPUT_BUFFER] = "";
79 regex_t preg;
80 regmatch_t pmatch[10];
81 char timestamp[10] = "";
82 char errbuf[MAX_INPUT_BUFFER];
83 int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
84 int eflags = 0;
85 int errcode, excode;
86 #endif
88 char *server_address = NULL;
89 char *community = NULL;
90 char *authpriv = NULL;
91 char *proto = NULL;
92 char *seclevel = NULL;
93 char *secname = NULL;
94 char *authproto = NULL;
95 char *authpasswd = NULL;
96 char *privpasswd = NULL;
97 char *oid;
98 char *label;
99 char *units;
100 char *port;
101 char string_value[MAX_INPUT_BUFFER] = "";
102 char **labels = NULL;
103 char **unitv = NULL;
104 size_t nlabels = 0;
105 size_t labels_size = 8;
106 size_t nunits = 0;
107 size_t unitv_size = 8;
108 int verbose = FALSE;
109 unsigned long lower_warn_lim[MAX_OIDS];
110 unsigned long upper_warn_lim[MAX_OIDS];
111 unsigned long lower_crit_lim[MAX_OIDS];
112 unsigned long upper_crit_lim[MAX_OIDS];
113 unsigned long response_value[MAX_OIDS];
114 int check_warning_value = FALSE;
115 int check_critical_value = FALSE;
116 unsigned long eval_method[MAX_OIDS];
117 char *delimiter;
118 char *output_delim;
119 char *miblist;
123 int
124 main (int argc, char **argv)
126         int i = 0;
127         int iresult = STATE_UNKNOWN;
128         int found = 0;
129         int result = STATE_DEPENDENT;
130         char input_buffer[MAX_INPUT_BUFFER];
131         char *command_line = NULL;
132         char *response = NULL;
133         char *outbuff;
134         char *output;
135         char *ptr = NULL;
136         char *p2 = NULL;
137         char *show = NULL;
139         setlocale (LC_ALL, "");
140         bindtextdomain (PACKAGE, LOCALEDIR);
141         textdomain (PACKAGE);
143         labels = malloc (labels_size);
144         unitv = malloc (unitv_size);
145         for (i = 0; i < MAX_OIDS; i++)
146                 eval_method[i] = CHECK_UNDEF;
147         i = 0;
149         oid = strdup ("");
150         label = strdup ("SNMP");
151         units = strdup ("");
152         port = strdup (DEFAULT_PORT);
153         outbuff = strdup ("");
154         output = strdup ("");
155         delimiter = strdup (DEFAULT_DELIMITER);
156         output_delim = strdup (DEFAULT_OUTPUT_DELIMITER);
157         miblist = strdup (DEFAULT_MIBLIST);
159         if (process_arguments (argc, argv) != TRUE)
160                 usage4 (_("Could not parse arguments"));
162         /* create the command line to execute */
163         asprintf (&command_line, "%s -t 1 -r %d -m %s -v %s %s %s:%s %s",
164                   PATH_TO_SNMPGET, timeout_interval - 1, miblist, proto,
165                   authpriv, server_address, port, oid);
166         if (verbose)
167                 printf ("%s\n", command_line);
169         /* run the command */
170         child_process = spopen (command_line);
171         if (child_process == NULL) {
172                 printf (_("Could not open pipe: %s\n"), command_line);
173                 exit (STATE_UNKNOWN);
174         }
176         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
177         if (child_stderr == NULL) {
178                 printf (_("Could not open stderr for %s\n"), command_line);
179         }
181         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
182                 asprintf (&output, "%s%s", output, input_buffer);
184         if (verbose)
185                 printf ("%s\n", output);
187         ptr = output;
189         while (ptr) {
191                 ptr = strstr (ptr, delimiter);
192                 if (ptr == NULL)
193                         break;
195                 ptr += strlen (delimiter);
196                 ptr += strspn (ptr, " ");
198                 found++;
200                 if (ptr[0] == '"') {
201                         ptr++;
202                         response = strpcpy (response, ptr, "\"");
203                         ptr = strpbrk (ptr, "\"");
204                         ptr += strspn (ptr, "\"\n");
205                 }
206                 else {
207                         response = strpcpy (response, ptr, "\n");
208                         ptr = strpbrk (ptr, "\n");
209                         ptr += strspn (ptr, "\n");
210                         while
211                                 (strstr (ptr, delimiter) &&
212                                  strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter)) {
213                                 response = strpcat (response, ptr, "\n");
214                                 ptr = strpbrk (ptr, "\n");
215                         }
216                         if (ptr && strstr (ptr, delimiter) == NULL) {
217                                 asprintf (&response, "%s%s", response, ptr);
218                                 ptr = NULL;
219                         }
220                 }
222                 /* We strip out the datatype indicator for PHBs */
223                 if (strstr (response, "Gauge: "))
224                         show = strstr (response, "Gauge: ") + 7;
225                 else if (strstr (response, "Gauge32: "))
226                         show = strstr (response, "Gauge32: ") + 9;
227                 else if (strstr (response, "Counter32: "))
228                         show = strstr (response, "Counter32: ") + 11;
229                 else if (strstr (response, "INTEGER: "))
230                         show = strstr (response, "INTEGER: ") + 9;
231                 else if (strstr (response, "STRING: "))
232                         show = strstr (response, "STRING: ") + 8;
233                 else
234                         show = response;
235                 p2 = show;
237                 iresult = STATE_DEPENDENT;
239                 /* Process this block for integer comparisons */
240                 if (eval_method[i] & CRIT_GT ||
241                     eval_method[i] & CRIT_LT ||
242                     eval_method[i] & CRIT_GE ||
243                     eval_method[i] & CRIT_LE ||
244                     eval_method[i] & CRIT_EQ ||
245                     eval_method[i] & CRIT_NE ||
246                     eval_method[i] & WARN_GT ||
247                     eval_method[i] & WARN_LT ||
248                     eval_method[i] & WARN_GE ||
249                     eval_method[i] & WARN_LE ||
250                     eval_method[i] & WARN_EQ ||
251                     eval_method[i] & WARN_NE) {
252                         p2 = strpbrk (p2, "0123456789");
253                         if (p2 == NULL) 
254                                 die (STATE_UNKNOWN,"No valid data returned");
255                         response_value[i] = strtoul (p2, NULL, 10);
256                         iresult = check_num (i);
257                         asprintf (&show, "%lu", response_value[i]);
258                 }
260                 /* Process this block for string matching */
261                 else if (eval_method[i] & CRIT_STRING) {
262                         if (strcmp (response, string_value))
263                                 iresult = STATE_CRITICAL;
264                         else
265                                 iresult = STATE_OK;
266                 }
268                 /* Process this block for regex matching */
269                 else if (eval_method[i] & CRIT_REGEX) {
270 #ifdef HAVE_REGEX_H
271                         excode = regexec (&preg, response, 10, pmatch, eflags);
272                         if (excode == 0) {
273                                 iresult = STATE_OK;
274                         }
275                         else if (excode != REG_NOMATCH) {
276                                 regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
277                                 printf (_("Execute Error: %s\n"), errbuf);
278                                 exit (STATE_CRITICAL);
279                         }
280                         else {
281                                 iresult = STATE_CRITICAL;
282                         }
283 #else
284                         printf (_("%s UNKNOWN: call for regex which was not a compiled option"), label);
285                         exit (STATE_UNKNOWN);
286 #endif
287                 }
289                 /* Process this block for existence-nonexistence checks */
290                 else {
291                         if (eval_method[i] & CRIT_PRESENT)
292                                 iresult = STATE_CRITICAL;
293                         else if (eval_method[i] & WARN_PRESENT)
294                                 iresult = STATE_WARNING;
295                         else if (response && iresult == STATE_DEPENDENT) 
296                                 iresult = STATE_OK;
297                 }
299                 /* Result is the worst outcome of all the OIDs tested */
300                 result = max_state (result, iresult);
302                 /* Prepend a label for this OID if there is one */
303                 if (nlabels > (size_t)1 && (size_t)i < nlabels && labels[i] != NULL)
304                         asprintf (&outbuff, "%s%s%s %s%s%s", outbuff,
305                                   (i == 0) ? " " : output_delim,
306                                   labels[i], mark (iresult), show, mark (iresult));
307                 else
308                         asprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim,
309                                   mark (iresult), show, mark (iresult));
311                 /* Append a unit string for this OID if there is one */
312                 if (nunits > (size_t)0 && (size_t)i < nunits && unitv[i] != NULL)
313                         asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
315                 i++;
317         }                                                                                                                       /* end while (ptr) */
319         if (found == 0)
320                 die (STATE_UNKNOWN,
321                      _("%s problem - No data received from host\nCMD: %s\n"),
322                      label,
323                      command_line);
325         /* WARNING if output found on stderr */
326         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
327                 result = max_state (result, STATE_WARNING);
329         /* close stderr */
330         (void) fclose (child_stderr);
332         /* close the pipe */
333         if (spclose (child_process))
334                 result = max_state (result, STATE_WARNING);
336 /*      if (nunits == 1 || i == 1) */
337 /*              printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units); */
338 /*      else */
339         printf ("%s %s -%s\n", label, state_text (result), outbuff);
341         return result;
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                         printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
402                         print_usage ();
403                         exit (STATE_UNKNOWN);
404                 case 'h':       /* help */
405                         print_help ();
406                         exit (STATE_OK); 
407                 case 'V':       /* version */
408                         print_revision (progname, revision);
409                         exit (STATE_OK);
410                 case 'v': /* verbose */
411                         verbose = TRUE;
412                         break;
414         /* Connection info */
415                 case 'C':                                                                       /* group or community */
416                         community = optarg;
417                         break;
418                 case 'H':                                                                       /* Host or server */
419                         server_address = optarg;
420                         break;
421                 case 'p':       /* TCP port number */
422                         port = optarg;
423                         break;
424                 case 'm':      /* List of MIBS  */
425                         miblist = optarg;
426                         break;
427                 case 'P':     /* SNMP protocol version */
428                         proto = optarg;
429                         break;
430                 case 'L':     /* security level */
431                         seclevel = optarg;
432                         break;
433                 case 'U':     /* security username */
434                         secname = optarg;
435                         break;
436                 case 'a':     /* auth protocol */
437                         authproto = optarg;
438                         break;
439                 case 'A':     /* auth passwd */
440                         authpasswd = optarg;
441                         break;
442                 case 'X':     /* priv passwd */
443                         privpasswd = optarg;
444                         break;
445                 case 't':       /* timeout period */
446                         if (!is_integer (optarg))
447                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
448                         else
449                                 timeout_interval = atoi (optarg);
450                         break;
452         /* Test parameters */
453                 case 'c':                                                                       /* critical time threshold */
454                         if (strspn (optarg, "0123456789:,") < strlen (optarg))
455                                 usage2 (_("Invalid critical threshold: %s\n"), optarg);
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                                 usage2 (_("Invalid warning threshold: %s\n"), optarg);
467                         for (ptr = optarg; ptr && ii < MAX_OIDS; ii++) {
468                                 if (lu_getll (&lower_warn_lim[ii], ptr) == 1)
469                                         eval_method[ii] |= WARN_LT;
470                                 if (lu_getul (&upper_warn_lim[ii], ptr) == 1)
471                                         eval_method[ii] |= WARN_GT;
472                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
473                         }
474                         break;
475                 case 'o':                                                                       /* object identifier */
476                 case 'e': /* PRELIMINARY - may change */
477                 case 'E': /* PRELIMINARY - may change */
478                         for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
479                                 ptr[0] = ' '; /* relpace comma with space */
480                         for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
481                                 j++; /* count OIDs */
482                         asprintf (&oid, "%s %s", (oid?oid:""), optarg);
483                         if (c == 'E' || c == 'e') {
484                                 jj++;
485                                 ii++;
486                         }
487                         if (c == 'E') 
488                                 eval_method[j+1] |= WARN_PRESENT;
489                         else if (c == 'e')
490                                 eval_method[j+1] |= CRIT_PRESENT;
491                         break;
492                 case 's':                                                                       /* string or substring */
493                         strncpy (string_value, optarg, sizeof (string_value) - 1);
494                         string_value[sizeof (string_value) - 1] = 0;
495                         eval_method[jj++] = CRIT_STRING;
496                         ii++;
497                         break;
498                 case 'R':                                                                       /* regex */
499 #ifdef HAVE_REGEX_H
500                         cflags = REG_ICASE;
501 #endif
502                 case 'r':                                                                       /* regex */
503 #ifdef HAVE_REGEX_H
504                         cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
505                         strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
506                         regex_expect[sizeof (regex_expect) - 1] = 0;
507                         errcode = regcomp (&preg, regex_expect, cflags);
508                         if (errcode != 0) {
509                                 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
510                                 printf (_("Could Not Compile Regular Expression"));
511                                 return ERROR;
512                         }
513                         eval_method[jj++] = CRIT_REGEX;
514                         ii++;
515 #else
516                         printf (_("%s UNKNOWN: call for regex which was not a compiled option"), label);
517                         exit (STATE_UNKNOWN);
518 #endif
519                         break;
521         /* Format */
522                 case 'd':                                                                       /* delimiter */
523                         delimiter = strscpy (delimiter, optarg);
524                         break;
525                 case 'D':                                                                       /* output-delimiter */
526                         output_delim = strscpy (output_delim, optarg);
527                         break;
528                 case 'l':                                                                       /* label */
529                         label = optarg;
530                         nlabels++;
531                         if (nlabels >= labels_size) {
532                                 labels_size += 8;
533                                 labels = realloc (labels, labels_size);
534                                 if (labels == NULL)
535                                         die (STATE_UNKNOWN,
536                                                                                  _("Could not realloc() labels[%d]"), nlabels);
537                         }
538                         labels[nlabels - 1] = optarg;
539                         ptr = thisarg (optarg);
540                         labels[nlabels - 1] = ptr;
541                         if (strstr (ptr, "'") == ptr)
542                                 labels[nlabels - 1] = ptr + 1;
543                         while (ptr && (ptr = nextarg (ptr))) {
544                                 if (nlabels >= labels_size) {
545                                         labels_size += 8;
546                                         labels = realloc (labels, labels_size);
547                                         if (labels == NULL)
548                                                 die (STATE_UNKNOWN, _("Could not realloc() labels\n"));
549                                 }
550                                 labels++;
551                                 ptr = thisarg (ptr);
552                                 if (strstr (ptr, "'") == ptr)
553                                         labels[nlabels - 1] = ptr + 1;
554                                 else
555                                         labels[nlabels - 1] = ptr;
556                         }
557                         break;
558                 case 'u':                                                                       /* units */
559                         units = optarg;
560                         nunits++;
561                         if (nunits >= unitv_size) {
562                                 unitv_size += 8;
563                                 unitv = realloc (unitv, unitv_size);
564                                 if (unitv == NULL)
565                                         die (STATE_UNKNOWN,
566                                                                                  _("Could not realloc() units [%d]\n"), nunits);
567                         }
568                         unitv[nunits - 1] = optarg;
569                         ptr = thisarg (optarg);
570                         unitv[nunits - 1] = ptr;
571                         if (strstr (ptr, "'") == ptr)
572                                 unitv[nunits - 1] = ptr + 1;
573                         while (ptr && (ptr = nextarg (ptr))) {
574                                 if (nunits >= unitv_size) {
575                                         unitv_size += 8;
576                                         unitv = realloc (unitv, unitv_size);
577                                         if (units == NULL)
578                                                 die (STATE_UNKNOWN, _("Could not realloc() units\n"));
579                                 }
580                                 nunits++;
581                                 ptr = thisarg (ptr);
582                                 if (strstr (ptr, "'") == ptr)
583                                         unitv[nunits - 1] = ptr + 1;
584                                 else
585                                         unitv[nunits - 1] = ptr;
586                         }
587                         break;
589                 }
590         }
592         if (server_address == NULL)
593                 server_address = argv[optind];
595         if (community == NULL)
596                 community = strdup (DEFAULT_COMMUNITY);
598         return validate_arguments ();
602 /******************************************************************************
604 @@-
605 <sect3>
606 <title>validate_arguments</title>
608 <para>&PROTO_validate_arguments;</para>
610 <para>Given a database name, this function returns TRUE if the string
611 is a valid PostgreSQL database name, and returns false if it is
612 not.</para>
614 <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
615 characters long and consist of letters, numbers, and underscores. The
616 first character cannot be a number, however.</para>
618 </sect3>
619 -@@
620 ******************************************************************************/
624 int
625 validate_arguments ()
628         /* Need better checks to verify seclevel and authproto choices */
629         
630         if (seclevel == NULL) 
631                 asprintf (&seclevel, "noAuthNoPriv");
634         if (authproto == NULL ) 
635                 asprintf(&authproto, DEFAULT_AUTH_PROTOCOL);
636         
637          
638         
639         if (proto == NULL || (strcmp(proto,DEFAULT_PROTOCOL) == 0) ) {        /* default protocol version */
640                 asprintf(&proto, DEFAULT_PROTOCOL);
641                 asprintf(&authpriv, "%s%s", "-c ", community);
642         }
643         else if ( strcmp (proto, "3") == 0 ) {                 /* snmpv3 args */
644                 asprintf(&proto, "%s", "3");
645                 
646                 if ( (strcmp(seclevel, "noAuthNoPriv") == 0) || seclevel == NULL ) {
647                         asprintf(&authpriv, "%s", "-l noAuthNoPriv" );
648                 }
649                 else if ( strcmp(seclevel, "authNoPriv") == 0 ) {
650                         if ( secname == NULL || authpasswd == NULL) {
651                                 printf (_("Missing secname (%s) or authpassword (%s) ! \n)"),secname, authpasswd );
652                                 print_usage ();
653                                 exit (STATE_UNKNOWN);
654                         }
655                         asprintf(&authpriv, "-l authNoPriv -a %s -u %s -A %s ", authproto, secname, authpasswd);
656                 }
657                 else if ( strcmp(seclevel, "authPriv") == 0 ) {
658                         if ( secname == NULL || authpasswd == NULL || privpasswd == NULL ) {
659                                 printf (("Missing secname (%s), authpassword (%s), or privpasswd (%s)! \n"),secname, authpasswd,privpasswd );
660                                 print_usage ();
661                                 exit (STATE_UNKNOWN);
662                         }
663                         asprintf(&authpriv, "-l authPriv -a %s -u %s -A %s -x DES -X %s ", authproto, secname, authpasswd, privpasswd);
664                 }
665                 
666         }
667         else {
668                 printf (_("Invalid SNMP version: %s\n"), proto);
669                 print_usage ();
670                 exit (STATE_UNKNOWN);                           
671         }
672                         
673         return OK;
678 char *
679 clarify_message (char *msg)
681         int i = 0;
682         int foo;
683         char tmpmsg_c[MAX_INPUT_BUFFER];
684         char *tmpmsg = (char *) &tmpmsg_c;
685         tmpmsg = strcpy (tmpmsg, msg);
686         if (!strncmp (tmpmsg, " Hex:", 5)) {
687                 tmpmsg = strtok (tmpmsg, ":");
688                 while ((tmpmsg = strtok (NULL, " "))) {
689                         foo = strtol (tmpmsg, NULL, 16);
690                         /* Translate chars that are not the same value in the printers
691                          * character set.
692                          */
693                         switch (foo) {
694                         case 208:
695                                 {
696                                         foo = 197;
697                                         break;
698                                 }
699                         case 216:
700                                 {
701                                         foo = 196;
702                                         break;
703                                 }
704                         }
705                         msg[i] = foo;
706                         i++;
707                 }
708                 msg[i] = 0;
709         }
710         return (msg);
715 int
716 check_num (int i)
718         int result;
719         result = STATE_OK;
720         if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
721                         lower_warn_lim[i] > upper_warn_lim[i]) {
722                 if (response_value[i] <= lower_warn_lim[i] &&
723                                 response_value[i] >= upper_warn_lim[i]) {
724                         result = STATE_WARNING;
725                 }
726         }
727         else if
728                 ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
729                  (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
730                  (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
731                  (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
732                  (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
733                  (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
734                 result = STATE_WARNING;
735         }
737         if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
738                         lower_crit_lim[i] > upper_crit_lim[i]) {
739                 if (response_value[i] <= lower_crit_lim[i] &&
740                                 response_value[i] >= upper_crit_lim[i]) {
741                         result = STATE_CRITICAL;
742                 }
743         }
744         else if
745                 ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
746                  (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
747                  (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
748                  (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
749                  (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
750                  (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
751                 result = STATE_CRITICAL;
752         }
754         return result;
759 int
760 lu_getll (unsigned long *ll, char *str)
762         char tmp[100];
763         if (strchr (str, ':') == NULL)
764                 return 0;
765         if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
766                 return 0;
767         if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
768                 return 1;
769         return 0;
774 int
775 lu_getul (unsigned long *ul, char *str)
777         char tmp[100];
778         if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
779                 return 1;
780         if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
781                 return 1;
782         if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
783                 return 1;
784         return 0;
789 /* trim leading whitespace
790          if there is a leading quote, make sure it balances */
792 char *
793 thisarg (char *str)
795         str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
796         if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
797                 if (strlen (str) == 1 || !strstr (str + 1, "'"))
798                         die (STATE_UNKNOWN, "Unbalanced quotes\n");
799         }
800         return str;
805 /* if there's a leading quote, advance to the trailing quote
806          set the trailing quote to '\x0'
807          if the string continues, advance beyond the comma */
809 char *
810 nextarg (char *str)
812         if (strstr (str, "'") == str) {
813                 str[0] = 0;
814                 if (strlen (str) > 1) {
815                         str = strstr (str + 1, "'");
816                         return (++str);
817                 }
818                 else {
819                         return NULL;
820                 }
821         }
822         if (strstr (str, ",") == str) {
823                 str[0] = 0;
824                 if (strlen (str) > 1) {
825                         return (++str);
826                 }
827                 else {
828                         return NULL;
829                 }
830         }
831         if ((str = strstr (str, ",")) && strlen (str) > 1) {
832                 str[0] = 0;
833                 return (++str);
834         }
835         return NULL;
840 void
841 print_help (void)
843         print_revision (progname, revision);
845         printf (_(COPYRIGHT), copyright, email);
847         printf (_("\
848 Check status of remote machines and obtain sustem information via SNMP\n\n"));
850         print_usage ();
852         printf (_(UT_HELP_VRSN));
854         printf (_(UT_HOST_PORT), 'p', DEFAULT_PORT);
856         /* SNMP and Authentication Protocol */
857         printf (_("\
858  -P, --protocol=[1|3]\n\
859     SNMP protocol version\n\
860  -L, --seclevel=[noAuthNoPriv|authNoPriv|authPriv]\n\
861     SNMPv3 securityLevel\n\
862  -a, --authproto=[MD5|SHA]\n\
863     SNMPv3 auth proto\n"));
865         /* Authentication Tokens*/
866         printf (_("\
867  -C, --community=STRING\n\
868     Optional community string for SNMP communication\n\
869     (default is \"%s\")\n\
870  -U, --secname=USERNAME\n\
871     SNMPv3 username\n\
872  -A, --authpassword=PASSWORD\n\
873     SNMPv3 authentication password\n\
874  -X, --privpasswd=PASSWORD\n\
875     SNMPv3 crypt passwd (DES)\n"), DEFAULT_COMMUNITY);
877         /* OID Stuff */
878         printf (_("\
879  -o, --oid=OID(s)\n\
880     Object identifier(s) whose value you wish to query\n\
881  -m, --miblist=STRING\n\
882     List of MIBS to be loaded (default = ALL)\n -d, --delimiter=STRING\n\
883     Delimiter to use when parsing returned data. Default is \"%s\"\n\
884     Any data on the right hand side of the delimiter is considered\n\
885     to be the data that should be used in the evaluation.\n"), DEFAULT_DELIMITER);
887         /* Tests Against Integers */
888         printf (_("\
889  -w, --warning=INTEGER_RANGE(s)\n\
890     Range(s) which will not result in a WARNING status\n\
891  -c, --critical=INTEGER_RANGE(s)\n\
892     Range(s) which will not result in a CRITICAL status\n"));
894         /* Tests Against Strings */
895         printf (_("\
896  -s, --string=STRING\n\
897     Return OK state (for that OID) if STRING is an exact match\n\
898  -r, --ereg=REGEX\n\
899     Return OK state (for that OID) if extended regular expression REGEX matches\n\
900  -R, --eregi=REGEX\n\
901     Return OK state (for that OID) if case-insensitive extended REGEX matches\n\
902  -l, --label=STRING\n\
903     Prefix label for output from plugin (default -s 'SNMP')\n"));
905         /* Output Formatting */
906         printf (_("\
907  -u, --units=STRING\n\
908     Units label(s) for output data (e.g., 'sec.').\n\
909  -D, --output-delimiter=STRING\n\
910     Separates output on multiple OID requests\n"));
912         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
914         printf (_(UT_VERBOSE));
916         printf (_("\n\
917 - This plugin uses the 'snmpget' command included with the NET-SNMP package.\n\
918   If you don't have the package installed, you will need to download it from\n\
919   http://net-snmp.sourceforge.net before you can use this plugin.\n"));
921         printf (_("\
922 - Multiple OIDs may be indicated by a comma- or space-delimited list (lists with\n\
923   internal spaces must be quoted) [max 8 OIDs]\n"));
925         printf (_("\
926 - Ranges are inclusive and are indicated with colons. When specified as\n\
927   'min:max' a STATE_OK will be returned if the result is within the indicated\n\
928   range or is equal to the upper or lower bound. A non-OK state will be\n\
929   returned if the result is outside the specified range.\n"));
931         printf (_("\
932 - If specified in the order 'max:min' a non-OK state will be returned if the\n\
933   result is within the (inclusive) range.\n"));
935         printf (_("\
936 - Upper or lower bounds may be omitted to skip checking the respective limit.\n\
937 - Bare integers are interpreted as upper limits.\n\
938 - When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'\n\
939 - Note that only one string and one regex may be checked at present\n\
940 - All evaluation methods other than PR, STR, and SUBSTR expect that the value\n\
941   returned from the SNMP query is an unsigned integer.\n"));
943         printf (_(UT_SUPPORT));
948 void
949 print_usage (void)
951         printf (_("\
952 Usage: %s -H <ip_address> -o <OID> [-w warn_range] [-c crit_range] \n\
953   [-C community] [-s string] [-r regex] [-R regexi] [-t timeout]\n\
954   [-l label] [-u units] [-p port-number] [-d delimiter]\n\
955   [-D output-delimiter] [-m miblist] [-P snmp version]\n\
956   [-L seclevel] [-U secname] [-a authproto] [-A authpasswd]\n\
957   [-X privpasswd]\n"), progname);
958         printf (_(UT_HLP_VRS), progname, progname);