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 = DEFAULT_COMMUNITY;
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 = "SNMP";
188 char *units = "";
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 = DEFAULT_DELIMITER;
207 char *output_delim = DEFAULT_OUTPUT_DELIMITER;
208 char *miblist = DEFAULT_MIBLIST;
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 = "";
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 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
255 asprintf (&output, "%s%s", output, input_buffer);
257 if (verbose)
258 printf ("%s\n", output);
260 ptr = output;
262 while (ptr) {
264 ptr = strstr (ptr, delimiter);
265 if (ptr == NULL)
266 break;
268 ptr += strlen (delimiter);
269 ptr += strspn (ptr, " ");
271 found++;
273 if (ptr[0] == '"') {
274 ptr++;
275 response = strpcpy (response, ptr, "\"");
276 ptr = strpbrk (ptr, "\"");
277 ptr += strspn (ptr, "\"\n");
278 }
279 else {
280 response = strpcpy (response, ptr, "\n");
281 ptr = strpbrk (ptr, "\n");
282 ptr += strspn (ptr, "\n");
283 while
284 (strstr (ptr, delimiter) &&
285 strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter)) {
286 response = strpcat (response, ptr, "\n");
287 ptr = strpbrk (ptr, "\n");
288 }
289 if (ptr && strstr (ptr, delimiter) == NULL) {
290 response = strscat (response, ptr);
291 ptr = NULL;
292 }
293 }
295 /* We strip out the datatype indicator for PHBs */
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 /* Process this block for integer comparisons */
309 if (eval_method[i] & CRIT_GT ||
310 eval_method[i] & CRIT_LT ||
311 eval_method[i] & CRIT_GE ||
312 eval_method[i] & CRIT_LE ||
313 eval_method[i] & CRIT_EQ ||
314 eval_method[i] & CRIT_NE ||
315 eval_method[i] & WARN_GT ||
316 eval_method[i] & WARN_LT ||
317 eval_method[i] & WARN_GE ||
318 eval_method[i] & WARN_LE ||
319 eval_method[i] & WARN_EQ ||
320 eval_method[i] & WARN_NE) {
321 p2 = strpbrk (p2, "0123456789");
322 response_value[i] = strtoul (p2, NULL, 10);
323 iresult = check_num (i);
324 asprintf (&show, "%lu", response_value[i]);
325 }
327 /* Process this block for string matching */
328 else if (eval_method[i] & CRIT_STRING) {
329 if (strcmp (response, string_value))
330 iresult = STATE_CRITICAL;
331 else
332 iresult = STATE_OK;
333 }
335 /* Process this block for regex matching */
336 else if (eval_method[i] & CRIT_REGEX) {
337 #ifdef HAVE_REGEX_H
338 excode = regexec (&preg, response, 10, pmatch, eflags);
339 if (excode == 0) {
340 iresult = STATE_OK;
341 }
342 else if (excode != REG_NOMATCH) {
343 regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
344 printf ("Execute Error: %s\n", errbuf);
345 exit (STATE_CRITICAL);
346 }
347 else {
348 iresult = STATE_CRITICAL;
349 }
350 #else
351 printf ("%s UNKNOWN: call for regex which was not a compiled option", label);
352 exit (STATE_UNKNOWN);
353 #endif
354 }
356 /* Process this block for existence-nonexistence checks */
357 else {
358 if (eval_method[i] & CRIT_PRESENT)
359 iresult = STATE_CRITICAL;
360 else if (eval_method[i] & WARN_PRESENT)
361 iresult = STATE_WARNING;
362 else if (response && iresult == STATE_DEPENDENT)
363 iresult = STATE_OK;
364 }
366 /* Result is the worst outcome of all the OIDs tested */
367 result = max_state (result, iresult);
369 /* Prepend a label for this OID if there is one */
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 /* Append a unit string for this OID if there is one */
379 if (nunits > 0 && i < nunits && unitv[i] != NULL)
380 asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
382 i++;
384 } /* end while (ptr) */
386 if (found == 0)
387 terminate
388 (STATE_UNKNOWN,
389 "%s problem - No data recieved from host\nCMD: %s\n",
390 label, command_line);
392 /* WARNING if output found on stderr */
393 if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
394 result = max_state (result, STATE_WARNING);
396 /* close stderr */
397 (void) fclose (child_stderr);
399 /* close the pipe */
400 if (spclose (child_process))
401 result = max_state (result, STATE_WARNING);
403 /* if (nunits == 1 || i == 1) */
404 /* printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units); */
405 /* else */
406 printf ("%s %s -%s\n", label, state_text (result), outbuff);
408 return result;
409 }
411 /* process command-line arguments */
412 int
413 process_arguments (int argc, char **argv)
414 {
415 char *ptr;
416 int c = 1;
417 int j = 0, jj = 0, ii = 0;
419 #ifdef HAVE_GETOPT_H
420 int option_index = 0;
421 static struct option long_options[] = {
422 STD_LONG_OPTS,
423 {"community", required_argument, 0, 'C'},
424 {"oid", required_argument, 0, 'o'},
425 {"object", required_argument, 0, 'o'},
426 {"delimiter", required_argument, 0, 'd'},
427 {"output-delimiter", required_argument, 0, 'D'},
428 {"string", required_argument, 0, 's'},
429 {"regex", required_argument, 0, 'r'},
430 {"ereg", required_argument, 0, 'r'},
431 {"eregi", required_argument, 0, 'R'},
432 {"label", required_argument, 0, 'l'},
433 {"units", required_argument, 0, 'u'},
434 {"port", required_argument, 0, 'p'},
435 {"miblist", required_argument, 0, 'm'},
436 {"protocol", required_argument, 0, 'P'},
437 {"seclevel", required_argument, 0, 'L'},
438 {"secname", required_argument, 0, 'U'},
439 {"authproto", required_argument, 0, 'a'},
440 {"authpasswd", required_argument, 0, 'A'},
441 {"privpasswd", required_argument, 0, 'X'},
442 {0, 0, 0, 0}
443 };
444 #endif
446 if (argc < 2)
447 return ERROR;
449 /* reverse compatibility for very old non-POSIX usage forms */
450 for (c = 1; c < argc; c++) {
451 if (strcmp ("-to", argv[c]) == 0)
452 strcpy (argv[c], "-t");
453 if (strcmp ("-wv", argv[c]) == 0)
454 strcpy (argv[c], "-w");
455 if (strcmp ("-cv", argv[c]) == 0)
456 strcpy (argv[c], "-c");
457 }
459 while (1) {
460 #ifdef HAVE_GETOPT_H
461 c =
462 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:",
463 long_options, &option_index);
464 #else
465 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:");
466 #endif
468 if (c == -1 || c == EOF)
469 break;
471 switch (c) {
472 case '?': /* usage */
473 usage3 ("Unknown argument", optopt);
474 case 'h': /* help */
475 print_help ();
476 exit (STATE_OK);
477 case 'V': /* version */
478 print_revision (PROGNAME, REVISION);
479 exit (STATE_OK);
480 case 'v': /* verbose */
481 verbose = TRUE;
482 break;
484 /* Connection info */
485 case 'C': /* group or community */
486 community = strscpy (community, optarg);
487 break;
488 case 'H': /* Host or server */
489 server_address = strscpy (server_address, optarg);
490 break;
491 case 'p': /* TCP port number */
492 port = strscpy(port, optarg);
493 break;
494 case 'm': /* List of MIBS */
495 miblist = strscpy(miblist, optarg);
496 break;
497 case 'P': /* SNMP protocol version */
498 proto = strscpy(proto, optarg);
499 break;
500 case 'L': /* security level */
501 seclevel = strscpy(seclevel,optarg);
502 break;
503 case 'U': /* security username */
504 secname = strscpy(secname, optarg);
505 break;
506 case 'a': /* auth protocol */
507 asprintf (&authproto, optarg);
508 break;
509 case 'A': /* auth passwd */
510 authpasswd = strscpy(authpasswd, optarg);
511 break;
512 case 'X': /* priv passwd */
513 privpasswd = strscpy(privpasswd, optarg);
514 break;
515 case 't': /* timeout period */
516 if (!is_integer (optarg))
517 usage2 ("Timeout Interval must be an integer", optarg);
518 timeout_interval = atoi (optarg);
519 break;
521 /* Test parameters */
522 case 'c': /* critical time threshold */
523 if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
524 printf ("Invalid critical threshold: %s\n", optarg);
525 print_usage ();
526 exit (STATE_UNKNOWN);
527 }
528 for (ptr = optarg; ptr && jj < MAX_OIDS; jj++) {
529 if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
530 eval_method[jj] |= CRIT_LT;
531 if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
532 eval_method[jj] |= CRIT_GT;
533 (ptr = index (ptr, ',')) ? ptr++ : ptr;
534 }
535 break;
536 case 'w': /* warning time threshold */
537 if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
538 printf ("Invalid warning threshold: %s\n", optarg);
539 print_usage ();
540 exit (STATE_UNKNOWN);
541 }
542 for (ptr = optarg; ptr && ii < MAX_OIDS; ii++) {
543 if (lu_getll (&lower_warn_lim[ii], ptr) == 1)
544 eval_method[ii] |= WARN_LT;
545 if (lu_getul (&upper_warn_lim[ii], ptr) == 1)
546 eval_method[ii] |= WARN_GT;
547 (ptr = index (ptr, ',')) ? ptr++ : ptr;
548 }
549 break;
550 case 'o': /* object identifier */
551 case 'e': /* PRELIMINARY - may change */
552 case 'E': /* PRELIMINARY - may change */
553 for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
554 ptr[0] = ' '; /* relpace comma with space */
555 for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
556 j++; /* count OIDs */
557 asprintf (&oid, "%s %s", (oid?oid:""), optarg);
558 if (c == 'E' || c == 'e') {
559 jj++;
560 ii++;
561 }
562 if (c == 'E')
563 eval_method[j+1] |= WARN_PRESENT;
564 else if (c == 'e')
565 eval_method[j+1] |= CRIT_PRESENT;
566 break;
567 case 's': /* string or substring */
568 strncpy (string_value, optarg, sizeof (string_value) - 1);
569 string_value[sizeof (string_value) - 1] = 0;
570 eval_method[jj++] = CRIT_STRING;
571 ii++;
572 break;
573 case 'R': /* regex */
574 #ifdef HAVE_REGEX_H
575 cflags = REG_ICASE;
576 #endif
577 case 'r': /* regex */
578 #ifdef HAVE_REGEX_H
579 cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
580 strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
581 regex_expect[sizeof (regex_expect) - 1] = 0;
582 errcode = regcomp (&preg, regex_expect, cflags);
583 if (errcode != 0) {
584 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
585 printf ("Could Not Compile Regular Expression");
586 return ERROR;
587 }
588 eval_method[jj++] = CRIT_REGEX;
589 ii++;
590 #else
591 printf ("%s UNKNOWN: call for regex which was not a compiled option", label);
592 exit (STATE_UNKNOWN);
593 #endif
594 break;
596 /* Format */
597 case 'd': /* delimiter */
598 delimiter = strscpy (delimiter, optarg);
599 break;
600 case 'D': /* output-delimiter */
601 output_delim = strscpy (output_delim, optarg);
602 break;
603 case 'l': /* label */
604 label = optarg;
605 nlabels++;
606 if (nlabels >= labels_size) {
607 labels_size += 8;
608 labels = realloc (labels, labels_size);
609 if (labels == NULL)
610 terminate (STATE_UNKNOWN,
611 "Could not realloc() labels[%d]", nlabels);
612 }
613 labels[nlabels - 1] = optarg;
614 ptr = thisarg (optarg);
615 if (strstr (ptr, "'") == ptr)
616 labels[nlabels - 1] = ptr + 1;
617 else
618 labels[nlabels - 1] = ptr;
619 while (ptr && (ptr = nextarg (ptr))) {
620 if (nlabels >= labels_size) {
621 labels_size += 8;
622 labels = realloc (labels, labels_size);
623 if (labels == NULL)
624 terminate (STATE_UNKNOWN, "Could not realloc() labels\n");
625 }
626 labels++;
627 ptr = thisarg (ptr);
628 if (strstr (ptr, "'") == ptr)
629 labels[nlabels - 1] = ptr + 1;
630 else
631 labels[nlabels - 1] = ptr;
632 }
633 break;
634 case 'u': /* units */
635 units = optarg;
636 nunits++;
637 if (nunits >= unitv_size) {
638 unitv_size += 8;
639 unitv = realloc (unitv, unitv_size);
640 if (unitv == NULL)
641 terminate (STATE_UNKNOWN,
642 "Could not realloc() units [%d]\n", nunits);
643 }
644 unitv[nunits - 1] = optarg;
645 ptr = thisarg (optarg);
646 if (strstr (ptr, "'") == ptr)
647 unitv[nunits - 1] = ptr + 1;
648 else
649 unitv[nunits - 1] = ptr;
650 while (ptr && (ptr = nextarg (ptr))) {
651 if (nunits >= unitv_size) {
652 unitv_size += 8;
653 unitv = realloc (unitv, unitv_size);
654 if (units == NULL)
655 terminate (STATE_UNKNOWN, "Could not realloc() units\n");
656 }
657 nunits++;
658 ptr = thisarg (ptr);
659 if (strstr (ptr, "'") == ptr)
660 unitv[nunits - 1] = ptr + 1;
661 else
662 unitv[nunits - 1] = ptr;
663 }
664 break;
666 }
667 }
669 if (server_address == NULL)
670 asprintf (&server_address, argv[optind]);
672 return validate_arguments ();
673 }
675 /******************************************************************************
677 @@-
678 <sect3>
679 <title>validate_arguments</title>
681 <para>&PROTO_validate_arguments;</para>
683 <para>Given a database name, this function returns TRUE if the string
684 is a valid PostgreSQL database name, and returns false if it is
685 not.</para>
687 <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
688 characters long and consist of letters, numbers, and underscores. The
689 first character cannot be a number, however.</para>
691 </sect3>
692 -@@
693 ******************************************************************************/
695 int
696 validate_arguments ()
697 {
699 /* Need better checks to verify seclevel and authproto choices */
701 if (seclevel == NULL)
702 asprintf (&seclevel, "noAuthNoPriv");
705 if (authproto == NULL )
706 asprintf(&authproto, DEFAULT_AUTH_PROTOCOL);
710 if (proto == NULL || (strcmp(proto,DEFAULT_PROTOCOL) == 0) ) { /* default protocol version */
711 asprintf(&proto, DEFAULT_PROTOCOL);
712 asprintf(&authpriv, "%s%s", "-c ", community);
713 }
714 else if ( strcmp (proto, "3") == 0 ) { /* snmpv3 args */
715 asprintf(&proto, "%s", "3");
717 if ( (strcmp(seclevel, "noAuthNoPriv") == 0) || seclevel == NULL ) {
718 asprintf(&authpriv, "%s", "-l noAuthNoPriv" );
719 }
720 else if ( strcmp(seclevel, "authNoPriv") == 0 ) {
721 if ( secname == NULL || authpasswd == NULL) {
722 printf ("Missing secname (%s) or authpassword (%s) ! \n",secname, authpasswd );
723 print_usage ();
724 exit (STATE_UNKNOWN);
725 }
726 asprintf(&authpriv, "-l authNoPriv -a %s -u %s -A %s ", authproto, secname, authpasswd);
727 }
728 else if ( strcmp(seclevel, "authPriv") == 0 ) {
729 if ( secname == NULL || authpasswd == NULL || privpasswd == NULL ) {
730 printf ("Missing secname (%s), authpassword (%s), or privpasswd (%s)! \n",secname, authpasswd,privpasswd );
731 print_usage ();
732 exit (STATE_UNKNOWN);
733 }
734 asprintf(&authpriv, "-l authPriv -a %s -u %s -A %s -x DES -X %s ", authproto, secname, authpasswd, privpasswd);
735 }
738 }
739 else {
740 printf ("Invalid SNMP version: %s\n", proto);
741 print_usage ();
742 exit (STATE_UNKNOWN);
743 }
748 return OK;
749 }
753 void
754 print_help (void)
755 {
756 print_revision (PROGNAME, REVISION);
757 printf
758 ("Copyright (c) %s %s <%s>\n\n%s\n",
759 COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
760 print_usage ();
761 printf
762 ("\nOptions:\n" LONGOPTIONS "\n" DESCRIPTION "\n" NOTES "\n",
763 DEFAULT_COMMUNITY, DEFAULT_PORT, DEFAULT_DELIMITER, DEFAULT_TIMEOUT);
764 support ();
765 }
767 void
768 print_usage (void)
769 {
770 printf
771 ("Usage:\n" " %s %s\n"
772 " %s (-h | --help) for detailed help\n"
773 " %s (-V | --version) for version information\n",
774 PROGNAME, OPTIONS, PROGNAME, PROGNAME);
775 }
776 \f
779 char *
780 clarify_message (char *msg)
781 {
782 int i = 0;
783 int foo;
784 char tmpmsg_c[MAX_INPUT_BUFFER];
785 char *tmpmsg = (char *) &tmpmsg_c;
786 tmpmsg = strcpy (tmpmsg, msg);
787 if (!strncmp (tmpmsg, " Hex:", 5)) {
788 tmpmsg = strtok (tmpmsg, ":");
789 while ((tmpmsg = strtok (NULL, " "))) {
790 foo = strtol (tmpmsg, NULL, 16);
791 /* Translate chars that are not the same value in the printers
792 * character set.
793 */
794 switch (foo) {
795 case 208:
796 {
797 foo = 197;
798 break;
799 }
800 case 216:
801 {
802 foo = 196;
803 break;
804 }
805 }
806 msg[i] = foo;
807 i++;
808 }
809 msg[i] = 0;
810 }
811 return (msg);
812 }
815 int
816 check_num (int i)
817 {
818 int result;
819 result = STATE_OK;
820 if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
821 lower_warn_lim[i] > upper_warn_lim[i]) {
822 if (response_value[i] <= lower_warn_lim[i] &&
823 response_value[i] >= upper_warn_lim[i]) {
824 result = STATE_WARNING;
825 }
826 }
827 else if
828 ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
829 (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
830 (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
831 (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
832 (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
833 (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
834 result = STATE_WARNING;
835 }
837 if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
838 lower_warn_lim[i] > upper_warn_lim[i]) {
839 if (response_value[i] <= lower_crit_lim[i] &&
840 response_value[i] >= upper_crit_lim[i]) {
841 result = STATE_CRITICAL;
842 }
843 }
844 else if
845 ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
846 (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
847 (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
848 (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
849 (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
850 (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
851 result = STATE_CRITICAL;
852 }
854 return result;
855 }
858 int
859 lu_getll (unsigned long *ll, char *str)
860 {
861 char tmp[100];
862 if (strchr (str, ':') == NULL)
863 return 0;
864 if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
865 return 0;
866 if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
867 return 1;
868 return 0;
869 }
871 int
872 lu_getul (unsigned long *ul, char *str)
873 {
874 char tmp[100];
875 if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
876 return 1;
877 if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
878 return 1;
879 if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
880 return 1;
881 return 0;
882 }
889 /* trim leading whitespace
890 if there is a leading quote, make sure it balances */
892 char *
893 thisarg (char *str)
894 {
895 str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
896 if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
897 if (strlen (str) == 1 || !strstr (str + 1, "'"))
898 terminate (STATE_UNKNOWN, "Unbalanced quotes\n");
899 }
900 return str;
901 }
904 /* if there's a leading quote, advance to the trailing quote
905 set the trailing quote to '\x0'
906 if the string continues, advance beyond the comma */
908 char *
909 nextarg (char *str)
910 {
911 if (strstr (str, "'") == str) {
912 if (strlen (str) > 1) {
913 str = strstr (str + 1, "'");
914 str[0] = 0;
915 return (++str);
916 }
917 else {
918 str[0] = 0;
919 return NULL;
920 }
921 }
922 if (strstr (str, ",") == str) {
923 if (strlen (str) > 1) {
924 str[0] = 0;
925 return (++str);
926 }
927 else {
928 str[0] = 0;
929 return NULL;
930 }
931 }
932 if ((str = strstr (str, ",")) && strlen (str) > 1) {
933 str[0] = 0;
934 return (++str);
935 }
936 return NULL;
937 }