Code

cleanup process_arguments, print_help, and print_usage
[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 = NULL;
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 = NULL;
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         outbuff = strscpy (outbuff, "");
201         for (i = 0; i < MAX_OIDS; i++)
202                 eval_method[i] = CHECK_UNDEF;
203         i = 0;
205         if (process_arguments (argc, argv) == ERROR)
206                 usage ("Incorrect arguments supplied\n");
208         /* create the command line to execute */
209         asprintf (&command_line, "%s -p %s -m ALL -v 1 %s -c %s %s",
210                   PATH_TO_SNMPGET, port, server_address, community, oid);
211         if (verbose)
212                 printf ("%s\n", command_line);
214         /* run the command */
215         child_process = spopen (command_line);
216         if (child_process == NULL) {
217                 printf ("Could not open pipe: %s\n", command_line);
218                 exit (STATE_UNKNOWN);
219         }
221         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
222         if (child_stderr == NULL) {
223                 printf ("Could not open stderr for %s\n", command_line);
224         }
226         asprintf (&output, "");
227         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
228                 asprintf (&output, "%s%s", output, input_buffer);
230         if (verbose)
231                 printf ("%s\n", output);
233         ptr = output;
235         while (ptr) {
237                 ptr = strstr (ptr, delimiter);
238                 if (ptr == NULL)
239                         break;
241                 ptr += strlen (delimiter);
242                 ptr += strspn (ptr, " ");
244                 found++;
246                 if (ptr[0] == '"') {
247                         ptr++;
248                         response = strpcpy (response, ptr, "\"");
249                         ptr = strpbrk (ptr, "\"");
250                         ptr += strspn (ptr, "\"\n");
251                 }
252                 else {
253                         response = strpcpy (response, ptr, "\n");
254                         ptr = strpbrk (ptr, "\n");
255                         ptr += strspn (ptr, "\n");
256                         while
257                                 (strstr (ptr, delimiter) &&
258                                  strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter)) {
259                                 response = strpcat (response, ptr, "\n");
260                                 ptr = strpbrk (ptr, "\n");
261                         }
262                         if (ptr && strstr (ptr, delimiter) == NULL) {
263                                 response = strscat (response, ptr);
264                                 ptr = NULL;
265                         }
266                 }
268                 if (strstr (response, "Gauge: "))
269                         show = strstr (response, "Gauge: ") + 7;
270                 else if (strstr (response, "Gauge32: "))
271                         show = strstr (response, "Gauge32: ") + 9;
272                 else
273                         show = response;
274                 p2 = show;
276                 iresult = STATE_DEPENDENT;
278                 if (eval_method[i] & CRIT_PRESENT) {
279                         iresult = STATE_CRITICAL;
280                 } else if (eval_method[i] & WARN_PRESENT) {
281                         iresult = STATE_WARNING;
282                 }
284                 if (eval_method[i] & CRIT_GT ||
285                                 eval_method[i] & CRIT_LT ||
286                                 eval_method[i] & CRIT_GE ||
287                                 eval_method[i] & CRIT_LE ||
288                                 eval_method[i] & CRIT_EQ ||
289                                 eval_method[i] & CRIT_NE ||
290                                 eval_method[i] & WARN_GT ||
291                                 eval_method[i] & WARN_LT ||
292                                 eval_method[i] & WARN_GE ||
293                                 eval_method[i] & WARN_LE ||
294                                 eval_method[i] & WARN_EQ || eval_method[i] & WARN_NE) {
295                         p2 = strpbrk (p2, "0123456789");
296                         response_value[i] = strtoul (p2, NULL, 10);
297                         iresult = check_num (i);
298                         asprintf (&show, "%lu", response_value[i]);
299                         /*asprintf (&show, "%s", response); */
300                 }
302                 else if (eval_method[i] & CRIT_STRING) {
303                         if (strcmp (response, string_value))
304                                 iresult = STATE_CRITICAL;
305                         else
306                                 iresult = STATE_OK;
307                 }
309                 else if (eval_method[i] & CRIT_REGEX) {
310 #ifdef HAVE_REGEX_H
311                         excode = regexec (&preg, response, 10, pmatch, eflags);
312                         if (excode == 0) {
313                                 iresult = STATE_OK;
314                         }
315                         else if (excode != REG_NOMATCH) {
316                                 regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
317                                 printf ("Execute Error: %s\n", errbuf);
318                                 exit (STATE_CRITICAL);
319                         }
320                         else {
321                                 iresult = STATE_CRITICAL;
322                         }
323 #else
324                         printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
325                         exit (STATE_UNKNOWN);
326 #endif
327                 }
329                 if (response && iresult == STATE_DEPENDENT)
330                         iresult = STATE_OK;
331                 else if (eval_method[i] & CRIT_PRESENT)
332                         iresult = STATE_CRITICAL;
333                 else
334                         iresult = STATE_WARNING;
336                 result = max_state (result, iresult);
338                 if (nlabels > 1 && i < nlabels && labels[i] != NULL)
339                         asprintf (&outbuff, "%s%s%s %s%s%s", outbuff,
340                                   (i == 0) ? " " : output_delim,
341                                   labels[i], mark (iresult), show, mark (iresult));
342                 else
343                         asprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim,
344                                   mark (iresult), show, mark (iresult));
346                 if (nunits > 0 && i < nunits)
347                         asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
349                 i++;
351         }                                                                                                                       /* end while */
353         if (found == 0)
354                 terminate
355                         (STATE_UNKNOWN,
356                          "%s problem - No data recieved from host\nCMD: %s\n",
357                          label, command_line);
359         /* WARNING if output found on stderr */
360         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
361                 result = max_state (result, STATE_WARNING);
363         /* close stderr */
364         (void) fclose (child_stderr);
366         /* close the pipe */
367         if (spclose (child_process))
368                 result = max_state (result, STATE_WARNING);
370         if (nunits > 0)
371                 printf ("%s %s -%s\n", label, state_text (result), outbuff);
372         else
373                 printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units);
375         return result;
378 /* process command-line arguments */
379 int
380 process_arguments (int argc, char **argv)
382         char *ptr;
383         int c = 1;
384         int j = 0, jj = 0;
386 #ifdef HAVE_GETOPT_H
387         int option_index = 0;
388         static struct option long_options[] = {
389                 STD_LONG_OPTS,
390                 {"community", required_argument, 0, 'C'},
391                 {"oid", required_argument, 0, 'o'},
392                 {"object", required_argument, 0, 'o'},
393                 {"delimiter", required_argument, 0, 'd'},
394                 {"output-delimiter", required_argument, 0, 'D'},
395                 {"string", required_argument, 0, 's'},
396                 {"regex", required_argument, 0, 'r'},
397                 {"ereg", required_argument, 0, 'r'},
398                 {"eregi", required_argument, 0, 'R'},
399                 {"label", required_argument, 0, 'l'},
400                 {"units", required_argument, 0, 'u'},
401                 {"port", required_argument, 0, 'p'},
402                 {0, 0, 0, 0}
403         };
404 #endif
406         if (argc < 2)
407                 return ERROR;
409         /* reverse compatibility for very old non-POSIX usage forms */
410         for (c = 1; c < argc; c++) {
411                 if (strcmp ("-to", argv[c]) == 0)
412                         strcpy (argv[c], "-t");
413                 if (strcmp ("-wv", argv[c]) == 0)
414                         strcpy (argv[c], "-w");
415                 if (strcmp ("-cv", argv[c]) == 0)
416                         strcpy (argv[c], "-c");
417         }
419         /* initialize some args */
420         asprintf (&oid, "");
422         while (1) {
423 #ifdef HAVE_GETOPT_H
424                 c =
425                         getopt_long (argc, argv, "+?hVt:c:w:H:C:o:e:E:d:D:s:R:r:l:u:p:",
426                                                                          long_options, &option_index);
427 #else
428                 c = getopt (argc, argv, "+?hVt:c:w:H:C:o:e:E:d:D:s:R:r:l:u:p:");
429 #endif
431                 if (c == -1 || c == EOF)
432                         break;
434                 switch (c) {
435                 case '?':       /* usage */
436                         usage2 ("Unknown argument", optarg);
437                 case 'h':       /* help */
438                         print_help ();
439                         exit (STATE_OK);
440                 case 'V':       /* version */
441                         print_revision (PROGNAME, REVISION);
442                         exit (STATE_OK);
443                 case 'v': /* verbose */
444                         verbose = TRUE;
445                         break;
446                 case 't':       /* timeout period */
447                         if (!is_integer (optarg))
448                                 usage2 ("Timeout Interval must be an integer", optarg);
449                         timeout_interval = atoi (optarg);
450                         break;
451                 case 'e': /* PRELIMINARY - may change */
452                         eval_method[j] |= WARN_PRESENT;
453                         for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
454                                 ptr[0] = ' '; /* relpace comma with space */
455                         for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
456                                 eval_method[++j] |= WARN_PRESENT;
457                         asprintf (&oid, "%s %s", oid, optarg);
458                         break;
459                 case 'E': /* PRELIMINARY - may change */
460                         eval_method[j] |= WARN_PRESENT;
461                         for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
462                                 ptr[0] = ' '; /* relpace comma with space */
463                         for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
464                                 eval_method[++j] |= CRIT_PRESENT;
465                         asprintf (&oid, "%s %s", oid, optarg);
466                         break;
467                 case 'c':                                                                       /* critical time threshold */
468                         if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
469                                 printf ("Invalid critical threshold: %s\n", optarg);
470                                 print_usage ();
471                                 exit (STATE_UNKNOWN);
472                         }
473                         for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
474                                 if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
475                                         eval_method[jj] |= CRIT_LT;
476                                 if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
477                                         eval_method[jj] |= CRIT_GT;
478                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
479                         }
480                         break;
481                 case 'w':                                                                       /* warning time threshold */
482                         if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
483                                 printf ("Invalid warning threshold: %s\n", optarg);
484                                 print_usage ();
485                                 exit (STATE_UNKNOWN);
486                         }
487                         for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
488                                 if (lu_getll (&lower_warn_lim[jj], ptr) == 1)
489                                         eval_method[jj] |= WARN_LT;
490                                 if (lu_getul (&upper_warn_lim[jj], ptr) == 1)
491                                         eval_method[jj] |= WARN_GT;
492                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
493                         }
494                         break;
495                 case 'H':                                                                       /* Host or server */
496                         server_address = strscpy (server_address, optarg);
497                         break;
498                 case 'C':                                                                       /* group or community */
499                         community = strscpy (community, optarg);
500                         break;
501                 case 'o':                                                                       /* object identifier */
502                         for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
503                                 ptr[0] = ' '; /* relpace comma with space */
504                         for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++) {
505                                 j++; /* count OIDs */
506                         }
507                         asprintf (&oid, "%s %s", oid, optarg);
508                         break;
509                 case 'd':                                                                       /* delimiter */
510                         delimiter = strscpy (delimiter, optarg);
511                         break;
512                 case 'D':                                                                       /* output-delimiter */
513                         output_delim = strscpy (output_delim, optarg);
514                         break;
515                 case 's':                                                                       /* string or substring */
516                         strncpy (string_value, optarg, sizeof (string_value) - 1);
517                         string_value[sizeof (string_value) - 1] = 0;
518                         eval_method[jj++] = CRIT_STRING;
519                         break;
520                 case 'R':                                                                       /* regex */
521 #ifdef HAVE_REGEX_H
522                         cflags = REG_ICASE;
523 #endif
524                 case 'r':                                                                       /* regex */
525 #ifdef HAVE_REGEX_H
526                         cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
527                         strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
528                         regex_expect[sizeof (regex_expect) - 1] = 0;
529                         errcode = regcomp (&preg, regex_expect, cflags);
530                         if (errcode != 0) {
531                                 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
532                                 printf ("Could Not Compile Regular Expression");
533                                 return ERROR;
534                         }
535                         eval_method[jj++] = CRIT_REGEX;
536 #else
537                         printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
538                         exit (STATE_UNKNOWN);
539 #endif
540                         break;
541                 case 'l':                                                                       /* label */
542                         label = optarg;
543                         nlabels++;
544                         if (nlabels >= labels_size) {
545                                 labels_size += 8;
546                                 labels = realloc (labels, labels_size);
547                                 if (labels == NULL)
548                                         terminate (STATE_UNKNOWN,
549                                                                                  "Could not realloc() labels[%d]", nlabels);
550                         }
551                         labels[nlabels - 1] = optarg;
552                         ptr = thisarg (optarg);
553                         if (strstr (ptr, "'") == ptr)
554                                 labels[nlabels - 1] = ptr + 1;
555                         else
556                                 labels[nlabels - 1] = ptr;
557                         while (ptr && (ptr = nextarg (ptr))) {
558                                 if (nlabels >= labels_size) {
559                                         labels_size += 8;
560                                         labels = realloc (labels, labels_size);
561                                         if (labels == NULL)
562                                                 terminate (STATE_UNKNOWN, "Could not realloc() labels\n");
563                                 }
564                                 labels++;
565                                 ptr = thisarg (ptr);
566                                 if (strstr (ptr, "'") == ptr)
567                                         labels[nlabels - 1] = ptr + 1;
568                                 else
569                                         labels[nlabels - 1] = ptr;
570                         }
571                         break;
572                 case 'u':                                                                       /* units */
573                         units = optarg;
574                         nunits++;
575                         if (nunits >= unitv_size) {
576                                 unitv_size += 8;
577                                 unitv = realloc (unitv, unitv_size);
578                                 if (unitv == NULL)
579                                         terminate (STATE_UNKNOWN,
580                                                                                  "Could not realloc() units [%d]\n", nunits);
581                         }
582                         unitv[nunits - 1] = optarg;
583                         ptr = thisarg (optarg);
584                         if (strstr (ptr, "'") == ptr)
585                                 unitv[nunits - 1] = ptr + 1;
586                         else
587                                 unitv[nunits - 1] = ptr;
588                         while (ptr && (ptr = nextarg (ptr))) {
589                                 if (nunits >= unitv_size) {
590                                         unitv_size += 8;
591                                         unitv = realloc (unitv, unitv_size);
592                                         if (units == NULL)
593                                                 terminate (STATE_UNKNOWN, "Could not realloc() units\n");
594                                 }
595                                 nunits++;
596                                 ptr = thisarg (ptr);
597                                 if (strstr (ptr, "'") == ptr)
598                                         unitv[nunits - 1] = ptr + 1;
599                                 else
600                                         unitv[nunits - 1] = ptr;
601                         }
602                         break;
603                 case 'p':       /* TCP port number */
604                         port = strscpy(port, optarg);
605                         break;
607                 }
608         }
610         if (server_address == NULL)
611                 asprintf (&server_address, argv[optind]);
613         return validate_arguments ();
616 /******************************************************************************
618 @@-
619 <sect3>
620 <title>validate_arguments</title>
622 <para>&PROTO_validate_arguments;</para>
624 <para>Given a database name, this function returns TRUE if the string
625 is a valid PostgreSQL database name, and returns false if it is
626 not.</para>
628 <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
629 characters long and consist of letters, numbers, and underscores. The
630 first character cannot be a number, however.</para>
632 </sect3>
633 -@@
634 ******************************************************************************/
636 int
637 validate_arguments ()
640         if (community == NULL)
641                 asprintf (&community, DEFAULT_COMMUNITY);
643         if (delimiter == NULL)
644                 asprintf (&delimiter, DEFAULT_DELIMITER);
646         if (output_delim == NULL)
647                 asprintf (&output_delim, DEFAULT_OUTPUT_DELIMITER);
649         if (label == NULL)
650                 asprintf (&label, "SNMP");
652         if (units == NULL)
653                 asprintf (&units, "");
655         if (port == NULL)
656                 asprintf (&port, DEFAULT_PORT);
658         return OK;
660 \f
663 void
664 print_help (void)
666         print_revision (PROGNAME, REVISION);
667         printf
668                 ("Copyright (c) %s %s <%s>\n\n%s\n",
669                  COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
670         print_usage ();
671         printf
672                 ("\nOptions:\n" LONGOPTIONS "\n" DESCRIPTION "\n" NOTES "\n", 
673                  DEFAULT_COMMUNITY, DEFAULT_PORT, DEFAULT_DELIMITER, DEFAULT_TIMEOUT);
674         support ();
677 void
678 print_usage (void)
680         printf
681                 ("Usage:\n" " %s %s\n"
682                  " %s (-h | --help) for detailed help\n"
683                  " %s (-V | --version) for version information\n",
684                  PROGNAME, OPTIONS, PROGNAME, PROGNAME);
686 \f
689 char *
690 clarify_message (char *msg)
692         int i = 0;
693         int foo;
694         char tmpmsg_c[MAX_INPUT_BUFFER];
695         char *tmpmsg = (char *) &tmpmsg_c;
696         tmpmsg = strcpy (tmpmsg, msg);
697         if (!strncmp (tmpmsg, " Hex:", 5)) {
698                 tmpmsg = strtok (tmpmsg, ":");
699                 while ((tmpmsg = strtok (NULL, " "))) {
700                         foo = strtol (tmpmsg, NULL, 16);
701                         /* Translate chars that are not the same value in the printers
702                          * character set.
703                          */
704                         switch (foo) {
705                         case 208:
706                                 {
707                                         foo = 197;
708                                         break;
709                                 }
710                         case 216:
711                                 {
712                                         foo = 196;
713                                         break;
714                                 }
715                         }
716                         msg[i] = foo;
717                         i++;
718                 }
719                 msg[i] = 0;
720         }
721         return (msg);
725 int
726 check_num (int i)
728         int result;
729         result = STATE_OK;
730         if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
731                         lower_warn_lim[i] > upper_warn_lim[i]) {
732                 if (response_value[i] <= lower_warn_lim[i] &&
733                                 response_value[i] >= upper_warn_lim[i]) {
734                         result = STATE_WARNING;
735                 }
736         }
737         else if
738                 ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
739                  (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
740                  (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
741                  (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
742                  (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
743                  (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
744                 result = STATE_WARNING;
745         }
747         if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
748                         lower_warn_lim[i] > upper_warn_lim[i]) {
749                 if (response_value[i] <= lower_crit_lim[i] &&
750                                 response_value[i] >= upper_crit_lim[i]) {
751                         result = STATE_CRITICAL;
752                 }
753         }
754         else if
755                 ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
756                  (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
757                  (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
758                  (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
759                  (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
760                  (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
761                 result = STATE_CRITICAL;
762         }
764         return result;
768 int
769 lu_getll (unsigned long *ll, char *str)
771         char tmp[100];
772         if (strchr (str, ':') == NULL)
773                 return 0;
774         if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
775                 return 0;
776         if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
777                 return 1;
778         return 0;
781 int
782 lu_getul (unsigned long *ul, char *str)
784         char tmp[100];
785         if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
786                 return 1;
787         if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
788                 return 1;
789         if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
790                 return 1;
791         return 0;
799 /* trim leading whitespace
800          if there is a leading quote, make sure it balances */
802 char *
803 thisarg (char *str)
805         str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
806         if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
807                 if (strlen (str) == 1 || !strstr (str + 1, "'"))
808                         terminate (STATE_UNKNOWN, "Unbalanced quotes\n");
809         }
810         return str;
814 /* if there's a leading quote, advance to the trailing quote
815          set the trailing quote to '\x0'
816          if the string continues, advance beyond the comma */
818 char *
819 nextarg (char *str)
821         if (strstr (str, "'") == str) {
822                 if (strlen (str) > 1) {
823                         str = strstr (str + 1, "'");
824                         str[0] = 0;
825                         return (++str);
826                 }
827                 else {
828                         str[0] = 0;
829                         return NULL;
830                 }
831         }
832         if (strstr (str, ",") == str) {
833                 if (strlen (str) > 1) {
834                         str[0] = 0;
835                         return (++str);
836                 }
837                 else {
838                         str[0] = 0;
839                         return NULL;
840                 }
841         }
842         if ((str = strstr (str, ",")) && strlen (str) > 1) {
843                 str[0] = 0;
844                 return (++str);
845         }
846         return NULL;