Code

cause snmpget try try for 1 second less than the timeout (allowing plugin to force...
[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 const char *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] [-P snmp version]\n\
38           [-L seclevel] [-U secname] [-a authproto] [-A authpasswd]\n\
39           [-X privpasswd]\n"
41 #define LONGOPTIONS "\
42  -H, --hostname=HOST\n\
43     Name or IP address of the device you wish to query\n\
44  -o, --oid=OID(s)\n\
45     Object identifier(s) whose value you wish to query\n\
46  -w, --warning=INTEGER_RANGE(s)\n\
47     Range(s) which will not result in a WARNING status\n\
48  -c, --critical=INTEGER_RANGE(s)\n\
49     Range(s) which will not result in a CRITICAL status\n\
50  -C, --community=STRING\n\
51     Optional community string for SNMP communication\n\
52     (default is \"%s\")\n\
53  -u, --units=STRING\n\
54     Units label(s) for output data (e.g., 'sec.').\n\
55  -p, --port=STRING\n\
56     UDP port number target is listening on. Default is \"%s\"\n\
57  -P, --protocol=[1|3]\n\
58     SNMP protocol version\n\
59  -L, --seclevel=[noAuthNoPriv|authNoPriv|authPriv]\n\
60     SNMPv3 securityLevel\n\
61  -U, --secname=USERNAME\n\
62     SNMPv3 username\n\
63  -a, --authproto=[MD5|SHA]\n\
64     SNMPv3 auth proto\n\
65  -A, --authpassword=PASSWORD\n\
66     SNMPv3 authentication password\n\
67  -X, --privpasswd=PASSWORD\n\
68     SNMPv3 crypt passwd (DES)\n\
69  -d, --delimiter=STRING\n\
70     Delimiter to use when parsing returned data. Default is \"%s\"\n\
71     Any data on the right hand side of the delimiter is considered\n\
72     to be the data that should be used in the evaluation.\n\
73  -t, --timeout=INTEGER\n\
74     Seconds to wait before plugin times out (see also nagios server timeout).\n\
75     Default is %d seconds\n\
76  -D, --output-delimiter=STRING\n\
77     Separates output on multiple OID requests\n\
78  -s, --string=STRING\n\
79     Return OK state (for that OID) if STRING is an exact match\n\
80  -r, --ereg=REGEX\n\
81     Return OK state (for that OID) if extended regular expression REGEX matches\n\
82  -R, --eregi=REGEX\n\
83     Return OK state (for that OID) if case-insensitive extended REGEX matches\n\
84  -l, --label=STRING\n\
85     Prefix label for output from plugin (default -s 'SNMP')\n\
86  -v, --verbose\n\
87     Debugging the output\n\
88  -m, --miblist=STRING\n\
89     List of MIBS to be loaded (default = ALL)\n"
91 #define NOTES "\
92 - This plugin uses the 'snmpget' command included with the NET-SNMP package.\n\
93   If you don't have the package installed, you will need to download it from\n\
94   http://net-snmp.sourceforge.net before you can use this plugin.\n\
95 - Multiple OIDs may be indicated by a comma- or space-delimited list (lists with\n\
96   internal spaces must be quoted) [max 8 OIDs]\n\
97 - Ranges are inclusive and are indicated with colons. When specified as\n\
98   'min:max' a STATE_OK will be returned if the result is within the indicated\n\
99   range or is equal to the upper or lower bound. A non-OK state will be\n\
100   returned if the result is outside the specified range.\n\
101 - If specified in the order 'max:min' a non-OK state will be returned if the\n\
102   result is within the (inclusive) range.\n\
103 - Upper or lower bounds may be omitted to skip checking the respective limit.\n\
104 - Bare integers are interpreted as upper limits.\n\
105 - When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'\n\
106 - Note that only one string and one regex may be checked at present\n\
107 - All evaluation methods other than PR, STR, and SUBSTR expect that the value\n\
108   returned from the SNMP query is an unsigned integer.\n"
110 #define DESCRIPTION "\
111 This plugin gets system information on a remote server via snmp.\n"
113 #define DEFAULT_COMMUNITY "public"
114 #define DEFAULT_PORT "161"
115 #define DEFAULT_TIMEOUT 10
116 #define DEFAULT_MIBLIST "ALL"
117 #define DEFAULT_PROTOCOL "1"
118 #define DEFAULT_AUTH_PROTOCOL "MD5"
120 #include "common.h"
121 #include "utils.h"
122 #include "popen.h"
124 #define mark(a) ((a)!=0?"*":"")
126 #define CHECK_UNDEF 0
127 #define CRIT_PRESENT 1
128 #define CRIT_STRING 2
129 #define CRIT_REGEX 4
130 #define CRIT_GT 8
131 #define CRIT_LT 16
132 #define CRIT_GE 32
133 #define CRIT_LE 64
134 #define CRIT_EQ 128
135 #define CRIT_NE 256
136 #define CRIT_RANGE 512
137 #define WARN_PRESENT 1024
138 #define WARN_STRING 2048
139 #define WARN_REGEX 4096
140 #define WARN_GT 8192
141 #define WARN_LT 16384
142 #define WARN_GE 32768
143 #define WARN_LE 65536
144 #define WARN_EQ 131072
145 #define WARN_NE 262144
146 #define WARN_RANGE 524288
148 #define MAX_OIDS 8
149 #define MAX_DELIM_LENGTH 8
150 #define DEFAULT_DELIMITER "="
151 #define DEFAULT_OUTPUT_DELIMITER " "
153 void print_usage (void);
154 void print_help (void);
155 int process_arguments (int, char **);
156 int validate_arguments (void);
157 int check_num (int);
158 char *clarify_message (char *);
159 int lu_getll (unsigned long *, char *);
160 int lu_getul (unsigned long *, char *);
161 char *thisarg (char *str);
162 char *nextarg (char *str);
164 #ifdef HAVE_REGEX_H
165 #include <regex.h>
166 char regex_expect[MAX_INPUT_BUFFER] = "";
167 regex_t preg;
168 regmatch_t pmatch[10];
169 char timestamp[10] = "";
170 char errbuf[MAX_INPUT_BUFFER];
171 int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
172 int eflags = 0;
173 int errcode, excode;
174 #endif
176 char *server_address = NULL;
177 char *community = DEFAULT_COMMUNITY;
178 char *authpriv = NULL;
179 char *proto = NULL;
180 char *seclevel = NULL;
181 char *secname = NULL;
182 char *authproto = NULL;
183 char *authpasswd = NULL;
184 char *privpasswd = NULL;
185 char *oid = "";
186 char *label = "SNMP";
187 char *units = "";
188 char *port = DEFAULT_PORT;
189 char string_value[MAX_INPUT_BUFFER] = "";
190 char **labels = NULL;
191 char **unitv = NULL;
192 int nlabels = 0;
193 int labels_size = 8;
194 int nunits = 0;
195 int unitv_size = 8;
196 int verbose = FALSE;
197 unsigned long lower_warn_lim[MAX_OIDS];
198 unsigned long upper_warn_lim[MAX_OIDS];
199 unsigned long lower_crit_lim[MAX_OIDS];
200 unsigned long upper_crit_lim[MAX_OIDS];
201 unsigned long response_value[MAX_OIDS];
202 int check_warning_value = FALSE;
203 int check_critical_value = FALSE;
204 int eval_method[MAX_OIDS];
205 char *delimiter = DEFAULT_DELIMITER;
206 char *output_delim = DEFAULT_OUTPUT_DELIMITER;
207 char *miblist = DEFAULT_MIBLIST;
210 int
211 main (int argc, char **argv)
213         int i = 0;
214         int iresult = STATE_UNKNOWN;
215         int found = 0;
216         int result = STATE_DEPENDENT;
217         char input_buffer[MAX_INPUT_BUFFER];
218         char *command_line = NULL;
219         char *response = NULL;
220         char *outbuff = "";
221         char *output = "";
222         char *ptr = NULL;
223         char *p2 = NULL;
224         char *show = NULL;
226         labels = malloc (labels_size);
227         unitv = malloc (unitv_size);
228         for (i = 0; i < MAX_OIDS; i++)
229                 eval_method[i] = CHECK_UNDEF;
230         i = 0;
232         if (process_arguments (argc, argv) == ERROR)
233                 usage ("Incorrect arguments supplied\n");
235         /* create the command line to execute */
236         asprintf (&command_line, "%s -t 1 -r %d -m %s -v %s %s %s:%s %s",
237                   PATH_TO_SNMPGET, timeout_interval - 1, miblist, proto,
238                   authpriv, server_address, port, oid);
239         if (verbose)
240                 printf ("%s\n", command_line);
242         /* run the command */
243         child_process = spopen (command_line);
244         if (child_process == NULL) {
245                 printf ("Could not open pipe: %s\n", command_line);
246                 exit (STATE_UNKNOWN);
247         }
249         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
250         if (child_stderr == NULL) {
251                 printf ("Could not open stderr for %s\n", command_line);
252         }
254         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
255                 asprintf (&output, "%s%s", output, input_buffer);
257         if (verbose)
258                 printf ("%s\n", output);
260         ptr = output;
262         while (ptr) {
264                 ptr = strstr (ptr, delimiter);
265                 if (ptr == NULL)
266                         break;
268                 ptr += strlen (delimiter);
269                 ptr += strspn (ptr, " ");
271                 found++;
273                 if (ptr[0] == '"') {
274                         ptr++;
275                         response = strpcpy (response, ptr, "\"");
276                         ptr = strpbrk (ptr, "\"");
277                         ptr += strspn (ptr, "\"\n");
278                 }
279                 else {
280                         response = strpcpy (response, ptr, "\n");
281                         ptr = strpbrk (ptr, "\n");
282                         ptr += strspn (ptr, "\n");
283                         while
284                                 (strstr (ptr, delimiter) &&
285                                  strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter)) {
286                                 response = strpcat (response, ptr, "\n");
287                                 ptr = strpbrk (ptr, "\n");
288                         }
289                         if (ptr && strstr (ptr, delimiter) == NULL) {
290                                 response = strscat (response, ptr);
291                                 ptr = NULL;
292                         }
293                 }
295                 /* We strip out the datatype indicator for PHBs */
296                 if (strstr (response, "Gauge: "))
297                         show = strstr (response, "Gauge: ") + 7;
298                 else if (strstr (response, "Gauge32: "))
299                         show = strstr (response, "Gauge32: ") + 9;
300                 else if (strstr (response, "Counter32: "))
301                         show = strstr (response, "Counter32: ") + 11;
302                 else if (strstr (response, "INTEGER: "))
303                         show = strstr (response, "INTEGER: ") + 9;
304                 else if (strstr (response, "STRING: "))
305                         show = strstr (response, "STRING: ") + 8;
306                 else
307                         show = response;
308                 p2 = show;
310                 iresult = STATE_DEPENDENT;
312                 /* Process this block for integer comparisons */
313                 if (eval_method[i] & CRIT_GT ||
314                     eval_method[i] & CRIT_LT ||
315                     eval_method[i] & CRIT_GE ||
316                     eval_method[i] & CRIT_LE ||
317                     eval_method[i] & CRIT_EQ ||
318                     eval_method[i] & CRIT_NE ||
319                     eval_method[i] & WARN_GT ||
320                     eval_method[i] & WARN_LT ||
321                     eval_method[i] & WARN_GE ||
322                     eval_method[i] & WARN_LE ||
323                     eval_method[i] & WARN_EQ ||
324                     eval_method[i] & WARN_NE) {
325                         p2 = strpbrk (p2, "0123456789");
326                         if (p2 == NULL) 
327                                 terminate (STATE_UNKNOWN,"No valid data returned");
328                         response_value[i] = strtoul (p2, NULL, 10);
329                         iresult = check_num (i);
330                         asprintf (&show, "%lu", response_value[i]);
331                 }
333                 /* Process this block for string matching */
334                 else if (eval_method[i] & CRIT_STRING) {
335                         if (strcmp (response, string_value))
336                                 iresult = STATE_CRITICAL;
337                         else
338                                 iresult = STATE_OK;
339                 }
341                 /* Process this block for regex matching */
342                 else if (eval_method[i] & CRIT_REGEX) {
343 #ifdef HAVE_REGEX_H
344                         excode = regexec (&preg, response, 10, pmatch, eflags);
345                         if (excode == 0) {
346                                 iresult = STATE_OK;
347                         }
348                         else if (excode != REG_NOMATCH) {
349                                 regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
350                                 printf ("Execute Error: %s\n", errbuf);
351                                 exit (STATE_CRITICAL);
352                         }
353                         else {
354                                 iresult = STATE_CRITICAL;
355                         }
356 #else
357                         printf ("%s UNKNOWN: call for regex which was not a compiled option", label);
358                         exit (STATE_UNKNOWN);
359 #endif
360                 }
362                 /* Process this block for existence-nonexistence checks */
363                 else {
364                         if (eval_method[i] & CRIT_PRESENT)
365                                 iresult = STATE_CRITICAL;
366                         else if (eval_method[i] & WARN_PRESENT)
367                                 iresult = STATE_WARNING;
368                         else if (response && iresult == STATE_DEPENDENT) 
369                                 iresult = STATE_OK;
370                 }
372                 /* Result is the worst outcome of all the OIDs tested */
373                 result = max_state (result, iresult);
375                 /* Prepend a label for this OID if there is one */
376                 if (nlabels > 1 && i < nlabels && labels[i] != NULL)
377                         asprintf (&outbuff, "%s%s%s %s%s%s", outbuff,
378                                   (i == 0) ? " " : output_delim,
379                                   labels[i], mark (iresult), show, mark (iresult));
380                 else
381                         asprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim,
382                                   mark (iresult), show, mark (iresult));
384                 /* Append a unit string for this OID if there is one */
385                 if (nunits > 0 && i < nunits && unitv[i] != NULL)
386                         asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
388                 i++;
390         }                                                                                                                       /* end while (ptr) */
392         if (found == 0)
393                 terminate
394                         (STATE_UNKNOWN,
395                          "%s problem - No data recieved from host\nCMD: %s\n",
396                          label, command_line);
398         /* WARNING if output found on stderr */
399         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
400                 result = max_state (result, STATE_WARNING);
402         /* close stderr */
403         (void) fclose (child_stderr);
405         /* close the pipe */
406         if (spclose (child_process))
407                 result = max_state (result, STATE_WARNING);
409 /*      if (nunits == 1 || i == 1) */
410 /*              printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units); */
411 /*      else */
412         printf ("%s %s -%s\n", label, state_text (result), outbuff);
414         return result;
417 /* process command-line arguments */
418 int
419 process_arguments (int argc, char **argv)
421         char *ptr;
422         int c = 1;
423         int j = 0, jj = 0, ii = 0;
425         int option_index = 0;
426         static struct option long_options[] = {
427                 STD_LONG_OPTS,
428                 {"community", required_argument, 0, 'C'},
429                 {"oid", required_argument, 0, 'o'},
430                 {"object", required_argument, 0, 'o'},
431                 {"delimiter", required_argument, 0, 'd'},
432                 {"output-delimiter", required_argument, 0, 'D'},
433                 {"string", required_argument, 0, 's'},
434                 {"regex", required_argument, 0, 'r'},
435                 {"ereg", required_argument, 0, 'r'},
436                 {"eregi", required_argument, 0, 'R'},
437                 {"label", required_argument, 0, 'l'},
438                 {"units", required_argument, 0, 'u'},
439                 {"port", required_argument, 0, 'p'},
440                 {"miblist", required_argument, 0, 'm'},
441                 {"protocol", required_argument, 0, 'P'},
442                 {"seclevel", required_argument, 0, 'L'},
443                 {"secname", required_argument, 0, 'U'},
444                 {"authproto", required_argument, 0, 'a'},
445                 {"authpasswd", required_argument, 0, 'A'},
446                 {"privpasswd", required_argument, 0, 'X'},
447                 {0, 0, 0, 0}
448         };
450         if (argc < 2)
451                 return ERROR;
453         /* reverse compatibility for very old non-POSIX usage forms */
454         for (c = 1; c < argc; c++) {
455                 if (strcmp ("-to", argv[c]) == 0)
456                         strcpy (argv[c], "-t");
457                 if (strcmp ("-wv", argv[c]) == 0)
458                         strcpy (argv[c], "-w");
459                 if (strcmp ("-cv", argv[c]) == 0)
460                         strcpy (argv[c], "-c");
461         }
463         while (1) {
464                 c = getopt_long (argc, argv, "hvVt:c:w:H:C:o:e:E:d:D:s:R:r:l:u:p:m:P:L:U:a:A:X:",
465                                                                          long_options, &option_index);
467                 if (c == -1 || c == EOF)
468                         break;
470                 switch (c) {
471                 case '?':       /* usage */
472                         usage3 ("Unknown argument", optopt);
473                 case 'h':       /* help */
474                         print_help ();
475                         exit (STATE_OK); 
476                 case 'V':       /* version */
477                         print_revision (progname, REVISION);
478                         exit (STATE_OK);
479                 case 'v': /* verbose */
480                         verbose = TRUE;
481                         break;
483         /* Connection info */
484                 case 'C':                                                                       /* group or community */
485                         community = strscpy (community, optarg);
486                         break;
487                 case 'H':                                                                       /* Host or server */
488                         server_address = strscpy (server_address, optarg);
489                         break;
490                 case 'p':       /* TCP port number */
491                         port = strscpy(port, optarg);
492                         break;
493                 case 'm':      /* List of MIBS  */
494                         miblist = strscpy(miblist, optarg);
495                         break;
496                 case 'P':     /* SNMP protocol version */
497                         proto = strscpy(proto, optarg);
498                         break;
499                 case 'L':     /* security level */
500                         seclevel = strscpy(seclevel,optarg);
501                         break;
502                 case 'U':     /* security username */
503                         secname = strscpy(secname, optarg);
504                         break;
505                 case 'a':     /* auth protocol */
506                         asprintf (&authproto, optarg);
507                         break;
508                 case 'A':     /* auth passwd */
509                         authpasswd = strscpy(authpasswd, optarg);
510                         break;
511                 case 'X':     /* priv passwd */
512                         privpasswd = strscpy(privpasswd, optarg);
513                         break;
514                 case 't':       /* timeout period */
515                         if (!is_integer (optarg))
516                                 usage2 ("Timeout Interval must be an integer", optarg);
517                         timeout_interval = atoi (optarg);
518                         break;
520         /* Test parameters */
521                 case 'c':                                                                       /* critical time threshold */
522                         if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
523                                 printf ("Invalid critical threshold: %s\n", optarg);
524                                 print_usage ();
525                                 exit (STATE_UNKNOWN);
526                         }
527                         for (ptr = optarg; ptr && jj < MAX_OIDS; jj++) {
528                                 if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
529                                         eval_method[jj] |= CRIT_LT;
530                                 if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
531                                         eval_method[jj] |= CRIT_GT;
532                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
533                         }
534                         break;
535                 case 'w':                                                                       /* warning time threshold */
536                         if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
537                                 printf ("Invalid warning threshold: %s\n", optarg);
538                                 print_usage ();
539                                 exit (STATE_UNKNOWN);
540                         }
541                         for (ptr = optarg; ptr && ii < MAX_OIDS; ii++) {
542                                 if (lu_getll (&lower_warn_lim[ii], ptr) == 1)
543                                         eval_method[ii] |= WARN_LT;
544                                 if (lu_getul (&upper_warn_lim[ii], ptr) == 1)
545                                         eval_method[ii] |= WARN_GT;
546                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
547                         }
548                         break;
549                 case 'o':                                                                       /* object identifier */
550                 case 'e': /* PRELIMINARY - may change */
551                 case 'E': /* PRELIMINARY - may change */
552                         for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
553                                 ptr[0] = ' '; /* relpace comma with space */
554                         for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
555                                 j++; /* count OIDs */
556                         asprintf (&oid, "%s %s", (oid?oid:""), optarg);
557                         if (c == 'E' || c == 'e') {
558                                 jj++;
559                                 ii++;
560                         }
561                         if (c == 'E') 
562                                 eval_method[j+1] |= WARN_PRESENT;
563                         else if (c == 'e')
564                                 eval_method[j+1] |= CRIT_PRESENT;
565                         break;
566                 case 's':                                                                       /* string or substring */
567                         strncpy (string_value, optarg, sizeof (string_value) - 1);
568                         string_value[sizeof (string_value) - 1] = 0;
569                         eval_method[jj++] = CRIT_STRING;
570                         ii++;
571                         break;
572                 case 'R':                                                                       /* regex */
573 #ifdef HAVE_REGEX_H
574                         cflags = REG_ICASE;
575 #endif
576                 case 'r':                                                                       /* regex */
577 #ifdef HAVE_REGEX_H
578                         cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
579                         strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
580                         regex_expect[sizeof (regex_expect) - 1] = 0;
581                         errcode = regcomp (&preg, regex_expect, cflags);
582                         if (errcode != 0) {
583                                 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
584                                 printf ("Could Not Compile Regular Expression");
585                                 return ERROR;
586                         }
587                         eval_method[jj++] = CRIT_REGEX;
588                         ii++;
589 #else
590                         printf ("%s UNKNOWN: call for regex which was not a compiled option", label);
591                         exit (STATE_UNKNOWN);
592 #endif
593                         break;
595         /* Format */
596                 case 'd':                                                                       /* delimiter */
597                         delimiter = strscpy (delimiter, optarg);
598                         break;
599                 case 'D':                                                                       /* output-delimiter */
600                         output_delim = strscpy (output_delim, optarg);
601                         break;
602                 case 'l':                                                                       /* label */
603                         label = optarg;
604                         nlabels++;
605                         if (nlabels >= labels_size) {
606                                 labels_size += 8;
607                                 labels = realloc (labels, labels_size);
608                                 if (labels == NULL)
609                                         terminate (STATE_UNKNOWN,
610                                                                                  "Could not realloc() labels[%d]", nlabels);
611                         }
612                         labels[nlabels - 1] = optarg;
613                         ptr = thisarg (optarg);
614                         if (strstr (ptr, "'") == ptr)
615                                 labels[nlabels - 1] = ptr + 1;
616                         else
617                                 labels[nlabels - 1] = ptr;
618                         while (ptr && (ptr = nextarg (ptr))) {
619                                 if (nlabels >= labels_size) {
620                                         labels_size += 8;
621                                         labels = realloc (labels, labels_size);
622                                         if (labels == NULL)
623                                                 terminate (STATE_UNKNOWN, "Could not realloc() labels\n");
624                                 }
625                                 labels++;
626                                 ptr = thisarg (ptr);
627                                 if (strstr (ptr, "'") == ptr)
628                                         labels[nlabels - 1] = ptr + 1;
629                                 else
630                                         labels[nlabels - 1] = ptr;
631                         }
632                         break;
633                 case 'u':                                                                       /* units */
634                         units = optarg;
635                         nunits++;
636                         if (nunits >= unitv_size) {
637                                 unitv_size += 8;
638                                 unitv = realloc (unitv, unitv_size);
639                                 if (unitv == NULL)
640                                         terminate (STATE_UNKNOWN,
641                                                                                  "Could not realloc() units [%d]\n", nunits);
642                         }
643                         unitv[nunits - 1] = optarg;
644                         ptr = thisarg (optarg);
645                         if (strstr (ptr, "'") == ptr)
646                                 unitv[nunits - 1] = ptr + 1;
647                         else
648                                 unitv[nunits - 1] = ptr;
649                         while (ptr && (ptr = nextarg (ptr))) {
650                                 if (nunits >= unitv_size) {
651                                         unitv_size += 8;
652                                         unitv = realloc (unitv, unitv_size);
653                                         if (units == NULL)
654                                                 terminate (STATE_UNKNOWN, "Could not realloc() units\n");
655                                 }
656                                 nunits++;
657                                 ptr = thisarg (ptr);
658                                 if (strstr (ptr, "'") == ptr)
659                                         unitv[nunits - 1] = ptr + 1;
660                                 else
661                                         unitv[nunits - 1] = ptr;
662                         }
663                         break;
665                 }
666         }
668         if (server_address == NULL)
669                 asprintf (&server_address, argv[optind]);
671         return validate_arguments ();
674 /******************************************************************************
676 @@-
677 <sect3>
678 <title>validate_arguments</title>
680 <para>&PROTO_validate_arguments;</para>
682 <para>Given a database name, this function returns TRUE if the string
683 is a valid PostgreSQL database name, and returns false if it is
684 not.</para>
686 <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
687 characters long and consist of letters, numbers, and underscores. The
688 first character cannot be a number, however.</para>
690 </sect3>
691 -@@
692 ******************************************************************************/
694 int
695 validate_arguments ()
698         /* Need better checks to verify seclevel and authproto choices */
699         
700         if (seclevel == NULL) 
701                 asprintf (&seclevel, "noAuthNoPriv");
704         if (authproto == NULL ) 
705                 asprintf(&authproto, DEFAULT_AUTH_PROTOCOL);
706         
707          
708         
709         if (proto == NULL || (strcmp(proto,DEFAULT_PROTOCOL) == 0) ) {        /* default protocol version */
710                 asprintf(&proto, DEFAULT_PROTOCOL);
711                 asprintf(&authpriv, "%s%s", "-c ", community);
712         }
713         else if ( strcmp (proto, "3") == 0 ) {                 /* snmpv3 args */
714                 asprintf(&proto, "%s", "3");
715                 
716                 if ( (strcmp(seclevel, "noAuthNoPriv") == 0) || seclevel == NULL ) {
717                         asprintf(&authpriv, "%s", "-l noAuthNoPriv" );
718                 }
719                 else if ( strcmp(seclevel, "authNoPriv") == 0 ) {
720                         if ( secname == NULL || authpasswd == NULL) {
721                                 printf ("Missing secname (%s) or authpassword (%s) ! \n",secname, authpasswd );
722                                 print_usage ();
723                                 exit (STATE_UNKNOWN);
724                         }
725                         asprintf(&authpriv, "-l authNoPriv -a %s -u %s -A %s ", authproto, secname, authpasswd);
726                 }
727                 else if ( strcmp(seclevel, "authPriv") == 0 ) {
728                         if ( secname == NULL || authpasswd == NULL || privpasswd == NULL ) {
729                                 printf ("Missing secname (%s), authpassword (%s), or privpasswd (%s)! \n",secname, authpasswd,privpasswd );
730                                 print_usage ();
731                                 exit (STATE_UNKNOWN);
732                         }
733                         asprintf(&authpriv, "-l authPriv -a %s -u %s -A %s -x DES -X %s ", authproto, secname, authpasswd, privpasswd);
734                 }
735                 
736                                                                         
737         }
738         else {
739                 printf ("Invalid SNMP version: %s\n", proto);
740                 print_usage ();
741                 exit (STATE_UNKNOWN);                           
742         }
743                         
744         
745         
747         return OK;
752 void
753 print_help (void)
755         print_revision (progname, REVISION);
756         printf
757                 ("Copyright (c) %s %s <%s>\n\n%s\n", COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
758         print_usage ();
759         printf
760                 ("\nOptions:\n" LONGOPTIONS "\n" DESCRIPTION "\n" NOTES "\n", 
761                  DEFAULT_COMMUNITY, DEFAULT_PORT, DEFAULT_DELIMITER, DEFAULT_TIMEOUT);
762         support ();
765 void
766 print_usage (void)
768         printf
769                 ("Usage:\n" " %s %s\n"
770                  " %s (-h | --help) for detailed help\n"
771                  " %s (-V | --version) for version information\n",
772                  progname, OPTIONS, progname, progname);
774 \f
777 char *
778 clarify_message (char *msg)
780         int i = 0;
781         int foo;
782         char tmpmsg_c[MAX_INPUT_BUFFER];
783         char *tmpmsg = (char *) &tmpmsg_c;
784         tmpmsg = strcpy (tmpmsg, msg);
785         if (!strncmp (tmpmsg, " Hex:", 5)) {
786                 tmpmsg = strtok (tmpmsg, ":");
787                 while ((tmpmsg = strtok (NULL, " "))) {
788                         foo = strtol (tmpmsg, NULL, 16);
789                         /* Translate chars that are not the same value in the printers
790                          * character set.
791                          */
792                         switch (foo) {
793                         case 208:
794                                 {
795                                         foo = 197;
796                                         break;
797                                 }
798                         case 216:
799                                 {
800                                         foo = 196;
801                                         break;
802                                 }
803                         }
804                         msg[i] = foo;
805                         i++;
806                 }
807                 msg[i] = 0;
808         }
809         return (msg);
813 int
814 check_num (int i)
816         int result;
817         result = STATE_OK;
818         if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
819                         lower_warn_lim[i] > upper_warn_lim[i]) {
820                 if (response_value[i] <= lower_warn_lim[i] &&
821                                 response_value[i] >= upper_warn_lim[i]) {
822                         result = STATE_WARNING;
823                 }
824         }
825         else if
826                 ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
827                  (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
828                  (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
829                  (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
830                  (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
831                  (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
832                 result = STATE_WARNING;
833         }
835         if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
836                         lower_warn_lim[i] > upper_warn_lim[i]) {
837                 if (response_value[i] <= lower_crit_lim[i] &&
838                                 response_value[i] >= upper_crit_lim[i]) {
839                         result = STATE_CRITICAL;
840                 }
841         }
842         else if
843                 ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
844                  (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
845                  (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
846                  (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
847                  (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
848                  (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
849                 result = STATE_CRITICAL;
850         }
852         return result;
856 int
857 lu_getll (unsigned long *ll, char *str)
859         char tmp[100];
860         if (strchr (str, ':') == NULL)
861                 return 0;
862         if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
863                 return 0;
864         if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
865                 return 1;
866         return 0;
869 int
870 lu_getul (unsigned long *ul, char *str)
872         char tmp[100];
873         if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
874                 return 1;
875         if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
876                 return 1;
877         if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
878                 return 1;
879         return 0;
887 /* trim leading whitespace
888          if there is a leading quote, make sure it balances */
890 char *
891 thisarg (char *str)
893         str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
894         if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
895                 if (strlen (str) == 1 || !strstr (str + 1, "'"))
896                         terminate (STATE_UNKNOWN, "Unbalanced quotes\n");
897         }
898         return str;
902 /* if there's a leading quote, advance to the trailing quote
903          set the trailing quote to '\x0'
904          if the string continues, advance beyond the comma */
906 char *
907 nextarg (char *str)
909         if (strstr (str, "'") == str) {
910                 if (strlen (str) > 1) {
911                         str = strstr (str + 1, "'");
912                         str[0] = 0;
913                         return (++str);
914                 }
915                 else {
916                         str[0] = 0;
917                         return NULL;
918                 }
919         }
920         if (strstr (str, ",") == str) {
921                 if (strlen (str) > 1) {
922                         str[0] = 0;
923                         return (++str);
924                 }
925                 else {
926                         str[0] = 0;
927                         return NULL;
928                 }
929         }
930         if ((str = strstr (str, ",")) && strlen (str) > 1) {
931                 str[0] = 0;
932                 return (++str);
933         }
934         return NULL;