1 /******************************************************************************
2 *
3 * Program: SNMP plugin for Nagios
4 * License: GPL
5 *
6 * License Information:
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 *./plugins/check_snmp 127.0.0.1 -c public -o .1.3.6.1.4.1.2021.9.1.2.1
23 *
24 *****************************************************************************/
26 #define PROGNAME "check_snmp"
27 #define REVISION "$Revision$"
28 #define COPYRIGHT "1999-2002"
29 #define AUTHOR "Ethan Galstad"
30 #define EMAIL "nagios@nagios.org"
31 #define SUMMARY "Check status of remote machines using SNMP.\n"
33 #define OPTIONS "\
34 -H <ip_address> -o <OID> [-w warn_range] [-c crit_range] \n\
35 [-C community] [-s string] [-r regex] [-R regexi] [-t timeout]\n\
36 [-l label] [-u units] [-p port-number] [-d delimiter]\n\
37 [-D output-delimiter] [-m miblist] [-P snmp version]\n\
38 [-L seclevel] [-U secname] [-a authproto] [-A authpasswd]\n\
39 [-X privpasswd]\n"
41 #define LONGOPTIONS "\
42 -H, --hostname=HOST\n\
43 Name or IP address of the device you wish to query\n\
44 -o, --oid=OID(s)\n\
45 Object identifier(s) whose value you wish to query\n\
46 -w, --warning=INTEGER_RANGE(s)\n\
47 Range(s) which will not result in a WARNING status\n\
48 -c, --critical=INTEGER_RANGE(s)\n\
49 Range(s) which will not result in a CRITICAL status\n\
50 -C, --community=STRING\n\
51 Optional community string for SNMP communication\n\
52 (default is \"%s\")\n\
53 -u, --units=STRING\n\
54 Units label(s) for output data (e.g., 'sec.').\n\
55 -p, --port=STRING\n\
56 UDP port number target is listening on. Default is \"%s\"\n\
57 -P, --protocol=[1|3]\n\
58 SNMP protocol version\n\
59 -L, --seclevel=[noAuthNoPriv|authNoPriv|authPriv]\n\
60 SNMPv3 securityLevel\n\
61 -U, --secname=USERNAME\n\
62 SNMPv3 username\n\
63 -a, --authproto=[MD5|SHA]\n\
64 SNMPv3 auth proto\n\
65 -A, --authpassword=PASSWORD\n\
66 SNMPv3 authentication password\n\
67 -X, --privpasswd=PASSWORD\n\
68 SNMPv3 crypt passwd (DES)\n\
69 -d, --delimiter=STRING\n\
70 Delimiter to use when parsing returned data. Default is \"%s\"\n\
71 Any data on the right hand side of the delimiter is considered\n\
72 to be the data that should be used in the evaluation.\n\
73 -t, --timeout=INTEGER\n\
74 Seconds to wait before plugin times out (see also nagios server timeout).\n\
75 Default is %d seconds\n\
76 -D, --output-delimiter=STRING\n\
77 Separates output on multiple OID requests\n\
78 -s, --string=STRING\n\
79 Return OK state (for that OID) if STRING is an exact match\n\
80 -r, --ereg=REGEX\n\
81 Return OK state (for that OID) if extended regular expression REGEX matches\n\
82 -R, --eregi=REGEX\n\
83 Return OK state (for that OID) if case-insensitive extended REGEX matches\n\
84 -l, --label=STRING\n\
85 Prefix label for output from plugin (default -s 'SNMP')\n\
86 -v, --verbose\n\
87 Debugging the output\n\
88 -m, --miblist=STRING\n\
89 List of MIBS to be loaded (default = ALL)\n"
91 #define NOTES "\
92 - This plugin uses the 'snmpget' command included with the NET-SNMP package.\n\
93 If you don't have the package installed, you will need to download it from\n\
94 http://net-snmp.sourceforge.net before you can use this plugin.\n\
95 - Multiple OIDs may be indicated by a comma- or space-delimited list (lists with\n\
96 internal spaces must be quoted) [max 8 OIDs]\n\
97 - Ranges are inclusive and are indicated with colons. When specified as\n\
98 'min:max' a STATE_OK will be returned if the result is within the indicated\n\
99 range or is equal to the upper or lower bound. A non-OK state will be\n\
100 returned if the result is outside the specified range.\n\
101 - If specified in the order 'max:min' a non-OK state will be returned if the\n\
102 result is within the (inclusive) range.\n\
103 - Upper or lower bounds may be omitted to skip checking the respective limit.\n\
104 - Bare integers are interpreted as upper limits.\n\
105 - When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'\n\
106 - Note that only one string and one regex may be checked at present\n\
107 - All evaluation methods other than PR, STR, and SUBSTR expect that the value\n\
108 returned from the SNMP query is an unsigned integer.\n"
110 #define DESCRIPTION "\
111 This plugin gets system information on a remote server via snmp.\n"
113 #define DEFAULT_COMMUNITY "public"
114 #define DEFAULT_PORT "161"
115 #define DEFAULT_TIMEOUT 10
116 #define DEFAULT_MIBLIST "ALL"
117 #define DEFAULT_PROTOCOL "1"
118 #define DEFAULT_AUTH_PROTOCOL "MD5"
120 #include "common.h"
121 #include "utils.h"
122 #include "popen.h"
124 #define mark(a) ((a)!=0?"*":"")
126 #define CHECK_UNDEF 0
127 #define CRIT_PRESENT 1
128 #define CRIT_STRING 2
129 #define CRIT_REGEX 4
130 #define CRIT_GT 8
131 #define CRIT_LT 16
132 #define CRIT_GE 32
133 #define CRIT_LE 64
134 #define CRIT_EQ 128
135 #define CRIT_NE 256
136 #define CRIT_RANGE 512
137 #define WARN_PRESENT 1024
138 #define WARN_STRING 2048
139 #define WARN_REGEX 4096
140 #define WARN_GT 8192
141 #define WARN_LT 16384
142 #define WARN_GE 32768
143 #define WARN_LE 65536
144 #define WARN_EQ 131072
145 #define WARN_NE 262144
146 #define WARN_RANGE 524288
148 #define MAX_OIDS 8
149 #define MAX_DELIM_LENGTH 8
150 #define DEFAULT_DELIMITER "="
151 #define DEFAULT_OUTPUT_DELIMITER " "
153 void print_usage (void);
154 void print_help (void);
155 int process_arguments (int, char **);
156 int validate_arguments (void);
157 int check_num (int);
158 char *clarify_message (char *);
159 int lu_getll (unsigned long *, char *);
160 int lu_getul (unsigned long *, char *);
161 char *thisarg (char *str);
162 char *nextarg (char *str);
164 #ifdef HAVE_REGEX_H
165 #include <regex.h>
166 char regex_expect[MAX_INPUT_BUFFER] = "";
167 regex_t preg;
168 regmatch_t pmatch[10];
169 char timestamp[10] = "";
170 char regex[MAX_INPUT_BUFFER];
171 char errbuf[MAX_INPUT_BUFFER];
172 int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
173 int eflags = 0;
174 int errcode, excode;
175 #endif
177 char *server_address = NULL;
178 char *community = NULL;
179 char *authpriv = NULL;
180 char *proto = NULL;
181 char *seclevel = NULL;
182 char *secname = NULL;
183 char *authproto = NULL;
184 char *authpasswd = NULL;
185 char *privpasswd = NULL;
186 char *oid = "";
187 char *label = NULL;
188 char *units = NULL;
189 char *port = DEFAULT_PORT;
190 char string_value[MAX_INPUT_BUFFER] = "";
191 char **labels = NULL;
192 char **unitv = NULL;
193 int nlabels = 0;
194 int labels_size = 8;
195 int nunits = 0;
196 int unitv_size = 8;
197 int verbose = FALSE;
198 unsigned long lower_warn_lim[MAX_OIDS];
199 unsigned long upper_warn_lim[MAX_OIDS];
200 unsigned long lower_crit_lim[MAX_OIDS];
201 unsigned long upper_crit_lim[MAX_OIDS];
202 unsigned long response_value[MAX_OIDS];
203 int check_warning_value = FALSE;
204 int check_critical_value = FALSE;
205 int eval_method[MAX_OIDS];
206 char *delimiter = NULL;
207 char *output_delim = NULL;
208 char *miblist = NULL;
211 int
212 main (int argc, char **argv)
213 {
214 int i = 0;
215 int iresult = STATE_UNKNOWN;
216 int found = 0;
217 int result = STATE_DEPENDENT;
218 char input_buffer[MAX_INPUT_BUFFER];
219 char *command_line = NULL;
220 char *response = NULL;
221 char *outbuff = "";
222 char *output = NULL;
223 char *ptr = NULL;
224 char *p2 = NULL;
225 char *show = NULL;
227 labels = malloc (labels_size);
228 unitv = malloc (unitv_size);
229 for (i = 0; i < MAX_OIDS; i++)
230 eval_method[i] = CHECK_UNDEF;
231 i = 0;
233 if (process_arguments (argc, argv) == ERROR)
234 usage ("Incorrect arguments supplied\n");
236 /* create the command line to execute */
237 asprintf (&command_line, "%s -m %s -v %s %s %s:%s %s",
238 PATH_TO_SNMPGET, miblist, proto, authpriv, server_address, port, oid);
239 if (verbose)
240 printf ("%s\n", command_line);
242 /* run the command */
243 child_process = spopen (command_line);
244 if (child_process == NULL) {
245 printf ("Could not open pipe: %s\n", command_line);
246 exit (STATE_UNKNOWN);
247 }
249 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
250 if (child_stderr == NULL) {
251 printf ("Could not open stderr for %s\n", command_line);
252 }
254 asprintf (&output, "");
255 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
256 asprintf (&output, "%s%s", output, input_buffer);
258 if (verbose)
259 printf ("%s\n", output);
261 ptr = output;
263 while (ptr) {
265 ptr = strstr (ptr, delimiter);
266 if (ptr == NULL)
267 break;
269 ptr += strlen (delimiter);
270 ptr += strspn (ptr, " ");
272 found++;
274 if (ptr[0] == '"') {
275 ptr++;
276 response = strpcpy (response, ptr, "\"");
277 ptr = strpbrk (ptr, "\"");
278 ptr += strspn (ptr, "\"\n");
279 }
280 else {
281 response = strpcpy (response, ptr, "\n");
282 ptr = strpbrk (ptr, "\n");
283 ptr += strspn (ptr, "\n");
284 while
285 (strstr (ptr, delimiter) &&
286 strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter)) {
287 response = strpcat (response, ptr, "\n");
288 ptr = strpbrk (ptr, "\n");
289 }
290 if (ptr && strstr (ptr, delimiter) == NULL) {
291 response = strscat (response, ptr);
292 ptr = NULL;
293 }
294 }
296 if (strstr (response, "Gauge: "))
297 show = strstr (response, "Gauge: ") + 7;
298 else if (strstr (response, "Gauge32: "))
299 show = strstr (response, "Gauge32: ") + 9;
300 else if (strstr (response, "INTEGER: "))
301 show = strstr (response, "INTEGER: ") + 9;
302 else
303 show = response;
304 p2 = show;
306 iresult = STATE_DEPENDENT;
308 if (eval_method[i] & CRIT_PRESENT) {
309 iresult = STATE_CRITICAL;
310 } else if (eval_method[i] & WARN_PRESENT) {
311 iresult = STATE_WARNING;
312 }
314 if (eval_method[i] & CRIT_GT ||
315 eval_method[i] & CRIT_LT ||
316 eval_method[i] & CRIT_GE ||
317 eval_method[i] & CRIT_LE ||
318 eval_method[i] & CRIT_EQ ||
319 eval_method[i] & CRIT_NE ||
320 eval_method[i] & WARN_GT ||
321 eval_method[i] & WARN_LT ||
322 eval_method[i] & WARN_GE ||
323 eval_method[i] & WARN_LE ||
324 eval_method[i] & WARN_EQ ||
325 eval_method[i] & WARN_NE) {
326 p2 = strpbrk (p2, "0123456789");
327 response_value[i] = strtoul (p2, NULL, 10);
328 iresult = check_num (i);
329 asprintf (&show, "%lu", response_value[i]);
330 /*asprintf (&show, "%s", response); */
331 }
333 else if (eval_method[i] & CRIT_STRING) {
334 if (strcmp (response, string_value))
335 iresult = STATE_CRITICAL;
336 else
337 iresult = STATE_OK;
338 }
340 else if (eval_method[i] & CRIT_REGEX) {
341 #ifdef HAVE_REGEX_H
342 excode = regexec (&preg, response, 10, pmatch, eflags);
343 if (excode == 0) {
344 iresult = STATE_OK;
345 }
346 else if (excode != REG_NOMATCH) {
347 regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
348 printf ("Execute Error: %s\n", errbuf);
349 exit (STATE_CRITICAL);
350 }
351 else {
352 iresult = STATE_CRITICAL;
353 }
354 #else
355 printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
356 exit (STATE_UNKNOWN);
357 #endif
358 }
360 if (response && iresult == STATE_DEPENDENT)
361 iresult = STATE_OK;
362 else if (eval_method[i] & CRIT_PRESENT)
363 iresult = STATE_CRITICAL;
364 else if (eval_method[i] & WARN_PRESENT)
365 iresult = STATE_WARNING;
367 result = max_state (result, iresult);
369 if (nlabels > 1 && i < nlabels && labels[i] != NULL)
370 asprintf (&outbuff, "%s%s%s %s%s%s", outbuff,
371 (i == 0) ? " " : output_delim,
372 labels[i], mark (iresult), show, mark (iresult));
373 else
374 asprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim,
375 mark (iresult), show, mark (iresult));
377 if (nunits > 0 && i < nunits)
378 asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
380 i++;
382 } /* end while (ptr) */
384 if (found == 0)
385 terminate
386 (STATE_UNKNOWN,
387 "%s problem - No data recieved from host\nCMD: %s\n",
388 label, command_line);
390 /* WARNING if output found on stderr */
391 if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
392 result = max_state (result, STATE_WARNING);
394 /* close stderr */
395 (void) fclose (child_stderr);
397 /* close the pipe */
398 if (spclose (child_process))
399 result = max_state (result, STATE_WARNING);
401 /* if (nunits == 1 || i == 1) */
402 /* printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units); */
403 /* else */
404 printf ("%s %s -%s\n", label, state_text (result), outbuff);
406 return result;
407 }
409 /* process command-line arguments */
410 int
411 process_arguments (int argc, char **argv)
412 {
413 char *ptr;
414 int c = 1;
415 int j = 0, jj = 0;
417 #ifdef HAVE_GETOPT_H
418 int option_index = 0;
419 static struct option long_options[] = {
420 STD_LONG_OPTS,
421 {"community", required_argument, 0, 'C'},
422 {"oid", required_argument, 0, 'o'},
423 {"object", required_argument, 0, 'o'},
424 {"delimiter", required_argument, 0, 'd'},
425 {"output-delimiter", required_argument, 0, 'D'},
426 {"string", required_argument, 0, 's'},
427 {"regex", required_argument, 0, 'r'},
428 {"ereg", required_argument, 0, 'r'},
429 {"eregi", required_argument, 0, 'R'},
430 {"label", required_argument, 0, 'l'},
431 {"units", required_argument, 0, 'u'},
432 {"port", required_argument, 0, 'p'},
433 {"miblist", required_argument, 0, 'm'},
434 {"protocol", required_argument, 0, 'P'},
435 {"seclevel", required_argument, 0, 'L'},
436 {"secname", required_argument, 0, 'U'},
437 {"authproto", required_argument, 0, 'a'},
438 {"authpasswd", required_argument, 0, 'A'},
439 {"privpasswd", required_argument, 0, 'X'},
440 {0, 0, 0, 0}
441 };
442 #endif
444 if (argc < 2)
445 return ERROR;
447 /* reverse compatibility for very old non-POSIX usage forms */
448 for (c = 1; c < argc; c++) {
449 if (strcmp ("-to", argv[c]) == 0)
450 strcpy (argv[c], "-t");
451 if (strcmp ("-wv", argv[c]) == 0)
452 strcpy (argv[c], "-w");
453 if (strcmp ("-cv", argv[c]) == 0)
454 strcpy (argv[c], "-c");
455 }
457 while (1) {
458 #ifdef HAVE_GETOPT_H
459 c =
460 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:",
461 long_options, &option_index);
462 #else
463 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:");
464 #endif
466 if (c == -1 || c == EOF)
467 break;
469 switch (c) {
470 case '?': /* usage */
471 usage3 ("Unknown argument", optopt);
472 case 'h': /* help */
473 print_help ();
474 exit (STATE_OK);
475 case 'V': /* version */
476 print_revision (PROGNAME, REVISION);
477 exit (STATE_OK);
478 case 'v': /* verbose */
479 verbose = TRUE;
480 break;
481 case 't': /* timeout period */
482 if (!is_integer (optarg))
483 usage2 ("Timeout Interval must be an integer", optarg);
484 timeout_interval = atoi (optarg);
485 break;
486 case 'e': /* PRELIMINARY - may change */
487 eval_method[j] |= WARN_PRESENT;
488 for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
489 ptr[0] = ' '; /* relpace comma with space */
490 for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
491 eval_method[++j] |= WARN_PRESENT;
492 asprintf (&oid, "%s %s", (oid?oid:""), optarg);
493 break;
494 case 'E': /* PRELIMINARY - may change */
495 eval_method[j] |= WARN_PRESENT;
496 for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
497 ptr[0] = ' '; /* relpace comma with space */
498 for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
499 eval_method[++j] |= CRIT_PRESENT;
500 asprintf (&oid, "%s %s", (oid?oid:""), optarg);
501 break;
502 case 'c': /* critical time threshold */
503 if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
504 printf ("Invalid critical threshold: %s\n", optarg);
505 print_usage ();
506 exit (STATE_UNKNOWN);
507 }
508 for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
509 if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
510 eval_method[jj] |= CRIT_LT;
511 if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
512 eval_method[jj] |= CRIT_GT;
513 (ptr = index (ptr, ',')) ? ptr++ : ptr;
514 }
515 break;
516 case 'w': /* warning time threshold */
517 if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
518 printf ("Invalid warning threshold: %s\n", optarg);
519 print_usage ();
520 exit (STATE_UNKNOWN);
521 }
522 for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
523 if (lu_getll (&lower_warn_lim[jj], ptr) == 1)
524 eval_method[jj] |= WARN_LT;
525 if (lu_getul (&upper_warn_lim[jj], ptr) == 1)
526 eval_method[jj] |= WARN_GT;
527 (ptr = index (ptr, ',')) ? ptr++ : ptr;
528 }
529 break;
530 case 'H': /* Host or server */
531 server_address = strscpy (server_address, optarg);
532 break;
533 case 'C': /* group or community */
534 community = strscpy (community, optarg);
535 break;
536 case 'o': /* object identifier */
537 for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
538 ptr[0] = ' '; /* relpace comma with space */
539 for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
540 j++; /* count OIDs */
541 asprintf (&oid, "%s %s", (oid?oid:""), optarg);
542 break;
543 case 'd': /* delimiter */
544 delimiter = strscpy (delimiter, optarg);
545 break;
546 case 'D': /* output-delimiter */
547 output_delim = strscpy (output_delim, optarg);
548 break;
549 case 's': /* string or substring */
550 strncpy (string_value, optarg, sizeof (string_value) - 1);
551 string_value[sizeof (string_value) - 1] = 0;
552 eval_method[jj++] = CRIT_STRING;
553 break;
554 case 'R': /* regex */
555 #ifdef HAVE_REGEX_H
556 cflags = REG_ICASE;
557 #endif
558 case 'r': /* regex */
559 #ifdef HAVE_REGEX_H
560 cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
561 strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
562 regex_expect[sizeof (regex_expect) - 1] = 0;
563 errcode = regcomp (&preg, regex_expect, cflags);
564 if (errcode != 0) {
565 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
566 printf ("Could Not Compile Regular Expression");
567 return ERROR;
568 }
569 eval_method[jj++] = CRIT_REGEX;
570 #else
571 printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
572 exit (STATE_UNKNOWN);
573 #endif
574 break;
575 case 'l': /* label */
576 label = optarg;
577 nlabels++;
578 if (nlabels >= labels_size) {
579 labels_size += 8;
580 labels = realloc (labels, labels_size);
581 if (labels == NULL)
582 terminate (STATE_UNKNOWN,
583 "Could not realloc() labels[%d]", nlabels);
584 }
585 labels[nlabels - 1] = optarg;
586 ptr = thisarg (optarg);
587 if (strstr (ptr, "'") == ptr)
588 labels[nlabels - 1] = ptr + 1;
589 else
590 labels[nlabels - 1] = ptr;
591 while (ptr && (ptr = nextarg (ptr))) {
592 if (nlabels >= labels_size) {
593 labels_size += 8;
594 labels = realloc (labels, labels_size);
595 if (labels == NULL)
596 terminate (STATE_UNKNOWN, "Could not realloc() labels\n");
597 }
598 labels++;
599 ptr = thisarg (ptr);
600 if (strstr (ptr, "'") == ptr)
601 labels[nlabels - 1] = ptr + 1;
602 else
603 labels[nlabels - 1] = ptr;
604 }
605 break;
606 case 'u': /* units */
607 units = optarg;
608 nunits++;
609 if (nunits >= unitv_size) {
610 unitv_size += 8;
611 unitv = realloc (unitv, unitv_size);
612 if (unitv == NULL)
613 terminate (STATE_UNKNOWN,
614 "Could not realloc() units [%d]\n", nunits);
615 }
616 unitv[nunits - 1] = optarg;
617 ptr = thisarg (optarg);
618 if (strstr (ptr, "'") == ptr)
619 unitv[nunits - 1] = ptr + 1;
620 else
621 unitv[nunits - 1] = ptr;
622 while (ptr && (ptr = nextarg (ptr))) {
623 if (nunits >= unitv_size) {
624 unitv_size += 8;
625 unitv = realloc (unitv, unitv_size);
626 if (units == NULL)
627 terminate (STATE_UNKNOWN, "Could not realloc() units\n");
628 }
629 nunits++;
630 ptr = thisarg (ptr);
631 if (strstr (ptr, "'") == ptr)
632 unitv[nunits - 1] = ptr + 1;
633 else
634 unitv[nunits - 1] = ptr;
635 }
636 break;
637 case 'p': /* TCP port number */
638 port = strscpy(port, optarg);
639 break;
640 case 'm': /* List of MIBS */
641 miblist = strscpy(miblist, optarg);
642 break;
643 case 'P': /* SNMP protocol version */
644 proto = strscpy(proto, optarg);
645 break;
646 case 'L': /* security level */
647 seclevel = strscpy(seclevel,optarg);
648 break;
649 case 'U': /* security username */
650 secname = strscpy(secname, optarg);
651 break;
652 case 'a': /* auth protocol */
653 asprintf (&authproto, optarg);
654 break;
655 case 'A': /* auth passwd */
656 authpasswd = strscpy(authpasswd, optarg);
657 break;
658 case 'X': /* priv passwd */
659 privpasswd = strscpy(privpasswd, optarg);
660 break;
662 }
663 }
665 if (server_address == NULL)
666 asprintf (&server_address, argv[optind]);
668 return validate_arguments ();
669 }
671 /******************************************************************************
673 @@-
674 <sect3>
675 <title>validate_arguments</title>
677 <para>&PROTO_validate_arguments;</para>
679 <para>Given a database name, this function returns TRUE if the string
680 is a valid PostgreSQL database name, and returns false if it is
681 not.</para>
683 <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
684 characters long and consist of letters, numbers, and underscores. The
685 first character cannot be a number, however.</para>
687 </sect3>
688 -@@
689 ******************************************************************************/
691 int
692 validate_arguments ()
693 {
695 if (community == NULL)
696 asprintf (&community, DEFAULT_COMMUNITY);
698 if (delimiter == NULL)
699 asprintf (&delimiter, DEFAULT_DELIMITER);
701 if (output_delim == NULL)
702 asprintf (&output_delim, DEFAULT_OUTPUT_DELIMITER);
704 if (miblist == NULL)
705 asprintf (&miblist, DEFAULT_MIBLIST);
707 if (label == NULL)
708 asprintf (&label, "SNMP");
710 if (units == NULL)
711 asprintf (&units, "");
713 /* Need better checks to verify seclevel and authproto choices */
715 if (seclevel == NULL)
716 asprintf (&seclevel, "noAuthNoPriv");
719 if (authproto == NULL )
720 asprintf(&authproto, DEFAULT_AUTH_PROTOCOL);
724 if (proto == NULL || (strcmp(proto,DEFAULT_PROTOCOL) == 0) ) { /* default protocol version */
725 asprintf(&proto, DEFAULT_PROTOCOL);
726 asprintf(&authpriv, "%s%s", "-c ", community);
727 }
728 else if ( strcmp (proto, "3") == 0 ) { /* snmpv3 args */
729 asprintf(&proto, "%s", "3");
731 if ( (strcmp(seclevel, "noAuthNoPriv") == 0) || seclevel == NULL ) {
732 asprintf(&authpriv, "%s", "-l noAuthNoPriv" );
733 }
734 else if ( strcmp(seclevel, "authNoPriv") == 0 ) {
735 if ( secname == NULL || authpasswd == NULL) {
736 printf ("Missing secname (%s) or authpassword (%s) ! \n",secname, authpasswd );
737 print_usage ();
738 exit (STATE_UNKNOWN);
739 }
740 asprintf(&authpriv, "-l authNoPriv -a %s -u %s -A %s ", authproto, secname, authpasswd);
741 }
742 else if ( strcmp(seclevel, "authPriv") == 0 ) {
743 if ( secname == NULL || authpasswd == NULL || privpasswd == NULL ) {
744 printf ("Missing secname (%s), authpassword (%s), or privpasswd (%s)! \n",secname, authpasswd,privpasswd );
745 print_usage ();
746 exit (STATE_UNKNOWN);
747 }
748 asprintf(&authpriv, "-l authPriv -a %s -u %s -A %s -x DES -X %s ", authproto, secname, authpasswd, privpasswd);
749 }
752 }
753 else {
754 printf ("Invalid SNMP version: %s\n", proto);
755 print_usage ();
756 exit (STATE_UNKNOWN);
757 }
762 return OK;
763 }
767 void
768 print_help (void)
769 {
770 print_revision (PROGNAME, REVISION);
771 printf
772 ("Copyright (c) %s %s <%s>\n\n%s\n",
773 COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
774 print_usage ();
775 printf
776 ("\nOptions:\n" LONGOPTIONS "\n" DESCRIPTION "\n" NOTES "\n",
777 DEFAULT_COMMUNITY, DEFAULT_PORT, DEFAULT_DELIMITER, DEFAULT_TIMEOUT);
778 support ();
779 }
781 void
782 print_usage (void)
783 {
784 printf
785 ("Usage:\n" " %s %s\n"
786 " %s (-h | --help) for detailed help\n"
787 " %s (-V | --version) for version information\n",
788 PROGNAME, OPTIONS, PROGNAME, PROGNAME);
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 }