8ae48cd4fe94cc687f4d090f749f5f54854121f5
1 /******************************************************************************
2 *
3 * Nagios check_dns plugin
4 *
5 * License: GPL
6 * Copyright (c) 1999-2006 nagios-plugins team
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
11 *
12 * This file contains the check_dns plugin
13 *
14 * LIMITATION: nslookup on Solaris 7 can return output over 2 lines, which will not
15 * be picked up by this plugin
16 *
17 * License Information:
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 *
33 *
34 * $Id$
35 *
36 ******************************************************************************/
38 const char *progname = "check_dns";
39 const char *revision = "$Revision$";
40 const char *copyright = "2000-2006";
41 const char *email = "nagiosplug-devel@lists.sourceforge.net";
43 #include "common.h"
44 #include "utils.h"
45 #include "utils_base.h"
46 #include "netutils.h"
47 #include "runcmd.h"
49 int process_arguments (int, char **);
50 int validate_arguments (void);
51 int error_scan (char *);
52 void print_help (void);
53 void print_usage (void);
55 #define ADDRESS_LENGTH 256
56 char query_address[ADDRESS_LENGTH] = "";
57 char dns_server[ADDRESS_LENGTH] = "";
58 char ptr_server[ADDRESS_LENGTH] = "";
59 int verbose = FALSE;
60 char expected_address[ADDRESS_LENGTH] = "";
61 int match_expected_address = FALSE;
62 int expect_authority = FALSE;
63 thresholds *time_thresholds = NULL;
65 static int
66 qstrcmp(const void *p1, const void *p2)
67 {
68 /* The actual arguments to this function are "pointers to
69 pointers to char", but strcmp() arguments are "pointers
70 to char", hence the following cast plus dereference */
71 return strcmp(* (char * const *) p1, * (char * const *) p2);
72 }
75 int
76 main (int argc, char **argv)
77 {
78 char *command_line = NULL;
79 char input_buffer[MAX_INPUT_BUFFER];
80 char *address = NULL;
81 char **addresses = NULL;
82 int n_addresses = 0;
83 char *msg = NULL;
84 char *temp_buffer = NULL;
85 int non_authoritative = FALSE;
86 int result = STATE_UNKNOWN;
87 double elapsed_time;
88 long microsec;
89 struct timeval tv;
90 int multi_address;
91 int parse_address = FALSE; /* This flag scans for Address: but only after Name: */
92 output chld_out, chld_err;
93 size_t i;
95 setlocale (LC_ALL, "");
96 bindtextdomain (PACKAGE, LOCALEDIR);
97 textdomain (PACKAGE);
99 /* Set signal handling and alarm */
100 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
101 usage_va(_("Cannot catch SIGALRM"));
102 }
104 if (process_arguments (argc, argv) == ERROR) {
105 usage_va(_("Could not parse arguments"));
106 }
108 /* get the command to run */
109 asprintf (&command_line, "%s %s %s", NSLOOKUP_COMMAND, query_address, dns_server);
111 alarm (timeout_interval);
112 gettimeofday (&tv, NULL);
114 if (verbose)
115 printf ("%s\n", command_line);
117 /* run the command */
118 if((np_runcmd(command_line, &chld_out, &chld_err, 0)) != 0) {
119 msg = (char *)_("nslookup returned an error status");
120 result = STATE_WARNING;
121 }
123 /* scan stdout */
124 for(i = 0; i < chld_out.lines; i++) {
125 if (verbose)
126 puts(chld_out.line[i]);
128 if (strstr (chld_out.line[i], ".in-addr.arpa")) {
129 if ((temp_buffer = strstr (chld_out.line[i], "name = ")))
130 address = strdup (temp_buffer + 7);
131 else {
132 msg = (char *)_("Warning plugin error");
133 result = STATE_WARNING;
134 }
135 }
137 /* the server is responding, we just got the host name... */
138 if (strstr (chld_out.line[i], "Name:"))
139 parse_address = TRUE;
140 else if (parse_address == TRUE && (strstr (chld_out.line[i], "Address:") ||
141 strstr (chld_out.line[i], "Addresses:"))) {
142 temp_buffer = index (chld_out.line[i], ':');
143 temp_buffer++;
145 /* Strip leading spaces */
146 for (; *temp_buffer != '\0' && *temp_buffer == ' '; temp_buffer++)
147 /* NOOP */;
149 strip(temp_buffer);
150 if (temp_buffer==NULL || strlen(temp_buffer)==0) {
151 die (STATE_CRITICAL,
152 _("DNS CRITICAL - '%s' returned empty host name string\n"),
153 NSLOOKUP_COMMAND);
154 }
156 if (addresses == NULL)
157 addresses = malloc(sizeof(*addresses)*10);
158 else if (!(n_addresses % 10))
159 addresses = realloc(addresses,sizeof(*addresses) * (n_addresses + 10));
160 addresses[n_addresses++] = strdup(temp_buffer);
161 }
162 else if (strstr (chld_out.line[i], _("Non-authoritative answer:"))) {
163 non_authoritative = TRUE;
164 }
167 result = error_scan (chld_out.line[i]);
168 if (result != STATE_OK) {
169 msg = strchr (chld_out.line[i], ':');
170 if(msg) msg++;
171 break;
172 }
173 }
175 /* scan stderr */
176 for(i = 0; i < chld_err.lines; i++) {
177 if (verbose)
178 puts(chld_err.line[i]);
180 if (error_scan (chld_err.line[i]) != STATE_OK) {
181 result = max_state (result, error_scan (chld_err.line[i]));
182 msg = strchr(input_buffer, ':');
183 if(msg) msg++;
184 }
185 }
187 if (addresses) {
188 int i,slen;
189 char *adrp;
190 qsort(addresses, n_addresses, sizeof(*addresses), qstrcmp);
191 for(i=0, slen=1; i < n_addresses; i++) {
192 slen += strlen(addresses[i])+1;
193 }
194 adrp = address = malloc(slen);
195 for(i=0; i < n_addresses; i++) {
196 if (i) *adrp++ = ',';
197 strcpy(adrp, addresses[i]);
198 adrp += strlen(addresses[i]);
199 }
200 *adrp = 0;
201 } else
202 die (STATE_CRITICAL,
203 _("DNS CRITICAL - '%s' msg parsing exited with no address\n"),
204 NSLOOKUP_COMMAND);
206 /* compare to expected address */
207 if (result == STATE_OK && match_expected_address && strcmp(address, expected_address)) {
208 result = STATE_CRITICAL;
209 asprintf(&msg, _("expected '%s' but got '%s'"), expected_address, address);
210 }
212 /* check if authoritative */
213 if (result == STATE_OK && expect_authority && non_authoritative) {
214 result = STATE_CRITICAL;
215 asprintf(&msg, _("server %s is not authoritative for %s"), dns_server, query_address);
216 }
218 microsec = deltime (tv);
219 elapsed_time = (double)microsec / 1.0e6;
221 if (result == STATE_OK) {
222 if (strchr (address, ',') == NULL)
223 multi_address = FALSE;
224 else
225 multi_address = TRUE;
227 result = get_status(elapsed_time, time_thresholds);
228 if (result == STATE_OK) {
229 printf ("DNS %s: ", _("OK"));
230 } else if (result == STATE_WARNING) {
231 printf ("DNS %s: ", _("WARNING"));
232 } else if (result == STATE_CRITICAL) {
233 printf ("DNS %s: ", _("CRITICAL"));
234 }
235 printf (ngettext("%.3f second response time", "%.3f seconds response time", elapsed_time), elapsed_time);
236 printf (_(". %s returns %s"), query_address, address);
237 printf ("|%s\n", fperfdata ("time", elapsed_time, "s", FALSE, 0, FALSE, 0, TRUE, 0, FALSE, 0));
238 }
239 else if (result == STATE_WARNING)
240 printf (_("DNS WARNING - %s\n"),
241 !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
242 else if (result == STATE_CRITICAL)
243 printf (_("DNS CRITICAL - %s\n"),
244 !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
245 else
246 printf (_("DNS UNKNOW - %s\n"),
247 !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
249 return result;
250 }
254 int
255 error_scan (char *input_buffer)
256 {
258 /* the DNS lookup timed out */
259 if (strstr (input_buffer, _("Note: nslookup is deprecated and may be removed from future releases.")) ||
260 strstr (input_buffer, _("Consider using the `dig' or `host' programs instead. Run nslookup with")) ||
261 strstr (input_buffer, _("the `-sil[ent]' option to prevent this message from appearing.")))
262 return STATE_OK;
264 /* DNS server is not running... */
265 else if (strstr (input_buffer, "No response from server"))
266 die (STATE_CRITICAL, _("No response from DNS %s\n"), dns_server);
268 /* Host name is valid, but server doesn't have records... */
269 else if (strstr (input_buffer, "No records"))
270 die (STATE_CRITICAL, _("DNS %s has no records\n"), dns_server);
272 /* Connection was refused */
273 else if (strstr (input_buffer, "Connection refused") ||
274 strstr (input_buffer, "Couldn't find server") ||
275 strstr (input_buffer, "Refused") ||
276 (strstr (input_buffer, "** server can't find") &&
277 strstr (input_buffer, ": REFUSED")))
278 die (STATE_CRITICAL, _("Connection to DNS %s was refused\n"), dns_server);
280 /* Query refused (usually by an ACL in the namserver) */
281 else if (strstr (input_buffer, "Query refused"))
282 die (STATE_CRITICAL, _("Query was refused by DNS server at %s\n"), dns_server);
284 /* No information (e.g. nameserver IP has two PTR records) */
285 else if (strstr (input_buffer, "No information"))
286 die (STATE_CRITICAL, _("No information returned by DNS server at %s\n"), dns_server);
288 /* Host or domain name does not exist */
289 else if (strstr (input_buffer, "Non-existent") ||
290 strstr (input_buffer, "** server can't find") ||
291 strstr (input_buffer,"NXDOMAIN"))
292 die (STATE_CRITICAL, _("Domain %s was not found by the server\n"), query_address);
294 /* Network is unreachable */
295 else if (strstr (input_buffer, "Network is unreachable"))
296 die (STATE_CRITICAL, _("Network is unreachable\n"));
298 /* Internal server failure */
299 else if (strstr (input_buffer, "Server failure"))
300 die (STATE_CRITICAL, _("DNS failure for %s\n"), dns_server);
302 /* Request error or the DNS lookup timed out */
303 else if (strstr (input_buffer, "Format error") ||
304 strstr (input_buffer, "Timed out"))
305 return STATE_WARNING;
307 return STATE_OK;
309 }
312 /* process command-line arguments */
313 int
314 process_arguments (int argc, char **argv)
315 {
316 int c;
317 char *warning = NULL;
318 char *critical = NULL;
320 int opt_index = 0;
321 static struct option long_opts[] = {
322 {"help", no_argument, 0, 'h'},
323 {"version", no_argument, 0, 'V'},
324 {"verbose", no_argument, 0, 'v'},
325 {"timeout", required_argument, 0, 't'},
326 {"hostname", required_argument, 0, 'H'},
327 {"server", required_argument, 0, 's'},
328 {"reverse-server", required_argument, 0, 'r'},
329 {"expected-address", required_argument, 0, 'a'},
330 {"expect-authority", no_argument, 0, 'A'},
331 {"warning", no_argument, 0, 'w'},
332 {"critical", no_argument, 0, 'c'},
333 {0, 0, 0, 0}
334 };
336 if (argc < 2)
337 return ERROR;
339 for (c = 1; c < argc; c++)
340 if (strcmp ("-to", argv[c]) == 0)
341 strcpy (argv[c], "-t");
343 while (1) {
344 c = getopt_long (argc, argv, "hVvAt:H:s:r:a:w:c:", long_opts, &opt_index);
346 if (c == -1 || c == EOF)
347 break;
349 switch (c) {
350 case 'h': /* help */
351 print_help ();
352 exit (STATE_OK);
353 case 'V': /* version */
354 print_revision (progname, revision);
355 exit (STATE_OK);
356 case 'v': /* version */
357 verbose = TRUE;
358 break;
359 case 't': /* timeout period */
360 timeout_interval = atoi (optarg);
361 break;
362 case 'H': /* hostname */
363 if (strlen (optarg) >= ADDRESS_LENGTH)
364 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
365 strcpy (query_address, optarg);
366 break;
367 case 's': /* server name */
368 /* TODO: this host_or_die check is probably unnecessary.
369 * Better to confirm nslookup response matches */
370 host_or_die(optarg);
371 if (strlen (optarg) >= ADDRESS_LENGTH)
372 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
373 strcpy (dns_server, optarg);
374 break;
375 case 'r': /* reverse server name */
376 /* TODO: Is this host_or_die necessary? */
377 host_or_die(optarg);
378 if (strlen (optarg) >= ADDRESS_LENGTH)
379 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
380 strcpy (ptr_server, optarg);
381 break;
382 case 'a': /* expected address */
383 if (strlen (optarg) >= ADDRESS_LENGTH)
384 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
385 strcpy (expected_address, optarg);
386 match_expected_address = TRUE;
387 break;
388 case 'A': /* expect authority */
389 expect_authority = TRUE;
390 break;
391 case 'w':
392 warning = optarg;
393 break;
394 case 'c':
395 critical = optarg;
396 break;
397 default: /* args not parsable */
398 usage5();
399 }
400 }
402 c = optind;
403 if (strlen(query_address)==0 && c<argc) {
404 if (strlen(argv[c])>=ADDRESS_LENGTH)
405 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
406 strcpy (query_address, argv[c++]);
407 }
409 if (strlen(dns_server)==0 && c<argc) {
410 /* TODO: See -s option */
411 host_or_die(argv[c]);
412 if (strlen(argv[c]) >= ADDRESS_LENGTH)
413 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
414 strcpy (dns_server, argv[c++]);
415 }
417 set_thresholds(&time_thresholds, warning, critical);
419 return validate_arguments ();
420 }
423 int
424 validate_arguments ()
425 {
426 if (query_address[0] == 0)
427 return ERROR;
429 return OK;
430 }
433 void
434 print_help (void)
435 {
436 print_revision (progname, revision);
438 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
439 printf (COPYRIGHT, copyright, email);
441 printf ("%s\n", _("This plugin uses the nslookup program to obtain the IP address for the given host/domain query."));
442 printf ("%s\n", _("An optional DNS server to use may be specified."));
443 printf ("%s\n", _("If no DNS server is specified, the default server(s) specified in /etc/resolv.conf will be used."));
445 printf ("\n\n");
447 print_usage ();
449 printf (_(UT_HELP_VRSN));
451 printf (" -H, --hostname=HOST\n");
452 printf (" %s\n", _("The name or address you want to query"));
453 printf (" -s, --server=HOST\n");
454 printf (" %s\n", _("Optional DNS server you want to use for the lookup"));
455 printf (" -a, --expected-address=IP-ADDRESS|HOST\n");
456 printf (" %s\n", _("Optional IP-ADDRESS you expect the DNS server to return. HOST must end with ."));
457 printf (" %s\n", _("Multiple addresses can be separated with commas, and need to be sorted."));
458 printf (" -A, --expect-authority\n");
459 printf (" %s\n", _("Optionally expect the DNS server to be authoritative for the lookup"));
460 printf (" -w, --warning=seconds\n");
461 printf (" %s\n", _("Return warning if elapsed time exceeds value. Default off"));
462 printf (" -c, --critical=seconds\n");
463 printf (" %s\n", _("Return critical if elapsed time exceeds value. Default off"));
465 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
466 printf (_(UT_SUPPORT));
467 }
470 void
471 print_usage (void)
472 {
473 printf (_("Usage:"));
474 printf ("%s -H host [-s server] [-a expected-address] [-A] [-t timeout] [-w warn] [-c crit]\n", progname);
475 }