afc23a4f1b8fe3a74ee99cbc18506a06cc32a267
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 struct timeval tv;
82 int multi_address;
84 /* Set signal handling and alarm */
85 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
86 printf (_("Cannot catch SIGALRM"));
87 return STATE_UNKNOWN;
88 }
90 if (process_arguments (argc, argv) != OK) {
91 print_usage ();
92 return STATE_UNKNOWN;
93 }
95 /* get the command to run */
96 asprintf (&command_line, "%s %s %s", NSLOOKUP_COMMAND, query_address, dns_server);
98 alarm (timeout_interval);
99 gettimeofday (&tv, NULL);
101 if (verbose)
102 printf ("%s\n", command_line);
103 /* run the command */
104 child_process = spopen (command_line);
105 if (child_process == NULL) {
106 printf (_("Could not open pipe: %s\n"), command_line);
107 return STATE_UNKNOWN;
108 }
110 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
111 if (child_stderr == NULL)
112 printf (_("Could not open stderr for %s\n"), command_line);
114 /* scan stdout */
115 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
117 if (verbose)
118 printf ("%s\n", input_buffer);
120 if (strstr (input_buffer, ".in-addr.arpa")) {
121 if ((temp_buffer = strstr (input_buffer, "name = ")))
122 address = strdup (temp_buffer + 7);
123 else {
124 output = strdup (_("Unknown error (plugin)"));
125 result = STATE_WARNING;
126 }
127 }
129 /* the server is responding, we just got the host name... */
130 if (strstr (input_buffer, "Name:")) {
132 /* get the host address */
133 if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
134 break;
136 if (verbose)
137 printf ("%s\n", input_buffer);
139 if ((temp_buffer = index (input_buffer, ':'))) {
140 temp_buffer++;
141 /* Strip leading spaces */
142 for (; *temp_buffer != '\0' && *temp_buffer == ' '; temp_buffer++)
143 /* NOOP */;
144 address = strdup (temp_buffer);
145 strip (address);
146 if (address==NULL || strlen(address)==0)
147 die (STATE_CRITICAL,
148 _("DNS CRITICAL - '%s' returned empty host name string\n"),
149 NSLOOKUP_COMMAND);
150 result = STATE_OK;
151 }
152 else {
153 output = strdup (_("Unknown error (plugin)"));
154 result = STATE_WARNING;
155 }
157 break;
158 }
160 result = error_scan (input_buffer);
161 if (result != STATE_OK) {
162 output = strdup (1 + index (input_buffer, ':'));
163 strip (output);
164 break;
165 }
167 }
169 /* scan stderr */
170 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
171 if (error_scan (input_buffer) != STATE_OK) {
172 result = max_state (result, error_scan (input_buffer));
173 output = strdup (1 + index (input_buffer, ':'));
174 strip (output);
175 }
176 }
178 /* close stderr */
179 (void) fclose (child_stderr);
181 /* close stdout */
182 if (spclose (child_process)) {
183 result = max_state (result, STATE_WARNING);
184 if (!strcmp (output, ""))
185 output = strdup (_("nslookup returned error status"));
186 }
188 /* If we got here, we should have an address string,
189 and we can segfault if we do not */
190 if (address==NULL || strlen(address)==0)
191 die (STATE_CRITICAL,
192 _("DNS CRITICAL - '%s' output parsing exited with no address\n"),
193 NSLOOKUP_COMMAND);
195 /* compare to expected address */
196 if (result == STATE_OK && match_expected_address && strcmp(address, expected_address)) {
197 result = STATE_CRITICAL;
198 asprintf(&output, _("expected %s but got %s"), expected_address, address);
199 }
201 elapsed_time = delta_time (tv);
203 if (result == STATE_OK) {
204 if (strchr (address, ',') == NULL)
205 multi_address = FALSE;
206 else
207 multi_address = TRUE;
209 printf (_("DNS ok - %.3f seconds response time, address%s %s|time=%.3f\n"),
210 elapsed_time, (multi_address==TRUE ? "es are" : " is"), address, elapsed_time);
211 }
212 else if (result == STATE_WARNING)
213 printf (_("DNS WARNING - %s\n"),
214 !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output);
215 else if (result == STATE_CRITICAL)
216 printf (_("DNS CRITICAL - %s\n"),
217 !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output);
218 else
219 printf (_("DNS problem - %s\n"),
220 !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output);
222 return result;
223 }
225 int
226 error_scan (char *input_buffer)
227 {
229 /* the DNS lookup timed out */
230 if (strstr (input_buffer, "Note: nslookup is deprecated and may be removed from future releases.") ||
231 strstr (input_buffer, "Consider using the `dig' or `host' programs instead. Run nslookup with") ||
232 strstr (input_buffer, "the `-sil[ent]' option to prevent this message from appearing."))
233 return STATE_OK;
235 /* DNS server is not running... */
236 else if (strstr (input_buffer, "No response from server"))
237 die (STATE_CRITICAL, _("No response from name server %s\n"), dns_server);
239 /* Host name is valid, but server doesn't have records... */
240 else if (strstr (input_buffer, "No records"))
241 die (STATE_CRITICAL, _("Name server %s has no records\n"), dns_server);
243 /* Connection was refused */
244 else if (strstr (input_buffer, "Connection refused") ||
245 (strstr (input_buffer, "** server can't find") &&
246 strstr (input_buffer, ": REFUSED")) ||
247 (strstr (input_buffer, "Refused")))
248 die (STATE_CRITICAL, _("Connection to name server %s was refused\n"), dns_server);
250 /* Host or domain name does not exist */
251 else if (strstr (input_buffer, "Non-existent") ||
252 strstr (input_buffer, "** server can't find") ||
253 strstr (input_buffer,"NXDOMAIN"))
254 die (STATE_CRITICAL, _("Domain %s was not found by the server\n"), query_address);
256 /* Network is unreachable */
257 else if (strstr (input_buffer, "Network is unreachable"))
258 die (STATE_CRITICAL, _("Network is unreachable\n"));
260 /* Internal server failure */
261 else if (strstr (input_buffer, "Server failure"))
262 die (STATE_CRITICAL, _("Server failure for %s\n"), dns_server);
264 /* Request error or the DNS lookup timed out */
265 else if (strstr (input_buffer, "Format error") ||
266 strstr (input_buffer, "Timed out"))
267 return STATE_WARNING;
269 return STATE_OK;
271 }
273 /* process command-line arguments */
274 int
275 process_arguments (int argc, char **argv)
276 {
277 int c;
279 int opt_index = 0;
280 static struct option long_opts[] = {
281 {"help", no_argument, 0, 'h'},
282 {"version", no_argument, 0, 'V'},
283 {"verbose", no_argument, 0, 'v'},
284 {"timeout", required_argument, 0, 't'},
285 {"hostname", required_argument, 0, 'H'},
286 {"server", required_argument, 0, 's'},
287 {"reverse-server", required_argument, 0, 'r'},
288 {"expected-address", required_argument, 0, 'a'},
289 {0, 0, 0, 0}
290 };
292 if (argc < 2)
293 return ERROR;
295 for (c = 1; c < argc; c++)
296 if (strcmp ("-to", argv[c]) == 0)
297 strcpy (argv[c], "-t");
299 while (1) {
300 c = getopt_long (argc, argv, "hVvt:H:s:r:a:", long_opts, &opt_index);
302 if (c == -1 || c == EOF)
303 break;
305 switch (c) {
306 case '?': /* args not parsable */
307 printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
308 print_usage ();
309 exit (STATE_UNKNOWN);
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 is_host check is probably unnecessary. Better to confirm nslookup
329 response matches */
330 if (is_host (optarg) == FALSE) {
331 printf (_("Invalid server name/address\n\n"));
332 print_usage ();
333 exit (STATE_UNKNOWN);
334 }
335 if (strlen (optarg) >= ADDRESS_LENGTH)
336 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
337 strcpy (dns_server, optarg);
338 break;
339 case 'r': /* reverse server name */
340 /* TODO: Is this is_host necessary? */
341 if (is_host (optarg) == FALSE) {
342 printf (_("Invalid host name/address\n\n"));
343 print_usage ();
344 exit (STATE_UNKNOWN);
345 }
346 if (strlen (optarg) >= ADDRESS_LENGTH)
347 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
348 strcpy (ptr_server, optarg);
349 break;
350 case 'a': /* expected address */
351 if (strlen (optarg) >= ADDRESS_LENGTH)
352 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
353 strcpy (expected_address, optarg);
354 match_expected_address = TRUE;
355 break;
356 }
357 }
359 c = optind;
360 if (strlen(query_address)==0 && c<argc) {
361 if (strlen(argv[c])>=ADDRESS_LENGTH)
362 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
363 strcpy (query_address, argv[c++]);
364 }
366 if (strlen(dns_server)==0 && c<argc) {
367 /* TODO: See -s option */
368 if (is_host(argv[c]) == FALSE) {
369 printf (_("Invalid name/address: %s\n\n"), argv[c]);
370 return ERROR;
371 }
372 if (strlen(argv[c]) >= ADDRESS_LENGTH)
373 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
374 strcpy (dns_server, argv[c++]);
375 }
377 return validate_arguments ();
378 }
380 int
381 validate_arguments ()
382 {
383 if (query_address[0] == 0)
384 return ERROR;
385 else
386 return OK;
387 }
393 \f
394 void
395 print_help (void)
396 {
397 print_revision (progname, revision);
399 printf (_(COPYRIGHT), copyright, email);
401 print_usage ();
403 printf (_(UT_HELP_VRSN));
405 printf (_("\
406 -H, --hostname=HOST\n\
407 The name or address you want to query\n\
408 -s, --server=HOST\n\
409 Optional DNS server you want to use for the lookup\n\
410 -a, --expected-address=IP-ADDRESS\n\
411 Optional IP address you expect the DNS server to return\n"));
413 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
415 printf (_("\n\
416 This plugin uses the nslookup program to obtain the IP address\n\
417 for the given host/domain query. A optional DNS server to use may\n\
418 be specified. If no DNS server is specified, the default server(s)\n\
419 specified in /etc/resolv.conf will be used.\n"));
421 printf (_(UT_SUPPORT));
422 }
427 void
428 print_usage (void)
429 {
430 printf (_("\
431 Usage: %s -H host [-s server] [-a expected-address] [-t timeout]\n\
432 %s --help\n\
433 %s --version\n"),
434 progname, progname, progname);
435 }