Code

change ssprintf to asprintf
[nagiosplug.git] / plugins / check_snmp.c
1 /******************************************************************************
2  *
3  * CHECK_SNMP.C
4  *
5  * Program: SNMP plugin for Nagios
6  * License: GPL
7  * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
8  *
9  * Last Modified: $Date$
10  *
11  * Description:
12  *
13  * This plugin uses the 'snmpget' command included with the UCD-SNMP
14  * package.  If you don't have the package installed you will need to
15  * download it from http://ucd-snmp.ucdavis.edu before you can use
16  * this plugin.
17  *
18  * License Information:
19  *
20  * This program is free software; you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation; either version 2 of the License, or
23  * (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  * GNU General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program; if not, write to the Free Software
32  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33  *./plugins/check_snmp 127.0.0.1 -c public -o .1.3.6.1.4.1.2021.9.1.2.1
34  *****************************************************************************/
36 #include "common.h"
37 #include "utils.h"
38 #include "popen.h"
40 #define PROGNAME check_snmp
42 #define mark(a) ((a)!=0?"*":"")
44 #define CHECK_UNDEF 0
45 #define CRIT_PRESENT 1
46 #define CRIT_STRING 2
47 #define CRIT_REGEX 4
48 #define CRIT_GT 8
49 #define CRIT_LT 16
50 #define CRIT_GE 32
51 #define CRIT_LE 64
52 #define CRIT_EQ 128
53 #define CRIT_NE 256
54 #define CRIT_RANGE 512
55 #define WARN_PRESENT 1024
56 #define WARN_STRING 2048
57 #define WARN_REGEX 4096
58 #define WARN_GT 8192
59 #define WARN_LT 16384
60 #define WARN_GE 32768
61 #define WARN_LE 65536
62 #define WARN_EQ 131072
63 #define WARN_NE 262144
64 #define WARN_RANGE 524288
66 #define MAX_OIDS 8
67 #define MAX_DELIM_LENGTH 8
68 #define DEFAULT_DELIMITER "="
69 #define DEFAULT_OUTPUT_DELIMITER " "
71 void print_usage (void);
72 void print_help (char *);
73 int process_arguments (int, char **);
74 int call_getopt (int, char **);
75 int check_num (int);
76 char *clarify_message (char *);
77 int lu_getll (unsigned long *, char *);
78 int lu_getul (unsigned long *, char *);
79 char *thisarg (char *str);
80 char *nextarg (char *str);
82 #ifdef HAVE_REGEX_H
83 #include <regex.h>
84 char regex_expect[MAX_INPUT_BUFFER] = "";
85 regex_t preg;
86 regmatch_t pmatch[10];
87 char timestamp[10] = "";
88 char regex[MAX_INPUT_BUFFER];
89 char errbuf[MAX_INPUT_BUFFER];
90 int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
91 int eflags = 0;
92 int errcode, excode;
93 #endif
95 char *server_address = NULL;
96 char *community = NULL;
97 char oid[MAX_INPUT_BUFFER] = "";
98 char *label = NULL;
99 char *units = NULL;
100 char *port = NULL;
101 char string_value[MAX_INPUT_BUFFER] = "";
102 char **labels = NULL;
103 char **unitv = NULL;
104 int nlabels = 0;
105 int labels_size = 8;
106 int nunits = 0;
107 int unitv_size = 8;
108 unsigned long lower_warn_lim[MAX_OIDS];
109 unsigned long upper_warn_lim[MAX_OIDS];
110 unsigned long lower_crit_lim[MAX_OIDS];
111 unsigned long upper_crit_lim[MAX_OIDS];
112 unsigned long response_value[MAX_OIDS];
113 int check_warning_value = FALSE;
114 int check_critical_value = FALSE;
115 int eval_method[MAX_OIDS];
116 char *delimiter = NULL;
117 char *output_delim = NULL;
120 int
121 main (int argc, char **argv)
123         int i = 0;
124         int iresult = STATE_UNKNOWN;
125         int found = 0;
126         int result = STATE_DEPENDENT;
127         char input_buffer[MAX_INPUT_BUFFER];
128         char *command_line = NULL;
129         char *response = NULL;
130         char *outbuff = NULL;
131         char *output = NULL;
132         char *ptr = NULL;
133         char *p2 = NULL;
134         char *show = NULL;
136         labels = malloc (labels_size);
137         unitv = malloc (unitv_size);
138         outbuff = strscpy (outbuff, "");
139         for (i = 0; i < MAX_OIDS; i++)
140                 eval_method[i] = CHECK_UNDEF;
141         i = 0;
143         if (process_arguments (argc, argv) == ERROR)
144                 usage ("Incorrect arguments supplied\n");
146         /* create the command line to execute */
147         asprintf
148                 (&command_line,
149                  "%s -p %s -m ALL -v 1 %s -c %s %s",
150                  PATH_TO_SNMPGET, port, server_address, community, oid);
152         /* run the command */
153         child_process = spopen (command_line);
154         if (child_process == NULL) {
155                 printf ("Could not open pipe: %s\n", command_line);
156                 exit (STATE_UNKNOWN);
157         }
159         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
160         if (child_stderr == NULL) {
161                 printf ("Could not open stderr for %s\n", command_line);
162         }
164         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
165                 output = strscat (output, input_buffer);
167         ptr = output;
169         while (ptr) {
171                 ptr = strstr (ptr, delimiter);
172                 if (ptr == NULL)
173                         break;
175                 ptr += strlen (delimiter);
176                 ptr += strspn (ptr, " ");
178                 found++;
180                 if (ptr[0] == '"') {
181                         ptr++;
182                         response = strpcpy (response, ptr, "\"");
183                         ptr = strpbrk (ptr, "\"");
184                         ptr += strspn (ptr, "\"\n");
185                 }
186                 else {
187                         response = strpcpy (response, ptr, "\n");
188                         ptr = strpbrk (ptr, "\n");
189                         ptr += strspn (ptr, "\n");
190                         while
191                                 (strstr (ptr, delimiter) &&
192                                  strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter)) {
193                                 response = strpcat (response, ptr, "\n");
194                                 ptr = strpbrk (ptr, "\n");
195                         }
196                         if (ptr && strstr (ptr, delimiter) == NULL) {
197                                 response = strscat (response, ptr);
198                                 ptr = NULL;
199                         }
200                 }
202                 if (strstr (response, "Gauge: "))
203                         show = strstr (response, "Gauge: ") + 7;
204                 else if (strstr (response, "Gauge32: "))
205                         show = strstr (response, "Gauge32: ") + 9;
206                 else
207                         show = response;
208                 p2 = show;
210                 if (eval_method[i] & CRIT_GT ||
211                                 eval_method[i] & CRIT_LT ||
212                                 eval_method[i] & CRIT_GE ||
213                                 eval_method[i] & CRIT_LE ||
214                                 eval_method[i] & CRIT_EQ ||
215                                 eval_method[i] & CRIT_NE ||
216                                 eval_method[i] & WARN_GT ||
217                                 eval_method[i] & WARN_LT ||
218                                 eval_method[i] & WARN_GE ||
219                                 eval_method[i] & WARN_LE ||
220                                 eval_method[i] & WARN_EQ || eval_method[i] & WARN_NE) {
221                         p2 = strpbrk (p2, "0123456789");
222                         response_value[i] = strtoul (p2, NULL, 10);
223                         iresult = check_num (i);
224                         asprintf (&show, "%lu", response_value[i]);
225                         /*asprintf (&show, "%s", response); */
226                 }
228                 else if (eval_method[i] & CRIT_STRING) {
229                         if (strcmp (response, string_value))
230                                 iresult = STATE_CRITICAL;
231                         else
232                                 iresult = STATE_OK;
233                 }
235                 else if (eval_method[i] & CRIT_REGEX) {
236 #ifdef HAVE_REGEX_H
237                         excode = regexec (&preg, response, 10, pmatch, eflags);
238                         if (excode == 0) {
239                                 iresult = STATE_OK;
240                         }
241                         else if (excode != REG_NOMATCH) {
242                                 regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
243                                 printf ("Execute Error: %s\n", errbuf);
244                                 exit (STATE_CRITICAL);
245                         }
246                         else {
247                                 iresult = STATE_CRITICAL;
248                         }
249 #else
250                         printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
251                         exit (STATE_UNKNOWN);
252 #endif
253                 }
255                 else {
256                         if (response)
257                                 iresult = STATE_OK;
258                         else if (eval_method[i] & CRIT_PRESENT)
259                                 iresult = STATE_CRITICAL;
260                         else
261                                 iresult = STATE_WARNING;
262                 }
264                 result = max_state (result, iresult);
266                 if (nlabels > 1 && i < nlabels && labels[i] != NULL)
267                         asprintf
268                                 (&outbuff,
269                                  "%s%s%s %s%s%s",
270                                  outbuff,
271                                  (i == 0) ? " " : output_delim,
272                                  labels[i], mark (iresult), show, mark (iresult));
273                 else
274                         asprintf (&outbuff, "%s%s%s%s%s", outbuff,
275                                  (i == 0) ? " " : output_delim, mark (iresult), show, mark (iresult));
277                 if (nunits > 0 && i < nunits)
278                         asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
280                 i++;
282         }                                                                                                                       /* end while */
284         if (found == 0)
285                 terminate
286                         (STATE_UNKNOWN,
287                          "%s problem - No data recieved from host\nCMD: %s\n",
288                          label, command_line);
290         /* WARNING if output found on stderr */
291         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
292                 result = max_state (result, STATE_WARNING);
294         /* close stderr */
295         (void) fclose (child_stderr);
297         /* close the pipe */
298         if (spclose (child_process))
299                 result = max_state (result, STATE_WARNING);
301         if (nunits > 0)
302                 printf ("%s %s -%s\n", label, state_text (result), outbuff);
303         else
304                 printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units);
306         return result;
309 /* process command-line arguments */
310 int
311 process_arguments (int argc, char **argv)
313         int c;
315         if (argc < 2)
316                 return ERROR;
318         for (c = 1; c < argc; c++) {
319                 if (strcmp ("-to", argv[c]) == 0)
320                         strcpy (argv[c], "-t");
321                 if (strcmp ("-wv", argv[c]) == 0)
322                         strcpy (argv[c], "-w");
323                 if (strcmp ("-cv", argv[c]) == 0)
324                         strcpy (argv[c], "-c");
325         }
327         c = 0;
328         while (c += (call_getopt (argc - c, &argv[c]))) {
329                 if (argc <= c)
330                         break;
331                 if (server_address == NULL)
332                         server_address = strscpy (NULL, argv[c]);
333         }
335         if (community == NULL)
336                 community = strscpy (NULL, "public");
338         if (delimiter == NULL)
339                 delimiter = strscpy (NULL, DEFAULT_DELIMITER);
341         if (output_delim == NULL)
342                 output_delim = strscpy (NULL, DEFAULT_OUTPUT_DELIMITER);
344         if (label == NULL)
345                 label = strscpy (NULL, "SNMP");
347         if (units == NULL)
348                 units = strscpy (NULL, "");
350         if (port == NULL)
351                 port = strscpy(NULL,"161");
353         return c;
356 int
357 call_getopt (int argc, char **argv)
359         char *ptr;
360         int c, i = 1;
361         int j = 0, jj = 0;
363 #ifdef HAVE_GETOPT_H
364         int option_index = 0;
365         static struct option long_options[] = {
366                 {"help", no_argument, 0, 'h'},
367                 {"version", no_argument, 0, 'V'},
368                 {"timeout", required_argument, 0, 't'},
369                 {"critical", required_argument, 0, 'c'},
370                 {"warning", required_argument, 0, 'w'},
371                 {"hostname", required_argument, 0, 'H'},
372                 {"community", required_argument, 0, 'C'},
373                 {"oid", required_argument, 0, 'o'},
374                 {"object", required_argument, 0, 'o'},
375                 {"delimiter", required_argument, 0, 'd'},
376                 {"output-delimiter", required_argument, 0, 'D'},
377                 {"string", required_argument, 0, 's'},
378                 {"regex", required_argument, 0, 'r'},
379                 {"ereg", required_argument, 0, 'r'},
380                 {"eregi", required_argument, 0, 'R'},
381                 {"label", required_argument, 0, 'l'},
382                 {"units", required_argument, 0, 'u'},
383                 {"port", required_argument, 0, 'p'},
384                 {0, 0, 0, 0}
385         };
386 #endif
388         while (1) {
389 #ifdef HAVE_GETOPT_H
390                 c =
391                         getopt_long (argc, argv, "+?hVt:c:w:H:C:o:d:D:s:R:r:l:u:p:",
392                                                                          long_options, &option_index);
393 #else
394                 c = getopt (argc, argv, "+?hVt:c:w:H:C:o:d:D:s:R:r:l:u:p:");
395 #endif
397                 if (c == -1 || c == EOF)
398                         break;
400                 i++;
401                 switch (c) {
402                 case 't':
403                 case 'c':
404                 case 'w':
405                 case 'H':
406                 case 'C':
407                 case 'o':
408                 case 'd':
409                 case 'D':
410                 case 's':
411                 case 'R':
412                 case 'r':
413                 case 'l':
414                 case 'u':
415                 case 'p':
416                         i++;
417                 }
419                 switch (c) {
420                 case '?':                                                                       /* help */
421                         printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg);
422                         print_usage ();
423                         exit (STATE_UNKNOWN);
424                 case 'h':                                                                       /* help */
425                         print_help (my_basename (argv[0]));
426                         exit (STATE_OK);
427                 case 'V':                                                                       /* version */
428                         print_revision (my_basename (argv[0]), "$Revision$");
429                         exit (STATE_OK);
430                 case 't':                                                                       /* timeout period */
431                         if (!is_integer (optarg)) {
432                                 printf ("%s: Timeout Interval must be an integer!\n\n",
433                                                                 my_basename (argv[0]));
434                                 print_usage ();
435                                 exit (STATE_UNKNOWN);
436                         }
437                         timeout_interval = atoi (optarg);
438                         break;
439                 case 'c':                                                                       /* critical time threshold */
440                         if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
441                                 printf ("Invalid critical threshold: %s\n", optarg);
442                                 print_usage ();
443                                 exit (STATE_UNKNOWN);
444                         }
445                         for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
446                                 if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
447                                         eval_method[jj] |= CRIT_LT;
448                                 if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
449                                         eval_method[jj] |= CRIT_GT;
450                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
451                         }
452                         break;
453                 case 'w':                                                                       /* warning time threshold */
454                         if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
455                                 printf ("Invalid warning threshold: %s\n", optarg);
456                                 print_usage ();
457                                 exit (STATE_UNKNOWN);
458                         }
459                         for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
460                                 if (lu_getll (&lower_warn_lim[jj], ptr) == 1)
461                                         eval_method[jj] |= WARN_LT;
462                                 if (lu_getul (&upper_warn_lim[jj], ptr) == 1)
463                                         eval_method[jj] |= WARN_GT;
464                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
465                         }
466                         break;
467                 case 'H':                                                                       /* Host or server */
468                         server_address = strscpy (server_address, optarg);
469                         break;
470                 case 'C':                                                                       /* group or community */
471                         community = strscpy (community, optarg);
472                         break;
473                 case 'o':                                                                       /* object identifier */
474                         for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
475                                 ptr[0] = ' ';
476                         strncpy (oid, optarg, sizeof (oid) - 1);
477                         oid[sizeof (oid) - 1] = 0;
478                         for (ptr = optarg, j = 1; (ptr = index (ptr, ' ')); ptr++)
479                                 j++;
480                         break;
481                 case 'd':                                                                       /* delimiter */
482                         delimiter = strscpy (delimiter, optarg);
483                         break;
484                 case 'D':                                                                       /* output-delimiter */
485                         output_delim = strscpy (output_delim, optarg);
486                         break;
487                 case 's':                                                                       /* string or substring */
488                         strncpy (string_value, optarg, sizeof (string_value) - 1);
489                         string_value[sizeof (string_value) - 1] = 0;
490                         eval_method[jj++] = CRIT_STRING;
491                         break;
492                 case 'R':                                                                       /* regex */
493 #ifdef HAVE_REGEX_H
494                         cflags = REG_ICASE;
495 #endif
496                 case 'r':                                                                       /* regex */
497 #ifdef HAVE_REGEX_H
498                         cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
499                         strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
500                         regex_expect[sizeof (regex_expect) - 1] = 0;
501                         errcode = regcomp (&preg, regex_expect, cflags);
502                         if (errcode != 0) {
503                                 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
504                                 printf ("Could Not Compile Regular Expression");
505                                 return ERROR;
506                         }
507                         eval_method[jj++] = CRIT_REGEX;
508 #else
509                         printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
510                         exit (STATE_UNKNOWN);
511 #endif
512                         break;
513                 case 'l':                                                                       /* label */
514                         label = optarg;
515                         nlabels++;
516                         if (nlabels >= labels_size) {
517                                 labels_size += 8;
518                                 labels = realloc (labels, labels_size);
519                                 if (labels == NULL)
520                                         terminate (STATE_UNKNOWN,
521                                                                                  "Could not realloc() labels[%d]", nlabels);
522                         }
523                         labels[nlabels - 1] = optarg;
524                         ptr = thisarg (optarg);
525                         if (strstr (ptr, "'") == ptr)
526                                 labels[nlabels - 1] = ptr + 1;
527                         else
528                                 labels[nlabels - 1] = ptr;
529                         while (ptr && (ptr = nextarg (ptr))) {
530                                 if (nlabels >= labels_size) {
531                                         labels_size += 8;
532                                         labels = realloc (labels, labels_size);
533                                         if (labels == NULL)
534                                                 terminate (STATE_UNKNOWN, "Could not realloc() labels\n");
535                                 }
536                                 labels++;
537                                 ptr = thisarg (ptr);
538                                 if (strstr (ptr, "'") == ptr)
539                                         labels[nlabels - 1] = ptr + 1;
540                                 else
541                                         labels[nlabels - 1] = ptr;
542                         }
543                         break;
544                 case 'u':                                                                       /* units */
545                         units = optarg;
546                         nunits++;
547                         if (nunits >= unitv_size) {
548                                 unitv_size += 8;
549                                 unitv = realloc (unitv, unitv_size);
550                                 if (unitv == NULL)
551                                         terminate (STATE_UNKNOWN,
552                                                                                  "Could not realloc() units [%d]\n", nunits);
553                         }
554                         unitv[nunits - 1] = optarg;
555                         ptr = thisarg (optarg);
556                         if (strstr (ptr, "'") == ptr)
557                                 unitv[nunits - 1] = ptr + 1;
558                         else
559                                 unitv[nunits - 1] = ptr;
560                         while (ptr && (ptr = nextarg (ptr))) {
561                                 if (nunits >= unitv_size) {
562                                         unitv_size += 8;
563                                         unitv = realloc (unitv, unitv_size);
564                                         if (units == NULL)
565                                                 terminate (STATE_UNKNOWN, "Could not realloc() units\n");
566                                 }
567                                 nunits++;
568                                 ptr = thisarg (ptr);
569                                 if (strstr (ptr, "'") == ptr)
570                                         unitv[nunits - 1] = ptr + 1;
571                                 else
572                                         unitv[nunits - 1] = ptr;
573                         }
574                         break;
575                 case 'p':       /* TCP port number */
576                         port = strscpy(port, optarg);
577                         break;
579                 }
580         }
581         return i;
584 void
585 print_usage (void)
587         printf
588                 ("Usage: check_snmp -H <ip_address> -o <OID> [-w warn_range] [-c crit_range] \n"
589                  "                  [-C community] [-s string] [-r regex] [-R regexi] [-t timeout]\n"
590                  "                  [-l label] [-u units] [-p port-number] [-d delimiter] [-D output-delimiter]\n"
591                  "       check_snmp --help\n" "       check_snmp --version\n");
594 void
595 print_help (char *cmd)
597         printf ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n"
598                                         "License: GPL\n\n");
599         print_usage ();
600         printf
601                 ("\nOptions:\n"
602                  " -h, --help\n"
603                  "    Print detailed help screen\n"
604                  " -V, --version\n"
605                  "    Print version information\n"
606                  " -H, --hostname=HOST\n"
607                  "    Name or IP address of the device you wish to query\n"
608                  " -o, --oid=OID(s)\n"
609                  "    Object identifier(s) whose value you wish to query\n"
610                  " -w, --warning=INTEGER_RANGE(s)\n"
611                  "    Range(s) which will not result in a WARNING status\n"
612                  " -c, --critical=INTEGER_RANGE(s)\n"
613                  "    Range(s) which will not result in a CRITICAL status\n"
614                  " -C, --community=STRING\n"
615                  "    Optional community string for SNMP communication\n"
616                  "    (default is \"public\")\n"
617                  " -u, --units=STRING\n"
618                  "    Units label(s) for output data (e.g., 'sec.').\n"
619                  " -p, --port=STRING\n"
620          "    UDP port number target is listening on.\n"
621                  " -d, --delimiter=STRING\n"
622                  "    Delimiter to use when parsing returned data. Default is \"%s\"\n"
623                  "    Any data on the right hand side of the delimiter is considered\n"
624                  "    to be the data that should be used in the evaluation.\n"
625                  " -t, --timeout=INTEGER\n"
626                  "    Seconds to wait before plugin times out (see also nagios server timeout)\n"
627                  " -D, --output-delimiter=STRING\n"
628                  "    Separates output on multiple OID requests\n"
629                  " -s, --string=STRING\n"
630                  "    Return OK state (for that OID) if STRING is an exact match\n"
631                  " -r, --ereg=REGEX\n"
632                  "    Return OK state (for that OID) if extended regular expression REGEX matches\n"
633                  " -R, --eregi=REGEX\n"
634                  "    Return OK state (for that OID) if case-insensitive extended REGEX matches\n"
635                  " -l, --label=STRING\n"
636                  "    Prefix label for output from plugin (default -s 'SNMP')\n\n"
637                  "- This plugin uses the 'snmpget' command included with the UCD-SNMP package.\n"
638                  "  If you don't have the package installed, you will need to download it from\n"
639                  "  http://ucd-snmp.ucdavis.edu before you can use this plugin.\n"
640                  "- Multiple OIDs may be indicated by a comma- or space-delimited list (lists with\n"
641                  "  internal spaces must be quoted)\n"
642                  "- Ranges are inclusive and are indicated with colons. When specified as\n"
643                  "  'min:max' a STATE_OK will be returned if the result is within the indicated\n"
644                  "  range or is equal to the upper or lower bound. A non-OK state will be\n"
645                  "  returned if the result is outside the specified range.\n"
646                  "- If spcified in the order 'max:min' a non-OK state will be returned if the\n"
647                  "  result is within the (inclusive) range.\n"
648                  "- Upper or lower bounds may be omitted to skip checking the respective limit.\n"
649                  "- Bare integers are interpreted as upper limits.\n"
650                  "- When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'\n"
651                  "- Note that only one string and one regex may be checked at present\n"
652                  "- All evaluation methods other than PR, STR, and SUBSTR expect that the value\n"
653                  "  returned from the SNMP query is an unsigned integer.\n\n",
654                  DEFAULT_DELIMITER);
657 char *
658 clarify_message (char *msg)
660         int i = 0;
661         int foo;
662         char tmpmsg_c[MAX_INPUT_BUFFER];
663         char *tmpmsg = (char *) &tmpmsg_c;
664         tmpmsg = strcpy (tmpmsg, msg);
665         if (!strncmp (tmpmsg, " Hex:", 5)) {
666                 tmpmsg = strtok (tmpmsg, ":");
667                 while ((tmpmsg = strtok (NULL, " "))) {
668                         foo = strtol (tmpmsg, NULL, 16);
669                         /* Translate chars that are not the same value in the printers
670                          * character set.
671                          */
672                         switch (foo) {
673                         case 208:
674                                 {
675                                         foo = 197;
676                                         break;
677                                 }
678                         case 216:
679                                 {
680                                         foo = 196;
681                                         break;
682                                 }
683                         }
684                         msg[i] = foo;
685                         i++;
686                 }
687                 msg[i] = 0;
688         }
689         return (msg);
693 int
694 check_num (int i)
696         int result;
697         result = STATE_OK;
698         if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
699                         lower_warn_lim[i] > upper_warn_lim[i]) {
700                 if (response_value[i] <= lower_warn_lim[i] &&
701                                 response_value[i] >= upper_warn_lim[i]) {
702                         result = STATE_WARNING;
703                 }
704         }
705         else if
706                 ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
707                  (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
708                  (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
709                  (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
710                  (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
711                  (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
712                 result = STATE_WARNING;
713         }
715         if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
716                         lower_warn_lim[i] > upper_warn_lim[i]) {
717                 if (response_value[i] <= lower_crit_lim[i] &&
718                                 response_value[i] >= upper_crit_lim[i]) {
719                         result = STATE_CRITICAL;
720                 }
721         }
722         else if
723                 ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
724                  (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
725                  (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
726                  (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
727                  (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
728                  (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
729                 result = STATE_CRITICAL;
730         }
732         return result;
736 int
737 lu_getll (unsigned long *ll, char *str)
739         char tmp[100];
740         if (strchr (str, ':') == NULL)
741                 return 0;
742         if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
743                 return 0;
744         if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
745                 return 1;
746         return 0;
749 int
750 lu_getul (unsigned long *ul, char *str)
752         char tmp[100];
753         if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
754                 return 1;
755         if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
756                 return 1;
757         if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
758                 return 1;
759         return 0;
767 /* trim leading whitespace
768          if there is a leading quote, make sure it balances */
770 char *
771 thisarg (char *str)
773         str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
774         if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
775                 if (strlen (str) == 1 || !strstr (str + 1, "'"))
776                         terminate (STATE_UNKNOWN, "Unbalanced quotes\n");
777         }
778         return str;
782 /* if there's a leading quote, advance to the trailing quote
783          set the trailing quote to '\x0'
784          if the string continues, advance beyond the comma */
786 char *
787 nextarg (char *str)
789         if (strstr (str, "'") == str) {
790                 if (strlen (str) > 1) {
791                         str = strstr (str + 1, "'");
792                         str[0] = 0;
793                         return (++str);
794                 }
795                 else {
796                         str[0] = 0;
797                         return NULL;
798                 }
799         }
800         if (strstr (str, ",") == str) {
801                 if (strlen (str) > 1) {
802                         str[0] = 0;
803                         return (++str);
804                 }
805                 else {
806                         str[0] = 0;
807                         return NULL;
808                 }
809         }
810         if ((str = strstr (str, ",")) && strlen (str) > 1) {
811                 str[0] = 0;
812                 return (++str);
813         }
814         return NULL;