Code

more internationalization 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  $Id$
18  
19 ******************************************************************************/
21 const char *progname = "check_snmp";
22 const char *revision = "$Revision$";
23 const char *copyright = "1999-2004";
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) == ERROR)
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 (show, 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 (_("Call for regex which was not a compiled option"));
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                         usage2 (_("Unknown argument"), optarg);
402                 case 'h':       /* help */
403                         print_help ();
404                         exit (STATE_OK); 
405                 case 'V':       /* version */
406                         print_revision (progname, revision);
407                         exit (STATE_OK);
408                 case 'v': /* verbose */
409                         verbose = TRUE;
410                         break;
412         /* Connection info */
413                 case 'C':                                                                       /* group or community */
414                         community = optarg;
415                         break;
416                 case 'H':                                                                       /* Host or server */
417                         server_address = optarg;
418                         break;
419                 case 'p':       /* TCP port number */
420                         port = optarg;
421                         break;
422                 case 'm':      /* List of MIBS  */
423                         miblist = optarg;
424                         break;
425                 case 'P':     /* SNMP protocol version */
426                         proto = optarg;
427                         break;
428                 case 'L':     /* security level */
429                         seclevel = optarg;
430                         break;
431                 case 'U':     /* security username */
432                         secname = optarg;
433                         break;
434                 case 'a':     /* auth protocol */
435                         authproto = optarg;
436                         break;
437                 case 'A':     /* auth passwd */
438                         authpasswd = optarg;
439                         break;
440                 case 'X':     /* priv passwd */
441                         privpasswd = optarg;
442                         break;
443                 case 't':       /* timeout period */
444                         if (!is_integer (optarg))
445                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
446                         else
447                                 timeout_interval = atoi (optarg);
448                         break;
450         /* Test parameters */
451                 case 'c':                                                                       /* critical time threshold */
452                         if (strspn (optarg, "0123456789:,") < strlen (optarg))
453                                 usage2 (_("Invalid critical threshold: %s\n"), optarg);
454                         for (ptr = optarg; ptr && jj < MAX_OIDS; jj++) {
455                                 if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
456                                         eval_method[jj] |= CRIT_LT;
457                                 if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
458                                         eval_method[jj] |= CRIT_GT;
459                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
460                         }
461                         break;
462                 case 'w':                                                                       /* warning time threshold */
463                         if (strspn (optarg, "0123456789:,") < strlen (optarg))
464                                 usage2 (_("Invalid warning threshold: %s\n"), optarg);
465                         for (ptr = optarg; ptr && ii < MAX_OIDS; ii++) {
466                                 if (lu_getll (&lower_warn_lim[ii], ptr) == 1)
467                                         eval_method[ii] |= WARN_LT;
468                                 if (lu_getul (&upper_warn_lim[ii], ptr) == 1)
469                                         eval_method[ii] |= WARN_GT;
470                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
471                         }
472                         break;
473                 case 'o':                                                                       /* object identifier */
474                 case 'e': /* PRELIMINARY - may change */
475                 case 'E': /* PRELIMINARY - may change */
476                         for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
477                                 ptr[0] = ' '; /* relpace comma with space */
478                         for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
479                                 j++; /* count OIDs */
480                         asprintf (&oid, "%s %s", (oid?oid:""), optarg);
481                         if (c == 'E' || c == 'e') {
482                                 jj++;
483                                 ii++;
484                         }
485                         if (c == 'E') 
486                                 eval_method[j+1] |= WARN_PRESENT;
487                         else if (c == 'e')
488                                 eval_method[j+1] |= CRIT_PRESENT;
489                         break;
490                 case 's':                                                                       /* string or substring */
491                         strncpy (string_value, optarg, sizeof (string_value) - 1);
492                         string_value[sizeof (string_value) - 1] = 0;
493                         eval_method[jj++] = CRIT_STRING;
494                         ii++;
495                         break;
496                 case 'R':                                                                       /* regex */
497 #ifdef HAVE_REGEX_H
498                         cflags = REG_ICASE;
499 #endif
500                 case 'r':                                                                       /* regex */
501 #ifdef HAVE_REGEX_H
502                         cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
503                         strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
504                         regex_expect[sizeof (regex_expect) - 1] = 0;
505                         errcode = regcomp (&preg, regex_expect, cflags);
506                         if (errcode != 0) {
507                                 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
508                                 printf (_("Could Not Compile Regular Expression"));
509                                 return ERROR;
510                         }
511                         eval_method[jj++] = CRIT_REGEX;
512                         ii++;
513 #else
514                         printf (_("call for regex which was not a compiled option"));
515                         exit (STATE_UNKNOWN);
516 #endif
517                         break;
519         /* Format */
520                 case 'd':                                                                       /* delimiter */
521                         delimiter = strscpy (delimiter, optarg);
522                         break;
523                 case 'D':                                                                       /* output-delimiter */
524                         output_delim = strscpy (output_delim, optarg);
525                         break;
526                 case 'l':                                                                       /* label */
527                         label = optarg;
528                         nlabels++;
529                         if (nlabels >= labels_size) {
530                                 labels_size += 8;
531                                 labels = realloc (labels, labels_size);
532                                 if (labels == NULL)
533                                         die (STATE_UNKNOWN, _("Could not reallocate labels[%d]"), nlabels);
534                         }
535                         labels[nlabels - 1] = optarg;
536                         ptr = thisarg (optarg);
537                         labels[nlabels - 1] = ptr;
538                         if (strstr (ptr, "'") == ptr)
539                                 labels[nlabels - 1] = ptr + 1;
540                         while (ptr && (ptr = nextarg (ptr))) {
541                                 if (nlabels >= labels_size) {
542                                         labels_size += 8;
543                                         labels = realloc (labels, labels_size);
544                                         if (labels == NULL)
545                                                 die (STATE_UNKNOWN, _("Could not reallocate labels\n"));
546                                 }
547                                 labels++;
548                                 ptr = thisarg (ptr);
549                                 if (strstr (ptr, "'") == ptr)
550                                         labels[nlabels - 1] = ptr + 1;
551                                 else
552                                         labels[nlabels - 1] = ptr;
553                         }
554                         break;
555                 case 'u':                                                                       /* units */
556                         units = optarg;
557                         nunits++;
558                         if (nunits >= unitv_size) {
559                                 unitv_size += 8;
560                                 unitv = realloc (unitv, unitv_size);
561                                 if (unitv == NULL)
562                                         die (STATE_UNKNOWN, _("Could not reallocate units [%d]\n"), nunits);
563                         }
564                         unitv[nunits - 1] = optarg;
565                         ptr = thisarg (optarg);
566                         unitv[nunits - 1] = ptr;
567                         if (strstr (ptr, "'") == ptr)
568                                 unitv[nunits - 1] = ptr + 1;
569                         while (ptr && (ptr = nextarg (ptr))) {
570                                 if (nunits >= unitv_size) {
571                                         unitv_size += 8;
572                                         unitv = realloc (unitv, unitv_size);
573                                         if (units == NULL)
574                                                 die (STATE_UNKNOWN, _("Could not realloc() units\n"));
575                                 }
576                                 nunits++;
577                                 ptr = thisarg (ptr);
578                                 if (strstr (ptr, "'") == ptr)
579                                         unitv[nunits - 1] = ptr + 1;
580                                 else
581                                         unitv[nunits - 1] = ptr;
582                         }
583                         break;
585                 }
586         }
588         if (server_address == NULL)
589                 server_address = argv[optind];
591         if (community == NULL)
592                 community = strdup (DEFAULT_COMMUNITY);
594         return validate_arguments ();
598 /******************************************************************************
600 @@-
601 <sect3>
602 <title>validate_arguments</title>
604 <para>&PROTO_validate_arguments;</para>
606 <para>Given a database name, this function returns TRUE if the string
607 is a valid PostgreSQL database name, and returns false if it is
608 not.</para>
610 <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
611 characters long and consist of letters, numbers, and underscores. The
612 first character cannot be a number, however.</para>
614 </sect3>
615 -@@
616 ******************************************************************************/
620 int
621 validate_arguments ()
624         /* Need better checks to verify seclevel and authproto choices */
625         
626         if (seclevel == NULL) 
627                 asprintf (&seclevel, "noAuthNoPriv");
630         if (authproto == NULL ) 
631                 asprintf(&authproto, DEFAULT_AUTH_PROTOCOL);
632         
633          
634         
635         if (proto == NULL || (strcmp(proto,DEFAULT_PROTOCOL) == 0) ) {        /* default protocol version */
636                 asprintf(&proto, DEFAULT_PROTOCOL);
637                 asprintf(&authpriv, "%s%s", "-c ", community);
638         }
639         else if ( strcmp (proto, "3") == 0 ) {                 /* snmpv3 args */
640                 asprintf(&proto, "%s", "3");
641                 
642                 if ( (strcmp(seclevel, "noAuthNoPriv") == 0) || seclevel == NULL ) {
643                         asprintf(&authpriv, "%s", "-l noAuthNoPriv" );
644                 }
645                 else if ( strcmp(seclevel, "authNoPriv") == 0 ) {
646                         if ( secname == NULL || authpasswd == NULL) {
647                                 printf (_("Missing secname (%s) or authpassword (%s) ! \n"),secname, authpasswd );
648                                 print_usage ();
649                                 exit (STATE_UNKNOWN);
650                         }
651                         asprintf(&authpriv, "-l authNoPriv -a %s -u %s -A %s ", authproto, secname, authpasswd);
652                 }
653                 else if ( strcmp(seclevel, "authPriv") == 0 ) {
654                         if ( secname == NULL || authpasswd == NULL || privpasswd == NULL ) {
655                                 printf (_("Missing secname (%s), authpassword (%s), or privpasswd (%s)! \n"),secname, authpasswd,privpasswd );
656                                 print_usage ();
657                                 exit (STATE_UNKNOWN);
658                         }
659                         asprintf(&authpriv, "-l authPriv -a %s -u %s -A %s -x DES -X %s ", authproto, secname, authpasswd, privpasswd);
660                 }
661                 
662         }
663         else {
664                 usage2 (_("Invalid SNMP version"), proto);
665         }
666                         
667         return OK;
672 char *
673 clarify_message (char *msg)
675         int i = 0;
676         int foo;
677         char tmpmsg_c[MAX_INPUT_BUFFER];
678         char *tmpmsg = (char *) &tmpmsg_c;
679         tmpmsg = strcpy (tmpmsg, msg);
680         if (!strncmp (tmpmsg, " Hex:", 5)) {
681                 tmpmsg = strtok (tmpmsg, ":");
682                 while ((tmpmsg = strtok (NULL, " "))) {
683                         foo = strtol (tmpmsg, NULL, 16);
684                         /* Translate chars that are not the same value in the printers
685                          * character set.
686                          */
687                         switch (foo) {
688                         case 208:
689                                 {
690                                         foo = 197;
691                                         break;
692                                 }
693                         case 216:
694                                 {
695                                         foo = 196;
696                                         break;
697                                 }
698                         }
699                         msg[i] = foo;
700                         i++;
701                 }
702                 msg[i] = 0;
703         }
704         return (msg);
709 int
710 check_num (int i)
712         int result;
713         result = STATE_OK;
714         if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
715                         lower_warn_lim[i] > upper_warn_lim[i]) {
716                 if (response_value[i] <= lower_warn_lim[i] &&
717                                 response_value[i] >= upper_warn_lim[i]) {
718                         result = STATE_WARNING;
719                 }
720         }
721         else if
722                 ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
723                  (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
724                  (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
725                  (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
726                  (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
727                  (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
728                 result = STATE_WARNING;
729         }
731         if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
732                         lower_crit_lim[i] > upper_crit_lim[i]) {
733                 if (response_value[i] <= lower_crit_lim[i] &&
734                                 response_value[i] >= upper_crit_lim[i]) {
735                         result = STATE_CRITICAL;
736                 }
737         }
738         else if
739                 ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
740                  (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
741                  (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
742                  (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
743                  (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
744                  (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
745                 result = STATE_CRITICAL;
746         }
748         return result;
753 int
754 lu_getll (unsigned long *ll, char *str)
756         char tmp[100];
757         if (strchr (str, ':') == NULL)
758                 return 0;
759         if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
760                 return 0;
761         if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
762                 return 1;
763         return 0;
768 int
769 lu_getul (unsigned long *ul, char *str)
771         char tmp[100];
772         if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
773                 return 1;
774         if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
775                 return 1;
776         if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
777                 return 1;
778         return 0;
783 /* trim leading whitespace
784          if there is a leading quote, make sure it balances */
786 char *
787 thisarg (char *str)
789         str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
790         if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
791                 if (strlen (str) == 1 || !strstr (str + 1, "'"))
792                         die (STATE_UNKNOWN, _("Unbalanced quotes\n"));
793         }
794         return str;
799 /* if there's a leading quote, advance to the trailing quote
800          set the trailing quote to '\x0'
801          if the string continues, advance beyond the comma */
803 char *
804 nextarg (char *str)
806         if (strstr (str, "'") == str) {
807                 str[0] = 0;
808                 if (strlen (str) > 1) {
809                         str = strstr (str + 1, "'");
810                         return (++str);
811                 }
812                 else {
813                         return NULL;
814                 }
815         }
816         if (strstr (str, ",") == str) {
817                 str[0] = 0;
818                 if (strlen (str) > 1) {
819                         return (++str);
820                 }
821                 else {
822                         return NULL;
823                 }
824         }
825         if ((str = strstr (str, ",")) && strlen (str) > 1) {
826                 str[0] = 0;
827                 return (++str);
828         }
829         return NULL;
834 void
835 print_help (void)
837         print_revision (progname, revision);
839         printf (COPYRIGHT, copyright, email);
841         printf (_("\
842 Check status of remote machines and obtain sustem information via SNMP\n\n"));
844         print_usage ();
846         printf (_(UT_HELP_VRSN));
848         printf (_(UT_HOST_PORT), 'p', DEFAULT_PORT);
850         /* SNMP and Authentication Protocol */
851         printf (_("\
852  -P, --protocol=[1|3]\n\
853     SNMP protocol version\n\
854  -L, --seclevel=[noAuthNoPriv|authNoPriv|authPriv]\n\
855     SNMPv3 securityLevel\n\
856  -a, --authproto=[MD5|SHA]\n\
857     SNMPv3 auth proto\n"));
859         /* Authentication Tokens*/
860         printf (_("\
861  -C, --community=STRING\n\
862     Optional community string for SNMP communication\n\
863     (default is \"%s\")\n\
864  -U, --secname=USERNAME\n\
865     SNMPv3 username\n\
866  -A, --authpassword=PASSWORD\n\
867     SNMPv3 authentication password\n\
868  -X, --privpasswd=PASSWORD\n\
869     SNMPv3 crypt passwd (DES)\n"), DEFAULT_COMMUNITY);
871         /* OID Stuff */
872         printf (_("\
873  -o, --oid=OID(s)\n\
874     Object identifier(s) whose value you wish to query\n\
875  -m, --miblist=STRING\n\
876     List of MIBS to be loaded (default = ALL)\n -d, --delimiter=STRING\n\
877     Delimiter to use when parsing returned data. Default is \"%s\"\n\
878     Any data on the right hand side of the delimiter is considered\n\
879     to be the data that should be used in the evaluation.\n"), DEFAULT_DELIMITER);
881         /* Tests Against Integers */
882         printf (_("\
883  -w, --warning=INTEGER_RANGE(s)\n\
884     Range(s) which will not result in a WARNING status\n\
885  -c, --critical=INTEGER_RANGE(s)\n\
886     Range(s) which will not result in a CRITICAL status\n"));
888         /* Tests Against Strings */
889         printf (_("\
890  -s, --string=STRING\n\
891     Return OK state (for that OID) if STRING is an exact match\n\
892  -r, --ereg=REGEX\n\
893     Return OK state (for that OID) if extended regular expression REGEX matches\n\
894  -R, --eregi=REGEX\n\
895     Return OK state (for that OID) if case-insensitive extended REGEX matches\n\
896  -l, --label=STRING\n\
897     Prefix label for output from plugin (default -s 'SNMP')\n"));
899         /* Output Formatting */
900         printf (_("\
901  -u, --units=STRING\n\
902     Units label(s) for output data (e.g., 'sec.').\n\
903  -D, --output-delimiter=STRING\n\
904     Separates output on multiple OID requests\n"));
906         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
908         printf (_(UT_VERBOSE));
910         printf (_("\n\
911 - This plugin uses the 'snmpget' command included with the NET-SNMP package.\n\
912   If you don't have the package installed, you will need to download it from\n\
913   http://net-snmp.sourceforge.net before you can use this plugin.\n"));
915         printf (_("\
916 - Multiple OIDs may be indicated by a comma- or space-delimited list (lists with\n\
917   internal spaces must be quoted) [max 8 OIDs]\n"));
919         printf (_("\
920 - Ranges are inclusive and are indicated with colons. When specified as\n\
921   'min:max' a STATE_OK will be returned if the result is within the indicated\n\
922   range or is equal to the upper or lower bound. A non-OK state will be\n\
923   returned if the result is outside the specified range.\n"));
925         printf (_("\
926 - If specified in the order 'max:min' a non-OK state will be returned if the\n\
927   result is within the (inclusive) range.\n"));
929         printf (_("\
930 - Upper or lower bounds may be omitted to skip checking the respective limit.\n\
931 - Bare integers are interpreted as upper limits.\n\
932 - When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'\n\
933 - Note that only one string and one regex may be checked at present\n\
934 - All evaluation methods other than PR, STR, and SUBSTR expect that the value\n\
935   returned from the SNMP query is an unsigned integer.\n"));
937         printf (_(UT_SUPPORT));
942 void
943 print_usage (void)
945         printf ("\
946 Usage: %s -H <ip_address> -o <OID> [-w warn_range] [-c crit_range] \n\
947                   [-C community] [-s string] [-r regex] [-R regexi] [-t timeout]\n\
948                   [-l label] [-u units] [-p port-number] [-d delimiter]\n\
949                   [-D output-delimiter] [-m miblist] [-P snmp version]\n\
950                   [-L seclevel] [-U secname] [-a authproto] [-A authpasswd]\n\
951                   [-X privpasswd]\n", progname);