454f813b30c0bda831874013a4fb30322468abd7
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 int
66 main (int argc, char **argv)
67 {
68 char *command_line = NULL;
69 char input_buffer[MAX_INPUT_BUFFER];
70 char *address = NULL;
71 char *msg = NULL;
72 char *temp_buffer = NULL;
73 int non_authoritative = FALSE;
74 int result = STATE_UNKNOWN;
75 double elapsed_time;
76 long microsec;
77 struct timeval tv;
78 int multi_address;
79 int parse_address = FALSE; /* This flag scans for Address: but only after Name: */
80 output chld_out, chld_err;
81 size_t i;
83 setlocale (LC_ALL, "");
84 bindtextdomain (PACKAGE, LOCALEDIR);
85 textdomain (PACKAGE);
87 /* Set signal handling and alarm */
88 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
89 usage_va(_("Cannot catch SIGALRM"));
90 }
92 if (process_arguments (argc, argv) == ERROR) {
93 usage_va(_("Could not parse arguments"));
94 }
96 /* get the command to run */
97 asprintf (&command_line, "%s %s %s", NSLOOKUP_COMMAND, query_address, dns_server);
99 alarm (timeout_interval);
100 gettimeofday (&tv, NULL);
102 if (verbose)
103 printf ("%s\n", command_line);
105 /* run the command */
106 if((np_runcmd(command_line, &chld_out, &chld_err, 0)) != 0) {
107 msg = (char *)_("nslookup returned an error status");
108 result = STATE_WARNING;
109 }
111 /* scan stdout */
112 for(i = 0; i < chld_out.lines; i++) {
113 if (verbose)
114 puts(chld_out.line[i]);
116 if (strstr (chld_out.line[i], ".in-addr.arpa")) {
117 if ((temp_buffer = strstr (chld_out.line[i], "name = ")))
118 address = strdup (temp_buffer + 7);
119 else {
120 msg = (char *)_("Warning plugin error");
121 result = STATE_WARNING;
122 }
123 }
125 /* the server is responding, we just got the host name... */
126 if (strstr (chld_out.line[i], "Name:"))
127 parse_address = TRUE;
128 else if (parse_address == TRUE && (strstr (chld_out.line[i], "Address:") ||
129 strstr (chld_out.line[i], "Addresses:"))) {
130 temp_buffer = index (chld_out.line[i], ':');
131 temp_buffer++;
133 /* Strip leading spaces */
134 for (; *temp_buffer != '\0' && *temp_buffer == ' '; temp_buffer++)
135 /* NOOP */;
137 strip(temp_buffer);
138 if (temp_buffer==NULL || strlen(temp_buffer)==0) {
139 die (STATE_CRITICAL,
140 _("DNS CRITICAL - '%s' returned empty host name string\n"),
141 NSLOOKUP_COMMAND);
142 }
144 if (address == NULL)
145 address = strdup (temp_buffer);
146 else
147 asprintf(&address, "%s,%s", address, temp_buffer);
148 }
150 else if (strstr (chld_out.line[i], _("Non-authoritative answer:"))) {
151 non_authoritative = TRUE;
152 }
154 result = error_scan (chld_out.line[i]);
155 if (result != STATE_OK) {
156 msg = strchr (chld_out.line[i], ':');
157 if(msg) msg++;
158 break;
159 }
160 }
162 /* scan stderr */
163 for(i = 0; i < chld_err.lines; i++) {
164 if (verbose)
165 puts(chld_err.line[i]);
167 if (error_scan (chld_err.line[i]) != STATE_OK) {
168 result = max_state (result, error_scan (chld_err.line[i]));
169 msg = strchr(input_buffer, ':');
170 if(msg) msg++;
171 }
172 }
174 /* If we got here, we should have an address string,
175 * and we can segfault if we do not */
176 if (address==NULL || strlen(address)==0)
177 die (STATE_CRITICAL,
178 _("DNS CRITICAL - '%s' msg parsing exited with no address\n"),
179 NSLOOKUP_COMMAND);
181 /* compare to expected address */
182 if (result == STATE_OK && match_expected_address && strcmp(address, expected_address)) {
183 result = STATE_CRITICAL;
184 asprintf(&msg, _("expected '%s' but got '%s'"), expected_address, address);
185 }
187 /* check if authoritative */
188 if (result == STATE_OK && expect_authority && non_authoritative) {
189 result = STATE_CRITICAL;
190 asprintf(&msg, _("server %s is not authoritative for %s"), dns_server, query_address);
191 }
193 microsec = deltime (tv);
194 elapsed_time = (double)microsec / 1.0e6;
196 if (result == STATE_OK) {
197 if (strchr (address, ',') == NULL)
198 multi_address = FALSE;
199 else
200 multi_address = TRUE;
202 result = get_status(elapsed_time, time_thresholds);
203 if (result == STATE_OK) {
204 printf ("DNS %s: ", _("OK"));
205 } else if (result == STATE_WARNING) {
206 printf ("DNS %s: ", _("WARNING"));
207 } else if (result == STATE_CRITICAL) {
208 printf ("DNS %s: ", _("CRITICAL"));
209 }
210 printf (ngettext("%.3f second response time", "%.3f seconds response time", elapsed_time), elapsed_time);
211 printf (_(". %s returns %s"), query_address, address);
212 printf ("|%s\n", fperfdata ("time", elapsed_time, "s", FALSE, 0, FALSE, 0, TRUE, 0, FALSE, 0));
213 }
214 else if (result == STATE_WARNING)
215 printf (_("DNS WARNING - %s\n"),
216 !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
217 else if (result == STATE_CRITICAL)
218 printf (_("DNS CRITICAL - %s\n"),
219 !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
220 else
221 printf (_("DNS UNKNOW - %s\n"),
222 !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
224 return result;
225 }
229 int
230 error_scan (char *input_buffer)
231 {
233 /* the DNS lookup timed out */
234 if (strstr (input_buffer, _("Note: nslookup is deprecated and may be removed from future releases.")) ||
235 strstr (input_buffer, _("Consider using the `dig' or `host' programs instead. Run nslookup with")) ||
236 strstr (input_buffer, _("the `-sil[ent]' option to prevent this message from appearing.")))
237 return STATE_OK;
239 /* DNS server is not running... */
240 else if (strstr (input_buffer, "No response from server"))
241 die (STATE_CRITICAL, _("No response from DNS %s\n"), dns_server);
243 /* Host name is valid, but server doesn't have records... */
244 else if (strstr (input_buffer, "No records"))
245 die (STATE_CRITICAL, _("DNS %s has no records\n"), dns_server);
247 /* Connection was refused */
248 else if (strstr (input_buffer, "Connection refused") ||
249 strstr (input_buffer, "Couldn't find server") ||
250 strstr (input_buffer, "Refused") ||
251 (strstr (input_buffer, "** server can't find") &&
252 strstr (input_buffer, ": REFUSED")))
253 die (STATE_CRITICAL, _("Connection to DNS %s was refused\n"), dns_server);
255 /* Query refused (usually by an ACL in the namserver) */
256 else if (strstr (input_buffer, "Query refused"))
257 die (STATE_CRITICAL, _("Query was refused by DNS server at %s\n"), dns_server);
259 /* No information (e.g. nameserver IP has two PTR records) */
260 else if (strstr (input_buffer, "No information"))
261 die (STATE_CRITICAL, _("No information returned by DNS server at %s\n"), dns_server);
263 /* Host or domain name does not exist */
264 else if (strstr (input_buffer, "Non-existent") ||
265 strstr (input_buffer, "** server can't find") ||
266 strstr (input_buffer,"NXDOMAIN"))
267 die (STATE_CRITICAL, _("Domain %s was not found by the server\n"), query_address);
269 /* Network is unreachable */
270 else if (strstr (input_buffer, "Network is unreachable"))
271 die (STATE_CRITICAL, _("Network is unreachable\n"));
273 /* Internal server failure */
274 else if (strstr (input_buffer, "Server failure"))
275 die (STATE_CRITICAL, _("DNS failure for %s\n"), dns_server);
277 /* Request error or the DNS lookup timed out */
278 else if (strstr (input_buffer, "Format error") ||
279 strstr (input_buffer, "Timed out"))
280 return STATE_WARNING;
282 return STATE_OK;
284 }
287 /* process command-line arguments */
288 int
289 process_arguments (int argc, char **argv)
290 {
291 int c;
292 char *warning = NULL;
293 char *critical = NULL;
295 int opt_index = 0;
296 static struct option long_opts[] = {
297 {"help", no_argument, 0, 'h'},
298 {"version", no_argument, 0, 'V'},
299 {"verbose", no_argument, 0, 'v'},
300 {"timeout", required_argument, 0, 't'},
301 {"hostname", required_argument, 0, 'H'},
302 {"server", required_argument, 0, 's'},
303 {"reverse-server", required_argument, 0, 'r'},
304 {"expected-address", required_argument, 0, 'a'},
305 {"expect-authority", no_argument, 0, 'A'},
306 {"warning", no_argument, 0, 'w'},
307 {"critical", no_argument, 0, 'c'},
308 {0, 0, 0, 0}
309 };
311 if (argc < 2)
312 return ERROR;
314 for (c = 1; c < argc; c++)
315 if (strcmp ("-to", argv[c]) == 0)
316 strcpy (argv[c], "-t");
318 while (1) {
319 c = getopt_long (argc, argv, "hVvAt:H:s:r:a:w:c:", long_opts, &opt_index);
321 if (c == -1 || c == EOF)
322 break;
324 switch (c) {
325 case 'h': /* help */
326 print_help ();
327 exit (STATE_OK);
328 case 'V': /* version */
329 print_revision (progname, revision);
330 exit (STATE_OK);
331 case 'v': /* version */
332 verbose = TRUE;
333 break;
334 case 't': /* timeout period */
335 timeout_interval = atoi (optarg);
336 break;
337 case 'H': /* hostname */
338 if (strlen (optarg) >= ADDRESS_LENGTH)
339 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
340 strcpy (query_address, optarg);
341 break;
342 case 's': /* server name */
343 /* TODO: this host_or_die check is probably unnecessary.
344 * Better to confirm nslookup response matches */
345 host_or_die(optarg);
346 if (strlen (optarg) >= ADDRESS_LENGTH)
347 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
348 strcpy (dns_server, optarg);
349 break;
350 case 'r': /* reverse server name */
351 /* TODO: Is this host_or_die necessary? */
352 host_or_die(optarg);
353 if (strlen (optarg) >= ADDRESS_LENGTH)
354 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
355 strcpy (ptr_server, optarg);
356 break;
357 case 'a': /* expected address */
358 if (strlen (optarg) >= ADDRESS_LENGTH)
359 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
360 strcpy (expected_address, optarg);
361 match_expected_address = TRUE;
362 break;
363 case 'A': /* expect authority */
364 expect_authority = TRUE;
365 break;
366 case 'w':
367 warning = optarg;
368 break;
369 case 'c':
370 critical = optarg;
371 break;
372 default: /* args not parsable */
373 usage5();
374 }
375 }
377 c = optind;
378 if (strlen(query_address)==0 && c<argc) {
379 if (strlen(argv[c])>=ADDRESS_LENGTH)
380 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
381 strcpy (query_address, argv[c++]);
382 }
384 if (strlen(dns_server)==0 && c<argc) {
385 /* TODO: See -s option */
386 host_or_die(argv[c]);
387 if (strlen(argv[c]) >= ADDRESS_LENGTH)
388 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
389 strcpy (dns_server, argv[c++]);
390 }
392 set_thresholds(&time_thresholds, warning, critical);
394 return validate_arguments ();
395 }
398 int
399 validate_arguments ()
400 {
401 if (query_address[0] == 0)
402 return ERROR;
404 return OK;
405 }
408 void
409 print_help (void)
410 {
411 print_revision (progname, revision);
413 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
414 printf (COPYRIGHT, copyright, email);
416 printf ("%s\n", _("This plugin uses the nslookup program to obtain the IP address for the given host/domain query."));
417 printf ("%s\n", _("An optional DNS server to use may be specified."));
418 printf ("%s\n", _("If no DNS server is specified, the default server(s) specified in /etc/resolv.conf will be used."));
420 printf ("\n\n");
422 print_usage ();
424 printf (_(UT_HELP_VRSN));
426 printf (" -H, --hostname=HOST\n");
427 printf (" %s\n", _("The name or address you want to query"));
428 printf (" -s, --server=HOST\n");
429 printf (" %s\n", _("Optional DNS server you want to use for the lookup"));
430 printf (" -a, --expected-address=IP-ADDRESS|HOST\n");
431 printf (" %s\n", _("Optional IP-ADDRESS you expect the DNS server to return. HOST must end with ."));
432 printf (" -A, --expect-authority\n");
433 printf (" %s\n", _("Optionally expect the DNS server to be authoritative for the lookup"));
434 printf (" -w, --warning=seconds\n");
435 printf (" %s\n", _("Return warning if elapsed time exceeds value. Default off"));
436 printf (" -c, --critical=seconds\n");
437 printf (" %s\n", _("Return critical if elapsed time exceeds value. Default off"));
439 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
440 printf (_(UT_SUPPORT));
441 }
444 void
445 print_usage (void)
446 {
447 printf (_("Usage:"));
448 printf ("%s -H host [-s server] [-a expected-address] [-A] [-t timeout] [-w warn] [-c crit]\n", progname);
449 }