Code

markup for translation
[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 = DEFAULT_COMMUNITY;
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 = "SNMP";
97 char *units = "";
98 char *port = DEFAULT_PORT;
99 char string_value[MAX_INPUT_BUFFER] = "";
100 char **labels = NULL;
101 char **unitv = NULL;
102 int nlabels = 0;
103 int labels_size = 8;
104 int nunits = 0;
105 int 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 = DEFAULT_DELIMITER;
116 char *output_delim = DEFAULT_OUTPUT_DELIMITER;
117 char *miblist = DEFAULT_MIBLIST;
123 \f
124 int
125 main (int argc, char **argv)
127         int i = 0;
128         int iresult = STATE_UNKNOWN;
129         int found = 0;
130         int result = STATE_DEPENDENT;
131         char input_buffer[MAX_INPUT_BUFFER];
132         char *command_line = NULL;
133         char *response = NULL;
134         char *outbuff = "";
135         char *output = "";
136         char *ptr = NULL;
137         char *p2 = NULL;
138         char *show = NULL;
140         labels = malloc (labels_size);
141         unitv = malloc (unitv_size);
142         for (i = 0; i < MAX_OIDS; i++)
143                 eval_method[i] = CHECK_UNDEF;
144         i = 0;
146         if (process_arguments (argc, argv) == ERROR)
147                 usage (_("Incorrect arguments supplied\n"));
149         /* create the command line to execute */
150         asprintf (&command_line, "%s -t 1 -r %d -m %s -v %s %s %s:%s %s",
151                   PATH_TO_SNMPGET, timeout_interval - 1, miblist, proto,
152                   authpriv, server_address, port, oid);
153         if (verbose)
154                 printf ("%s\n", command_line);
156         /* run the command */
157         child_process = spopen (command_line);
158         if (child_process == NULL) {
159                 printf (_("Could not open pipe: %s\n"), command_line);
160                 exit (STATE_UNKNOWN);
161         }
163         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
164         if (child_stderr == NULL) {
165                 printf (_("Could not open stderr for %s\n"), command_line);
166         }
168         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
169                 asprintf (&output, "%s%s", output, input_buffer);
171         if (verbose)
172                 printf ("%s\n", output);
174         ptr = output;
176         while (ptr) {
178                 ptr = strstr (ptr, delimiter);
179                 if (ptr == NULL)
180                         break;
182                 ptr += strlen (delimiter);
183                 ptr += strspn (ptr, " ");
185                 found++;
187                 if (ptr[0] == '"') {
188                         ptr++;
189                         response = strpcpy (response, ptr, "\"");
190                         ptr = strpbrk (ptr, "\"");
191                         ptr += strspn (ptr, "\"\n");
192                 }
193                 else {
194                         response = strpcpy (response, ptr, "\n");
195                         ptr = strpbrk (ptr, "\n");
196                         ptr += strspn (ptr, "\n");
197                         while
198                                 (strstr (ptr, delimiter) &&
199                                  strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter)) {
200                                 response = strpcat (response, ptr, "\n");
201                                 ptr = strpbrk (ptr, "\n");
202                         }
203                         if (ptr && strstr (ptr, delimiter) == NULL) {
204                                 response = strscat (response, ptr);
205                                 ptr = NULL;
206                         }
207                 }
209                 /* We strip out the datatype indicator for PHBs */
210                 if (strstr (response, "Gauge: "))
211                         show = strstr (response, "Gauge: ") + 7;
212                 else if (strstr (response, "Gauge32: "))
213                         show = strstr (response, "Gauge32: ") + 9;
214                 else if (strstr (response, "Counter32: "))
215                         show = strstr (response, "Counter32: ") + 11;
216                 else if (strstr (response, "INTEGER: "))
217                         show = strstr (response, "INTEGER: ") + 9;
218                 else if (strstr (response, "STRING: "))
219                         show = strstr (response, "STRING: ") + 8;
220                 else
221                         show = response;
222                 p2 = show;
224                 iresult = STATE_DEPENDENT;
226                 /* Process this block for integer comparisons */
227                 if (eval_method[i] & CRIT_GT ||
228                     eval_method[i] & CRIT_LT ||
229                     eval_method[i] & CRIT_GE ||
230                     eval_method[i] & CRIT_LE ||
231                     eval_method[i] & CRIT_EQ ||
232                     eval_method[i] & CRIT_NE ||
233                     eval_method[i] & WARN_GT ||
234                     eval_method[i] & WARN_LT ||
235                     eval_method[i] & WARN_GE ||
236                     eval_method[i] & WARN_LE ||
237                     eval_method[i] & WARN_EQ ||
238                     eval_method[i] & WARN_NE) {
239                         p2 = strpbrk (p2, "0123456789");
240                         if (p2 == NULL) 
241                                 die (STATE_UNKNOWN,"No valid data returned");
242                         response_value[i] = strtoul (p2, NULL, 10);
243                         iresult = check_num (i);
244                         asprintf (&show, "%lu", response_value[i]);
245                 }
247                 /* Process this block for string matching */
248                 else if (eval_method[i] & CRIT_STRING) {
249                         if (strcmp (response, string_value))
250                                 iresult = STATE_CRITICAL;
251                         else
252                                 iresult = STATE_OK;
253                 }
255                 /* Process this block for regex matching */
256                 else if (eval_method[i] & CRIT_REGEX) {
257 #ifdef HAVE_REGEX_H
258                         excode = regexec (&preg, response, 10, pmatch, eflags);
259                         if (excode == 0) {
260                                 iresult = STATE_OK;
261                         }
262                         else if (excode != REG_NOMATCH) {
263                                 regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
264                                 printf (_("Execute Error: %s\n"), errbuf);
265                                 exit (STATE_CRITICAL);
266                         }
267                         else {
268                                 iresult = STATE_CRITICAL;
269                         }
270 #else
271                         printf (_("%s UNKNOWN: call for regex which was not a compiled option"), label);
272                         exit (STATE_UNKNOWN);
273 #endif
274                 }
276                 /* Process this block for existence-nonexistence checks */
277                 else {
278                         if (eval_method[i] & CRIT_PRESENT)
279                                 iresult = STATE_CRITICAL;
280                         else if (eval_method[i] & WARN_PRESENT)
281                                 iresult = STATE_WARNING;
282                         else if (response && iresult == STATE_DEPENDENT) 
283                                 iresult = STATE_OK;
284                 }
286                 /* Result is the worst outcome of all the OIDs tested */
287                 result = max_state (result, iresult);
289                 /* Prepend a label for this OID if there is one */
290                 if (nlabels > 1 && i < nlabels && labels[i] != NULL)
291                         asprintf (&outbuff, "%s%s%s %s%s%s", outbuff,
292                                   (i == 0) ? " " : output_delim,
293                                   labels[i], mark (iresult), show, mark (iresult));
294                 else
295                         asprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim,
296                                   mark (iresult), show, mark (iresult));
298                 /* Append a unit string for this OID if there is one */
299                 if (nunits > 0 && i < nunits && unitv[i] != NULL)
300                         asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
302                 i++;
304         }                                                                                                                       /* end while (ptr) */
306         if (found == 0)
307                 die (STATE_UNKNOWN,
308                      _("%s problem - No data recieved from host\nCMD: %s\n"),
309                      label,
310                      command_line);
312         /* WARNING if output found on stderr */
313         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
314                 result = max_state (result, STATE_WARNING);
316         /* close stderr */
317         (void) fclose (child_stderr);
319         /* close the pipe */
320         if (spclose (child_process))
321                 result = max_state (result, STATE_WARNING);
323 /*      if (nunits == 1 || i == 1) */
324 /*              printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units); */
325 /*      else */
326         printf ("%s %s -%s\n", label, state_text (result), outbuff);
328         return result;
335 \f
336 /* process command-line arguments */
337 int
338 process_arguments (int argc, char **argv)
340         char *ptr;
341         int c = 1;
342         int j = 0, jj = 0, ii = 0;
344         int option_index = 0;
345         static struct option long_options[] = {
346                 STD_LONG_OPTS,
347                 {"community", required_argument, 0, 'C'},
348                 {"oid", required_argument, 0, 'o'},
349                 {"object", required_argument, 0, 'o'},
350                 {"delimiter", required_argument, 0, 'd'},
351                 {"output-delimiter", required_argument, 0, 'D'},
352                 {"string", required_argument, 0, 's'},
353                 {"regex", required_argument, 0, 'r'},
354                 {"ereg", required_argument, 0, 'r'},
355                 {"eregi", required_argument, 0, 'R'},
356                 {"label", required_argument, 0, 'l'},
357                 {"units", required_argument, 0, 'u'},
358                 {"port", required_argument, 0, 'p'},
359                 {"miblist", required_argument, 0, 'm'},
360                 {"protocol", required_argument, 0, 'P'},
361                 {"seclevel", required_argument, 0, 'L'},
362                 {"secname", required_argument, 0, 'U'},
363                 {"authproto", required_argument, 0, 'a'},
364                 {"authpasswd", required_argument, 0, 'A'},
365                 {"privpasswd", required_argument, 0, 'X'},
366                 {0, 0, 0, 0}
367         };
369         if (argc < 2)
370                 return ERROR;
372         /* reverse compatibility for very old non-POSIX usage forms */
373         for (c = 1; c < argc; c++) {
374                 if (strcmp ("-to", argv[c]) == 0)
375                         strcpy (argv[c], "-t");
376                 if (strcmp ("-wv", argv[c]) == 0)
377                         strcpy (argv[c], "-w");
378                 if (strcmp ("-cv", argv[c]) == 0)
379                         strcpy (argv[c], "-c");
380         }
382         while (1) {
383                 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:",
384                                                                          long_options, &option_index);
386                 if (c == -1 || c == EOF)
387                         break;
389                 switch (c) {
390                 case '?':       /* usage */
391                         usage3 ("Unknown argument", optopt);
392                 case 'h':       /* help */
393                         print_help ();
394                         exit (STATE_OK); 
395                 case 'V':       /* version */
396                         print_revision (progname, revision);
397                         exit (STATE_OK);
398                 case 'v': /* verbose */
399                         verbose = TRUE;
400                         break;
402         /* Connection info */
403                 case 'C':                                                                       /* group or community */
404                         community = strscpy (community, optarg);
405                         break;
406                 case 'H':                                                                       /* Host or server */
407                         server_address = strscpy (server_address, optarg);
408                         break;
409                 case 'p':       /* TCP port number */
410                         port = strscpy(port, optarg);
411                         break;
412                 case 'm':      /* List of MIBS  */
413                         miblist = strscpy(miblist, optarg);
414                         break;
415                 case 'P':     /* SNMP protocol version */
416                         proto = strscpy(proto, optarg);
417                         break;
418                 case 'L':     /* security level */
419                         seclevel = strscpy(seclevel,optarg);
420                         break;
421                 case 'U':     /* security username */
422                         secname = strscpy(secname, optarg);
423                         break;
424                 case 'a':     /* auth protocol */
425                         asprintf (&authproto, optarg);
426                         break;
427                 case 'A':     /* auth passwd */
428                         authpasswd = strscpy(authpasswd, optarg);
429                         break;
430                 case 'X':     /* priv passwd */
431                         privpasswd = strscpy(privpasswd, optarg);
432                         break;
433                 case 't':       /* timeout period */
434                         if (!is_integer (optarg))
435                                 usage2 (_("Timeout Interval must be an integer"), optarg);
436                         timeout_interval = atoi (optarg);
437                         break;
439         /* Test parameters */
440                 case 'c':                                                                       /* critical time threshold */
441                         if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
442                                 printf (_("Invalid critical threshold: %s\n"), optarg);
443                                 print_usage ();
444                                 exit (STATE_UNKNOWN);
445                         }
446                         for (ptr = optarg; ptr && jj < MAX_OIDS; jj++) {
447                                 if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
448                                         eval_method[jj] |= CRIT_LT;
449                                 if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
450                                         eval_method[jj] |= CRIT_GT;
451                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
452                         }
453                         break;
454                 case 'w':                                                                       /* warning time threshold */
455                         if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
456                                 printf (_("Invalid warning threshold: %s\n"), optarg);
457                                 print_usage ();
458                                 exit (STATE_UNKNOWN);
459                         }
460                         for (ptr = optarg; ptr && ii < MAX_OIDS; ii++) {
461                                 if (lu_getll (&lower_warn_lim[ii], ptr) == 1)
462                                         eval_method[ii] |= WARN_LT;
463                                 if (lu_getul (&upper_warn_lim[ii], ptr) == 1)
464                                         eval_method[ii] |= WARN_GT;
465                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
466                         }
467                         break;
468                 case 'o':                                                                       /* object identifier */
469                 case 'e': /* PRELIMINARY - may change */
470                 case 'E': /* PRELIMINARY - may change */
471                         for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
472                                 ptr[0] = ' '; /* relpace comma with space */
473                         for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
474                                 j++; /* count OIDs */
475                         asprintf (&oid, "%s %s", (oid?oid:""), optarg);
476                         if (c == 'E' || c == 'e') {
477                                 jj++;
478                                 ii++;
479                         }
480                         if (c == 'E') 
481                                 eval_method[j+1] |= WARN_PRESENT;
482                         else if (c == 'e')
483                                 eval_method[j+1] |= CRIT_PRESENT;
484                         break;
485                 case 's':                                                                       /* string or substring */
486                         strncpy (string_value, optarg, sizeof (string_value) - 1);
487                         string_value[sizeof (string_value) - 1] = 0;
488                         eval_method[jj++] = CRIT_STRING;
489                         ii++;
490                         break;
491                 case 'R':                                                                       /* regex */
492 #ifdef HAVE_REGEX_H
493                         cflags = REG_ICASE;
494 #endif
495                 case 'r':                                                                       /* regex */
496 #ifdef HAVE_REGEX_H
497                         cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
498                         strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
499                         regex_expect[sizeof (regex_expect) - 1] = 0;
500                         errcode = regcomp (&preg, regex_expect, cflags);
501                         if (errcode != 0) {
502                                 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
503                                 printf (_("Could Not Compile Regular Expression"));
504                                 return ERROR;
505                         }
506                         eval_method[jj++] = CRIT_REGEX;
507                         ii++;
508 #else
509                         printf (_("%s UNKNOWN: call for regex which was not a compiled option"), label);
510                         exit (STATE_UNKNOWN);
511 #endif
512                         break;
514         /* Format */
515                 case 'd':                                                                       /* delimiter */
516                         delimiter = strscpy (delimiter, optarg);
517                         break;
518                 case 'D':                                                                       /* output-delimiter */
519                         output_delim = strscpy (output_delim, optarg);
520                         break;
521                 case 'l':                                                                       /* label */
522                         label = optarg;
523                         nlabels++;
524                         if (nlabels >= labels_size) {
525                                 labels_size += 8;
526                                 labels = realloc (labels, labels_size);
527                                 if (labels == NULL)
528                                         die (STATE_UNKNOWN,
529                                                                                  _("Could not realloc() labels[%d]"), nlabels);
530                         }
531                         labels[nlabels - 1] = optarg;
532                         ptr = thisarg (optarg);
533                         if (strstr (ptr, "'") == ptr)
534                                 labels[nlabels - 1] = ptr + 1;
535                         else
536                                 labels[nlabels - 1] = ptr;
537                         while (ptr && (ptr = nextarg (ptr))) {
538                                 if (nlabels >= labels_size) {
539                                         labels_size += 8;
540                                         labels = realloc (labels, labels_size);
541                                         if (labels == NULL)
542                                                 die (STATE_UNKNOWN, _("Could not realloc() labels\n"));
543                                 }
544                                 labels++;
545                                 ptr = thisarg (ptr);
546                                 if (strstr (ptr, "'") == ptr)
547                                         labels[nlabels - 1] = ptr + 1;
548                                 else
549                                         labels[nlabels - 1] = ptr;
550                         }
551                         break;
552                 case 'u':                                                                       /* units */
553                         units = optarg;
554                         nunits++;
555                         if (nunits >= unitv_size) {
556                                 unitv_size += 8;
557                                 unitv = realloc (unitv, unitv_size);
558                                 if (unitv == NULL)
559                                         die (STATE_UNKNOWN,
560                                                                                  _("Could not realloc() units [%d]\n"), nunits);
561                         }
562                         unitv[nunits - 1] = optarg;
563                         ptr = thisarg (optarg);
564                         if (strstr (ptr, "'") == ptr)
565                                 unitv[nunits - 1] = ptr + 1;
566                         else
567                                 unitv[nunits - 1] = ptr;
568                         while (ptr && (ptr = nextarg (ptr))) {
569                                 if (nunits >= unitv_size) {
570                                         unitv_size += 8;
571                                         unitv = realloc (unitv, unitv_size);
572                                         if (units == NULL)
573                                                 die (STATE_UNKNOWN, _("Could not realloc() units\n"));
574                                 }
575                                 nunits++;
576                                 ptr = thisarg (ptr);
577                                 if (strstr (ptr, "'") == ptr)
578                                         unitv[nunits - 1] = ptr + 1;
579                                 else
580                                         unitv[nunits - 1] = ptr;
581                         }
582                         break;
584                 }
585         }
587         if (server_address == NULL)
588                 asprintf (&server_address, argv[optind]);
590         return validate_arguments ();
596 /******************************************************************************
598 @@-
599 <sect3>
600 <title>validate_arguments</title>
602 <para>&PROTO_validate_arguments;</para>
604 <para>Given a database name, this function returns TRUE if the string
605 is a valid PostgreSQL database name, and returns false if it is
606 not.</para>
608 <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
609 characters long and consist of letters, numbers, and underscores. The
610 first character cannot be a number, however.</para>
612 </sect3>
613 -@@
614 ******************************************************************************/
616 int
617 validate_arguments ()
620         /* Need better checks to verify seclevel and authproto choices */
621         
622         if (seclevel == NULL) 
623                 asprintf (&seclevel, "noAuthNoPriv");
626         if (authproto == NULL ) 
627                 asprintf(&authproto, DEFAULT_AUTH_PROTOCOL);
628         
629          
630         
631         if (proto == NULL || (strcmp(proto,DEFAULT_PROTOCOL) == 0) ) {        /* default protocol version */
632                 asprintf(&proto, DEFAULT_PROTOCOL);
633                 asprintf(&authpriv, "%s%s", "-c ", community);
634         }
635         else if ( strcmp (proto, "3") == 0 ) {                 /* snmpv3 args */
636                 asprintf(&proto, "%s", "3");
637                 
638                 if ( (strcmp(seclevel, "noAuthNoPriv") == 0) || seclevel == NULL ) {
639                         asprintf(&authpriv, "%s", "-l noAuthNoPriv" );
640                 }
641                 else if ( strcmp(seclevel, "authNoPriv") == 0 ) {
642                         if ( secname == NULL || authpasswd == NULL) {
643                                 printf (_("Missing secname (%s) or authpassword (%s) ! \n)"),secname, authpasswd );
644                                 print_usage ();
645                                 exit (STATE_UNKNOWN);
646                         }
647                         asprintf(&authpriv, "-l authNoPriv -a %s -u %s -A %s ", authproto, secname, authpasswd);
648                 }
649                 else if ( strcmp(seclevel, "authPriv") == 0 ) {
650                         if ( secname == NULL || authpasswd == NULL || privpasswd == NULL ) {
651                                 printf (("Missing secname (%s), authpassword (%s), or privpasswd (%s)! \n"),secname, authpasswd,privpasswd );
652                                 print_usage ();
653                                 exit (STATE_UNKNOWN);
654                         }
655                         asprintf(&authpriv, "-l authPriv -a %s -u %s -A %s -x DES -X %s ", authproto, secname, authpasswd, privpasswd);
656                 }
657                 
658                                                                         
659         }
660         else {
661                 printf (_("Invalid SNMP version: %s\n"), proto);
662                 print_usage ();
663                 exit (STATE_UNKNOWN);                           
664         }
665                         
666         
667         
669         return OK;
676 \f
677 char *
678 clarify_message (char *msg)
680         int i = 0;
681         int foo;
682         char tmpmsg_c[MAX_INPUT_BUFFER];
683         char *tmpmsg = (char *) &tmpmsg_c;
684         tmpmsg = strcpy (tmpmsg, msg);
685         if (!strncmp (tmpmsg, " Hex:", 5)) {
686                 tmpmsg = strtok (tmpmsg, ":");
687                 while ((tmpmsg = strtok (NULL, " "))) {
688                         foo = strtol (tmpmsg, NULL, 16);
689                         /* Translate chars that are not the same value in the printers
690                          * character set.
691                          */
692                         switch (foo) {
693                         case 208:
694                                 {
695                                         foo = 197;
696                                         break;
697                                 }
698                         case 216:
699                                 {
700                                         foo = 196;
701                                         break;
702                                 }
703                         }
704                         msg[i] = foo;
705                         i++;
706                 }
707                 msg[i] = 0;
708         }
709         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_warn_lim[i] > upper_warn_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;
760 int
761 lu_getll (unsigned long *ll, char *str)
763         char tmp[100];
764         if (strchr (str, ':') == NULL)
765                 return 0;
766         if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
767                 return 0;
768         if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
769                 return 1;
770         return 0;
776 int
777 lu_getul (unsigned long *ul, char *str)
779         char tmp[100];
780         if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
781                 return 1;
782         if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
783                 return 1;
784         if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
785                 return 1;
786         return 0;
792 /* trim leading whitespace
793          if there is a leading quote, make sure it balances */
795 char *
796 thisarg (char *str)
798         str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
799         if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
800                 if (strlen (str) == 1 || !strstr (str + 1, "'"))
801                         die (STATE_UNKNOWN, "Unbalanced quotes\n");
802         }
803         return str;
809 /* if there's a leading quote, advance to the trailing quote
810          set the trailing quote to '\x0'
811          if the string continues, advance beyond the comma */
813 char *
814 nextarg (char *str)
816         if (strstr (str, "'") == str) {
817                 if (strlen (str) > 1) {
818                         str = strstr (str + 1, "'");
819                         str[0] = 0;
820                         return (++str);
821                 }
822                 else {
823                         str[0] = 0;
824                         return NULL;
825                 }
826         }
827         if (strstr (str, ",") == str) {
828                 if (strlen (str) > 1) {
829                         str[0] = 0;
830                         return (++str);
831                 }
832                 else {
833                         str[0] = 0;
834                         return NULL;
835                 }
836         }
837         if ((str = strstr (str, ",")) && strlen (str) > 1) {
838                 str[0] = 0;
839                 return (++str);
840         }
841         return NULL;
848 \f
849 void
850 print_help (void)
852         print_revision (progname, revision);
854         printf (_(COPYRIGHT), copyright, email);
856         printf (_("\
857 Check status of remote machines and obtain sustem information via SNMP\n\n"));
859         print_usage ();
861         printf (_(UT_HELP_VRSN));
863         printf (_(UT_HOST_PORT), 'p', DEFAULT_PORT);
865         /* SNMP and Authentication Protocol */
866         printf (_("\
867  -P, --protocol=[1|3]\n\
868     SNMP protocol version\n\
869  -L, --seclevel=[noAuthNoPriv|authNoPriv|authPriv]\n\
870     SNMPv3 securityLevel\n\
871  -a, --authproto=[MD5|SHA]\n\
872     SNMPv3 auth proto\n"));
874         /* Authentication Tokens*/
875         printf (_("\
876  -C, --community=STRING\n\
877     Optional community string for SNMP communication\n\
878     (default is \"%s\")\n\
879  -U, --secname=USERNAME\n\
880     SNMPv3 username\n\
881  -A, --authpassword=PASSWORD\n\
882     SNMPv3 authentication password\n\
883  -X, --privpasswd=PASSWORD\n\
884     SNMPv3 crypt passwd (DES)\n"), DEFAULT_COMMUNITY);
886         /* OID Stuff */
887         printf (_("\
888  -o, --oid=OID(s)\n\
889     Object identifier(s) whose value you wish to query\n\
890  -m, --miblist=STRING\n\
891     List of MIBS to be loaded (default = ALL)\n -d, --delimiter=STRING\n\
892     Delimiter to use when parsing returned data. Default is \"%s\"\n\
893     Any data on the right hand side of the delimiter is considered\n\
894     to be the data that should be used in the evaluation.\n"), DEFAULT_DELIMITER);
896         /* Tests Against Integers */
897         printf (_("\
898  -w, --warning=INTEGER_RANGE(s)\n\
899     Range(s) which will not result in a WARNING status\n\
900  -c, --critical=INTEGER_RANGE(s)\n\
901     Range(s) which will not result in a CRITICAL status\n"));
903         /* Tests Against Strings */
904         printf (_("\
905  -s, --string=STRING\n\
906     Return OK state (for that OID) if STRING is an exact match\n\
907  -r, --ereg=REGEX\n\
908     Return OK state (for that OID) if extended regular expression REGEX matches\n\
909  -R, --eregi=REGEX\n\
910     Return OK state (for that OID) if case-insensitive extended REGEX matches\n\
911  -l, --label=STRING\n\
912     Prefix label for output from plugin (default -s 'SNMP')\n"));
914         /* Output Formatting */
915         printf (_("\
916  -u, --units=STRING\n\
917     Units label(s) for output data (e.g., 'sec.').\n\
918  -D, --output-delimiter=STRING\n\
919     Separates output on multiple OID requests\n"));
921         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
923         printf (_(UT_VERBOSE));
925         printf (_("\n\
926 - This plugin uses the 'snmpget' command included with the NET-SNMP package.\n\
927   If you don't have the package installed, you will need to download it from\n\
928   http://net-snmp.sourceforge.net before you can use this plugin.\n"));
930         printf (_("\
931 - Multiple OIDs may be indicated by a comma- or space-delimited list (lists with\n\
932   internal spaces must be quoted) [max 8 OIDs]\n"));
934         printf (_("\
935 - Ranges are inclusive and are indicated with colons. When specified as\n\
936   'min:max' a STATE_OK will be returned if the result is within the indicated\n\
937   range or is equal to the upper or lower bound. A non-OK state will be\n\
938   returned if the result is outside the specified range.\n"));
940         printf (_("\
941 - If specified in the order 'max:min' a non-OK state will be returned if the\n\
942   result is within the (inclusive) range.\n"));
944         printf (_("\
945 - Upper or lower bounds may be omitted to skip checking the respective limit.\n\
946 - Bare integers are interpreted as upper limits.\n\
947 - When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'\n\
948 - Note that only one string and one regex may be checked at present\n\
949 - All evaluation methods other than PR, STR, and SUBSTR expect that the value\n\
950   returned from the SNMP query is an unsigned integer.\n"));
952         printf (_(UT_SUPPORT));
955 void
956 print_usage (void)
958         printf (_("\
959 Usage: %s -H <ip_address> -o <OID> [-w warn_range] [-c crit_range] \n\
960   [-C community] [-s string] [-r regex] [-R regexi] [-t timeout]\n\
961   [-l label] [-u units] [-p port-number] [-d delimiter]\n\
962   [-D output-delimiter] [-m miblist] [-P snmp version]\n\
963   [-L seclevel] [-U secname] [-a authproto] [-A authpasswd]\n\
964   [-X privpasswd]\n"), progname);
965         printf (_(UT_HLP_VRS), progname, progname);