Code

option to specify a miblist - llow
[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] [-m miblist]"
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\
72  -v, --verbose\n\
73     Debugging the output\n\
74  -m, --miblist=STRING\n\
75     List of MIBS to be loaded (default = ALL)\n"
77 #define NOTES "\
78 - This plugin uses the 'snmpget' command included with the NET-SNMP package.\n\
79   If you don't have the package installed, you will need to download it from\n\
80   http://net-snmp.sourceforge.net before you can use this plugin.\n\
81 - Multiple OIDs may be indicated by a comma- or space-delimited list (lists with\n\
82   internal spaces must be quoted) [max 8 OIDs]\n\
83 - Ranges are inclusive and are indicated with colons. When specified as\n\
84   'min:max' a STATE_OK will be returned if the result is within the indicated\n\
85   range or is equal to the upper or lower bound. A non-OK state will be\n\
86   returned if the result is outside the specified range.\n\
87 - If specified in the order 'max:min' a non-OK state will be returned if the\n\
88   result is within the (inclusive) range.\n\
89 - Upper or lower bounds may be omitted to skip checking the respective limit.\n\
90 - Bare integers are interpreted as upper limits.\n\
91 - When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'\n\
92 - Note that only one string and one regex may be checked at present\n\
93 - All evaluation methods other than PR, STR, and SUBSTR expect that the value\n\
94   returned from the SNMP query is an unsigned integer.\n"
96 #define DESCRIPTION "\
97 This plugin gets system information on a remote server via snmp.\n"
99 #define DEFAULT_COMMUNITY "public"
100 #define DEFAULT_PORT "161"
101 #define DEFAULT_TIMEOUT 10
102 #define DEFAULT_MIBLIST "ALL"
104 #include "common.h"
105 #include "utils.h"
106 #include "popen.h"
108 #define mark(a) ((a)!=0?"*":"")
110 #define CHECK_UNDEF 0
111 #define CRIT_PRESENT 1
112 #define CRIT_STRING 2
113 #define CRIT_REGEX 4
114 #define CRIT_GT 8
115 #define CRIT_LT 16
116 #define CRIT_GE 32
117 #define CRIT_LE 64
118 #define CRIT_EQ 128
119 #define CRIT_NE 256
120 #define CRIT_RANGE 512
121 #define WARN_PRESENT 1024
122 #define WARN_STRING 2048
123 #define WARN_REGEX 4096
124 #define WARN_GT 8192
125 #define WARN_LT 16384
126 #define WARN_GE 32768
127 #define WARN_LE 65536
128 #define WARN_EQ 131072
129 #define WARN_NE 262144
130 #define WARN_RANGE 524288
132 #define MAX_OIDS 8
133 #define MAX_DELIM_LENGTH 8
134 #define DEFAULT_DELIMITER "="
135 #define DEFAULT_OUTPUT_DELIMITER " "
137 void print_usage (void);
138 void print_help (void);
139 int process_arguments (int, char **);
140 int validate_arguments (void);
141 int check_num (int);
142 char *clarify_message (char *);
143 int lu_getll (unsigned long *, char *);
144 int lu_getul (unsigned long *, char *);
145 char *thisarg (char *str);
146 char *nextarg (char *str);
148 #ifdef HAVE_REGEX_H
149 #include <regex.h>
150 char regex_expect[MAX_INPUT_BUFFER] = "";
151 regex_t preg;
152 regmatch_t pmatch[10];
153 char timestamp[10] = "";
154 char regex[MAX_INPUT_BUFFER];
155 char errbuf[MAX_INPUT_BUFFER];
156 int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
157 int eflags = 0;
158 int errcode, excode;
159 #endif
161 char *server_address = NULL;
162 char *community = NULL;
163 char *oid = "";
164 char *label = NULL;
165 char *units = NULL;
166 char *port = DEFAULT_PORT;
167 char string_value[MAX_INPUT_BUFFER] = "";
168 char **labels = NULL;
169 char **unitv = NULL;
170 int nlabels = 0;
171 int labels_size = 8;
172 int nunits = 0;
173 int unitv_size = 8;
174 int verbose = FALSE;
175 unsigned long lower_warn_lim[MAX_OIDS];
176 unsigned long upper_warn_lim[MAX_OIDS];
177 unsigned long lower_crit_lim[MAX_OIDS];
178 unsigned long upper_crit_lim[MAX_OIDS];
179 unsigned long response_value[MAX_OIDS];
180 int check_warning_value = FALSE;
181 int check_critical_value = FALSE;
182 int eval_method[MAX_OIDS];
183 char *delimiter = NULL;
184 char *output_delim = NULL;
185 char *miblist = NULL;
188 int
189 main (int argc, char **argv)
191         int i = 0;
192         int iresult = STATE_UNKNOWN;
193         int found = 0;
194         int result = STATE_DEPENDENT;
195         char input_buffer[MAX_INPUT_BUFFER];
196         char *command_line = NULL;
197         char *response = NULL;
198         char *outbuff = "";
199         char *output = NULL;
200         char *ptr = NULL;
201         char *p2 = NULL;
202         char *show = NULL;
204         labels = malloc (labels_size);
205         unitv = malloc (unitv_size);
206         for (i = 0; i < MAX_OIDS; i++)
207                 eval_method[i] = CHECK_UNDEF;
208         i = 0;
210         if (process_arguments (argc, argv) == ERROR)
211                 usage ("Incorrect arguments supplied\n");
213         /* create the command line to execute */
214         asprintf (&command_line, "%s -m %s -v 1 -c %s %s:%s %s",
215                   PATH_TO_SNMPGET, miblist, community, server_address, port, oid);
216         if (verbose)
217                 printf ("%s\n", command_line);
219         /* run the command */
220         child_process = spopen (command_line);
221         if (child_process == NULL) {
222                 printf ("Could not open pipe: %s\n", command_line);
223                 exit (STATE_UNKNOWN);
224         }
226         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
227         if (child_stderr == NULL) {
228                 printf ("Could not open stderr for %s\n", command_line);
229         }
231         asprintf (&output, "");
232         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
233                 asprintf (&output, "%s%s", output, input_buffer);
235         if (verbose)
236                 printf ("%s\n", output);
238         ptr = output;
240         while (ptr) {
242                 ptr = strstr (ptr, delimiter);
243                 if (ptr == NULL)
244                         break;
246                 ptr += strlen (delimiter);
247                 ptr += strspn (ptr, " ");
249                 found++;
251                 if (ptr[0] == '"') {
252                         ptr++;
253                         response = strpcpy (response, ptr, "\"");
254                         ptr = strpbrk (ptr, "\"");
255                         ptr += strspn (ptr, "\"\n");
256                 }
257                 else {
258                         response = strpcpy (response, ptr, "\n");
259                         ptr = strpbrk (ptr, "\n");
260                         ptr += strspn (ptr, "\n");
261                         while
262                                 (strstr (ptr, delimiter) &&
263                                  strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter)) {
264                                 response = strpcat (response, ptr, "\n");
265                                 ptr = strpbrk (ptr, "\n");
266                         }
267                         if (ptr && strstr (ptr, delimiter) == NULL) {
268                                 response = strscat (response, ptr);
269                                 ptr = NULL;
270                         }
271                 }
273                 if (strstr (response, "Gauge: "))
274                         show = strstr (response, "Gauge: ") + 7;
275                 else if (strstr (response, "Gauge32: "))
276                         show = strstr (response, "Gauge32: ") + 9;
277                 else
278                         show = response;
279                 p2 = show;
281                 iresult = STATE_DEPENDENT;
283                 if (eval_method[i] & CRIT_PRESENT) {
284                         iresult = STATE_CRITICAL;
285                 } else if (eval_method[i] & WARN_PRESENT) {
286                         iresult = STATE_WARNING;
287                 }
289                 if (eval_method[i] & CRIT_GT ||
290                                 eval_method[i] & CRIT_LT ||
291                                 eval_method[i] & CRIT_GE ||
292                                 eval_method[i] & CRIT_LE ||
293                                 eval_method[i] & CRIT_EQ ||
294                                 eval_method[i] & CRIT_NE ||
295                                 eval_method[i] & WARN_GT ||
296                                 eval_method[i] & WARN_LT ||
297                                 eval_method[i] & WARN_GE ||
298                                 eval_method[i] & WARN_LE ||
299                                 eval_method[i] & WARN_EQ || eval_method[i] & WARN_NE) {
300                         p2 = strpbrk (p2, "0123456789");
301                         response_value[i] = strtoul (p2, NULL, 10);
302                         iresult = check_num (i);
303                         asprintf (&show, "%lu", response_value[i]);
304                         /*asprintf (&show, "%s", response); */
305                 }
307                 else if (eval_method[i] & CRIT_STRING) {
308                         if (strcmp (response, string_value))
309                                 iresult = STATE_CRITICAL;
310                         else
311                                 iresult = STATE_OK;
312                 }
314                 else if (eval_method[i] & CRIT_REGEX) {
315 #ifdef HAVE_REGEX_H
316                         excode = regexec (&preg, response, 10, pmatch, eflags);
317                         if (excode == 0) {
318                                 iresult = STATE_OK;
319                         }
320                         else if (excode != REG_NOMATCH) {
321                                 regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
322                                 printf ("Execute Error: %s\n", errbuf);
323                                 exit (STATE_CRITICAL);
324                         }
325                         else {
326                                 iresult = STATE_CRITICAL;
327                         }
328 #else
329                         printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
330                         exit (STATE_UNKNOWN);
331 #endif
332                 }
334                 if (response && iresult == STATE_DEPENDENT)
335                         iresult = STATE_OK;
336                 else if (eval_method[i] & CRIT_PRESENT)
337                         iresult = STATE_CRITICAL;
338                 else
339                         iresult = STATE_WARNING;
341                 result = max_state (result, iresult);
343                 if (nlabels > 1 && i < nlabels && labels[i] != NULL)
344                         asprintf (&outbuff, "%s%s%s %s%s%s", outbuff,
345                                   (i == 0) ? " " : output_delim,
346                                   labels[i], mark (iresult), show, mark (iresult));
347                 else
348                         asprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim,
349                                   mark (iresult), show, mark (iresult));
351                 if (nunits > 0 && i < nunits)
352                         asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
354                 i++;
356         }                                                                                                                       /* end while */
358         if (found == 0)
359                 terminate
360                         (STATE_UNKNOWN,
361                          "%s problem - No data recieved from host\nCMD: %s\n",
362                          label, command_line);
364         /* WARNING if output found on stderr */
365         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
366                 result = max_state (result, STATE_WARNING);
368         /* close stderr */
369         (void) fclose (child_stderr);
371         /* close the pipe */
372         if (spclose (child_process))
373                 result = max_state (result, STATE_WARNING);
375         if (nunits > 0)
376                 printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units);
377         else
378                 printf ("%s %s -%s\n", label, state_text (result), outbuff);
380         return result;
383 /* process command-line arguments */
384 int
385 process_arguments (int argc, char **argv)
387         char *ptr;
388         int c = 1;
389         int j = 0, jj = 0;
391 #ifdef HAVE_GETOPT_H
392         int option_index = 0;
393         static struct option long_options[] = {
394                 STD_LONG_OPTS,
395                 {"community", required_argument, 0, 'C'},
396                 {"oid", required_argument, 0, 'o'},
397                 {"object", required_argument, 0, 'o'},
398                 {"delimiter", required_argument, 0, 'd'},
399                 {"output-delimiter", required_argument, 0, 'D'},
400                 {"string", required_argument, 0, 's'},
401                 {"regex", required_argument, 0, 'r'},
402                 {"ereg", required_argument, 0, 'r'},
403                 {"eregi", required_argument, 0, 'R'},
404                 {"label", required_argument, 0, 'l'},
405                 {"units", required_argument, 0, 'u'},
406                 {"port", required_argument, 0, 'p'},
407                 {0, 0, 0, 0}
408         };
409 #endif
411         if (argc < 2)
412                 return ERROR;
414         /* reverse compatibility for very old non-POSIX usage forms */
415         for (c = 1; c < argc; c++) {
416                 if (strcmp ("-to", argv[c]) == 0)
417                         strcpy (argv[c], "-t");
418                 if (strcmp ("-wv", argv[c]) == 0)
419                         strcpy (argv[c], "-w");
420                 if (strcmp ("-cv", argv[c]) == 0)
421                         strcpy (argv[c], "-c");
422         }
424         while (1) {
425 #ifdef HAVE_GETOPT_H
426                 c =
427                         getopt_long (argc, argv, "hvVt:c:w:H:C:o:e:E:d:D:s:R:r:l:u:p:m:",
428                                                                          long_options, &option_index);
429 #else
430                 c = getopt (argc, argv, "hvVt:c:w:H:C:o:e:E:d:D:s:R:r:l:u:p:m:");
431 #endif
433                 if (c == -1 || c == EOF)
434                         break;
436                 switch (c) {
437                 case '?':       /* usage */
438                         usage3 ("Unknown argument", optopt);
439                 case 'h':       /* help */
440                         print_help ();
441                         exit (STATE_OK);
442                 case 'V':       /* version */
443                         print_revision (PROGNAME, REVISION);
444                         exit (STATE_OK);
445                 case 'v': /* verbose */
446                         verbose = TRUE;
447                         break;
448                 case 't':       /* timeout period */
449                         if (!is_integer (optarg))
450                                 usage2 ("Timeout Interval must be an integer", optarg);
451                         timeout_interval = atoi (optarg);
452                         break;
453                 case 'e': /* PRELIMINARY - may change */
454                         eval_method[j] |= WARN_PRESENT;
455                         for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
456                                 ptr[0] = ' '; /* relpace comma with space */
457                         for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
458                                 eval_method[++j] |= WARN_PRESENT;
459                         asprintf (&oid, "%s %s", (oid?oid:""), optarg);
460                         break;
461                 case 'E': /* PRELIMINARY - may change */
462                         eval_method[j] |= WARN_PRESENT;
463                         for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
464                                 ptr[0] = ' '; /* relpace comma with space */
465                         for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
466                                 eval_method[++j] |= CRIT_PRESENT;
467                         asprintf (&oid, "%s %s", (oid?oid:""), optarg);
468                         break;
469                 case 'c':                                                                       /* critical time threshold */
470                         if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
471                                 printf ("Invalid critical threshold: %s\n", optarg);
472                                 print_usage ();
473                                 exit (STATE_UNKNOWN);
474                         }
475                         for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
476                                 if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
477                                         eval_method[jj] |= CRIT_LT;
478                                 if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
479                                         eval_method[jj] |= CRIT_GT;
480                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
481                         }
482                         break;
483                 case 'w':                                                                       /* warning time threshold */
484                         if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
485                                 printf ("Invalid warning threshold: %s\n", optarg);
486                                 print_usage ();
487                                 exit (STATE_UNKNOWN);
488                         }
489                         for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
490                                 if (lu_getll (&lower_warn_lim[jj], ptr) == 1)
491                                         eval_method[jj] |= WARN_LT;
492                                 if (lu_getul (&upper_warn_lim[jj], ptr) == 1)
493                                         eval_method[jj] |= WARN_GT;
494                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
495                         }
496                         break;
497                 case 'H':                                                                       /* Host or server */
498                         server_address = strscpy (server_address, optarg);
499                         break;
500                 case 'C':                                                                       /* group or community */
501                         community = strscpy (community, optarg);
502                         break;
503                 case 'o':                                                                       /* object identifier */
504                         for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
505                                 ptr[0] = ' '; /* relpace comma with space */
506                         for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
507                                 j++; /* count OIDs */
508                         asprintf (&oid, "%s %s", (oid?oid:""), optarg);
509                         break;
510                 case 'd':                                                                       /* delimiter */
511                         delimiter = strscpy (delimiter, optarg);
512                         break;
513                 case 'D':                                                                       /* output-delimiter */
514                         output_delim = strscpy (output_delim, optarg);
515                         break;
516                 case 's':                                                                       /* string or substring */
517                         strncpy (string_value, optarg, sizeof (string_value) - 1);
518                         string_value[sizeof (string_value) - 1] = 0;
519                         eval_method[jj++] = CRIT_STRING;
520                         break;
521                 case 'R':                                                                       /* regex */
522 #ifdef HAVE_REGEX_H
523                         cflags = REG_ICASE;
524 #endif
525                 case 'r':                                                                       /* regex */
526 #ifdef HAVE_REGEX_H
527                         cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
528                         strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
529                         regex_expect[sizeof (regex_expect) - 1] = 0;
530                         errcode = regcomp (&preg, regex_expect, cflags);
531                         if (errcode != 0) {
532                                 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
533                                 printf ("Could Not Compile Regular Expression");
534                                 return ERROR;
535                         }
536                         eval_method[jj++] = CRIT_REGEX;
537 #else
538                         printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
539                         exit (STATE_UNKNOWN);
540 #endif
541                         break;
542                 case 'l':                                                                       /* label */
543                         label = optarg;
544                         nlabels++;
545                         if (nlabels >= labels_size) {
546                                 labels_size += 8;
547                                 labels = realloc (labels, labels_size);
548                                 if (labels == NULL)
549                                         terminate (STATE_UNKNOWN,
550                                                                                  "Could not realloc() labels[%d]", nlabels);
551                         }
552                         labels[nlabels - 1] = optarg;
553                         ptr = thisarg (optarg);
554                         if (strstr (ptr, "'") == ptr)
555                                 labels[nlabels - 1] = ptr + 1;
556                         else
557                                 labels[nlabels - 1] = ptr;
558                         while (ptr && (ptr = nextarg (ptr))) {
559                                 if (nlabels >= labels_size) {
560                                         labels_size += 8;
561                                         labels = realloc (labels, labels_size);
562                                         if (labels == NULL)
563                                                 terminate (STATE_UNKNOWN, "Could not realloc() labels\n");
564                                 }
565                                 labels++;
566                                 ptr = thisarg (ptr);
567                                 if (strstr (ptr, "'") == ptr)
568                                         labels[nlabels - 1] = ptr + 1;
569                                 else
570                                         labels[nlabels - 1] = ptr;
571                         }
572                         break;
573                 case 'u':                                                                       /* units */
574                         units = optarg;
575                         nunits++;
576                         if (nunits >= unitv_size) {
577                                 unitv_size += 8;
578                                 unitv = realloc (unitv, unitv_size);
579                                 if (unitv == NULL)
580                                         terminate (STATE_UNKNOWN,
581                                                                                  "Could not realloc() units [%d]\n", nunits);
582                         }
583                         unitv[nunits - 1] = optarg;
584                         ptr = thisarg (optarg);
585                         if (strstr (ptr, "'") == ptr)
586                                 unitv[nunits - 1] = ptr + 1;
587                         else
588                                 unitv[nunits - 1] = ptr;
589                         while (ptr && (ptr = nextarg (ptr))) {
590                                 if (nunits >= unitv_size) {
591                                         unitv_size += 8;
592                                         unitv = realloc (unitv, unitv_size);
593                                         if (units == NULL)
594                                                 terminate (STATE_UNKNOWN, "Could not realloc() units\n");
595                                 }
596                                 nunits++;
597                                 ptr = thisarg (ptr);
598                                 if (strstr (ptr, "'") == ptr)
599                                         unitv[nunits - 1] = ptr + 1;
600                                 else
601                                         unitv[nunits - 1] = ptr;
602                         }
603                         break;
604                 case 'p':       /* TCP port number */
605                         port = strscpy(port, optarg);
606                         break;
607                 case 'm':      /* List of MIBS  */
608                         miblist = strscpy(miblist, optarg);
609                         break;
611                 }
612         }
614         if (server_address == NULL)
615                 asprintf (&server_address, argv[optind]);
617         return validate_arguments ();
620 /******************************************************************************
622 @@-
623 <sect3>
624 <title>validate_arguments</title>
626 <para>&PROTO_validate_arguments;</para>
628 <para>Given a database name, this function returns TRUE if the string
629 is a valid PostgreSQL database name, and returns false if it is
630 not.</para>
632 <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
633 characters long and consist of letters, numbers, and underscores. The
634 first character cannot be a number, however.</para>
636 </sect3>
637 -@@
638 ******************************************************************************/
640 int
641 validate_arguments ()
644         if (community == NULL)
645                 asprintf (&community, DEFAULT_COMMUNITY);
647         if (delimiter == NULL)
648                 asprintf (&delimiter, DEFAULT_DELIMITER);
650         if (output_delim == NULL)
651                 asprintf (&output_delim, DEFAULT_OUTPUT_DELIMITER);
653         if (miblist == NULL)
654                 asprintf (&miblist, DEFAULT_MIBLIST);
656         if (label == NULL)
657                 asprintf (&label, "SNMP");
659         if (units == NULL)
660                 asprintf (&units, "");
662         return OK;
664 \f
667 void
668 print_help (void)
670         print_revision (PROGNAME, REVISION);
671         printf
672                 ("Copyright (c) %s %s <%s>\n\n%s\n",
673                  COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
674         print_usage ();
675         printf
676                 ("\nOptions:\n" LONGOPTIONS "\n" DESCRIPTION "\n" NOTES "\n", 
677                  DEFAULT_COMMUNITY, DEFAULT_PORT, DEFAULT_DELIMITER, DEFAULT_TIMEOUT);
678         support ();
681 void
682 print_usage (void)
684         printf
685                 ("Usage:\n" " %s %s\n"
686                  " %s (-h | --help) for detailed help\n"
687                  " %s (-V | --version) for version information\n",
688                  PROGNAME, OPTIONS, PROGNAME, PROGNAME);
690 \f
693 char *
694 clarify_message (char *msg)
696         int i = 0;
697         int foo;
698         char tmpmsg_c[MAX_INPUT_BUFFER];
699         char *tmpmsg = (char *) &tmpmsg_c;
700         tmpmsg = strcpy (tmpmsg, msg);
701         if (!strncmp (tmpmsg, " Hex:", 5)) {
702                 tmpmsg = strtok (tmpmsg, ":");
703                 while ((tmpmsg = strtok (NULL, " "))) {
704                         foo = strtol (tmpmsg, NULL, 16);
705                         /* Translate chars that are not the same value in the printers
706                          * character set.
707                          */
708                         switch (foo) {
709                         case 208:
710                                 {
711                                         foo = 197;
712                                         break;
713                                 }
714                         case 216:
715                                 {
716                                         foo = 196;
717                                         break;
718                                 }
719                         }
720                         msg[i] = foo;
721                         i++;
722                 }
723                 msg[i] = 0;
724         }
725         return (msg);
729 int
730 check_num (int i)
732         int result;
733         result = STATE_OK;
734         if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
735                         lower_warn_lim[i] > upper_warn_lim[i]) {
736                 if (response_value[i] <= lower_warn_lim[i] &&
737                                 response_value[i] >= upper_warn_lim[i]) {
738                         result = STATE_WARNING;
739                 }
740         }
741         else if
742                 ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
743                  (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
744                  (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
745                  (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
746                  (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
747                  (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
748                 result = STATE_WARNING;
749         }
751         if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
752                         lower_warn_lim[i] > upper_warn_lim[i]) {
753                 if (response_value[i] <= lower_crit_lim[i] &&
754                                 response_value[i] >= upper_crit_lim[i]) {
755                         result = STATE_CRITICAL;
756                 }
757         }
758         else if
759                 ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
760                  (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
761                  (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
762                  (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
763                  (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
764                  (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
765                 result = STATE_CRITICAL;
766         }
768         return result;
772 int
773 lu_getll (unsigned long *ll, char *str)
775         char tmp[100];
776         if (strchr (str, ':') == NULL)
777                 return 0;
778         if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
779                 return 0;
780         if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
781                 return 1;
782         return 0;
785 int
786 lu_getul (unsigned long *ul, char *str)
788         char tmp[100];
789         if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
790                 return 1;
791         if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
792                 return 1;
793         if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
794                 return 1;
795         return 0;
803 /* trim leading whitespace
804          if there is a leading quote, make sure it balances */
806 char *
807 thisarg (char *str)
809         str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
810         if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
811                 if (strlen (str) == 1 || !strstr (str + 1, "'"))
812                         terminate (STATE_UNKNOWN, "Unbalanced quotes\n");
813         }
814         return str;
818 /* if there's a leading quote, advance to the trailing quote
819          set the trailing quote to '\x0'
820          if the string continues, advance beyond the comma */
822 char *
823 nextarg (char *str)
825         if (strstr (str, "'") == str) {
826                 if (strlen (str) > 1) {
827                         str = strstr (str + 1, "'");
828                         str[0] = 0;
829                         return (++str);
830                 }
831                 else {
832                         str[0] = 0;
833                         return NULL;
834                 }
835         }
836         if (strstr (str, ",") == str) {
837                 if (strlen (str) > 1) {
838                         str[0] = 0;
839                         return (++str);
840                 }
841                 else {
842                         str[0] = 0;
843                         return NULL;
844                 }
845         }
846         if ((str = strstr (str, ",")) && strlen (str) > 1) {
847                 str[0] = 0;
848                 return (++str);
849         }
850         return NULL;