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-2004";
27 const char *email = "nagiosplug-devel@lists.sourceforge.net";
29 #include "common.h"
30 #include "popen.h"
31 #include "utils.h"
32 #include "netutils.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;
49 int
50 main (int argc, char **argv)
51 {
52 char *command_line = NULL;
53 char input_buffer[MAX_INPUT_BUFFER];
54 char *output = NULL;
55 char *address = NULL;
56 char *temp_buffer = NULL;
57 int non_authoritative = FALSE;
58 int result = STATE_UNKNOWN;
59 double elapsed_time;
60 long microsec;
61 struct timeval tv;
62 int multi_address;
63 int parse_address = FALSE; /* This flag scans for Address: but only after Name: */
65 setlocale (LC_ALL, "");
66 bindtextdomain (PACKAGE, LOCALEDIR);
67 textdomain (PACKAGE);
69 /* Set signal handling and alarm */
70 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
71 usage4 (_("Cannot catch SIGALRM"));
72 }
74 if (process_arguments (argc, argv) == ERROR) {
75 usage4 (_("Could not parse arguments"));
76 }
78 /* get the command to run */
79 asprintf (&command_line, "%s %s %s", NSLOOKUP_COMMAND, query_address, dns_server);
81 alarm (timeout_interval);
82 gettimeofday (&tv, NULL);
84 if (verbose)
85 printf ("%s\n", command_line);
87 /* run the command */
88 child_process = spopen (command_line);
89 if (child_process == NULL) {
90 printf (_("Could not open pipe: %s\n"), command_line);
91 return STATE_UNKNOWN;
92 }
94 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
95 if (child_stderr == NULL)
96 printf (_("Could not open stderr for %s\n"), command_line);
98 /* scan stdout */
99 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
101 if (verbose)
102 printf ("%s", input_buffer);
104 if (strstr (input_buffer, ".in-addr.arpa")) {
105 if ((temp_buffer = strstr (input_buffer, "name = ")))
106 address = strdup (temp_buffer + 7);
107 else {
108 output = strdup (_("Warning plugin error"));
109 result = STATE_WARNING;
110 }
111 }
113 /* the server is responding, we just got the host name... */
114 if (strstr (input_buffer, "Name:"))
115 parse_address = TRUE;
116 else if (parse_address == TRUE && (strstr (input_buffer, "Address:") ||
117 strstr (input_buffer, "Addresses:"))) {
118 temp_buffer = index (input_buffer, ':');
119 temp_buffer++;
121 /* Strip leading spaces */
122 for (; *temp_buffer != '\0' && *temp_buffer == ' '; temp_buffer++)
123 /* NOOP */;
125 strip(temp_buffer);
126 if (temp_buffer==NULL || strlen(temp_buffer)==0) {
127 die (STATE_CRITICAL,
128 _("DNS CRITICAL - '%s' returned empty host name string\n"),
129 NSLOOKUP_COMMAND);
130 }
132 if (address == NULL)
133 address = strdup (temp_buffer);
134 else
135 asprintf(&address, "%s,%s", address, temp_buffer);
136 }
138 else if (strstr (input_buffer, _("Non-authoritative answer:"))) {
139 non_authoritative = TRUE;
140 }
142 result = error_scan (input_buffer);
143 if (result != STATE_OK) {
144 output = strdup (1 + index (input_buffer, ':'));
145 strip (output);
146 break;
147 }
149 }
151 /* scan stderr */
152 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
154 if (verbose)
155 printf ("%s", input_buffer);
157 if (error_scan (input_buffer) != STATE_OK) {
158 result = max_state (result, error_scan (input_buffer));
159 output = strdup (1 + index (input_buffer, ':'));
160 strip (output);
161 }
162 }
164 /* close stderr */
165 (void) fclose (child_stderr);
167 /* close stdout */
168 if (spclose (child_process)) {
169 result = max_state (result, STATE_WARNING);
170 if (output == NULL || !strcmp (output, ""))
171 output = strdup (_("nslookup returned error status"));
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' output 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(&output, _("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(&output, _("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 printf ("DNS %s: ", _("OK"));
203 printf (ngettext("%.3f second response time ", "%.3f seconds response time ", elapsed_time), elapsed_time);
204 printf (_("%s returns %s"), query_address, address);
205 printf ("|%s\n", fperfdata ("time", elapsed_time, "s", FALSE, 0, FALSE, 0, TRUE, 0, FALSE, 0));
206 }
207 else if (result == STATE_WARNING)
208 printf (_("DNS WARNING - %s\n"),
209 !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output);
210 else if (result == STATE_CRITICAL)
211 printf (_("DNS CRITICAL - %s\n"),
212 !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output);
213 else
214 printf (_("DNS UNKNOW - %s\n"),
215 !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output);
217 return result;
218 }
222 int
223 error_scan (char *input_buffer)
224 {
226 /* the DNS lookup timed out */
227 if (strstr (input_buffer, _("Note: nslookup is deprecated and may be removed from future releases.")) ||
228 strstr (input_buffer, _("Consider using the `dig' or `host' programs instead. Run nslookup with")) ||
229 strstr (input_buffer, _("the `-sil[ent]' option to prevent this message from appearing.")))
230 return STATE_OK;
232 /* DNS server is not running... */
233 else if (strstr (input_buffer, "No response from server"))
234 die (STATE_CRITICAL, _("No response from DNS %s\n"), dns_server);
236 /* Host name is valid, but server doesn't have records... */
237 else if (strstr (input_buffer, "No records"))
238 die (STATE_CRITICAL, _("DNS %s has no records\n"), dns_server);
240 /* Connection was refused */
241 else if (strstr (input_buffer, "Connection refused") ||
242 strstr (input_buffer, "Couldn't find server") ||
243 strstr (input_buffer, "Refused") ||
244 (strstr (input_buffer, "** server can't find") &&
245 strstr (input_buffer, ": REFUSED")))
246 die (STATE_CRITICAL, _("Connection to DNS %s was refused\n"), dns_server);
248 /* Query refused (usually by an ACL in the namserver) */
249 else if (strstr (input_buffer, "Query refused"))
250 die (STATE_CRITICAL, _("Query was refused by DNS server at %s\n"), dns_server);
252 /* No information (e.g. nameserver IP has two PTR records) */
253 else if (strstr (input_buffer, "No information"))
254 die (STATE_CRITICAL, _("No information returned by DNS server at %s\n"), dns_server);
256 /* Host or domain name does not exist */
257 else if (strstr (input_buffer, "Non-existent") ||
258 strstr (input_buffer, "** server can't find") ||
259 strstr (input_buffer,"NXDOMAIN"))
260 die (STATE_CRITICAL, _("Domain %s was not found by the server\n"), query_address);
262 /* Network is unreachable */
263 else if (strstr (input_buffer, "Network is unreachable"))
264 die (STATE_CRITICAL, _("Network is unreachable\n"));
266 /* Internal server failure */
267 else if (strstr (input_buffer, "Server failure"))
268 die (STATE_CRITICAL, _("DNS failure for %s\n"), dns_server);
270 /* Request error or the DNS lookup timed out */
271 else if (strstr (input_buffer, "Format error") ||
272 strstr (input_buffer, "Timed out"))
273 return STATE_WARNING;
275 return STATE_OK;
277 }
280 /* process command-line arguments */
281 int
282 process_arguments (int argc, char **argv)
283 {
284 int c;
286 int opt_index = 0;
287 static struct option long_opts[] = {
288 {"help", no_argument, 0, 'h'},
289 {"version", no_argument, 0, 'V'},
290 {"verbose", no_argument, 0, 'v'},
291 {"timeout", required_argument, 0, 't'},
292 {"hostname", required_argument, 0, 'H'},
293 {"server", required_argument, 0, 's'},
294 {"reverse-server", required_argument, 0, 'r'},
295 {"expected-address", required_argument, 0, 'a'},
296 {"expect-authority", no_argument, 0, 'A'},
297 {0, 0, 0, 0}
298 };
300 if (argc < 2)
301 return ERROR;
303 for (c = 1; c < argc; c++)
304 if (strcmp ("-to", argv[c]) == 0)
305 strcpy (argv[c], "-t");
307 while (1) {
308 c = getopt_long (argc, argv, "hVvAt:H:s:r:a:", long_opts, &opt_index);
310 if (c == -1 || c == EOF)
311 break;
313 switch (c) {
314 case '?': /* args not parsable */
315 usage2 (_("Unknown argument"), optarg);
316 case 'h': /* help */
317 print_help ();
318 exit (STATE_OK);
319 case 'V': /* version */
320 print_revision (progname, revision);
321 exit (STATE_OK);
322 case 'v': /* version */
323 verbose = TRUE;
324 break;
325 case 't': /* timeout period */
326 timeout_interval = atoi (optarg);
327 break;
328 case 'H': /* hostname */
329 if (strlen (optarg) >= ADDRESS_LENGTH)
330 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
331 strcpy (query_address, optarg);
332 break;
333 case 's': /* server name */
334 /* TODO: this is_host check is probably unnecessary. */
335 /* Better to confirm nslookup response matches */
336 if (is_host (optarg) == FALSE) {
337 usage2 (_("Invalid hostname/address"), optarg);
338 }
339 if (strlen (optarg) >= ADDRESS_LENGTH)
340 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
341 strcpy (dns_server, optarg);
342 break;
343 case 'r': /* reverse server name */
344 /* TODO: Is this is_host necessary? */
345 if (is_host (optarg) == FALSE) {
346 usage2 (_("Invalid hostname/address"), optarg);
347 }
348 if (strlen (optarg) >= ADDRESS_LENGTH)
349 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
350 strcpy (ptr_server, optarg);
351 break;
352 case 'a': /* expected address */
353 if (strlen (optarg) >= ADDRESS_LENGTH)
354 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
355 strcpy (expected_address, optarg);
356 match_expected_address = TRUE;
357 break;
358 case 'A': /* expect authority */
359 expect_authority = TRUE;
360 break;
361 }
362 }
364 c = optind;
365 if (strlen(query_address)==0 && c<argc) {
366 if (strlen(argv[c])>=ADDRESS_LENGTH)
367 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
368 strcpy (query_address, argv[c++]);
369 }
371 if (strlen(dns_server)==0 && c<argc) {
372 /* TODO: See -s option */
373 if (is_host(argv[c]) == FALSE) {
374 printf (_("Invalid hostname/address: %s\n\n"), argv[c]);
375 return ERROR;
376 }
377 if (strlen(argv[c]) >= ADDRESS_LENGTH)
378 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
379 strcpy (dns_server, argv[c++]);
380 }
382 return validate_arguments ();
383 }
386 int
387 validate_arguments ()
388 {
389 if (query_address[0] == 0)
390 return ERROR;
391 else
392 return OK;
393 }
396 void
397 print_help (void)
398 {
399 print_revision (progname, revision);
401 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
402 printf (COPYRIGHT, copyright, email);
404 printf (_("\
405 This plugin uses the nslookup program to obtain the IP address\n\
406 for the given host/domain query. A optional DNS server to use may\n\
407 be specified. If no DNS server is specified, the default server(s)\n\
408 specified in /etc/resolv.conf will be used.\n\n"));
410 print_usage ();
412 printf (_(UT_HELP_VRSN));
414 printf (_("\
415 -H, --hostname=HOST\n\
416 The name or address you want to query\n\
417 -s, --server=HOST\n\
418 Optional DNS server you want to use for the lookup\n\
419 -a, --expected-address=IP-ADDRESS\n\
420 Optional IP address you expect the DNS server to return\n\
421 -A, --expect-authority\n\
422 Optionally expect the DNS server to be authoritative for the lookup\n"));
424 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
426 printf (_(UT_SUPPORT));
427 }
430 void
431 print_usage (void)
432 {
433 printf ("\
434 Usage: %s -H host [-s server] [-a expected-address] [-A] [-t timeout]\n", progname);
435 }