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"
51 const char *progname = "check_dns";
52 #define REVISION "$Revision$"
53 #define COPYRIGHT "2000-2002"
55 int process_arguments (int, char **);
56 int validate_arguments (void);
57 void print_usage (void);
58 void print_help (void);
59 int error_scan (char *);
61 #define ADDRESS_LENGTH 256
62 char query_address[ADDRESS_LENGTH] = "";
63 char dns_server[ADDRESS_LENGTH] = "";
64 char ptr_server[ADDRESS_LENGTH] = "";
65 int verbose = FALSE;
66 char expected_address[ADDRESS_LENGTH] = "";
67 int match_expected_address = FALSE;
69 int
70 main (int argc, char **argv)
71 {
72 char *command_line = NULL;
73 char input_buffer[MAX_INPUT_BUFFER];
74 char *output = NULL;
75 char *address = NULL;
76 char *temp_buffer = NULL;
77 int result = STATE_UNKNOWN;
79 /* Set signal handling and alarm */
80 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
81 printf ("Cannot catch SIGALRM");
82 return STATE_UNKNOWN;
83 }
85 if (process_arguments (argc, argv) != OK) {
86 print_usage ();
87 return STATE_UNKNOWN;
88 }
90 /* get the command to run */
91 asprintf (&command_line, "%s %s %s", NSLOOKUP_COMMAND, query_address, dns_server);
93 alarm (timeout_interval);
94 time (&start_time);
96 if (verbose)
97 printf ("%s\n", command_line);
98 /* run the command */
99 child_process = spopen (command_line);
100 if (child_process == NULL) {
101 printf ("Could not open pipe: %s\n", command_line);
102 return STATE_UNKNOWN;
103 }
105 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
106 if (child_stderr == NULL)
107 printf ("Could not open stderr for %s\n", command_line);
109 /* scan stdout */
110 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
112 if (verbose)
113 printf ("%s\n", input_buffer);
115 if (strstr (input_buffer, ".in-addr.arpa")) {
116 if ((temp_buffer = strstr (input_buffer, "name = ")))
117 address = strscpy (address, temp_buffer + 7);
118 else {
119 output = strscpy (output, "Unknown error (plugin)");
120 result = STATE_WARNING;
121 }
122 }
124 /* the server is responding, we just got the host name... */
125 if (strstr (input_buffer, "Name:")) {
127 /* get the host address */
128 if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
129 break;
131 if (verbose)
132 printf ("%s\n", input_buffer);
134 if ((temp_buffer = index (input_buffer, ':'))) {
135 address = strscpy (address, temp_buffer + 2);
136 strip (address);
137 result = STATE_OK;
138 }
139 else {
140 output = strscpy (output, "Unknown error (plugin)");
141 result = STATE_WARNING;
142 }
144 break;
145 }
147 result = error_scan (input_buffer);
148 if (result != STATE_OK) {
149 output = strscpy (output, 1 + index (input_buffer, ':'));
150 break;
151 }
153 }
155 /* scan stderr */
156 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
157 if (error_scan (input_buffer) != STATE_OK) {
158 result = max_state (result, error_scan (input_buffer));
159 output = strscpy (output, 1 + index (input_buffer, ':'));
160 }
161 }
163 /* close stderr */
164 (void) fclose (child_stderr);
166 /* close stdout */
167 if (spclose (child_process)) {
168 result = max_state (result, STATE_WARNING);
169 if (!strcmp (output, ""))
170 output = strscpy (output, "nslookup returned error status");
171 }
173 /* compare to expected address */
174 if (result == STATE_OK && match_expected_address && strcmp(address, expected_address)) {
175 result = STATE_CRITICAL;
176 asprintf(&output, "expected %s but got %s", expected_address, address);
177 }
179 (void) time (&end_time);
181 if (result == STATE_OK)
182 printf ("DNS ok - %d seconds response time, Address(es) is/are %s\n",
183 (int) (end_time - start_time), address);
184 else if (result == STATE_WARNING)
185 printf ("DNS WARNING - %s\n",
186 !strcmp (output, "") ? " Probably a non-existent host/domain" : output);
187 else if (result == STATE_CRITICAL)
188 printf ("DNS CRITICAL - %s\n",
189 !strcmp (output, "") ? " Probably a non-existent host/domain" : output);
190 else
191 printf ("DNS problem - %s\n",
192 !strcmp (output, "") ? " Probably a non-existent host/domain" : output);
194 return result;
195 }
197 int
198 error_scan (char *input_buffer)
199 {
201 /* the DNS lookup timed out */
202 if (strstr (input_buffer,
203 "Note: nslookup is deprecated and may be removed from future releases.")
204 || strstr (input_buffer,
205 "Consider using the `dig' or `host' programs instead. Run nslookup with")
206 || strstr (input_buffer,
207 "the `-sil[ent]' option to prevent this message from appearing."))
208 return STATE_OK;
210 /* the DNS lookup timed out */
211 else if (strstr (input_buffer, "Timed out"))
212 return STATE_WARNING;
214 /* DNS server is not running... */
215 else if (strstr (input_buffer, "No response from server"))
216 return STATE_CRITICAL;
218 /* Host name is valid, but server doesn't have records... */
219 else if (strstr (input_buffer, "No records"))
220 return STATE_WARNING;
222 /* Host or domain name does not exist */
223 else if (strstr (input_buffer, "Non-existent"))
224 return STATE_CRITICAL;
225 else if (strstr (input_buffer, "** server can't find"))
226 return STATE_CRITICAL;
227 else if(strstr(input_buffer,"NXDOMAIN")) /* 9.x */
228 return STATE_CRITICAL;
230 /* Connection was refused */
231 else if (strstr (input_buffer, "Connection refused"))
232 return STATE_CRITICAL;
234 /* Network is unreachable */
235 else if (strstr (input_buffer, "Network is unreachable"))
236 return STATE_CRITICAL;
238 /* Internal server failure */
239 else if (strstr (input_buffer, "Server failure"))
240 return STATE_CRITICAL;
242 /* DNS server refused to service request */
243 else if (strstr (input_buffer, "Refused"))
244 return STATE_CRITICAL;
246 /* Request error */
247 else if (strstr (input_buffer, "Format error"))
248 return STATE_WARNING;
250 else
251 return STATE_OK;
253 }
255 /* process command-line arguments */
256 int
257 process_arguments (int argc, char **argv)
258 {
259 int c;
261 #ifdef HAVE_GETOPT_H
262 int opt_index = 0;
263 static struct option long_opts[] = {
264 {"help", no_argument, 0, 'h'},
265 {"version", no_argument, 0, 'V'},
266 {"verbose", no_argument, 0, 'v'},
267 {"timeout", required_argument, 0, 't'},
268 {"hostname", required_argument, 0, 'H'},
269 {"server", required_argument, 0, 's'},
270 {"reverse-server", required_argument, 0, 'r'},
271 {"expected-address", required_argument, 0, 'a'},
272 {0, 0, 0, 0}
273 };
274 #endif
276 if (argc < 2)
277 return ERROR;
279 for (c = 1; c < argc; c++)
280 if (strcmp ("-to", argv[c]) == 0)
281 strcpy (argv[c], "-t");
283 while (1) {
284 #ifdef HAVE_GETOPT_H
285 c = getopt_long (argc, argv, "hVvt:H:s:r:a:", long_opts, &opt_index);
286 #else
287 c = getopt (argc, argv, "hVvt:H:s:r:a:");
288 #endif
290 if (c == -1 || c == EOF)
291 break;
293 switch (c) {
294 case '?': /* args not parsable */
295 printf ("%s: Unknown argument: %s\n\n", progname, optarg);
296 print_usage ();
297 exit (STATE_UNKNOWN);
298 case 'h': /* help */
299 print_help ();
300 exit (STATE_OK);
301 case 'V': /* version */
302 print_revision (progname, REVISION);
303 exit (STATE_OK);
304 case 'v': /* version */
305 verbose = TRUE;
306 break;
307 case 't': /* timeout period */
308 timeout_interval = atoi (optarg);
309 break;
310 case 'H': /* hostname */
311 if (is_host (optarg) == FALSE) {
312 printf ("Invalid host name/address\n\n");
313 print_usage ();
314 exit (STATE_UNKNOWN);
315 }
316 if (strlen (optarg) >= ADDRESS_LENGTH)
317 terminate (STATE_UNKNOWN, "Input buffer overflow\n");
318 strcpy (query_address, optarg);
319 break;
320 case 's': /* server name */
321 if (is_host (optarg) == FALSE) {
322 printf ("Invalid server name/address\n\n");
323 print_usage ();
324 exit (STATE_UNKNOWN);
325 }
326 if (strlen (optarg) >= ADDRESS_LENGTH)
327 terminate (STATE_UNKNOWN, "Input buffer overflow\n");
328 strcpy (dns_server, optarg);
329 break;
330 case 'r': /* reverse server name */
331 if (is_host (optarg) == FALSE) {
332 printf ("Invalid host name/address\n\n");
333 print_usage ();
334 exit (STATE_UNKNOWN);
335 }
336 if (strlen (optarg) >= ADDRESS_LENGTH)
337 terminate (STATE_UNKNOWN, "Input buffer overflow\n");
338 strcpy (ptr_server, optarg);
339 break;
340 case 'a': /* expected address */
341 if (is_dotted_quad (optarg) == FALSE) {
342 printf ("Invalid expected address\n\n");
343 print_usage ();
344 exit (STATE_UNKNOWN);
345 }
346 if (strlen (optarg) >= ADDRESS_LENGTH)
347 terminate (STATE_UNKNOWN, "Input buffer overflow\n");
348 strcpy (expected_address, optarg);
349 match_expected_address = TRUE;
350 break;
351 }
352 }
354 c = optind;
355 if (strlen(query_address)==0 && c<argc) {
356 if (is_host(argv[c])==FALSE) {
357 printf ("Invalid name/address: %s\n\n", argv[c]);
358 return ERROR;
359 }
360 if (strlen(argv[c])>=ADDRESS_LENGTH)
361 terminate (STATE_UNKNOWN, "Input buffer overflow\n");
362 strcpy (query_address, argv[c++]);
363 }
365 if (strlen(dns_server)==0 && c<argc) {
366 if (is_host(argv[c]) == FALSE) {
367 printf ("Invalid name/address: %s\n\n", argv[c]);
368 return ERROR;
369 }
370 if (strlen(argv[c]) >= ADDRESS_LENGTH)
371 terminate (STATE_UNKNOWN, "Input buffer overflow\n");
372 strcpy (dns_server, argv[c++]);
373 }
375 return validate_arguments ();
376 }
378 int
379 validate_arguments ()
380 {
381 if (query_address[0] == 0)
382 return ERROR;
383 else
384 return OK;
385 }
387 void
388 print_usage (void)
389 {
390 printf ("Usage: %s -H host [-s server] [-a expected-address] [-t timeout]\n" " %s --help\n"
391 " %s --version\n", progname, progname, progname);
392 }
394 void
395 print_help (void)
396 {
397 print_revision (progname, REVISION);
398 printf ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n\n");
399 print_usage ();
400 printf
401 ("\nOptions:\n"
402 "-H, --hostname=HOST\n"
403 " The name or address you want to query\n"
404 "-s, --server=HOST\n"
405 " Optional DNS server you want to use for the lookup\n"
406 "-a, --expected-address=IP-ADDRESS\n"
407 " Optional IP address you expect the DNS server to return\n"
408 "-t, --timeout=INTEGER\n"
409 " Seconds before connection times out (default: %d)\n"
410 "-h, --help\n"
411 " Print detailed help\n"
412 "-V, --version\n"
413 " Print version numbers and license information\n"
414 "\n"
415 "This plugin uses the nslookup program to obtain the IP address\n"
416 "for the given host/domain query. A optional DNS server to use may\n"
417 "be specified. If no DNS server is specified, the default server(s)\n"
418 "specified in /etc/resolv.conf will be used.\n", DEFAULT_SOCKET_TIMEOUT);
419 }