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, "Refused") ||
243 (strstr (input_buffer, "** server can't find") &&
244 strstr (input_buffer, ": REFUSED")))
245 die (STATE_CRITICAL, _("Connection to DNS %s was refused\n"), dns_server);
247 /* Query refused (usually by an ACL in the namserver) */
248 else if (strstr (input_buffer, "Query refused"))
249 die (STATE_CRITICAL, _("Query was refused by DNS server at %s\n"), dns_server);
251 /* No information (e.g. nameserver IP has two PTR records) */
252 else if (strstr (input_buffer, "No information"))
253 die (STATE_CRITICAL, _("No information returned by DNS server at %s\n"), dns_server);
255 /* Host or domain name does not exist */
256 else if (strstr (input_buffer, "Non-existent") ||
257 strstr (input_buffer, "** server can't find") ||
258 strstr (input_buffer,"NXDOMAIN"))
259 die (STATE_CRITICAL, _("Domain %s was not found by the server\n"), query_address);
261 /* Network is unreachable */
262 else if (strstr (input_buffer, "Network is unreachable"))
263 die (STATE_CRITICAL, _("Network is unreachable\n"));
265 /* Internal server failure */
266 else if (strstr (input_buffer, "Server failure"))
267 die (STATE_CRITICAL, _("DNS failure for %s\n"), dns_server);
269 /* Request error or the DNS lookup timed out */
270 else if (strstr (input_buffer, "Format error") ||
271 strstr (input_buffer, "Timed out"))
272 return STATE_WARNING;
274 return STATE_OK;
276 }
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 printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
316 print_usage ();
317 exit (STATE_UNKNOWN);
318 case 'h': /* help */
319 print_help ();
320 exit (STATE_OK);
321 case 'V': /* version */
322 print_revision (progname, revision);
323 exit (STATE_OK);
324 case 'v': /* version */
325 verbose = TRUE;
326 break;
327 case 't': /* timeout period */
328 timeout_interval = atoi (optarg);
329 break;
330 case 'H': /* hostname */
331 if (strlen (optarg) >= ADDRESS_LENGTH)
332 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
333 strcpy (query_address, optarg);
334 break;
335 case 's': /* server name */
336 /* TODO: this is_host check is probably unnecessary. */
337 /* Better to confirm nslookup response matches */
338 if (is_host (optarg) == FALSE) {
339 usage2 (_("Invalid hostname/address"), optarg);
340 }
341 if (strlen (optarg) >= ADDRESS_LENGTH)
342 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
343 strcpy (dns_server, optarg);
344 break;
345 case 'r': /* reverse server name */
346 /* TODO: Is this is_host necessary? */
347 if (is_host (optarg) == FALSE) {
348 usage2 (_("Invalid hostname/address"), optarg);
349 }
350 if (strlen (optarg) >= ADDRESS_LENGTH)
351 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
352 strcpy (ptr_server, optarg);
353 break;
354 case 'a': /* expected address */
355 if (strlen (optarg) >= ADDRESS_LENGTH)
356 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
357 strcpy (expected_address, optarg);
358 match_expected_address = TRUE;
359 break;
360 case 'A': /* expect authority */
361 expect_authority = TRUE;
362 break;
363 }
364 }
366 c = optind;
367 if (strlen(query_address)==0 && c<argc) {
368 if (strlen(argv[c])>=ADDRESS_LENGTH)
369 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
370 strcpy (query_address, argv[c++]);
371 }
373 if (strlen(dns_server)==0 && c<argc) {
374 /* TODO: See -s option */
375 if (is_host(argv[c]) == FALSE) {
376 printf (_("Invalid hostname/address: %s\n\n"), argv[c]);
377 return ERROR;
378 }
379 if (strlen(argv[c]) >= ADDRESS_LENGTH)
380 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
381 strcpy (dns_server, argv[c++]);
382 }
384 return validate_arguments ();
385 }
389 int
390 validate_arguments ()
391 {
392 if (query_address[0] == 0)
393 return ERROR;
394 else
395 return OK;
396 }
400 void
401 print_help (void)
402 {
403 print_revision (progname, revision);
405 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
406 printf (COPYRIGHT, copyright, email);
408 printf (_("\
409 This plugin uses the nslookup program to obtain the IP address\n\
410 for the given host/domain query. A optional DNS server to use may\n\
411 be specified. If no DNS server is specified, the default server(s)\n\
412 specified in /etc/resolv.conf will be used.\n\n"));
414 print_usage ();
416 printf (_(UT_HELP_VRSN));
418 printf (_("\
419 -H, --hostname=HOST\n\
420 The name or address you want to query\n\
421 -s, --server=HOST\n\
422 Optional DNS server you want to use for the lookup\n\
423 -a, --expected-address=IP-ADDRESS\n\
424 Optional IP address you expect the DNS server to return\n\
425 -A, --expect-authority\n\
426 Optionally expect the DNS server to be authoritative for the lookup\n"));
428 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
430 printf (_(UT_SUPPORT));
431 }
435 void
436 print_usage (void)
437 {
438 printf ("\
439 Usage: %s -H host [-s server] [-a expected-address] [-A] [-t timeout]\n", progname);
440 }