1 /******************************************************************************
2 *
3 * CHECK_DNS.C
4 *
5 * Program: DNS plugin for Nagios
6 * License: GPL
7 * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
8 *
9 * Last Modified: $Date$
10 *
11 * Notes:
12 * - Safe popen added by Karl DeBisschop 9-11-99
13 * - expected-address parameter added by Alex Chaffee - 7 Oct 2002
14 *
15 * Command line: (see print_usage)
16 *
17 * Description:
18 *
19 * This program will use the nslookup program to obtain the IP address
20 * for a given host name. A optional DNS server may be specified. If
21 * no DNS server is specified, the default server(s) for the system
22 * are used.
23 *
24 * Return Values:
25 * OK The DNS query was successful (host IP address was returned).
26 * WARNING The DNS server responded, but could not fulfill the request.
27 * CRITICAL The DNS server is not responding or encountered an error.
28 *
29 * License Information:
30 *
31 * This program is free software; you can redistribute it and/or modify
32 * it under the terms of the GNU General Public License as published by
33 * the Free Software Foundation; either version 2 of the License, or
34 * (at your option) any later version.
35 *
36 * This program is distributed in the hope that it will be useful,
37 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 * GNU General Public License for more details.
40 *
41 * You should have received a copy of the GNU General Public License
42 * along with this program; if not, write to the Free Software
43 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
44 *
45 *****************************************************************************/
47 #include "common.h"
48 #include "popen.h"
49 #include "utils.h"
50 #include "netutils.h"
52 const char *progname = "check_dns";
53 const char *revision = "$Revision$";
54 const char *copyright = "2000-2003";
55 const char *email = "nagiosplug-devel@lists.sourceforge.net";
57 int process_arguments (int, char **);
58 int validate_arguments (void);
59 int error_scan (char *);
60 void print_help (void);
61 void print_usage (void);
63 #define ADDRESS_LENGTH 256
64 char query_address[ADDRESS_LENGTH] = "";
65 char dns_server[ADDRESS_LENGTH] = "";
66 char ptr_server[ADDRESS_LENGTH] = "";
67 int verbose = FALSE;
68 char expected_address[ADDRESS_LENGTH] = "";
69 int match_expected_address = FALSE;
71 int
72 main (int argc, char **argv)
73 {
74 char *command_line = NULL;
75 char input_buffer[MAX_INPUT_BUFFER];
76 char *output = NULL;
77 char *address = NULL;
78 char *temp_buffer = NULL;
79 int result = STATE_UNKNOWN;
80 double elapsed_time;
81 long microsec;
82 struct timeval tv;
83 int multi_address;
85 setlocale (LC_ALL, "");
86 bindtextdomain (PACKAGE, LOCALEDIR);
87 textdomain (PACKAGE);
89 /* Set signal handling and alarm */
90 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
91 printf (_("Cannot catch SIGALRM"));
92 return STATE_UNKNOWN;
93 }
95 if (process_arguments (argc, argv) != OK) {
96 print_usage ();
97 return STATE_UNKNOWN;
98 }
100 /* get the command to run */
101 asprintf (&command_line, "%s %s %s", NSLOOKUP_COMMAND, query_address, dns_server);
103 alarm (timeout_interval);
104 gettimeofday (&tv, NULL);
106 if (verbose)
107 printf ("%s\n", command_line);
108 /* run the command */
109 child_process = spopen (command_line);
110 if (child_process == NULL) {
111 printf (_("Could not open pipe: %s\n"), command_line);
112 return STATE_UNKNOWN;
113 }
115 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
116 if (child_stderr == NULL)
117 printf (_("Could not open stderr for %s\n"), command_line);
119 /* scan stdout */
120 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
122 if (verbose)
123 printf ("%s\n", input_buffer);
125 if (strstr (input_buffer, ".in-addr.arpa")) {
126 if ((temp_buffer = strstr (input_buffer, "name = ")))
127 address = strdup (temp_buffer + 7);
128 else {
129 output = strdup (_("Unknown error (plugin)"));
130 result = STATE_WARNING;
131 }
132 }
134 /* the server is responding, we just got the host name... */
135 if (strstr (input_buffer, "Name:")) {
137 /* get the host address */
138 if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
139 break;
141 if (verbose)
142 printf ("%s\n", input_buffer);
144 if ((temp_buffer = index (input_buffer, ':'))) {
145 temp_buffer++;
146 /* Strip leading spaces */
147 for (; *temp_buffer != '\0' && *temp_buffer == ' '; temp_buffer++)
148 /* NOOP */;
149 address = strdup (temp_buffer);
150 strip (address);
151 if (address==NULL || strlen(address)==0)
152 die (STATE_CRITICAL,
153 _("DNS CRITICAL - '%s' returned empty host name string\n"),
154 NSLOOKUP_COMMAND);
155 result = STATE_OK;
156 }
157 else {
158 output = strdup (_("Unknown error (plugin)"));
159 result = STATE_WARNING;
160 }
162 break;
163 }
165 result = error_scan (input_buffer);
166 if (result != STATE_OK) {
167 output = strdup (1 + index (input_buffer, ':'));
168 strip (output);
169 break;
170 }
172 }
174 /* scan stderr */
175 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
176 if (error_scan (input_buffer) != STATE_OK) {
177 result = max_state (result, error_scan (input_buffer));
178 output = strdup (1 + index (input_buffer, ':'));
179 strip (output);
180 }
181 }
183 /* close stderr */
184 (void) fclose (child_stderr);
186 /* close stdout */
187 if (spclose (child_process)) {
188 result = max_state (result, STATE_WARNING);
189 if (!strcmp (output, ""))
190 output = strdup (_("nslookup returned error status"));
191 }
193 /* If we got here, we should have an address string,
194 and we can segfault if we do not */
195 if (address==NULL || strlen(address)==0)
196 die (STATE_CRITICAL,
197 _("DNS CRITICAL - '%s' output parsing exited with no address\n"),
198 NSLOOKUP_COMMAND);
200 /* compare to expected address */
201 if (result == STATE_OK && match_expected_address && strcmp(address, expected_address)) {
202 result = STATE_CRITICAL;
203 asprintf(&output, _("expected %s but got %s"), expected_address, address);
204 }
206 microsec = deltime (tv);
207 elapsed_time = (double)microsec / 1.0e6;
209 if (result == STATE_OK) {
210 if (strchr (address, ',') == NULL)
211 multi_address = FALSE;
212 else
213 multi_address = TRUE;
215 printf (_("DNS ok - %.3f seconds response time, address%s %s|time=%ldus\n"),
216 elapsed_time, (multi_address==TRUE ? "es are" : " is"), address, microsec);
217 }
218 else if (result == STATE_WARNING)
219 printf (_("DNS WARNING - %s\n"),
220 !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output);
221 else if (result == STATE_CRITICAL)
222 printf (_("DNS CRITICAL - %s\n"),
223 !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output);
224 else
225 printf (_("DNS problem - %s\n"),
226 !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output);
228 return result;
229 }
231 int
232 error_scan (char *input_buffer)
233 {
235 /* the DNS lookup timed out */
236 if (strstr (input_buffer, "Note: nslookup is deprecated and may be removed from future releases.") ||
237 strstr (input_buffer, "Consider using the `dig' or `host' programs instead. Run nslookup with") ||
238 strstr (input_buffer, "the `-sil[ent]' option to prevent this message from appearing."))
239 return STATE_OK;
241 /* DNS server is not running... */
242 else if (strstr (input_buffer, "No response from server"))
243 die (STATE_CRITICAL, _("No response from name server %s\n"), dns_server);
245 /* Host name is valid, but server doesn't have records... */
246 else if (strstr (input_buffer, "No records"))
247 die (STATE_CRITICAL, _("Name server %s has no records\n"), dns_server);
249 /* Connection was refused */
250 else if (strstr (input_buffer, "Connection refused") ||
251 (strstr (input_buffer, "** server can't find") &&
252 strstr (input_buffer, ": REFUSED")) ||
253 (strstr (input_buffer, "Refused")))
254 die (STATE_CRITICAL, _("Connection to name server %s was refused\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, _("Server 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 }
279 /* process command-line arguments */
280 int
281 process_arguments (int argc, char **argv)
282 {
283 int c;
285 int opt_index = 0;
286 static struct option long_opts[] = {
287 {"help", no_argument, 0, 'h'},
288 {"version", no_argument, 0, 'V'},
289 {"verbose", no_argument, 0, 'v'},
290 {"timeout", required_argument, 0, 't'},
291 {"hostname", required_argument, 0, 'H'},
292 {"server", required_argument, 0, 's'},
293 {"reverse-server", required_argument, 0, 'r'},
294 {"expected-address", required_argument, 0, 'a'},
295 {0, 0, 0, 0}
296 };
298 if (argc < 2)
299 return ERROR;
301 for (c = 1; c < argc; c++)
302 if (strcmp ("-to", argv[c]) == 0)
303 strcpy (argv[c], "-t");
305 while (1) {
306 c = getopt_long (argc, argv, "hVvt:H:s:r:a:", long_opts, &opt_index);
308 if (c == -1 || c == EOF)
309 break;
311 switch (c) {
312 case '?': /* args not parsable */
313 printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
314 print_usage ();
315 exit (STATE_UNKNOWN);
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. Better to confirm nslookup
335 response matches */
336 if (is_host (optarg) == FALSE) {
337 printf (_("Invalid server name/address\n\n"));
338 print_usage ();
339 exit (STATE_UNKNOWN);
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 printf (_("Invalid host name/address\n\n"));
349 print_usage ();
350 exit (STATE_UNKNOWN);
351 }
352 if (strlen (optarg) >= ADDRESS_LENGTH)
353 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
354 strcpy (ptr_server, optarg);
355 break;
356 case 'a': /* expected address */
357 if (strlen (optarg) >= ADDRESS_LENGTH)
358 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
359 strcpy (expected_address, optarg);
360 match_expected_address = TRUE;
361 break;
362 }
363 }
365 c = optind;
366 if (strlen(query_address)==0 && c<argc) {
367 if (strlen(argv[c])>=ADDRESS_LENGTH)
368 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
369 strcpy (query_address, argv[c++]);
370 }
372 if (strlen(dns_server)==0 && c<argc) {
373 /* TODO: See -s option */
374 if (is_host(argv[c]) == FALSE) {
375 printf (_("Invalid name/address: %s\n\n"), argv[c]);
376 return ERROR;
377 }
378 if (strlen(argv[c]) >= ADDRESS_LENGTH)
379 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
380 strcpy (dns_server, argv[c++]);
381 }
383 return validate_arguments ();
384 }
386 int
387 validate_arguments ()
388 {
389 if (query_address[0] == 0)
390 return ERROR;
391 else
392 return OK;
393 }
399 \f
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 print_usage ();
410 printf (_(UT_HELP_VRSN));
412 printf (_("\
413 -H, --hostname=HOST\n\
414 The name or address you want to query\n\
415 -s, --server=HOST\n\
416 Optional DNS server you want to use for the lookup\n\
417 -a, --expected-address=IP-ADDRESS\n\
418 Optional IP address you expect the DNS server to return\n"));
420 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
422 printf (_("\n\
423 This plugin uses the nslookup program to obtain the IP address\n\
424 for the given host/domain query. A optional DNS server to use may\n\
425 be specified. If no DNS server is specified, the default server(s)\n\
426 specified in /etc/resolv.conf will be used.\n"));
428 printf (_(UT_SUPPORT));
429 }
434 void
435 print_usage (void)
436 {
437 printf (_("\
438 Usage: %s -H host [-s server] [-a expected-address] [-t timeout]\n\
439 %s --help\n\
440 %s --version\n"),
441 progname, progname, progname);
442 }