Code

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