Code

more snmpv3 patches
[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] [-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 regex[MAX_INPUT_BUFFER];
171 char errbuf[MAX_INPUT_BUFFER];
172 int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
173 int eflags = 0;
174 int errcode, excode;
175 #endif
177 char *server_address = NULL;
178 char *community = NULL;
179 char *authpriv = NULL;
180 char *proto = NULL;
181 char *seclevel = NULL;
182 char *secname = NULL;
183 char *authproto = NULL;
184 char *authpasswd = NULL;
185 char *privpasswd = NULL;
186 char *oid = "";
187 char *label = NULL;
188 char *units = NULL;
189 char *port = DEFAULT_PORT;
190 char string_value[MAX_INPUT_BUFFER] = "";
191 char **labels = NULL;
192 char **unitv = NULL;
193 int nlabels = 0;
194 int labels_size = 8;
195 int nunits = 0;
196 int unitv_size = 8;
197 int verbose = FALSE;
198 unsigned long lower_warn_lim[MAX_OIDS];
199 unsigned long upper_warn_lim[MAX_OIDS];
200 unsigned long lower_crit_lim[MAX_OIDS];
201 unsigned long upper_crit_lim[MAX_OIDS];
202 unsigned long response_value[MAX_OIDS];
203 int check_warning_value = FALSE;
204 int check_critical_value = FALSE;
205 int eval_method[MAX_OIDS];
206 char *delimiter = NULL;
207 char *output_delim = NULL;
208 char *miblist = NULL;
211 int
212 main (int argc, char **argv)
214         int i = 0;
215         int iresult = STATE_UNKNOWN;
216         int found = 0;
217         int result = STATE_DEPENDENT;
218         char input_buffer[MAX_INPUT_BUFFER];
219         char *command_line = NULL;
220         char *response = NULL;
221         char *outbuff = "";
222         char *output = NULL;
223         char *ptr = NULL;
224         char *p2 = NULL;
225         char *show = NULL;
227         labels = malloc (labels_size);
228         unitv = malloc (unitv_size);
229         for (i = 0; i < MAX_OIDS; i++)
230                 eval_method[i] = CHECK_UNDEF;
231         i = 0;
233         if (process_arguments (argc, argv) == ERROR)
234                 usage ("Incorrect arguments supplied\n");
236         /* create the command line to execute */
237         asprintf (&command_line, "%s -m %s -v %s %s %s:%s %s",
238                   PATH_TO_SNMPGET, miblist, proto, 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         asprintf (&output, "");
255         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
256                 asprintf (&output, "%s%s", output, input_buffer);
258         if (verbose)
259                 printf ("%s\n", output);
261         ptr = output;
263         while (ptr) {
265                 ptr = strstr (ptr, delimiter);
266                 if (ptr == NULL)
267                         break;
269                 ptr += strlen (delimiter);
270                 ptr += strspn (ptr, " ");
272                 found++;
274                 if (ptr[0] == '"') {
275                         ptr++;
276                         response = strpcpy (response, ptr, "\"");
277                         ptr = strpbrk (ptr, "\"");
278                         ptr += strspn (ptr, "\"\n");
279                 }
280                 else {
281                         response = strpcpy (response, ptr, "\n");
282                         ptr = strpbrk (ptr, "\n");
283                         ptr += strspn (ptr, "\n");
284                         while
285                                 (strstr (ptr, delimiter) &&
286                                  strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter)) {
287                                 response = strpcat (response, ptr, "\n");
288                                 ptr = strpbrk (ptr, "\n");
289                         }
290                         if (ptr && strstr (ptr, delimiter) == NULL) {
291                                 response = strscat (response, ptr);
292                                 ptr = NULL;
293                         }
294                 }
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
301                         show = response;
302                 p2 = show;
304                 iresult = STATE_DEPENDENT;
306                 if (eval_method[i] & CRIT_PRESENT) {
307                         iresult = STATE_CRITICAL;
308                 } else if (eval_method[i] & WARN_PRESENT) {
309                         iresult = STATE_WARNING;
310                 }
312                 if (eval_method[i] & CRIT_GT ||
313                                 eval_method[i] & CRIT_LT ||
314                                 eval_method[i] & CRIT_GE ||
315                                 eval_method[i] & CRIT_LE ||
316                                 eval_method[i] & CRIT_EQ ||
317                                 eval_method[i] & CRIT_NE ||
318                                 eval_method[i] & WARN_GT ||
319                                 eval_method[i] & WARN_LT ||
320                                 eval_method[i] & WARN_GE ||
321                                 eval_method[i] & WARN_LE ||
322                                 eval_method[i] & WARN_EQ || eval_method[i] & WARN_NE) {
323                         p2 = strpbrk (p2, "0123456789");
324                         response_value[i] = strtoul (p2, NULL, 10);
325                         iresult = check_num (i);
326                         asprintf (&show, "%lu", response_value[i]);
327                         /*asprintf (&show, "%s", response); */
328                 }
330                 else if (eval_method[i] & CRIT_STRING) {
331                         if (strcmp (response, string_value))
332                                 iresult = STATE_CRITICAL;
333                         else
334                                 iresult = STATE_OK;
335                 }
337                 else if (eval_method[i] & CRIT_REGEX) {
338 #ifdef HAVE_REGEX_H
339                         excode = regexec (&preg, response, 10, pmatch, eflags);
340                         if (excode == 0) {
341                                 iresult = STATE_OK;
342                         }
343                         else if (excode != REG_NOMATCH) {
344                                 regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
345                                 printf ("Execute Error: %s\n", errbuf);
346                                 exit (STATE_CRITICAL);
347                         }
348                         else {
349                                 iresult = STATE_CRITICAL;
350                         }
351 #else
352                         printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
353                         exit (STATE_UNKNOWN);
354 #endif
355                 }
357                 if (response && iresult == STATE_DEPENDENT)
358                         iresult = STATE_OK;
359                 else if (eval_method[i] & CRIT_PRESENT)
360                         iresult = STATE_CRITICAL;
361                 else
362                         iresult = STATE_WARNING;
364                 result = max_state (result, iresult);
366                 if (nlabels > 1 && i < nlabels && labels[i] != NULL)
367                         asprintf (&outbuff, "%s%s%s %s%s%s", outbuff,
368                                   (i == 0) ? " " : output_delim,
369                                   labels[i], mark (iresult), show, mark (iresult));
370                 else
371                         asprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim,
372                                   mark (iresult), show, mark (iresult));
374                 if (nunits > 0 && i < nunits)
375                         asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
377                 i++;
379         }                                                                                                                       /* end while */
381         if (found == 0)
382                 terminate
383                         (STATE_UNKNOWN,
384                          "%s problem - No data recieved from host\nCMD: %s\n",
385                          label, command_line);
387         /* WARNING if output found on stderr */
388         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
389                 result = max_state (result, STATE_WARNING);
391         /* close stderr */
392         (void) fclose (child_stderr);
394         /* close the pipe */
395         if (spclose (child_process))
396                 result = max_state (result, STATE_WARNING);
398         if (nunits > 0)
399                 printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units);
400         else
401                 printf ("%s %s -%s\n", label, state_text (result), outbuff);
403         return result;
406 /* process command-line arguments */
407 int
408 process_arguments (int argc, char **argv)
410         char *ptr;
411         int c = 1;
412         int j = 0, jj = 0;
414 #ifdef HAVE_GETOPT_H
415         int option_index = 0;
416         static struct option long_options[] = {
417                 STD_LONG_OPTS,
418                 {"community", required_argument, 0, 'C'},
419                 {"oid", required_argument, 0, 'o'},
420                 {"object", required_argument, 0, 'o'},
421                 {"delimiter", required_argument, 0, 'd'},
422                 {"output-delimiter", required_argument, 0, 'D'},
423                 {"string", required_argument, 0, 's'},
424                 {"regex", required_argument, 0, 'r'},
425                 {"ereg", required_argument, 0, 'r'},
426                 {"eregi", required_argument, 0, 'R'},
427                 {"label", required_argument, 0, 'l'},
428                 {"units", required_argument, 0, 'u'},
429                 {"port", required_argument, 0, 'p'},
430                 {"miblist", required_argument, 0, 'm'},
431                 {"protocol", required_argument, 0, 'P'},
432                 {"seclevel", required_argument, 0, 'L'},
433                 {"secname", required_argument, 0, 'U'},
434                 {"authproto", required_argument, 0, 'a'},
435                 {"authpasswd", required_argument, 0, 'A'},
436                 {"privpasswd", required_argument, 0, 'X'},
437                 {0, 0, 0, 0}
438         };
439 #endif
441         if (argc < 2)
442                 return ERROR;
444         /* reverse compatibility for very old non-POSIX usage forms */
445         for (c = 1; c < argc; c++) {
446                 if (strcmp ("-to", argv[c]) == 0)
447                         strcpy (argv[c], "-t");
448                 if (strcmp ("-wv", argv[c]) == 0)
449                         strcpy (argv[c], "-w");
450                 if (strcmp ("-cv", argv[c]) == 0)
451                         strcpy (argv[c], "-c");
452         }
454         while (1) {
455 #ifdef HAVE_GETOPT_H
456                 c =
457                         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:",
458                                                                          long_options, &option_index);
459 #else
460                 c = getopt (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:");
461 #endif
463                 if (c == -1 || c == EOF)
464                         break;
466                 switch (c) {
467                 case '?':       /* usage */
468                         usage3 ("Unknown argument", optopt);
469                 case 'h':       /* help */
470                         print_help ();
471                         exit (STATE_OK); 
472                 case 'V':       /* version */
473                         print_revision (PROGNAME, REVISION);
474                         exit (STATE_OK);
475                 case 'v': /* verbose */
476                         verbose = TRUE;
477                         break;
478                 case 't':       /* timeout period */
479                         if (!is_integer (optarg))
480                                 usage2 ("Timeout Interval must be an integer", optarg);
481                         timeout_interval = atoi (optarg);
482                         break;
483                 case 'e': /* PRELIMINARY - may change */
484                         eval_method[j] |= WARN_PRESENT;
485                         for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
486                                 ptr[0] = ' '; /* relpace comma with space */
487                         for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
488                                 eval_method[++j] |= WARN_PRESENT;
489                         asprintf (&oid, "%s %s", (oid?oid:""), optarg);
490                         break;
491                 case 'E': /* PRELIMINARY - may change */
492                         eval_method[j] |= WARN_PRESENT;
493                         for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
494                                 ptr[0] = ' '; /* relpace comma with space */
495                         for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
496                                 eval_method[++j] |= CRIT_PRESENT;
497                         asprintf (&oid, "%s %s", (oid?oid:""), optarg);
498                         break;
499                 case 'c':                                                                       /* critical time threshold */
500                         if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
501                                 printf ("Invalid critical threshold: %s\n", optarg);
502                                 print_usage ();
503                                 exit (STATE_UNKNOWN);
504                         }
505                         for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
506                                 if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
507                                         eval_method[jj] |= CRIT_LT;
508                                 if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
509                                         eval_method[jj] |= CRIT_GT;
510                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
511                         }
512                         break;
513                 case 'w':                                                                       /* warning time threshold */
514                         if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
515                                 printf ("Invalid warning threshold: %s\n", optarg);
516                                 print_usage ();
517                                 exit (STATE_UNKNOWN);
518                         }
519                         for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
520                                 if (lu_getll (&lower_warn_lim[jj], ptr) == 1)
521                                         eval_method[jj] |= WARN_LT;
522                                 if (lu_getul (&upper_warn_lim[jj], ptr) == 1)
523                                         eval_method[jj] |= WARN_GT;
524                                 (ptr = index (ptr, ',')) ? ptr++ : ptr;
525                         }
526                         break;
527                 case 'H':                                                                       /* Host or server */
528                         server_address = strscpy (server_address, optarg);
529                         break;
530                 case 'C':                                                                       /* group or community */
531                         community = strscpy (community, optarg);
532                         break;
533                 case 'o':                                                                       /* object identifier */
534                         for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
535                                 ptr[0] = ' '; /* relpace comma with space */
536                         for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
537                                 j++; /* count OIDs */
538                         asprintf (&oid, "%s %s", (oid?oid:""), optarg);
539                         break;
540                 case 'd':                                                                       /* delimiter */
541                         delimiter = strscpy (delimiter, optarg);
542                         break;
543                 case 'D':                                                                       /* output-delimiter */
544                         output_delim = strscpy (output_delim, optarg);
545                         break;
546                 case 's':                                                                       /* string or substring */
547                         strncpy (string_value, optarg, sizeof (string_value) - 1);
548                         string_value[sizeof (string_value) - 1] = 0;
549                         eval_method[jj++] = CRIT_STRING;
550                         break;
551                 case 'R':                                                                       /* regex */
552 #ifdef HAVE_REGEX_H
553                         cflags = REG_ICASE;
554 #endif
555                 case 'r':                                                                       /* regex */
556 #ifdef HAVE_REGEX_H
557                         cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
558                         strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
559                         regex_expect[sizeof (regex_expect) - 1] = 0;
560                         errcode = regcomp (&preg, regex_expect, cflags);
561                         if (errcode != 0) {
562                                 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
563                                 printf ("Could Not Compile Regular Expression");
564                                 return ERROR;
565                         }
566                         eval_method[jj++] = CRIT_REGEX;
567 #else
568                         printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
569                         exit (STATE_UNKNOWN);
570 #endif
571                         break;
572                 case 'l':                                                                       /* label */
573                         label = optarg;
574                         nlabels++;
575                         if (nlabels >= labels_size) {
576                                 labels_size += 8;
577                                 labels = realloc (labels, labels_size);
578                                 if (labels == NULL)
579                                         terminate (STATE_UNKNOWN,
580                                                                                  "Could not realloc() labels[%d]", nlabels);
581                         }
582                         labels[nlabels - 1] = optarg;
583                         ptr = thisarg (optarg);
584                         if (strstr (ptr, "'") == ptr)
585                                 labels[nlabels - 1] = ptr + 1;
586                         else
587                                 labels[nlabels - 1] = ptr;
588                         while (ptr && (ptr = nextarg (ptr))) {
589                                 if (nlabels >= labels_size) {
590                                         labels_size += 8;
591                                         labels = realloc (labels, labels_size);
592                                         if (labels == NULL)
593                                                 terminate (STATE_UNKNOWN, "Could not realloc() labels\n");
594                                 }
595                                 labels++;
596                                 ptr = thisarg (ptr);
597                                 if (strstr (ptr, "'") == ptr)
598                                         labels[nlabels - 1] = ptr + 1;
599                                 else
600                                         labels[nlabels - 1] = ptr;
601                         }
602                         break;
603                 case 'u':                                                                       /* units */
604                         units = optarg;
605                         nunits++;
606                         if (nunits >= unitv_size) {
607                                 unitv_size += 8;
608                                 unitv = realloc (unitv, unitv_size);
609                                 if (unitv == NULL)
610                                         terminate (STATE_UNKNOWN,
611                                                                                  "Could not realloc() units [%d]\n", nunits);
612                         }
613                         unitv[nunits - 1] = optarg;
614                         ptr = thisarg (optarg);
615                         if (strstr (ptr, "'") == ptr)
616                                 unitv[nunits - 1] = ptr + 1;
617                         else
618                                 unitv[nunits - 1] = ptr;
619                         while (ptr && (ptr = nextarg (ptr))) {
620                                 if (nunits >= unitv_size) {
621                                         unitv_size += 8;
622                                         unitv = realloc (unitv, unitv_size);
623                                         if (units == NULL)
624                                                 terminate (STATE_UNKNOWN, "Could not realloc() units\n");
625                                 }
626                                 nunits++;
627                                 ptr = thisarg (ptr);
628                                 if (strstr (ptr, "'") == ptr)
629                                         unitv[nunits - 1] = ptr + 1;
630                                 else
631                                         unitv[nunits - 1] = ptr;
632                         }
633                         break;
634                 case 'p':       /* TCP port number */
635                         port = strscpy(port, optarg);
636                         break;
637                 case 'm':      /* List of MIBS  */
638                         miblist = strscpy(miblist, optarg);
639                         break;
640                 case 'P':     /* SNMP protocol version */
641                         proto = strscpy(proto, optarg);
642                         break;
643                 case 'L':     /* security level */
644                         seclevel = strscpy(seclevel,optarg);
645                         break;
646                 case 'U':     /* security username */
647                         secname = strscpy(secname, optarg);
648                         break;
649                 case 'a':     /* auth protocol */
650                         asprintf (&authproto, optarg);
651                         break;
652                 case 'A':     /* auth passwd */
653                         authpasswd = strscpy(authpasswd, optarg);
654                         break;
655                 case 'X':     /* priv passwd */
656                         privpasswd = strscpy(privpasswd, optarg);
657                         break;
659                 }
660         }
662         if (server_address == NULL)
663                 asprintf (&server_address, argv[optind]);
665         return validate_arguments ();
668 /******************************************************************************
670 @@-
671 <sect3>
672 <title>validate_arguments</title>
674 <para>&PROTO_validate_arguments;</para>
676 <para>Given a database name, this function returns TRUE if the string
677 is a valid PostgreSQL database name, and returns false if it is
678 not.</para>
680 <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
681 characters long and consist of letters, numbers, and underscores. The
682 first character cannot be a number, however.</para>
684 </sect3>
685 -@@
686 ******************************************************************************/
688 int
689 validate_arguments ()
692         if (community == NULL)
693                 asprintf (&community, DEFAULT_COMMUNITY);
695         if (delimiter == NULL)
696                 asprintf (&delimiter, DEFAULT_DELIMITER);
698         if (output_delim == NULL)
699                 asprintf (&output_delim, DEFAULT_OUTPUT_DELIMITER);
701         if (miblist == NULL)
702                 asprintf (&miblist, DEFAULT_MIBLIST);
704         if (label == NULL)
705                 asprintf (&label, "SNMP");
707         if (units == NULL)
708                 asprintf (&units, "");
710         /* Need better checks to verify seclevel and authproto choices */
711         
712         if (seclevel == NULL) 
713                 asprintf (&seclevel, "noAuthNoPriv");
716         if (authproto == NULL ) 
717                 asprintf(&authproto, DEFAULT_AUTH_PROTOCOL);
718         
719          
720         
721         if (proto == NULL || (strcmp(proto,DEFAULT_PROTOCOL) == 0) ) {        /* default protocol version */
722                 asprintf(&proto, DEFAULT_PROTOCOL);
723                 asprintf(&authpriv, "%s%s", "-c ", community);
724         }
725         else if ( strcmp (proto, "3") == 0 ) {                 /* snmpv3 args */
726                 asprintf(&proto, "%s", "3");
727                 
728                 if ( (strcmp(seclevel, "noAuthNoPriv") == 0) || seclevel == NULL ) {
729                         authpriv = ssprintf(authpriv, "%s", "-l noAuthNoPriv" );
730                 }
731                 else if ( strcmp(seclevel, "authNoPriv") == 0 ) {
732                         if ( secname == NULL || authpasswd == NULL) {
733                                 printf ("Missing secname (%s) or authpassword (%s) ! \n",secname, authpasswd );
734                                 print_usage ();
735                                 exit (STATE_UNKNOWN);
736                         }
737                         authpriv = ssprintf(authpriv, "-l authNoPriv -a %s -u %s -A %s ", authproto, secname, authpasswd);
738                 }
739                 else if ( strcmp(seclevel, "authPriv") == 0 ) {
740                         if ( secname == NULL || authpasswd == NULL || privpasswd == NULL ) {
741                                 printf ("Missing secname (%s), authpassword (%s), or privpasswd (%s)! \n",secname, authpasswd,privpasswd );
742                                 print_usage ();
743                                 exit (STATE_UNKNOWN);
744                         }
745                         authpriv = ssprintf(authpriv, "-l authPriv -a %s -u %s -A %s -x DES -X %s ", authproto, secname, authpasswd, privpasswd);
746                 }
747                 
748                                                                         
749         }
750         else {
751                 printf ("Invalid SNMP version: %s\n", proto);
752                 print_usage ();
753                 exit (STATE_UNKNOWN);                           
754         }
755                         
756         
757         
759         return OK;
764 void
765 print_help (void)
767         print_revision (PROGNAME, REVISION);
768         printf
769                 ("Copyright (c) %s %s <%s>\n\n%s\n",
770                  COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
771         print_usage ();
772         printf
773                 ("\nOptions:\n" LONGOPTIONS "\n" DESCRIPTION "\n" NOTES "\n", 
774                  DEFAULT_COMMUNITY, DEFAULT_PORT, DEFAULT_DELIMITER, DEFAULT_TIMEOUT);
775         support ();
778 void
779 print_usage (void)
781         printf
782                 ("Usage:\n" " %s %s\n"
783                  " %s (-h | --help) for detailed help\n"
784                  " %s (-V | --version) for version information\n",
785                  PROGNAME, OPTIONS, PROGNAME, PROGNAME);
787 \f
790 char *
791 clarify_message (char *msg)
793         int i = 0;
794         int foo;
795         char tmpmsg_c[MAX_INPUT_BUFFER];
796         char *tmpmsg = (char *) &tmpmsg_c;
797         tmpmsg = strcpy (tmpmsg, msg);
798         if (!strncmp (tmpmsg, " Hex:", 5)) {
799                 tmpmsg = strtok (tmpmsg, ":");
800                 while ((tmpmsg = strtok (NULL, " "))) {
801                         foo = strtol (tmpmsg, NULL, 16);
802                         /* Translate chars that are not the same value in the printers
803                          * character set.
804                          */
805                         switch (foo) {
806                         case 208:
807                                 {
808                                         foo = 197;
809                                         break;
810                                 }
811                         case 216:
812                                 {
813                                         foo = 196;
814                                         break;
815                                 }
816                         }
817                         msg[i] = foo;
818                         i++;
819                 }
820                 msg[i] = 0;
821         }
822         return (msg);
826 int
827 check_num (int i)
829         int result;
830         result = STATE_OK;
831         if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
832                         lower_warn_lim[i] > upper_warn_lim[i]) {
833                 if (response_value[i] <= lower_warn_lim[i] &&
834                                 response_value[i] >= upper_warn_lim[i]) {
835                         result = STATE_WARNING;
836                 }
837         }
838         else if
839                 ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
840                  (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
841                  (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
842                  (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
843                  (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
844                  (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
845                 result = STATE_WARNING;
846         }
848         if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
849                         lower_warn_lim[i] > upper_warn_lim[i]) {
850                 if (response_value[i] <= lower_crit_lim[i] &&
851                                 response_value[i] >= upper_crit_lim[i]) {
852                         result = STATE_CRITICAL;
853                 }
854         }
855         else if
856                 ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
857                  (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
858                  (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
859                  (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
860                  (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
861                  (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
862                 result = STATE_CRITICAL;
863         }
865         return result;
869 int
870 lu_getll (unsigned long *ll, char *str)
872         char tmp[100];
873         if (strchr (str, ':') == NULL)
874                 return 0;
875         if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
876                 return 0;
877         if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
878                 return 1;
879         return 0;
882 int
883 lu_getul (unsigned long *ul, char *str)
885         char tmp[100];
886         if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
887                 return 1;
888         if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
889                 return 1;
890         if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
891                 return 1;
892         return 0;
900 /* trim leading whitespace
901          if there is a leading quote, make sure it balances */
903 char *
904 thisarg (char *str)
906         str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
907         if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
908                 if (strlen (str) == 1 || !strstr (str + 1, "'"))
909                         terminate (STATE_UNKNOWN, "Unbalanced quotes\n");
910         }
911         return str;
915 /* if there's a leading quote, advance to the trailing quote
916          set the trailing quote to '\x0'
917          if the string continues, advance beyond the comma */
919 char *
920 nextarg (char *str)
922         if (strstr (str, "'") == str) {
923                 if (strlen (str) > 1) {
924                         str = strstr (str + 1, "'");
925                         str[0] = 0;
926                         return (++str);
927                 }
928                 else {
929                         str[0] = 0;
930                         return NULL;
931                 }
932         }
933         if (strstr (str, ",") == str) {
934                 if (strlen (str) > 1) {
935                         str[0] = 0;
936                         return (++str);
937                 }
938                 else {
939                         str[0] = 0;
940                         return NULL;
941                 }
942         }
943         if ((str = strstr (str, ",")) && strlen (str) > 1) {
944                 str[0] = 0;
945                 return (++str);
946         }
947         return NULL;