Code

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