Code

fixed scanning for multiple OIDs, which was not working
[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 = NULL;
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 int verbose = FALSE;
109 unsigned long lower_warn_lim[MAX_OIDS];
110 unsigned long upper_warn_lim[MAX_OIDS];
111 unsigned long lower_crit_lim[MAX_OIDS];
112 unsigned long upper_crit_lim[MAX_OIDS];
113 unsigned long response_value[MAX_OIDS];
114 int check_warning_value = FALSE;
115 int check_critical_value = FALSE;
116 int eval_method[MAX_OIDS];
117 char *delimiter = NULL;
118 char *output_delim = NULL;
121 int
122 main (int argc, char **argv)
124         int i = 0;
125         int iresult = STATE_UNKNOWN;
126         int found = 0;
127         int result = STATE_DEPENDENT;
128         char input_buffer[MAX_INPUT_BUFFER];
129         char *command_line = NULL;
130         char *response = NULL;
131         char *outbuff = NULL;
132         char *output = NULL;
133         char *ptr = NULL;
134         char *p2 = NULL;
135         char *show = NULL;
137         labels = malloc (labels_size);
138         unitv = malloc (unitv_size);
139         outbuff = strscpy (outbuff, "");
140         for (i = 0; i < MAX_OIDS; i++)
141                 eval_method[i] = CHECK_UNDEF;
142         i = 0;
144         if (process_arguments (argc, argv) == ERROR)
145                 usage ("Incorrect arguments supplied\n");
147         /* create the command line to execute */
148         asprintf (&command_line, "%s -p %s -m ALL -v 1 %s -c %s %s",
149                   PATH_TO_SNMPGET, port, server_address, community, oid);
150         if (verbose)
151                 printf ("%s\n", command_line);
153         /* run the command */
154         child_process = spopen (command_line);
155         if (child_process == NULL) {
156                 printf ("Could not open pipe: %s\n", command_line);
157                 exit (STATE_UNKNOWN);
158         }
160         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
161         if (child_stderr == NULL) {
162                 printf ("Could not open stderr for %s\n", command_line);
163         }
165         asprintf (&output, "");
166         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
167                 asprintf (&output, "%s%s", output, input_buffer);
169         if (verbose)
170                 printf ("%s\n", output);
172         ptr = output;
174         while (ptr) {
176                 ptr = strstr (ptr, delimiter);
177                 if (ptr == NULL)
178                         break;
180                 ptr += strlen (delimiter);
181                 ptr += strspn (ptr, " ");
183                 found++;
185                 if (ptr[0] == '"') {
186                         ptr++;
187                         response = strpcpy (response, ptr, "\"");
188                         ptr = strpbrk (ptr, "\"");
189                         ptr += strspn (ptr, "\"\n");
190                 }
191                 else {
192                         response = strpcpy (response, ptr, "\n");
193                         ptr = strpbrk (ptr, "\n");
194                         ptr += strspn (ptr, "\n");
195                         while
196                                 (strstr (ptr, delimiter) &&
197                                  strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter)) {
198                                 response = strpcat (response, ptr, "\n");
199                                 ptr = strpbrk (ptr, "\n");
200                         }
201                         if (ptr && strstr (ptr, delimiter) == NULL) {
202                                 response = strscat (response, ptr);
203                                 ptr = NULL;
204                         }
205                 }
207                 if (strstr (response, "Gauge: "))
208                         show = strstr (response, "Gauge: ") + 7;
209                 else if (strstr (response, "Gauge32: "))
210                         show = strstr (response, "Gauge32: ") + 9;
211                 else
212                         show = response;
213                 p2 = show;
215                 if (eval_method[i] & CRIT_GT ||
216                                 eval_method[i] & CRIT_LT ||
217                                 eval_method[i] & CRIT_GE ||
218                                 eval_method[i] & CRIT_LE ||
219                                 eval_method[i] & CRIT_EQ ||
220                                 eval_method[i] & CRIT_NE ||
221                                 eval_method[i] & WARN_GT ||
222                                 eval_method[i] & WARN_LT ||
223                                 eval_method[i] & WARN_GE ||
224                                 eval_method[i] & WARN_LE ||
225                                 eval_method[i] & WARN_EQ || eval_method[i] & WARN_NE) {
226                         p2 = strpbrk (p2, "0123456789");
227                         response_value[i] = strtoul (p2, NULL, 10);
228                         iresult = check_num (i);
229                         asprintf (&show, "%lu", response_value[i]);
230                         /*asprintf (&show, "%s", response); */
231                 }
233                 else if (eval_method[i] & CRIT_STRING) {
234                         if (strcmp (response, string_value))
235                                 iresult = STATE_CRITICAL;
236                         else
237                                 iresult = STATE_OK;
238                 }
240                 else if (eval_method[i] & CRIT_REGEX) {
241 #ifdef HAVE_REGEX_H
242                         excode = regexec (&preg, response, 10, pmatch, eflags);
243                         if (excode == 0) {
244                                 iresult = STATE_OK;
245                         }
246                         else if (excode != REG_NOMATCH) {
247                                 regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
248                                 printf ("Execute Error: %s\n", errbuf);
249                                 exit (STATE_CRITICAL);
250                         }
251                         else {
252                                 iresult = STATE_CRITICAL;
253                         }
254 #else
255                         printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
256                         exit (STATE_UNKNOWN);
257 #endif
258                 }
260                 else {
261                         if (response)
262                                 iresult = STATE_OK;
263                         else if (eval_method[i] & CRIT_PRESENT)
264                                 iresult = STATE_CRITICAL;
265                         else
266                                 iresult = STATE_WARNING;
267                 }
269                 result = max_state (result, iresult);
271                 if (nlabels > 1 && i < nlabels && labels[i] != NULL)
272                         asprintf (&outbuff, "%s%s%s %s%s%s", outbuff,
273                                   (i == 0) ? " " : output_delim,
274                                   labels[i], mark (iresult), show, mark (iresult));
275                 else
276                         asprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim,
277                                   mark (iresult), show, mark (iresult));
279                 if (nunits > 0 && i < nunits)
280                         asprintf (&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         char *ptr;
316         int c, i = 1;
317         int j = 0, jj = 0;
319 #ifdef HAVE_GETOPT_H
320         int option_index = 0;
321         static struct option long_options[] = {
322                 STD_LONG_OPTS,
323                 {"community", required_argument, 0, 'C'},
324                 {"oid", required_argument, 0, 'o'},
325                 {"object", required_argument, 0, 'o'},
326                 {"delimiter", required_argument, 0, 'd'},
327                 {"output-delimiter", required_argument, 0, 'D'},
328                 {"string", required_argument, 0, 's'},
329                 {"regex", required_argument, 0, 'r'},
330                 {"ereg", required_argument, 0, 'r'},
331                 {"eregi", required_argument, 0, 'R'},
332                 {"label", required_argument, 0, 'l'},
333                 {"units", required_argument, 0, 'u'},
334                 {"port", required_argument, 0, 'p'},
335                 {0, 0, 0, 0}
336         };
337 #endif
339         if (argc < 2)
340                 return ERROR;
342         /* reverse compatibility for very old non-POSIX usage forms */
343         for (c = 1; c < argc; c++) {
344                 if (strcmp ("-to", argv[c]) == 0)
345                         strcpy (argv[c], "-t");
346                 if (strcmp ("-wv", argv[c]) == 0)
347                         strcpy (argv[c], "-w");
348                 if (strcmp ("-cv", argv[c]) == 0)
349                         strcpy (argv[c], "-c");
350         }
352         /* initialize some args */
353         asprintf (&oid, "");
355         while (1) {
356 #ifdef HAVE_GETOPT_H
357                 c =
358                         getopt_long (argc, argv, "+?hVt:c:w:H:C:o:d:D:s:R:r:l:u:p:",
359                                                                          long_options, &option_index);
360 #else
361                 c = getopt (argc, argv, "+?hVt:c:w:H:C:o:d:D:s:R:r:l:u:p:");
362 #endif
364                 if (c == -1 || c == EOF)
365                         break;
367                 switch (c) {
368                 case '?':                                                                       /* help */
369                         printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg);
370                         print_usage ();
371                         exit (STATE_UNKNOWN);
372                 case 'h':                                                                       /* help */
373                         print_help (my_basename (argv[0]));
374                         exit (STATE_OK);
375                 case 'V':                                                                       /* version */
376                         print_revision (my_basename (argv[0]), "$Revision$");
377                         exit (STATE_OK);
378                 case 'v': /* verbose */
379                         verbose = TRUE;
380                         break;
381                 case 't':                                                                       /* timeout period */
382                         if (!is_integer (optarg)) {
383                                 printf ("%s: Timeout Interval must be an integer!\n\n",
384                                                                 my_basename (argv[0]));
385                                 print_usage ();
386                                 exit (STATE_UNKNOWN);
387                         }
388                         timeout_interval = atoi (optarg);
389                         break;
390                 case 'c':                                                                       /* critical time threshold */
391                         if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
392                                 printf ("Invalid critical threshold: %s\n", optarg);
393                                 print_usage ();
394                                 exit (STATE_UNKNOWN);
395                         }
396                         for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
397                                 if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
398                                         eval_method[jj] |= CRIT_LT;
399                                 if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
400                                         eval_method[jj] |= CRIT_GT;
401                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
402                         }
403                         break;
404                 case 'w':                                                                       /* warning time threshold */
405                         if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
406                                 printf ("Invalid warning threshold: %s\n", optarg);
407                                 print_usage ();
408                                 exit (STATE_UNKNOWN);
409                         }
410                         for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
411                                 if (lu_getll (&lower_warn_lim[jj], ptr) == 1)
412                                         eval_method[jj] |= WARN_LT;
413                                 if (lu_getul (&upper_warn_lim[jj], ptr) == 1)
414                                         eval_method[jj] |= WARN_GT;
415                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
416                         }
417                         break;
418                 case 'H':                                                                       /* Host or server */
419                         server_address = strscpy (server_address, optarg);
420                         break;
421                 case 'C':                                                                       /* group or community */
422                         community = strscpy (community, optarg);
423                         break;
424                 case 'o':                                                                       /* object identifier */
425                         for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
426                                 ptr[0] = ' '; /* relpace comma with space */
427                         for (ptr = optarg, j = 1; (ptr = index (ptr, ' ')); ptr++)
428                                 j++; /* count OIDs */
429                         asprintf (&oid, "%s %s", oid, optarg);
430                         break;
431                 case 'd':                                                                       /* delimiter */
432                         delimiter = strscpy (delimiter, optarg);
433                         break;
434                 case 'D':                                                                       /* output-delimiter */
435                         output_delim = strscpy (output_delim, optarg);
436                         break;
437                 case 's':                                                                       /* string or substring */
438                         strncpy (string_value, optarg, sizeof (string_value) - 1);
439                         string_value[sizeof (string_value) - 1] = 0;
440                         eval_method[jj++] = CRIT_STRING;
441                         break;
442                 case 'R':                                                                       /* regex */
443 #ifdef HAVE_REGEX_H
444                         cflags = REG_ICASE;
445 #endif
446                 case 'r':                                                                       /* regex */
447 #ifdef HAVE_REGEX_H
448                         cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
449                         strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
450                         regex_expect[sizeof (regex_expect) - 1] = 0;
451                         errcode = regcomp (&preg, regex_expect, cflags);
452                         if (errcode != 0) {
453                                 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
454                                 printf ("Could Not Compile Regular Expression");
455                                 return ERROR;
456                         }
457                         eval_method[jj++] = CRIT_REGEX;
458 #else
459                         printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
460                         exit (STATE_UNKNOWN);
461 #endif
462                         break;
463                 case 'l':                                                                       /* label */
464                         label = optarg;
465                         nlabels++;
466                         if (nlabels >= labels_size) {
467                                 labels_size += 8;
468                                 labels = realloc (labels, labels_size);
469                                 if (labels == NULL)
470                                         terminate (STATE_UNKNOWN,
471                                                                                  "Could not realloc() labels[%d]", nlabels);
472                         }
473                         labels[nlabels - 1] = optarg;
474                         ptr = thisarg (optarg);
475                         if (strstr (ptr, "'") == ptr)
476                                 labels[nlabels - 1] = ptr + 1;
477                         else
478                                 labels[nlabels - 1] = ptr;
479                         while (ptr && (ptr = nextarg (ptr))) {
480                                 if (nlabels >= labels_size) {
481                                         labels_size += 8;
482                                         labels = realloc (labels, labels_size);
483                                         if (labels == NULL)
484                                                 terminate (STATE_UNKNOWN, "Could not realloc() labels\n");
485                                 }
486                                 labels++;
487                                 ptr = thisarg (ptr);
488                                 if (strstr (ptr, "'") == ptr)
489                                         labels[nlabels - 1] = ptr + 1;
490                                 else
491                                         labels[nlabels - 1] = ptr;
492                         }
493                         break;
494                 case 'u':                                                                       /* units */
495                         units = optarg;
496                         nunits++;
497                         if (nunits >= unitv_size) {
498                                 unitv_size += 8;
499                                 unitv = realloc (unitv, unitv_size);
500                                 if (unitv == NULL)
501                                         terminate (STATE_UNKNOWN,
502                                                                                  "Could not realloc() units [%d]\n", nunits);
503                         }
504                         unitv[nunits - 1] = optarg;
505                         ptr = thisarg (optarg);
506                         if (strstr (ptr, "'") == ptr)
507                                 unitv[nunits - 1] = ptr + 1;
508                         else
509                                 unitv[nunits - 1] = ptr;
510                         while (ptr && (ptr = nextarg (ptr))) {
511                                 if (nunits >= unitv_size) {
512                                         unitv_size += 8;
513                                         unitv = realloc (unitv, unitv_size);
514                                         if (units == NULL)
515                                                 terminate (STATE_UNKNOWN, "Could not realloc() units\n");
516                                 }
517                                 nunits++;
518                                 ptr = thisarg (ptr);
519                                 if (strstr (ptr, "'") == ptr)
520                                         unitv[nunits - 1] = ptr + 1;
521                                 else
522                                         unitv[nunits - 1] = ptr;
523                         }
524                         break;
525                 case 'p':       /* TCP port number */
526                         port = strscpy(port, optarg);
527                         break;
529                 }
530         }
532         c = optind;
533         if (server_address == NULL)
534                 server_address = strscpy (NULL, argv[c++]);
536         if (community == NULL)
537                 community = strscpy (NULL, "public");
539         if (delimiter == NULL)
540                 delimiter = strscpy (NULL, DEFAULT_DELIMITER);
542         if (output_delim == NULL)
543                 output_delim = strscpy (NULL, DEFAULT_OUTPUT_DELIMITER);
545         if (label == NULL)
546                 label = strscpy (NULL, "SNMP");
548         if (units == NULL)
549                 units = strscpy (NULL, "");
551         if (port == NULL)
552                 port = strscpy(NULL,"161");
554         return i;
557 void
558 print_usage (void)
560         printf
561                 ("Usage: check_snmp -H <ip_address> -o <OID> [-w warn_range] [-c crit_range] \n"
562                  "                  [-C community] [-s string] [-r regex] [-R regexi] [-t timeout]\n"
563                  "                  [-l label] [-u units] [-p port-number] [-d delimiter] [-D output-delimiter]\n"
564                  "       check_snmp --help\n" "       check_snmp --version\n");
567 void
568 print_help (char *cmd)
570         printf ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n"
571                                         "License: GPL\n\n");
572         print_usage ();
573         printf
574                 ("\nOptions:\n"
575                  " -h, --help\n"
576                  "    Print detailed help screen\n"
577                  " -V, --version\n"
578                  "    Print version information\n"
579                  " -H, --hostname=HOST\n"
580                  "    Name or IP address of the device you wish to query\n"
581                  " -o, --oid=OID(s)\n"
582                  "    Object identifier(s) whose value you wish to query\n"
583                  " -w, --warning=INTEGER_RANGE(s)\n"
584                  "    Range(s) which will not result in a WARNING status\n"
585                  " -c, --critical=INTEGER_RANGE(s)\n"
586                  "    Range(s) which will not result in a CRITICAL status\n"
587                  " -C, --community=STRING\n"
588                  "    Optional community string for SNMP communication\n"
589                  "    (default is \"public\")\n"
590                  " -u, --units=STRING\n"
591                  "    Units label(s) for output data (e.g., 'sec.').\n"
592                  " -p, --port=STRING\n"
593          "    UDP port number target is listening on.\n"
594                  " -d, --delimiter=STRING\n"
595                  "    Delimiter to use when parsing returned data. Default is \"%s\"\n"
596                  "    Any data on the right hand side of the delimiter is considered\n"
597                  "    to be the data that should be used in the evaluation.\n"
598                  " -t, --timeout=INTEGER\n"
599                  "    Seconds to wait before plugin times out (see also nagios server timeout)\n"
600                  " -D, --output-delimiter=STRING\n"
601                  "    Separates output on multiple OID requests\n"
602                  " -s, --string=STRING\n"
603                  "    Return OK state (for that OID) if STRING is an exact match\n"
604                  " -r, --ereg=REGEX\n"
605                  "    Return OK state (for that OID) if extended regular expression REGEX matches\n"
606                  " -R, --eregi=REGEX\n"
607                  "    Return OK state (for that OID) if case-insensitive extended REGEX matches\n"
608                  " -l, --label=STRING\n"
609                  "    Prefix label for output from plugin (default -s 'SNMP')\n\n"
610                  "- This plugin uses the 'snmpget' command included with the UCD-SNMP package.\n"
611                  "  If you don't have the package installed, you will need to download it from\n"
612                  "  http://ucd-snmp.ucdavis.edu before you can use this plugin.\n"
613                  "- Multiple OIDs may be indicated by a comma- or space-delimited list (lists with\n"
614                  "  internal spaces must be quoted)\n"
615                  "- Ranges are inclusive and are indicated with colons. When specified as\n"
616                  "  'min:max' a STATE_OK will be returned if the result is within the indicated\n"
617                  "  range or is equal to the upper or lower bound. A non-OK state will be\n"
618                  "  returned if the result is outside the specified range.\n"
619                  "- If spcified in the order 'max:min' a non-OK state will be returned if the\n"
620                  "  result is within the (inclusive) range.\n"
621                  "- Upper or lower bounds may be omitted to skip checking the respective limit.\n"
622                  "- Bare integers are interpreted as upper limits.\n"
623                  "- When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'\n"
624                  "- Note that only one string and one regex may be checked at present\n"
625                  "- All evaluation methods other than PR, STR, and SUBSTR expect that the value\n"
626                  "  returned from the SNMP query is an unsigned integer.\n\n",
627                  DEFAULT_DELIMITER);
630 char *
631 clarify_message (char *msg)
633         int i = 0;
634         int foo;
635         char tmpmsg_c[MAX_INPUT_BUFFER];
636         char *tmpmsg = (char *) &tmpmsg_c;
637         tmpmsg = strcpy (tmpmsg, msg);
638         if (!strncmp (tmpmsg, " Hex:", 5)) {
639                 tmpmsg = strtok (tmpmsg, ":");
640                 while ((tmpmsg = strtok (NULL, " "))) {
641                         foo = strtol (tmpmsg, NULL, 16);
642                         /* Translate chars that are not the same value in the printers
643                          * character set.
644                          */
645                         switch (foo) {
646                         case 208:
647                                 {
648                                         foo = 197;
649                                         break;
650                                 }
651                         case 216:
652                                 {
653                                         foo = 196;
654                                         break;
655                                 }
656                         }
657                         msg[i] = foo;
658                         i++;
659                 }
660                 msg[i] = 0;
661         }
662         return (msg);
666 int
667 check_num (int i)
669         int result;
670         result = STATE_OK;
671         if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
672                         lower_warn_lim[i] > upper_warn_lim[i]) {
673                 if (response_value[i] <= lower_warn_lim[i] &&
674                                 response_value[i] >= upper_warn_lim[i]) {
675                         result = STATE_WARNING;
676                 }
677         }
678         else if
679                 ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
680                  (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
681                  (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
682                  (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
683                  (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
684                  (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
685                 result = STATE_WARNING;
686         }
688         if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
689                         lower_warn_lim[i] > upper_warn_lim[i]) {
690                 if (response_value[i] <= lower_crit_lim[i] &&
691                                 response_value[i] >= upper_crit_lim[i]) {
692                         result = STATE_CRITICAL;
693                 }
694         }
695         else if
696                 ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
697                  (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
698                  (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
699                  (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
700                  (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
701                  (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
702                 result = STATE_CRITICAL;
703         }
705         return result;
709 int
710 lu_getll (unsigned long *ll, char *str)
712         char tmp[100];
713         if (strchr (str, ':') == NULL)
714                 return 0;
715         if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
716                 return 0;
717         if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
718                 return 1;
719         return 0;
722 int
723 lu_getul (unsigned long *ul, char *str)
725         char tmp[100];
726         if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
727                 return 1;
728         if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
729                 return 1;
730         if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
731                 return 1;
732         return 0;
740 /* trim leading whitespace
741          if there is a leading quote, make sure it balances */
743 char *
744 thisarg (char *str)
746         str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
747         if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
748                 if (strlen (str) == 1 || !strstr (str + 1, "'"))
749                         terminate (STATE_UNKNOWN, "Unbalanced quotes\n");
750         }
751         return str;
755 /* if there's a leading quote, advance to the trailing quote
756          set the trailing quote to '\x0'
757          if the string continues, advance beyond the comma */
759 char *
760 nextarg (char *str)
762         if (strstr (str, "'") == str) {
763                 if (strlen (str) > 1) {
764                         str = strstr (str + 1, "'");
765                         str[0] = 0;
766                         return (++str);
767                 }
768                 else {
769                         str[0] = 0;
770                         return NULL;
771                 }
772         }
773         if (strstr (str, ",") == str) {
774                 if (strlen (str) > 1) {
775                         str[0] = 0;
776                         return (++str);
777                 }
778                 else {
779                         str[0] = 0;
780                         return NULL;
781                 }
782         }
783         if ((str = strstr (str, ",")) && strlen (str) > 1) {
784                 str[0] = 0;
785                 return (++str);
786         }
787         return NULL;