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