Code

port option added, -c support net-snmpv5, complete response string output
[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         command_line = ssprintf
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                         /*For consistency- full SNMP response every time */
225                         show = ssprintf (show, "%d", 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 (result, iresult);
266                 if (nlabels > 1 && i < nlabels && labels[i] != NULL)
267                         outbuff = ssprintf
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                         outbuff = ssprintf
275                                 (outbuff,
276                                  "%s%s%s%s%s",
277                                  outbuff,
278                                  (i == 0) ? " " : output_delim, mark (iresult), show, mark (iresult));
280                 if (nunits > 0 && i < nunits)
281                         outbuff = ssprintf (outbuff, "%s %s", outbuff, unitv[i]);
283                 i++;
285         }                                                                                                                       /* end while */
287         if (found == 0)
288                 terminate
289                         (STATE_UNKNOWN,
290                          "%s problem - No data recieved from host\nCMD: %s\n",
291                          label, command_line);
293         /* WARNING if output found on stderr */
294         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
295                 result = max (result, STATE_WARNING);
297         /* close stderr */
298         (void) fclose (child_stderr);
300         /* close the pipe */
301         if (spclose (child_process))
302                 result = max (result, STATE_WARNING);
304         if (nunits > 0)
305                 printf ("%s %s -%s\n", label, state_text (result), outbuff);
306         else
307                 printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units);
309         return result;
312 /* process command-line arguments */
313 int
314 process_arguments (int argc, char **argv)
316         int c;
318         if (argc < 2)
319                 return ERROR;
321         for (c = 1; c < argc; c++) {
322                 if (strcmp ("-to", argv[c]) == 0)
323                         strcpy (argv[c], "-t");
324                 if (strcmp ("-wv", argv[c]) == 0)
325                         strcpy (argv[c], "-w");
326                 if (strcmp ("-cv", argv[c]) == 0)
327                         strcpy (argv[c], "-c");
328         }
330         c = 0;
331         while (c += (call_getopt (argc - c, &argv[c]))) {
332                 if (argc <= c)
333                         break;
334                 if (server_address == NULL)
335                         server_address = strscpy (NULL, argv[c]);
336         }
338         if (community == NULL)
339                 community = strscpy (NULL, "public");
341         if (delimiter == NULL)
342                 delimiter = strscpy (NULL, DEFAULT_DELIMITER);
344         if (output_delim == NULL)
345                 output_delim = strscpy (NULL, DEFAULT_OUTPUT_DELIMITER);
347         if (label == NULL)
348                 label = strscpy (NULL, "SNMP");
350         if (units == NULL)
351                 units = strscpy (NULL, "");
353         if (port == NULL)
354                 port = strscpy(NULL,"161");
356         return c;
359 int
360 call_getopt (int argc, char **argv)
362         char *ptr;
363         int c, i = 1;
364         int j = 0, jj = 0;
366 #ifdef HAVE_GETOPT_H
367         int option_index = 0;
368         static struct option long_options[] = {
369                 {"help", no_argument, 0, 'h'},
370                 {"version", no_argument, 0, 'V'},
371                 {"timeout", required_argument, 0, 't'},
372                 {"critical", required_argument, 0, 'c'},
373                 {"warning", required_argument, 0, 'w'},
374                 {"hostname", required_argument, 0, 'H'},
375                 {"community", required_argument, 0, 'C'},
376                 {"oid", required_argument, 0, 'o'},
377                 {"object", required_argument, 0, 'o'},
378                 {"delimiter", required_argument, 0, 'd'},
379                 {"output-delimiter", required_argument, 0, 'D'},
380                 {"string", required_argument, 0, 's'},
381                 {"regex", required_argument, 0, 'r'},
382                 {"ereg", required_argument, 0, 'r'},
383                 {"eregi", required_argument, 0, 'R'},
384                 {"label", required_argument, 0, 'l'},
385                 {"units", required_argument, 0, 'u'},
386                 {"port", required_argument, 0, 'p'},
387                 {0, 0, 0, 0}
388         };
389 #endif
391         while (1) {
392 #ifdef HAVE_GETOPT_H
393                 c =
394                         getopt_long (argc, argv, "+?hVt:c:w:H:C:o:d:D:s:R:r:l:u:p:",
395                                                                          long_options, &option_index);
396 #else
397                 c = getopt (argc, argv, "+?hVt:c:w:H:C:o:d:D:s:R:r:l:u:p:");
398 #endif
400                 if (c == -1 || c == EOF)
401                         break;
403                 i++;
404                 switch (c) {
405                 case 't':
406                 case 'c':
407                 case 'w':
408                 case 'H':
409                 case 'C':
410                 case 'o':
411                 case 'd':
412                 case 'D':
413                 case 's':
414                 case 'R':
415                 case 'r':
416                 case 'l':
417                 case 'u':
418                 case 'p':
419                         i++;
420                 }
422                 switch (c) {
423                 case '?':                                                                       /* help */
424                         printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg);
425                         print_usage ();
426                         exit (STATE_UNKNOWN);
427                 case 'h':                                                                       /* help */
428                         print_help (my_basename (argv[0]));
429                         exit (STATE_OK);
430                 case 'V':                                                                       /* version */
431                         print_revision (my_basename (argv[0]), "$Revision$");
432                         exit (STATE_OK);
433                 case 't':                                                                       /* timeout period */
434                         if (!is_integer (optarg)) {
435                                 printf ("%s: Timeout Interval must be an integer!\n\n",
436                                                                 my_basename (argv[0]));
437                                 print_usage ();
438                                 exit (STATE_UNKNOWN);
439                         }
440                         timeout_interval = atoi (optarg);
441                         break;
442                 case 'c':                                                                       /* critical time threshold */
443                         if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
444                                 printf ("Invalid critical threshold: %s\n", optarg);
445                                 print_usage ();
446                                 exit (STATE_UNKNOWN);
447                         }
448                         for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
449                                 if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
450                                         eval_method[jj] |= CRIT_LT;
451                                 if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
452                                         eval_method[jj] |= CRIT_GT;
453                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
454                         }
455                         break;
456                 case 'w':                                                                       /* warning time threshold */
457                         if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
458                                 printf ("Invalid warning threshold: %s\n", optarg);
459                                 print_usage ();
460                                 exit (STATE_UNKNOWN);
461                         }
462                         for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
463                                 if (lu_getll (&lower_warn_lim[jj], ptr) == 1)
464                                         eval_method[jj] |= WARN_LT;
465                                 if (lu_getul (&upper_warn_lim[jj], ptr) == 1)
466                                         eval_method[jj] |= WARN_GT;
467                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
468                         }
469                         break;
470                 case 'H':                                                                       /* Host or server */
471                         server_address = strscpy (server_address, optarg);
472                         break;
473                 case 'C':                                                                       /* group or community */
474                         community = strscpy (community, optarg);
475                         break;
476                 case 'o':                                                                       /* object identifier */
477                         for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
478                                 ptr[0] = ' ';
479                         strncpy (oid, optarg, sizeof (oid) - 1);
480                         oid[sizeof (oid) - 1] = 0;
481                         for (ptr = optarg, j = 1; (ptr = index (ptr, ' ')); ptr++)
482                                 j++;
483                         break;
484                 case 'd':                                                                       /* delimiter */
485                         delimiter = strscpy (delimiter, optarg);
486                         break;
487                 case 'D':                                                                       /* output-delimiter */
488                         output_delim = strscpy (output_delim, optarg);
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                         break;
495                 case 'R':                                                                       /* regex */
496 #ifdef HAVE_REGEX_H
497                         cflags = REG_ICASE;
498 #endif
499                 case 'r':                                                                       /* regex */
500 #ifdef HAVE_REGEX_H
501                         cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
502                         strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
503                         regex_expect[sizeof (regex_expect) - 1] = 0;
504                         errcode = regcomp (&preg, regex_expect, cflags);
505                         if (errcode != 0) {
506                                 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
507                                 printf ("Could Not Compile Regular Expression");
508                                 return ERROR;
509                         }
510                         eval_method[jj++] = CRIT_REGEX;
511 #else
512                         printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
513                         exit (STATE_UNKNOWN);
514 #endif
515                         break;
516                 case 'l':                                                                       /* label */
517                         label = optarg;
518                         nlabels++;
519                         if (nlabels >= labels_size) {
520                                 labels_size += 8;
521                                 labels = realloc (labels, labels_size);
522                                 if (labels == NULL)
523                                         terminate (STATE_UNKNOWN,
524                                                                                  "Could not realloc() labels[%d]", nlabels);
525                         }
526                         labels[nlabels - 1] = optarg;
527                         ptr = thisarg (optarg);
528                         if (strstr (ptr, "'") == ptr)
529                                 labels[nlabels - 1] = ptr + 1;
530                         else
531                                 labels[nlabels - 1] = ptr;
532                         while (ptr && (ptr = nextarg (ptr))) {
533                                 if (nlabels >= labels_size) {
534                                         labels_size += 8;
535                                         labels = realloc (labels, labels_size);
536                                         if (labels == NULL)
537                                                 terminate (STATE_UNKNOWN, "Could not realloc() labels\n");
538                                 }
539                                 labels++;
540                                 ptr = thisarg (ptr);
541                                 if (strstr (ptr, "'") == ptr)
542                                         labels[nlabels - 1] = ptr + 1;
543                                 else
544                                         labels[nlabels - 1] = ptr;
545                         }
546                         break;
547                 case 'u':                                                                       /* units */
548                         units = optarg;
549                         nunits++;
550                         if (nunits >= unitv_size) {
551                                 unitv_size += 8;
552                                 unitv = realloc (unitv, unitv_size);
553                                 if (unitv == NULL)
554                                         terminate (STATE_UNKNOWN,
555                                                                                  "Could not realloc() units [%d]\n", nunits);
556                         }
557                         unitv[nunits - 1] = optarg;
558                         ptr = thisarg (optarg);
559                         if (strstr (ptr, "'") == ptr)
560                                 unitv[nunits - 1] = ptr + 1;
561                         else
562                                 unitv[nunits - 1] = ptr;
563                         while (ptr && (ptr = nextarg (ptr))) {
564                                 if (nunits >= unitv_size) {
565                                         unitv_size += 8;
566                                         unitv = realloc (unitv, unitv_size);
567                                         if (units == NULL)
568                                                 terminate (STATE_UNKNOWN, "Could not realloc() units\n");
569                                 }
570                                 nunits++;
571                                 ptr = thisarg (ptr);
572                                 if (strstr (ptr, "'") == ptr)
573                                         unitv[nunits - 1] = ptr + 1;
574                                 else
575                                         unitv[nunits - 1] = ptr;
576                         }
577                         break;
578                 case 'p':       /* TCP port number */
579                         port = strscpy(port, optarg);
580                         break;
582                 }
583         }
584         return i;
587 void
588 print_usage (void)
590         printf
591                 ("Usage: check_snmp -H <ip_address> -o <OID> [-w warn_range] [-c crit_range] \n"
592                  "                  [-C community] [-s string] [-r regex] [-R regexi] [-t timeout]\n"
593                  "                  [-l label] [-u units] [-p port-number] [-d delimiter] [-D output-delimiter]\n"
594                  "       check_snmp --help\n" "       check_snmp --version\n");
597 void
598 print_help (char *cmd)
600         printf ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n"
601                                         "License: GPL\n\n");
602         print_usage ();
603         printf
604                 ("\nOptions:\n"
605                  " -h, --help\n"
606                  "    Print detailed help screen\n"
607                  " -V, --version\n"
608                  "    Print version information\n"
609                  " -H, --hostname=HOST\n"
610                  "    Name or IP address of the device you wish to query\n"
611                  " -o, --oid=OID(s)\n"
612                  "    Object identifier(s) whose value you wish to query\n"
613                  " -w, --warning=INTEGER_RANGE(s)\n"
614                  "    Range(s) which will not result in a WARNING status\n"
615                  " -c, --critical=INTEGER_RANGE(s)\n"
616                  "    Range(s) which will not result in a CRITICAL status\n"
617                  " -C, --community=STRING\n"
618                  "    Optional community string for SNMP communication\n"
619                  "    (default is \"public\")\n"
620                  " -u, --units=STRING\n"
621                  "    Units label(s) for output data (e.g., 'sec.').\n"
622                  " -p, --port=STRING\n"
623          "    UDP port number target is listening on.\n"
624                  " -d, --delimiter=STRING\n"
625                  "    Delimiter to use when parsing returned data. Default is \"%s\"\n"
626                  "    Any data on the right hand side of the delimiter is considered\n"
627                  "    to be the data that should be used in the evaluation.\n"
628                  " -t, --timeout=INTEGER\n"
629                  "    Seconds to wait before plugin times out (see also nagios server timeout)\n"
630                  " -D, --output-delimiter=STRING\n"
631                  "    Separates output on multiple OID requests\n"
632                  " -s, --string=STRING\n"
633                  "    Return OK state (for that OID) if STRING is an exact match\n"
634                  " -r, --ereg=REGEX\n"
635                  "    Return OK state (for that OID) if extended regular expression REGEX matches\n"
636                  " -R, --eregi=REGEX\n"
637                  "    Return OK state (for that OID) if case-insensitive extended REGEX matches\n"
638                  " -l, --label=STRING\n"
639                  "    Prefix label for output from plugin (default -s 'SNMP')\n\n"
640                  "- This plugin uses the 'snmpget' command included with the UCD-SNMP package.\n"
641                  "  If you don't have the package installed, you will need to download it from\n"
642                  "  http://ucd-snmp.ucdavis.edu before you can use this plugin.\n"
643                  "- Multiple OIDs may be indicated by a comma- or space-delimited list (lists with\n"
644                  "  internal spaces must be quoted)\n"
645                  "- Ranges are inclusive and are indicated with colons. When specified as\n"
646                  "  'min:max' a STATE_OK will be returned if the result is within the indicated\n"
647                  "  range or is equal to the upper or lower bound. A non-OK state will be\n"
648                  "  returned if the result is outside the specified range.\n"
649                  "- If spcified in the order 'max:min' a non-OK state will be returned if the\n"
650                  "  result is within the (inclusive) range.\n"
651                  "- Upper or lower bounds may be omitted to skip checking the respective limit.\n"
652                  "- Bare integers are interpreted as upper limits.\n"
653                  "- When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'\n"
654                  "- Note that only one string and one regex may be checked at present\n"
655                  "- All evaluation methods other than PR, STR, and SUBSTR expect that the value\n"
656                  "  returned from the SNMP query is an unsigned integer.\n\n",
657                  DEFAULT_DELIMITER);
660 char *
661 clarify_message (char *msg)
663         int i = 0;
664         int foo;
665         char tmpmsg_c[MAX_INPUT_BUFFER];
666         char *tmpmsg = (char *) &tmpmsg_c;
667         tmpmsg = strcpy (tmpmsg, msg);
668         if (!strncmp (tmpmsg, " Hex:", 5)) {
669                 tmpmsg = strtok (tmpmsg, ":");
670                 while ((tmpmsg = strtok (NULL, " "))) {
671                         foo = strtol (tmpmsg, NULL, 16);
672                         /* Translate chars that are not the same value in the printers
673                          * character set.
674                          */
675                         switch (foo) {
676                         case 208:
677                                 {
678                                         foo = 197;
679                                         break;
680                                 }
681                         case 216:
682                                 {
683                                         foo = 196;
684                                         break;
685                                 }
686                         }
687                         msg[i] = foo;
688                         i++;
689                 }
690                 msg[i] = 0;
691         }
692         return (msg);
696 int
697 check_num (int i)
699         int result;
700         result = STATE_OK;
701         if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
702                         lower_warn_lim[i] > upper_warn_lim[i]) {
703                 if (response_value[i] <= lower_warn_lim[i] &&
704                                 response_value[i] >= upper_warn_lim[i]) {
705                         result = STATE_WARNING;
706                 }
707         }
708         else if
709                 ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
710                  (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
711                  (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
712                  (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
713                  (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
714                  (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
715                 result = STATE_WARNING;
716         }
718         if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
719                         lower_warn_lim[i] > upper_warn_lim[i]) {
720                 if (response_value[i] <= lower_crit_lim[i] &&
721                                 response_value[i] >= upper_crit_lim[i]) {
722                         result = STATE_CRITICAL;
723                 }
724         }
725         else if
726                 ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
727                  (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
728                  (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
729                  (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
730                  (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
731                  (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
732                 result = STATE_CRITICAL;
733         }
735         return result;
739 int
740 lu_getll (unsigned long *ll, char *str)
742         char tmp[100];
743         if (strchr (str, ':') == NULL)
744                 return 0;
745         if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
746                 return 0;
747         if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
748                 return 1;
749         return 0;
752 int
753 lu_getul (unsigned long *ul, char *str)
755         char tmp[100];
756         if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
757                 return 1;
758         if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
759                 return 1;
760         if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
761                 return 1;
762         return 0;
770 /* trim leading whitespace
771          if there is a leading quote, make sure it balances */
773 char *
774 thisarg (char *str)
776         str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
777         if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
778                 if (strlen (str) == 1 || !strstr (str + 1, "'"))
779                         terminate (STATE_UNKNOWN, "Unbalanced quotes\n");
780         }
781         return str;
785 /* if there's a leading quote, advance to the trailing quote
786          set the trailing quote to '\x0'
787          if the string continues, advance beyond the comma */
789 char *
790 nextarg (char *str)
792         if (strstr (str, "'") == str) {
793                 if (strlen (str) > 1) {
794                         str = strstr (str + 1, "'");
795                         str[0] = 0;
796                         return (++str);
797                 }
798                 else {
799                         str[0] = 0;
800                         return NULL;
801                 }
802         }
803         if (strstr (str, ",") == str) {
804                 if (strlen (str) > 1) {
805                         str[0] = 0;
806                         return (++str);
807                 }
808                 else {
809                         str[0] = 0;
810                         return NULL;
811                 }
812         }
813         if ((str = strstr (str, ",")) && strlen (str) > 1) {
814                 str[0] = 0;
815                 return (++str);
816         }
817         return NULL;