Code

printf bug for large numbers - Jeff Murray
[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 -m ALL -v 1 %s %s %s",
150                  PATH_TO_SNMPGET, 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                         show = ssprintf (show, "%lu", response_value[i]);
225                 }
227                 else if (eval_method[i] & CRIT_STRING) {
228                         if (strcmp (response, string_value))
229                                 iresult = STATE_CRITICAL;
230                         else
231                                 iresult = STATE_OK;
232                 }
234                 else if (eval_method[i] & CRIT_REGEX) {
235 #ifdef HAVE_REGEX_H
236                         excode = regexec (&preg, response, 10, pmatch, eflags);
237                         if (excode == 0) {
238                                 iresult = STATE_OK;
239                         }
240                         else if (excode != REG_NOMATCH) {
241                                 regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
242                                 printf ("Execute Error: %s\n", errbuf);
243                                 exit (STATE_CRITICAL);
244                         }
245                         else {
246                                 iresult = STATE_CRITICAL;
247                         }
248 #else
249                         printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
250                         exit (STATE_UNKNOWN);
251 #endif
252                 }
254                 else {
255                         if (response)
256                                 iresult = STATE_OK;
257                         else if (eval_method[i] & CRIT_PRESENT)
258                                 iresult = STATE_CRITICAL;
259                         else
260                                 iresult = STATE_WARNING;
261                 }
263                 result = max_state (result, iresult);
265                 if (nlabels > 1 && i < nlabels && labels[i] != NULL)
266                         outbuff = ssprintf
267                                 (outbuff,
268                                  "%s%s%s %s%s%s",
269                                  outbuff,
270                                  (i == 0) ? " " : output_delim,
271                                  labels[i], mark (iresult), show, mark (iresult));
272                 else
273                         outbuff = ssprintf
274                                 (outbuff,
275                                  "%s%s%s%s%s",
276                                  outbuff,
277                                  (i == 0) ? " " : output_delim, mark (iresult), show, mark (iresult));
279                 if (nunits > 0 && i < nunits)
280                         outbuff = ssprintf (outbuff, "%s %s", outbuff, unitv[i]);
282                 i++;
284         }                                                                                                                       /* end while */
286         if (found == 0)
287                 terminate
288                         (STATE_UNKNOWN,
289                          "%s problem - No data recieved from host\nCMD: %s\n",
290                          label, command_line);
292         /* WARNING if output found on stderr */
293         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
294                 result = max_state (result, STATE_WARNING);
296         /* close stderr */
297         (void) fclose (child_stderr);
299         /* close the pipe */
300         if (spclose (child_process))
301                 result = max_state (result, STATE_WARNING);
303         if (nunits > 0)
304                 printf ("%s %s -%s\n", label, state_text (result), outbuff);
305         else
306                 printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units);
308         return result;
311 /* process command-line arguments */
312 int
313 process_arguments (int argc, char **argv)
315         int c;
317         if (argc < 2)
318                 return ERROR;
320         for (c = 1; c < argc; c++) {
321                 if (strcmp ("-to", argv[c]) == 0)
322                         strcpy (argv[c], "-t");
323                 if (strcmp ("-wv", argv[c]) == 0)
324                         strcpy (argv[c], "-w");
325                 if (strcmp ("-cv", argv[c]) == 0)
326                         strcpy (argv[c], "-c");
327         }
329         c = 0;
330         while (c += (call_getopt (argc - c, &argv[c]))) {
331                 if (argc <= c)
332                         break;
333                 if (server_address == NULL)
334                         server_address = strscpy (NULL, argv[c]);
335         }
337         if (community == NULL)
338                 community = strscpy (NULL, "public");
340         if (delimiter == NULL)
341                 delimiter = strscpy (NULL, DEFAULT_DELIMITER);
343         if (output_delim == NULL)
344                 output_delim = strscpy (NULL, DEFAULT_OUTPUT_DELIMITER);
346         if (label == NULL)
347                 label = strscpy (NULL, "SNMP");
349         if (units == NULL)
350                 units = strscpy (NULL, "");
352         if (port == NULL)
353                 port = strscpy(NULL,"161");
355         if (port == NULL)
356                 port = strscpy(NULL,"161");
358         return c;
361 int
362 call_getopt (int argc, char **argv)
364         char *ptr;
365         int c, i = 1;
366         int j = 0, jj = 0;
368 #ifdef HAVE_GETOPT_H
369         int option_index = 0;
370         static struct option long_options[] = {
371                 {"help", no_argument, 0, 'h'},
372                 {"version", no_argument, 0, 'V'},
373                 {"timeout", required_argument, 0, 't'},
374                 {"critical", required_argument, 0, 'c'},
375                 {"warning", required_argument, 0, 'w'},
376                 {"hostname", required_argument, 0, 'H'},
377                 {"community", required_argument, 0, 'C'},
378                 {"oid", required_argument, 0, 'o'},
379                 {"object", required_argument, 0, 'o'},
380                 {"delimiter", required_argument, 0, 'd'},
381                 {"output-delimiter", required_argument, 0, 'D'},
382                 {"string", required_argument, 0, 's'},
383                 {"regex", required_argument, 0, 'r'},
384                 {"ereg", required_argument, 0, 'r'},
385                 {"eregi", required_argument, 0, 'R'},
386                 {"label", required_argument, 0, 'l'},
387                 {"units", required_argument, 0, 'u'},
388                 {0, 0, 0, 0}
389         };
390 #endif
392         while (1) {
393 #ifdef HAVE_GETOPT_H
394                 c =
395                         getopt_long (argc, argv, "+?hVt:c:w:H:C:o:d:D:s:R:r:l:u:",
396                                                                          long_options, &option_index);
397 #else
398                 c = getopt (argc, argv, "+?hVt:c:w:H:C:o:d:D:s:R:r:l:u:");
399 #endif
401                 if (c == -1 || c == EOF)
402                         break;
404                 i++;
405                 switch (c) {
406                 case 't':
407                 case 'c':
408                 case 'w':
409                 case 'H':
410                 case 'C':
411                 case 'o':
412                 case 'd':
413                 case 'D':
414                 case 's':
415                 case 'R':
416                 case 'r':
417                 case 'l':
418                 case 'u':
419                 case 'p':
420                         i++;
421                 }
423                 switch (c) {
424                 case '?':                                                                       /* help */
425                         printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg);
426                         print_usage ();
427                         exit (STATE_UNKNOWN);
428                 case 'h':                                                                       /* help */
429                         print_help (my_basename (argv[0]));
430                         exit (STATE_OK);
431                 case 'V':                                                                       /* version */
432                         print_revision (my_basename (argv[0]), "$Revision$");
433                         exit (STATE_OK);
434                 case 't':                                                                       /* timeout period */
435                         if (!is_integer (optarg)) {
436                                 printf ("%s: Timeout Interval must be an integer!\n\n",
437                                                                 my_basename (argv[0]));
438                                 print_usage ();
439                                 exit (STATE_UNKNOWN);
440                         }
441                         timeout_interval = atoi (optarg);
442                         break;
443                 case 'c':                                                                       /* critical time threshold */
444                         if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
445                                 printf ("Invalid critical threshold: %s\n", optarg);
446                                 print_usage ();
447                                 exit (STATE_UNKNOWN);
448                         }
449                         for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
450                                 if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
451                                         eval_method[jj] |= CRIT_LT;
452                                 if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
453                                         eval_method[jj] |= CRIT_GT;
454                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
455                         }
456                         break;
457                 case 'w':                                                                       /* warning time threshold */
458                         if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
459                                 printf ("Invalid warning threshold: %s\n", optarg);
460                                 print_usage ();
461                                 exit (STATE_UNKNOWN);
462                         }
463                         for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
464                                 if (lu_getll (&lower_warn_lim[jj], ptr) == 1)
465                                         eval_method[jj] |= WARN_LT;
466                                 if (lu_getul (&upper_warn_lim[jj], ptr) == 1)
467                                         eval_method[jj] |= WARN_GT;
468                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
469                         }
470                         break;
471                 case 'H':                                                                       /* Host or server */
472                         server_address = strscpy (server_address, optarg);
473                         break;
474                 case 'C':                                                                       /* group or community */
475                         community = strscpy (community, optarg);
476                         break;
477                 case 'o':                                                                       /* object identifier */
478                         for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
479                                 ptr[0] = ' ';
480                         strncpy (oid, optarg, sizeof (oid) - 1);
481                         oid[sizeof (oid) - 1] = 0;
482                         for (ptr = optarg, j = 1; (ptr = index (ptr, ' ')); ptr++)
483                                 j++;
484                         break;
485                 case 'd':                                                                       /* delimiter */
486                         delimiter = strscpy (delimiter, optarg);
487                         break;
488                 case 'D':                                                                       /* output-delimiter */
489                         output_delim = strscpy (output_delim, optarg);
490                         break;
491                 case 's':                                                                       /* string or substring */
492                         strncpy (string_value, optarg, sizeof (string_value) - 1);
493                         string_value[sizeof (string_value) - 1] = 0;
494                         eval_method[jj++] = CRIT_STRING;
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 #else
513                         printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
514                         exit (STATE_UNKNOWN);
515 #endif
516                         break;
517                 case 'l':                                                                       /* label */
518                         label = optarg;
519                         nlabels++;
520                         if (nlabels >= labels_size) {
521                                 labels_size += 8;
522                                 labels = realloc (labels, labels_size);
523                                 if (labels == NULL)
524                                         terminate (STATE_UNKNOWN,
525                                                                                  "Could not realloc() labels[%d]", nlabels);
526                         }
527                         labels[nlabels - 1] = optarg;
528                         ptr = thisarg (optarg);
529                         if (strstr (ptr, "'") == ptr)
530                                 labels[nlabels - 1] = ptr + 1;
531                         else
532                                 labels[nlabels - 1] = ptr;
533                         while (ptr && (ptr = nextarg (ptr))) {
534                                 if (nlabels >= labels_size) {
535                                         labels_size += 8;
536                                         labels = realloc (labels, labels_size);
537                                         if (labels == NULL)
538                                                 terminate (STATE_UNKNOWN, "Could not realloc() labels\n");
539                                 }
540                                 labels++;
541                                 ptr = thisarg (ptr);
542                                 if (strstr (ptr, "'") == ptr)
543                                         labels[nlabels - 1] = ptr + 1;
544                                 else
545                                         labels[nlabels - 1] = ptr;
546                         }
547                         break;
548                 case 'u':                                                                       /* units */
549                         units = optarg;
550                         nunits++;
551                         if (nunits >= unitv_size) {
552                                 unitv_size += 8;
553                                 unitv = realloc (unitv, unitv_size);
554                                 if (unitv == NULL)
555                                         terminate (STATE_UNKNOWN,
556                                                                                  "Could not realloc() units [%d]\n", nunits);
557                         }
558                         unitv[nunits - 1] = optarg;
559                         ptr = thisarg (optarg);
560                         if (strstr (ptr, "'") == ptr)
561                                 unitv[nunits - 1] = ptr + 1;
562                         else
563                                 unitv[nunits - 1] = ptr;
564                         while (ptr && (ptr = nextarg (ptr))) {
565                                 if (nunits >= unitv_size) {
566                                         unitv_size += 8;
567                                         unitv = realloc (unitv, unitv_size);
568                                         if (units == NULL)
569                                                 terminate (STATE_UNKNOWN, "Could not realloc() units\n");
570                                 }
571                                 nunits++;
572                                 ptr = thisarg (ptr);
573                                 if (strstr (ptr, "'") == ptr)
574                                         unitv[nunits - 1] = ptr + 1;
575                                 else
576                                         unitv[nunits - 1] = ptr;
577                         }
578                         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] [-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                  "    TCP 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;