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 void
58 print_usage (void)
59 {
60 printf (_("\
61 Usage: %s -H host [-s server] [-a expected-address] [-t timeout]\n\
62 %s --help\n\
63 %s --version\n"),
64 progname, progname, progname);
65 }
67 void
68 print_help (void)
69 {
70 print_revision (progname, revision);
72 printf (_(COPYRIGHT), copyright, email);
74 print_usage ();
76 printf (_(HELP_VRSN));
78 printf (_("\
79 -H, --hostname=HOST\n\
80 The name or address you want to query\n\
81 -s, --server=HOST\n\
82 Optional DNS server you want to use for the lookup\n\
83 -a, --expected-address=IP-ADDRESS\n\
84 Optional IP address you expect the DNS server to return\n"));
86 printf (_(TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
88 printf (_("\n\
89 This plugin uses the nslookup program to obtain the IP address\n\
90 for the given host/domain query. A optional DNS server to use may\n\
91 be specified. If no DNS server is specified, the default server(s)\n\
92 specified in /etc/resolv.conf will be used.\n"));
93 }
95 int process_arguments (int, char **);
96 int validate_arguments (void);
97 int error_scan (char *);
99 #define ADDRESS_LENGTH 256
100 char query_address[ADDRESS_LENGTH] = "";
101 char dns_server[ADDRESS_LENGTH] = "";
102 char ptr_server[ADDRESS_LENGTH] = "";
103 int verbose = FALSE;
104 char expected_address[ADDRESS_LENGTH] = "";
105 int match_expected_address = FALSE;
107 int
108 main (int argc, char **argv)
109 {
110 char *command_line = NULL;
111 char input_buffer[MAX_INPUT_BUFFER];
112 char *output = NULL;
113 char *address = NULL;
114 char *temp_buffer = NULL;
115 int result = STATE_UNKNOWN;
116 double elapsed_time;
117 struct timeval tv;
118 int multi_address;
120 /* Set signal handling and alarm */
121 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
122 printf (_("Cannot catch SIGALRM"));
123 return STATE_UNKNOWN;
124 }
126 if (process_arguments (argc, argv) != OK) {
127 print_usage ();
128 return STATE_UNKNOWN;
129 }
131 /* get the command to run */
132 asprintf (&command_line, "%s %s %s", NSLOOKUP_COMMAND, query_address, dns_server);
134 alarm (timeout_interval);
135 gettimeofday (&tv, NULL);
137 if (verbose)
138 printf ("%s\n", command_line);
139 /* run the command */
140 child_process = spopen (command_line);
141 if (child_process == NULL) {
142 printf (_("Could not open pipe: %s\n"), command_line);
143 return STATE_UNKNOWN;
144 }
146 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
147 if (child_stderr == NULL)
148 printf (_("Could not open stderr for %s\n"), command_line);
150 /* scan stdout */
151 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
153 if (verbose)
154 printf ("%s\n", input_buffer);
156 if (strstr (input_buffer, ".in-addr.arpa")) {
157 if ((temp_buffer = strstr (input_buffer, "name = ")))
158 address = strscpy (address, temp_buffer + 7);
159 else {
160 output = strscpy (output, _("Unknown error (plugin)"));
161 result = STATE_WARNING;
162 }
163 }
165 /* the server is responding, we just got the host name... */
166 if (strstr (input_buffer, "Name:")) {
168 /* get the host address */
169 if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
170 break;
172 if (verbose)
173 printf ("%s\n", input_buffer);
175 if ((temp_buffer = index (input_buffer, ':'))) {
176 temp_buffer++;
177 /* Strip leading spaces */
178 for (; *temp_buffer != '\0' && *temp_buffer == ' '; temp_buffer++)
179 /* NOOP */;
180 address = strdup (temp_buffer);
181 strip (address);
182 if (address==NULL || strlen(address)==0)
183 terminate (STATE_CRITICAL,
184 _("DNS CRITICAL - '%s' returned empty host name string\n"),
185 NSLOOKUP_COMMAND);
186 result = STATE_OK;
187 }
188 else {
189 output = strdup (_("Unknown error (plugin)"));
190 result = STATE_WARNING;
191 }
193 break;
194 }
196 result = error_scan (input_buffer);
197 if (result != STATE_OK) {
198 output = strscpy (output, 1 + index (input_buffer, ':'));
199 strip (output);
200 break;
201 }
203 }
205 /* scan stderr */
206 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
207 if (error_scan (input_buffer) != STATE_OK) {
208 result = max_state (result, error_scan (input_buffer));
209 output = strscpy (output, 1 + index (input_buffer, ':'));
210 strip (output);
211 }
212 }
214 /* close stderr */
215 (void) fclose (child_stderr);
217 /* close stdout */
218 if (spclose (child_process)) {
219 result = max_state (result, STATE_WARNING);
220 if (!strcmp (output, ""))
221 output = strscpy (output, _("nslookup returned error status"));
222 }
224 /* If we got here, we should have an address string,
225 and we can segfault if we do not */
226 if (address==NULL || strlen(address)==0)
227 terminate (STATE_CRITICAL,
228 _("DNS CRITICAL - '%s' output parsing exited with no address\n"),
229 NSLOOKUP_COMMAND);
231 /* compare to expected address */
232 if (result == STATE_OK && match_expected_address && strcmp(address, expected_address)) {
233 result = STATE_CRITICAL;
234 asprintf(&output, _("expected %s but got %s"), expected_address, address);
235 }
237 elapsed_time = delta_time (tv);
239 if (result == STATE_OK) {
240 if (strchr (address, ',') == NULL)
241 multi_address = FALSE;
242 else
243 multi_address = TRUE;
245 printf (_("DNS ok - %.3f seconds response time, address%s %s|time=%.3f\n"),
246 elapsed_time, (multi_address==TRUE ? "es are" : " is"), address, elapsed_time);
247 }
248 else if (result == STATE_WARNING)
249 printf (_("DNS WARNING - %s\n"),
250 !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output);
251 else if (result == STATE_CRITICAL)
252 printf (_("DNS CRITICAL - %s\n"),
253 !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output);
254 else
255 printf (_("DNS problem - %s\n"),
256 !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output);
258 return result;
259 }
261 int
262 error_scan (char *input_buffer)
263 {
265 /* the DNS lookup timed out */
266 if (strstr (input_buffer, _("Note: nslookup is deprecated and may be removed from future releases.")) ||
267 strstr (input_buffer, _("Consider using the `dig' or `host' programs instead. Run nslookup with")) ||
268 strstr (input_buffer, _("the `-sil[ent]' option to prevent this message from appearing.")))
269 return STATE_OK;
271 /* the DNS lookup timed out */
272 else if (strstr (input_buffer, _("Timed out")))
273 return STATE_WARNING;
275 /* DNS server is not running... */
276 else if (strstr (input_buffer, _("No response from server")))
277 return STATE_CRITICAL;
279 /* Host name is valid, but server doesn't have records... */
280 else if (strstr (input_buffer, _("No records")))
281 return STATE_WARNING;
283 /* Host or domain name does not exist */
284 else if (strstr (input_buffer, _("Non-existent")))
285 return STATE_CRITICAL;
286 else if (strstr (input_buffer, _("** server can't find")))
287 return STATE_CRITICAL;
288 else if(strstr(input_buffer,"NXDOMAIN")) /* 9.x */
289 return STATE_CRITICAL;
291 /* Connection was refused */
292 else if (strstr (input_buffer, _("Connection refused")))
293 return STATE_CRITICAL;
295 /* Network is unreachable */
296 else if (strstr (input_buffer, _("Network is unreachable")))
297 return STATE_CRITICAL;
299 /* Internal server failure */
300 else if (strstr (input_buffer, _("Server failure")))
301 return STATE_CRITICAL;
303 /* DNS server refused to service request */
304 else if (strstr (input_buffer, _("Refused")))
305 return STATE_CRITICAL;
307 /* Request error */
308 else if (strstr (input_buffer, _("Format error")))
309 return STATE_WARNING;
311 else
312 return STATE_OK;
314 }
316 /* process command-line arguments */
317 int
318 process_arguments (int argc, char **argv)
319 {
320 int c;
322 int opt_index = 0;
323 static struct option long_opts[] = {
324 {"help", no_argument, 0, 'h'},
325 {"version", no_argument, 0, 'V'},
326 {"verbose", no_argument, 0, 'v'},
327 {"timeout", required_argument, 0, 't'},
328 {"hostname", required_argument, 0, 'H'},
329 {"server", required_argument, 0, 's'},
330 {"reverse-server", required_argument, 0, 'r'},
331 {"expected-address", required_argument, 0, 'a'},
332 {0, 0, 0, 0}
333 };
335 if (argc < 2)
336 return ERROR;
338 for (c = 1; c < argc; c++)
339 if (strcmp ("-to", argv[c]) == 0)
340 strcpy (argv[c], "-t");
342 while (1) {
343 c = getopt_long (argc, argv, "hVvt:H:s:r:a:", long_opts, &opt_index);
345 if (c == -1 || c == EOF)
346 break;
348 switch (c) {
349 case '?': /* args not parsable */
350 printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
351 print_usage ();
352 exit (STATE_UNKNOWN);
353 case 'h': /* help */
354 print_help ();
355 exit (STATE_OK);
356 case 'V': /* version */
357 print_revision (progname, revision);
358 exit (STATE_OK);
359 case 'v': /* version */
360 verbose = TRUE;
361 break;
362 case 't': /* timeout period */
363 timeout_interval = atoi (optarg);
364 break;
365 case 'H': /* hostname */
366 if (strlen (optarg) >= ADDRESS_LENGTH)
367 terminate (STATE_UNKNOWN, _("Input buffer overflow\n"));
368 strcpy (query_address, optarg);
369 break;
370 case 's': /* server name */
371 /* TODO: this is_host check is probably unnecessary. Better to confirm nslookup
372 response matches */
373 if (is_host (optarg) == FALSE) {
374 printf (_("Invalid server name/address\n\n"));
375 print_usage ();
376 exit (STATE_UNKNOWN);
377 }
378 if (strlen (optarg) >= ADDRESS_LENGTH)
379 terminate (STATE_UNKNOWN, _("Input buffer overflow\n"));
380 strcpy (dns_server, optarg);
381 break;
382 case 'r': /* reverse server name */
383 /* TODO: Is this is_host necessary? */
384 if (is_host (optarg) == FALSE) {
385 printf (_("Invalid host name/address\n\n"));
386 print_usage ();
387 exit (STATE_UNKNOWN);
388 }
389 if (strlen (optarg) >= ADDRESS_LENGTH)
390 terminate (STATE_UNKNOWN, _("Input buffer overflow\n"));
391 strcpy (ptr_server, optarg);
392 break;
393 case 'a': /* expected address */
394 if (strlen (optarg) >= ADDRESS_LENGTH)
395 terminate (STATE_UNKNOWN, _("Input buffer overflow\n"));
396 strcpy (expected_address, optarg);
397 match_expected_address = TRUE;
398 break;
399 }
400 }
402 c = optind;
403 if (strlen(query_address)==0 && c<argc) {
404 if (strlen(argv[c])>=ADDRESS_LENGTH)
405 terminate (STATE_UNKNOWN, _("Input buffer overflow\n"));
406 strcpy (query_address, argv[c++]);
407 }
409 if (strlen(dns_server)==0 && c<argc) {
410 /* TODO: See -s option */
411 if (is_host(argv[c]) == FALSE) {
412 printf (_("Invalid name/address: %s\n\n"), argv[c]);
413 return ERROR;
414 }
415 if (strlen(argv[c]) >= ADDRESS_LENGTH)
416 terminate (STATE_UNKNOWN, _("Input buffer overflow\n"));
417 strcpy (dns_server, argv[c++]);
418 }
420 return validate_arguments ();
421 }
423 int
424 validate_arguments ()
425 {
426 if (query_address[0] == 0)
427 return ERROR;
428 else
429 return OK;
430 }