Code

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