fc19120182c2daf8565adad80c790f220887af01
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 }
359 else {
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;
366 }
368 result = max_state (result, iresult);
370 if (nlabels > 1 && i < nlabels && labels[i] != NULL)
371 asprintf (&outbuff, "%s%s%s %s%s%s", outbuff,
372 (i == 0) ? " " : output_delim,
373 labels[i], mark (iresult), show, mark (iresult));
374 else
375 asprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim,
376 mark (iresult), show, mark (iresult));
378 if (nunits > 0 && i < nunits)
379 asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
381 i++;
383 } /* end while (ptr) */
385 if (found == 0)
386 terminate
387 (STATE_UNKNOWN,
388 "%s problem - No data recieved from host\nCMD: %s\n",
389 label, command_line);
391 /* WARNING if output found on stderr */
392 if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
393 result = max_state (result, STATE_WARNING);
395 /* close stderr */
396 (void) fclose (child_stderr);
398 /* close the pipe */
399 if (spclose (child_process))
400 result = max_state (result, STATE_WARNING);
402 /* if (nunits == 1 || i == 1) */
403 /* printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units); */
404 /* else */
405 printf ("%s %s -%s\n", label, state_text (result), outbuff);
407 return result;
408 }
410 /* process command-line arguments */
411 int
412 process_arguments (int argc, char **argv)
413 {
414 char *ptr;
415 int c = 1;
416 int j = 0, jj = 0;
418 #ifdef HAVE_GETOPT_H
419 int option_index = 0;
420 static struct option long_options[] = {
421 STD_LONG_OPTS,
422 {"community", required_argument, 0, 'C'},
423 {"oid", required_argument, 0, 'o'},
424 {"object", required_argument, 0, 'o'},
425 {"delimiter", required_argument, 0, 'd'},
426 {"output-delimiter", required_argument, 0, 'D'},
427 {"string", required_argument, 0, 's'},
428 {"regex", required_argument, 0, 'r'},
429 {"ereg", required_argument, 0, 'r'},
430 {"eregi", required_argument, 0, 'R'},
431 {"label", required_argument, 0, 'l'},
432 {"units", required_argument, 0, 'u'},
433 {"port", required_argument, 0, 'p'},
434 {"miblist", required_argument, 0, 'm'},
435 {"protocol", required_argument, 0, 'P'},
436 {"seclevel", required_argument, 0, 'L'},
437 {"secname", required_argument, 0, 'U'},
438 {"authproto", required_argument, 0, 'a'},
439 {"authpasswd", required_argument, 0, 'A'},
440 {"privpasswd", required_argument, 0, 'X'},
441 {0, 0, 0, 0}
442 };
443 #endif
445 if (argc < 2)
446 return ERROR;
448 /* reverse compatibility for very old non-POSIX usage forms */
449 for (c = 1; c < argc; c++) {
450 if (strcmp ("-to", argv[c]) == 0)
451 strcpy (argv[c], "-t");
452 if (strcmp ("-wv", argv[c]) == 0)
453 strcpy (argv[c], "-w");
454 if (strcmp ("-cv", argv[c]) == 0)
455 strcpy (argv[c], "-c");
456 }
458 while (1) {
459 #ifdef HAVE_GETOPT_H
460 c =
461 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:",
462 long_options, &option_index);
463 #else
464 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:");
465 #endif
467 if (c == -1 || c == EOF)
468 break;
470 switch (c) {
471 case '?': /* usage */
472 usage3 ("Unknown argument", optopt);
473 case 'h': /* help */
474 print_help ();
475 exit (STATE_OK);
476 case 'V': /* version */
477 print_revision (PROGNAME, REVISION);
478 exit (STATE_OK);
479 case 'v': /* verbose */
480 verbose = TRUE;
481 break;
482 case 't': /* timeout period */
483 if (!is_integer (optarg))
484 usage2 ("Timeout Interval must be an integer", optarg);
485 timeout_interval = atoi (optarg);
486 break;
487 case 'e': /* PRELIMINARY - may change */
488 eval_method[j] |= WARN_PRESENT;
489 for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
490 ptr[0] = ' '; /* relpace comma with space */
491 for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
492 eval_method[++j] |= WARN_PRESENT;
493 asprintf (&oid, "%s %s", (oid?oid:""), optarg);
494 break;
495 case 'E': /* PRELIMINARY - may change */
496 eval_method[j] |= WARN_PRESENT;
497 for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
498 ptr[0] = ' '; /* relpace comma with space */
499 for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
500 eval_method[++j] |= CRIT_PRESENT;
501 asprintf (&oid, "%s %s", (oid?oid:""), optarg);
502 break;
503 case 'c': /* critical time threshold */
504 if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
505 printf ("Invalid critical threshold: %s\n", optarg);
506 print_usage ();
507 exit (STATE_UNKNOWN);
508 }
509 for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
510 if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
511 eval_method[jj] |= CRIT_LT;
512 if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
513 eval_method[jj] |= CRIT_GT;
514 (ptr = index (ptr, ',')) ? ptr++ : ptr;
515 }
516 break;
517 case 'w': /* warning time threshold */
518 if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
519 printf ("Invalid warning threshold: %s\n", optarg);
520 print_usage ();
521 exit (STATE_UNKNOWN);
522 }
523 for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
524 if (lu_getll (&lower_warn_lim[jj], ptr) == 1)
525 eval_method[jj] |= WARN_LT;
526 if (lu_getul (&upper_warn_lim[jj], ptr) == 1)
527 eval_method[jj] |= WARN_GT;
528 (ptr = index (ptr, ',')) ? ptr++ : ptr;
529 }
530 break;
531 case 'H': /* Host or server */
532 server_address = strscpy (server_address, optarg);
533 break;
534 case 'C': /* group or community */
535 community = strscpy (community, optarg);
536 break;
537 case 'o': /* object identifier */
538 for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
539 ptr[0] = ' '; /* relpace comma with space */
540 for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
541 j++; /* count OIDs */
542 asprintf (&oid, "%s %s", (oid?oid:""), optarg);
543 break;
544 case 'd': /* delimiter */
545 delimiter = strscpy (delimiter, optarg);
546 break;
547 case 'D': /* output-delimiter */
548 output_delim = strscpy (output_delim, optarg);
549 break;
550 case 's': /* string or substring */
551 strncpy (string_value, optarg, sizeof (string_value) - 1);
552 string_value[sizeof (string_value) - 1] = 0;
553 eval_method[jj] = CRIT_STRING;
554 break;
555 case 'R': /* regex */
556 #ifdef HAVE_REGEX_H
557 cflags = REG_ICASE;
558 #endif
559 case 'r': /* regex */
560 #ifdef HAVE_REGEX_H
561 cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
562 strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
563 regex_expect[sizeof (regex_expect) - 1] = 0;
564 errcode = regcomp (&preg, regex_expect, cflags);
565 if (errcode != 0) {
566 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
567 printf ("Could Not Compile Regular Expression");
568 return ERROR;
569 }
570 eval_method[jj] = CRIT_REGEX;
571 #else
572 printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
573 exit (STATE_UNKNOWN);
574 #endif
575 break;
576 case 'l': /* label */
577 label = optarg;
578 nlabels++;
579 if (nlabels >= labels_size) {
580 labels_size += 8;
581 labels = realloc (labels, labels_size);
582 if (labels == NULL)
583 terminate (STATE_UNKNOWN,
584 "Could not realloc() labels[%d]", nlabels);
585 }
586 labels[nlabels - 1] = optarg;
587 ptr = thisarg (optarg);
588 if (strstr (ptr, "'") == ptr)
589 labels[nlabels - 1] = ptr + 1;
590 else
591 labels[nlabels - 1] = ptr;
592 while (ptr && (ptr = nextarg (ptr))) {
593 if (nlabels >= labels_size) {
594 labels_size += 8;
595 labels = realloc (labels, labels_size);
596 if (labels == NULL)
597 terminate (STATE_UNKNOWN, "Could not realloc() labels\n");
598 }
599 labels++;
600 ptr = thisarg (ptr);
601 if (strstr (ptr, "'") == ptr)
602 labels[nlabels - 1] = ptr + 1;
603 else
604 labels[nlabels - 1] = ptr;
605 }
606 break;
607 case 'u': /* units */
608 units = optarg;
609 nunits++;
610 if (nunits >= unitv_size) {
611 unitv_size += 8;
612 unitv = realloc (unitv, unitv_size);
613 if (unitv == NULL)
614 terminate (STATE_UNKNOWN,
615 "Could not realloc() units [%d]\n", nunits);
616 }
617 unitv[nunits - 1] = optarg;
618 ptr = thisarg (optarg);
619 if (strstr (ptr, "'") == ptr)
620 unitv[nunits - 1] = ptr + 1;
621 else
622 unitv[nunits - 1] = ptr;
623 while (ptr && (ptr = nextarg (ptr))) {
624 if (nunits >= unitv_size) {
625 unitv_size += 8;
626 unitv = realloc (unitv, unitv_size);
627 if (units == NULL)
628 terminate (STATE_UNKNOWN, "Could not realloc() units\n");
629 }
630 nunits++;
631 ptr = thisarg (ptr);
632 if (strstr (ptr, "'") == ptr)
633 unitv[nunits - 1] = ptr + 1;
634 else
635 unitv[nunits - 1] = ptr;
636 }
637 break;
638 case 'p': /* TCP port number */
639 port = strscpy(port, optarg);
640 break;
641 case 'm': /* List of MIBS */
642 miblist = strscpy(miblist, optarg);
643 break;
644 case 'P': /* SNMP protocol version */
645 proto = strscpy(proto, optarg);
646 break;
647 case 'L': /* security level */
648 seclevel = strscpy(seclevel,optarg);
649 break;
650 case 'U': /* security username */
651 secname = strscpy(secname, optarg);
652 break;
653 case 'a': /* auth protocol */
654 asprintf (&authproto, optarg);
655 break;
656 case 'A': /* auth passwd */
657 authpasswd = strscpy(authpasswd, optarg);
658 break;
659 case 'X': /* priv passwd */
660 privpasswd = strscpy(privpasswd, optarg);
661 break;
663 }
664 }
666 if (server_address == NULL)
667 asprintf (&server_address, argv[optind]);
669 return validate_arguments ();
670 }
672 /******************************************************************************
674 @@-
675 <sect3>
676 <title>validate_arguments</title>
678 <para>&PROTO_validate_arguments;</para>
680 <para>Given a database name, this function returns TRUE if the string
681 is a valid PostgreSQL database name, and returns false if it is
682 not.</para>
684 <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
685 characters long and consist of letters, numbers, and underscores. The
686 first character cannot be a number, however.</para>
688 </sect3>
689 -@@
690 ******************************************************************************/
692 int
693 validate_arguments ()
694 {
696 if (community == NULL)
697 asprintf (&community, DEFAULT_COMMUNITY);
699 if (delimiter == NULL)
700 asprintf (&delimiter, DEFAULT_DELIMITER);
702 if (output_delim == NULL)
703 asprintf (&output_delim, DEFAULT_OUTPUT_DELIMITER);
705 if (miblist == NULL)
706 asprintf (&miblist, DEFAULT_MIBLIST);
708 if (label == NULL)
709 asprintf (&label, "SNMP");
711 if (units == NULL)
712 asprintf (&units, "");
714 /* Need better checks to verify seclevel and authproto choices */
716 if (seclevel == NULL)
717 asprintf (&seclevel, "noAuthNoPriv");
720 if (authproto == NULL )
721 asprintf(&authproto, DEFAULT_AUTH_PROTOCOL);
725 if (proto == NULL || (strcmp(proto,DEFAULT_PROTOCOL) == 0) ) { /* default protocol version */
726 asprintf(&proto, DEFAULT_PROTOCOL);
727 asprintf(&authpriv, "%s%s", "-c ", community);
728 }
729 else if ( strcmp (proto, "3") == 0 ) { /* snmpv3 args */
730 asprintf(&proto, "%s", "3");
732 if ( (strcmp(seclevel, "noAuthNoPriv") == 0) || seclevel == NULL ) {
733 asprintf(&authpriv, "%s", "-l noAuthNoPriv" );
734 }
735 else if ( strcmp(seclevel, "authNoPriv") == 0 ) {
736 if ( secname == NULL || authpasswd == NULL) {
737 printf ("Missing secname (%s) or authpassword (%s) ! \n",secname, authpasswd );
738 print_usage ();
739 exit (STATE_UNKNOWN);
740 }
741 asprintf(&authpriv, "-l authNoPriv -a %s -u %s -A %s ", authproto, secname, authpasswd);
742 }
743 else if ( strcmp(seclevel, "authPriv") == 0 ) {
744 if ( secname == NULL || authpasswd == NULL || privpasswd == NULL ) {
745 printf ("Missing secname (%s), authpassword (%s), or privpasswd (%s)! \n",secname, authpasswd,privpasswd );
746 print_usage ();
747 exit (STATE_UNKNOWN);
748 }
749 asprintf(&authpriv, "-l authPriv -a %s -u %s -A %s -x DES -X %s ", authproto, secname, authpasswd, privpasswd);
750 }
753 }
754 else {
755 printf ("Invalid SNMP version: %s\n", proto);
756 print_usage ();
757 exit (STATE_UNKNOWN);
758 }
763 return OK;
764 }
768 void
769 print_help (void)
770 {
771 print_revision (PROGNAME, REVISION);
772 printf
773 ("Copyright (c) %s %s <%s>\n\n%s\n",
774 COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
775 print_usage ();
776 printf
777 ("\nOptions:\n" LONGOPTIONS "\n" DESCRIPTION "\n" NOTES "\n",
778 DEFAULT_COMMUNITY, DEFAULT_PORT, DEFAULT_DELIMITER, DEFAULT_TIMEOUT);
779 support ();
780 }
782 void
783 print_usage (void)
784 {
785 printf
786 ("Usage:\n" " %s %s\n"
787 " %s (-h | --help) for detailed help\n"
788 " %s (-V | --version) for version information\n",
789 PROGNAME, OPTIONS, PROGNAME, PROGNAME);
790 }
791 \f
794 char *
795 clarify_message (char *msg)
796 {
797 int i = 0;
798 int foo;
799 char tmpmsg_c[MAX_INPUT_BUFFER];
800 char *tmpmsg = (char *) &tmpmsg_c;
801 tmpmsg = strcpy (tmpmsg, msg);
802 if (!strncmp (tmpmsg, " Hex:", 5)) {
803 tmpmsg = strtok (tmpmsg, ":");
804 while ((tmpmsg = strtok (NULL, " "))) {
805 foo = strtol (tmpmsg, NULL, 16);
806 /* Translate chars that are not the same value in the printers
807 * character set.
808 */
809 switch (foo) {
810 case 208:
811 {
812 foo = 197;
813 break;
814 }
815 case 216:
816 {
817 foo = 196;
818 break;
819 }
820 }
821 msg[i] = foo;
822 i++;
823 }
824 msg[i] = 0;
825 }
826 return (msg);
827 }
830 int
831 check_num (int i)
832 {
833 int result;
834 result = STATE_OK;
835 if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
836 lower_warn_lim[i] > upper_warn_lim[i]) {
837 if (response_value[i] <= lower_warn_lim[i] &&
838 response_value[i] >= upper_warn_lim[i]) {
839 result = STATE_WARNING;
840 }
841 }
842 else if
843 ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
844 (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
845 (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
846 (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
847 (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
848 (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
849 result = STATE_WARNING;
850 }
852 if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
853 lower_warn_lim[i] > upper_warn_lim[i]) {
854 if (response_value[i] <= lower_crit_lim[i] &&
855 response_value[i] >= upper_crit_lim[i]) {
856 result = STATE_CRITICAL;
857 }
858 }
859 else if
860 ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
861 (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
862 (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
863 (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
864 (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
865 (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
866 result = STATE_CRITICAL;
867 }
869 return result;
870 }
873 int
874 lu_getll (unsigned long *ll, char *str)
875 {
876 char tmp[100];
877 if (strchr (str, ':') == NULL)
878 return 0;
879 if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
880 return 0;
881 if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
882 return 1;
883 return 0;
884 }
886 int
887 lu_getul (unsigned long *ul, char *str)
888 {
889 char tmp[100];
890 if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
891 return 1;
892 if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
893 return 1;
894 if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
895 return 1;
896 return 0;
897 }
904 /* trim leading whitespace
905 if there is a leading quote, make sure it balances */
907 char *
908 thisarg (char *str)
909 {
910 str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
911 if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
912 if (strlen (str) == 1 || !strstr (str + 1, "'"))
913 terminate (STATE_UNKNOWN, "Unbalanced quotes\n");
914 }
915 return str;
916 }
919 /* if there's a leading quote, advance to the trailing quote
920 set the trailing quote to '\x0'
921 if the string continues, advance beyond the comma */
923 char *
924 nextarg (char *str)
925 {
926 if (strstr (str, "'") == str) {
927 if (strlen (str) > 1) {
928 str = strstr (str + 1, "'");
929 str[0] = 0;
930 return (++str);
931 }
932 else {
933 str[0] = 0;
934 return NULL;
935 }
936 }
937 if (strstr (str, ",") == str) {
938 if (strlen (str) > 1) {
939 str[0] = 0;
940 return (++str);
941 }
942 else {
943 str[0] = 0;
944 return NULL;
945 }
946 }
947 if ((str = strstr (str, ",")) && strlen (str) > 1) {
948 str[0] = 0;
949 return (++str);
950 }
951 return NULL;
952 }