1 /******************************************************************************
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 LIMITATION: nslookup on Solaris 7 can return output over 2 lines, which will not
18 be picked up by this plugin
20 $Id$
22 ******************************************************************************/
24 const char *progname = "check_dns";
25 const char *revision = "$Revision$";
26 const char *copyright = "2000-2005";
27 const char *email = "nagiosplug-devel@lists.sourceforge.net";
29 #include "common.h"
30 #include "utils.h"
31 #include "netutils.h"
32 #include "runcmd.h"
34 int process_arguments (int, char **);
35 int validate_arguments (void);
36 int error_scan (char *);
37 void print_help (void);
38 void print_usage (void);
40 #define ADDRESS_LENGTH 256
41 char query_address[ADDRESS_LENGTH] = "";
42 char dns_server[ADDRESS_LENGTH] = "";
43 char ptr_server[ADDRESS_LENGTH] = "";
44 int verbose = FALSE;
45 char expected_address[ADDRESS_LENGTH] = "";
46 int match_expected_address = FALSE;
47 int expect_authority = FALSE;
48 thresholds *time_thresholds = NULL;
50 int
51 main (int argc, char **argv)
52 {
53 char *command_line = NULL;
54 char input_buffer[MAX_INPUT_BUFFER];
55 char *address = NULL;
56 char *msg = NULL;
57 char *temp_buffer = NULL;
58 int non_authoritative = FALSE;
59 int result = STATE_UNKNOWN;
60 double elapsed_time;
61 long microsec;
62 struct timeval tv;
63 int multi_address;
64 int parse_address = FALSE; /* This flag scans for Address: but only after Name: */
65 output chld_out, chld_err;
66 size_t i;
68 setlocale (LC_ALL, "");
69 bindtextdomain (PACKAGE, LOCALEDIR);
70 textdomain (PACKAGE);
72 /* Set signal handling and alarm */
73 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
74 usage_va(_("Cannot catch SIGALRM"));
75 }
77 if (process_arguments (argc, argv) == ERROR) {
78 usage_va(_("Could not parse arguments"));
79 }
81 /* get the command to run */
82 asprintf (&command_line, "%s %s %s", NSLOOKUP_COMMAND, query_address, dns_server);
84 alarm (timeout_interval);
85 gettimeofday (&tv, NULL);
87 if (verbose)
88 printf ("%s\n", command_line);
90 /* run the command */
91 if((np_runcmd(command_line, &chld_out, &chld_err, 0)) != 0) {
92 msg = (char *)_("nslookup returned an error status");
93 result = STATE_WARNING;
94 }
96 /* scan stdout */
97 for(i = 0; i < chld_out.lines; i++) {
98 if (verbose)
99 puts(chld_out.line[i]);
101 if (strstr (chld_out.line[i], ".in-addr.arpa")) {
102 if ((temp_buffer = strstr (chld_out.line[i], "name = ")))
103 address = strdup (temp_buffer + 7);
104 else {
105 msg = (char *)_("Warning plugin error");
106 result = STATE_WARNING;
107 }
108 }
110 /* the server is responding, we just got the host name... */
111 if (strstr (chld_out.line[i], "Name:"))
112 parse_address = TRUE;
113 else if (parse_address == TRUE && (strstr (chld_out.line[i], "Address:") ||
114 strstr (chld_out.line[i], "Addresses:"))) {
115 temp_buffer = index (chld_out.line[i], ':');
116 temp_buffer++;
118 /* Strip leading spaces */
119 for (; *temp_buffer != '\0' && *temp_buffer == ' '; temp_buffer++)
120 /* NOOP */;
122 strip(temp_buffer);
123 if (temp_buffer==NULL || strlen(temp_buffer)==0) {
124 die (STATE_CRITICAL,
125 _("DNS CRITICAL - '%s' returned empty host name string\n"),
126 NSLOOKUP_COMMAND);
127 }
129 if (address == NULL)
130 address = strdup (temp_buffer);
131 else
132 asprintf(&address, "%s,%s", address, temp_buffer);
133 }
135 else if (strstr (chld_out.line[i], _("Non-authoritative answer:"))) {
136 non_authoritative = TRUE;
137 }
139 result = error_scan (chld_out.line[i]);
140 if (result != STATE_OK) {
141 msg = strchr (chld_out.line[i], ':');
142 if(msg) msg++;
143 break;
144 }
145 }
147 /* scan stderr */
148 for(i = 0; i < chld_err.lines; i++) {
149 if (verbose)
150 puts(chld_err.line[i]);
152 if (error_scan (chld_err.line[i]) != STATE_OK) {
153 result = max_state (result, error_scan (chld_err.line[i]));
154 msg = strchr(input_buffer, ':');
155 if(msg) msg++;
156 }
157 }
159 /* If we got here, we should have an address string,
160 * and we can segfault if we do not */
161 if (address==NULL || strlen(address)==0)
162 die (STATE_CRITICAL,
163 _("DNS CRITICAL - '%s' msg parsing exited with no address\n"),
164 NSLOOKUP_COMMAND);
166 /* compare to expected address */
167 if (result == STATE_OK && match_expected_address && strcmp(address, expected_address)) {
168 result = STATE_CRITICAL;
169 asprintf(&msg, _("expected '%s' but got '%s'"), expected_address, address);
170 }
172 /* check if authoritative */
173 if (result == STATE_OK && expect_authority && non_authoritative) {
174 result = STATE_CRITICAL;
175 asprintf(&msg, _("server %s is not authoritative for %s"), dns_server, query_address);
176 }
178 microsec = deltime (tv);
179 elapsed_time = (double)microsec / 1.0e6;
181 if (result == STATE_OK) {
182 if (strchr (address, ',') == NULL)
183 multi_address = FALSE;
184 else
185 multi_address = TRUE;
187 result = get_status(elapsed_time, time_thresholds);
188 if (result == STATE_OK) {
189 printf ("DNS %s: ", _("OK"));
190 } else if (result == STATE_WARNING) {
191 printf ("DNS %s: ", _("WARNING"));
192 } else if (result == STATE_CRITICAL) {
193 printf ("DNS %s: ", _("CRITICAL"));
194 }
195 printf (ngettext("%.3f second response time", "%.3f seconds response time", elapsed_time), elapsed_time);
196 printf (_(". %s returns %s"), query_address, address);
197 printf ("|%s\n", fperfdata ("time", elapsed_time, "s", FALSE, 0, FALSE, 0, TRUE, 0, FALSE, 0));
198 }
199 else if (result == STATE_WARNING)
200 printf (_("DNS WARNING - %s\n"),
201 !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
202 else if (result == STATE_CRITICAL)
203 printf (_("DNS CRITICAL - %s\n"),
204 !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
205 else
206 printf (_("DNS UNKNOW - %s\n"),
207 !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
209 return result;
210 }
214 int
215 error_scan (char *input_buffer)
216 {
218 /* the DNS lookup timed out */
219 if (strstr (input_buffer, _("Note: nslookup is deprecated and may be removed from future releases.")) ||
220 strstr (input_buffer, _("Consider using the `dig' or `host' programs instead. Run nslookup with")) ||
221 strstr (input_buffer, _("the `-sil[ent]' option to prevent this message from appearing.")))
222 return STATE_OK;
224 /* DNS server is not running... */
225 else if (strstr (input_buffer, "No response from server"))
226 die (STATE_CRITICAL, _("No response from DNS %s\n"), dns_server);
228 /* Host name is valid, but server doesn't have records... */
229 else if (strstr (input_buffer, "No records"))
230 die (STATE_CRITICAL, _("DNS %s has no records\n"), dns_server);
232 /* Connection was refused */
233 else if (strstr (input_buffer, "Connection refused") ||
234 strstr (input_buffer, "Couldn't find server") ||
235 strstr (input_buffer, "Refused") ||
236 (strstr (input_buffer, "** server can't find") &&
237 strstr (input_buffer, ": REFUSED")))
238 die (STATE_CRITICAL, _("Connection to DNS %s was refused\n"), dns_server);
240 /* Query refused (usually by an ACL in the namserver) */
241 else if (strstr (input_buffer, "Query refused"))
242 die (STATE_CRITICAL, _("Query was refused by DNS server at %s\n"), dns_server);
244 /* No information (e.g. nameserver IP has two PTR records) */
245 else if (strstr (input_buffer, "No information"))
246 die (STATE_CRITICAL, _("No information returned by DNS server at %s\n"), dns_server);
248 /* Host or domain name does not exist */
249 else if (strstr (input_buffer, "Non-existent") ||
250 strstr (input_buffer, "** server can't find") ||
251 strstr (input_buffer,"NXDOMAIN"))
252 die (STATE_CRITICAL, _("Domain %s was not found by the server\n"), query_address);
254 /* Network is unreachable */
255 else if (strstr (input_buffer, "Network is unreachable"))
256 die (STATE_CRITICAL, _("Network is unreachable\n"));
258 /* Internal server failure */
259 else if (strstr (input_buffer, "Server failure"))
260 die (STATE_CRITICAL, _("DNS failure for %s\n"), dns_server);
262 /* Request error or the DNS lookup timed out */
263 else if (strstr (input_buffer, "Format error") ||
264 strstr (input_buffer, "Timed out"))
265 return STATE_WARNING;
267 return STATE_OK;
269 }
272 /* process command-line arguments */
273 int
274 process_arguments (int argc, char **argv)
275 {
276 int c;
277 char *warning = NULL;
278 char *critical = NULL;
280 int opt_index = 0;
281 static struct option long_opts[] = {
282 {"help", no_argument, 0, 'h'},
283 {"version", no_argument, 0, 'V'},
284 {"verbose", no_argument, 0, 'v'},
285 {"timeout", required_argument, 0, 't'},
286 {"hostname", required_argument, 0, 'H'},
287 {"server", required_argument, 0, 's'},
288 {"reverse-server", required_argument, 0, 'r'},
289 {"expected-address", required_argument, 0, 'a'},
290 {"expect-authority", no_argument, 0, 'A'},
291 {"warning", no_argument, 0, 'w'},
292 {"critical", no_argument, 0, 'c'},
293 {0, 0, 0, 0}
294 };
296 if (argc < 2)
297 return ERROR;
299 for (c = 1; c < argc; c++)
300 if (strcmp ("-to", argv[c]) == 0)
301 strcpy (argv[c], "-t");
303 while (1) {
304 c = getopt_long (argc, argv, "hVvAt:H:s:r:a:w:c:", long_opts, &opt_index);
306 if (c == -1 || c == EOF)
307 break;
309 switch (c) {
310 case 'h': /* help */
311 print_help ();
312 exit (STATE_OK);
313 case 'V': /* version */
314 print_revision (progname, revision);
315 exit (STATE_OK);
316 case 'v': /* version */
317 verbose = TRUE;
318 break;
319 case 't': /* timeout period */
320 timeout_interval = atoi (optarg);
321 break;
322 case 'H': /* hostname */
323 if (strlen (optarg) >= ADDRESS_LENGTH)
324 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
325 strcpy (query_address, optarg);
326 break;
327 case 's': /* server name */
328 /* TODO: this host_or_die check is probably unnecessary.
329 * Better to confirm nslookup response matches */
330 host_or_die(optarg);
331 if (strlen (optarg) >= ADDRESS_LENGTH)
332 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
333 strcpy (dns_server, optarg);
334 break;
335 case 'r': /* reverse server name */
336 /* TODO: Is this host_or_die necessary? */
337 host_or_die(optarg);
338 if (strlen (optarg) >= ADDRESS_LENGTH)
339 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
340 strcpy (ptr_server, optarg);
341 break;
342 case 'a': /* expected address */
343 if (strlen (optarg) >= ADDRESS_LENGTH)
344 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
345 strcpy (expected_address, optarg);
346 match_expected_address = TRUE;
347 break;
348 case 'A': /* expect authority */
349 expect_authority = TRUE;
350 break;
351 case 'w':
352 warning = optarg;
353 break;
354 case 'c':
355 critical = optarg;
356 break;
357 default: /* args not parsable */
358 usage_va(_("Unknown argument - %s"), optarg);
359 }
360 }
362 c = optind;
363 if (strlen(query_address)==0 && c<argc) {
364 if (strlen(argv[c])>=ADDRESS_LENGTH)
365 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
366 strcpy (query_address, argv[c++]);
367 }
369 if (strlen(dns_server)==0 && c<argc) {
370 /* TODO: See -s option */
371 host_or_die(argv[c]);
372 if (strlen(argv[c]) >= ADDRESS_LENGTH)
373 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
374 strcpy (dns_server, argv[c++]);
375 }
377 set_thresholds(&time_thresholds, warning, critical);
379 return validate_arguments ();
380 }
383 int
384 validate_arguments ()
385 {
386 if (query_address[0] == 0)
387 return ERROR;
389 return OK;
390 }
393 void
394 print_help (void)
395 {
396 print_revision (progname, revision);
398 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
399 printf (COPYRIGHT, copyright, email);
401 printf (_("This plugin uses the nslookup program to obtain the IP address for the given host/domain query."));
402 printf ("\n");
403 printf (_("An optional DNS server to use may be specified."));
404 printf ("\n");
405 printf (_("If no DNS server is specified, the default server(s) specified in /etc/resolv.conf will be used."));
406 printf ("\n\n");
408 print_usage ();
409 printf (_(UT_HELP_VRSN));
410 printf (" -H, --hostname=HOST\n");
411 printf (" %s\n", _("The name or address you want to query"));
412 printf (" -s, --server=HOST\n");
413 printf (" %s\n", _("Optional DNS server you want to use for the lookup"));
414 printf (" -a, --expected-address=IP-ADDRESS|HOST\n");
415 printf (" %s\n", _("Optional IP-ADDRESS you expect the DNS server to return. HOST must end with ."));
416 printf (" -A, --expect-authority\n");
417 printf (" %s\n", _("Optionally expect the DNS server to be authoritative for the lookup"));
418 printf (" -w, --warning=seconds\n");
419 printf (" %s\n", _("Return warning if elapsed time exceeds value. Default off"));
420 printf (" -c, --critical=seconds\n");
421 printf (" %s\n", _("Return critical if elapsed time exceeds value. Default off"));
423 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
424 printf (_(UT_SUPPORT));
425 }
428 void
429 print_usage (void)
430 {
431 printf (_("Usage:"));
433 printf ("%s -H host [-s server] [-a expected-address] [-A] [-t timeout] [-w warn] [-c crit]\n", progname);
434 }