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 #include "common.h"
25 #include "popen.h"
26 #include "utils.h"
27 #include "netutils.h"
29 const char *progname = "check_dns";
30 const char *revision = "$Revision$";
31 const char *copyright = "2000-2004";
32 const char *email = "nagiosplug-devel@lists.sourceforge.net";
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 printf (_("Cannot catch SIGALRM"));
72 return STATE_UNKNOWN;
73 }
75 if (process_arguments (argc, argv) != TRUE) {
76 usage (_("check_dns: could not parse arguments\n"));
77 }
79 /* get the command to run */
80 asprintf (&command_line, "%s %s %s", NSLOOKUP_COMMAND, query_address, dns_server);
82 alarm (timeout_interval);
83 gettimeofday (&tv, NULL);
85 if (verbose)
86 printf ("%s\n", command_line);
88 /* run the command */
89 child_process = spopen (command_line);
90 if (child_process == NULL) {
91 printf (_("Could not open pipe: %s\n"), command_line);
92 return STATE_UNKNOWN;
93 }
95 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
96 if (child_stderr == NULL)
97 printf (_("Could not open stderr for %s\n"), command_line);
99 /* scan stdout */
100 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
102 if (verbose)
103 printf ("%s", input_buffer);
105 if (strstr (input_buffer, ".in-addr.arpa")) {
106 if ((temp_buffer = strstr (input_buffer, "name = ")))
107 address = strdup (temp_buffer + 7);
108 else {
109 output = strdup (_("Warning plugin error"));
110 result = STATE_WARNING;
111 }
112 }
114 /* the server is responding, we just got the host name... */
115 if (strstr (input_buffer, "Name:"))
116 parse_address = TRUE;
117 else if (parse_address == TRUE && (strstr (input_buffer, "Address:") ||
118 strstr (input_buffer, "Addresses:"))) {
119 temp_buffer = index (input_buffer, ':');
120 temp_buffer++;
122 /* Strip leading spaces */
123 for (; *temp_buffer != '\0' && *temp_buffer == ' '; temp_buffer++)
124 /* NOOP */;
126 strip(temp_buffer);
127 if (temp_buffer==NULL || strlen(temp_buffer)==0) {
128 die (STATE_CRITICAL,
129 _("DNS CRITICAL - '%s' returned empty host name string\n"),
130 NSLOOKUP_COMMAND);
131 }
133 if (address == NULL)
134 address = strdup (temp_buffer);
135 else
136 asprintf(&address, "%s,%s", address, temp_buffer);
137 }
139 else if (strstr (input_buffer, "Non-authoritative answer:")) {
140 non_authoritative = TRUE;
141 }
143 result = error_scan (input_buffer);
144 if (result != STATE_OK) {
145 output = strdup (1 + index (input_buffer, ':'));
146 strip (output);
147 break;
148 }
150 }
152 /* scan stderr */
153 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
154 if (error_scan (input_buffer) != STATE_OK) {
155 result = max_state (result, error_scan (input_buffer));
156 output = strdup (1 + index (input_buffer, ':'));
157 strip (output);
158 }
159 }
161 /* close stderr */
162 (void) fclose (child_stderr);
164 /* close stdout */
165 if (spclose (child_process)) {
166 result = max_state (result, STATE_WARNING);
167 if (!strcmp (output, ""))
168 output = strdup (_("nslookup returned error status"));
169 }
171 /* If we got here, we should have an address string,
172 and we can segfault if we do not */
173 if (address==NULL || strlen(address)==0)
174 die (STATE_CRITICAL,
175 _("DNS CRITICAL - '%s' output parsing exited with no address\n"),
176 NSLOOKUP_COMMAND);
178 /* compare to expected address */
179 if (result == STATE_OK && match_expected_address && strcmp(address, expected_address)) {
180 result = STATE_CRITICAL;
181 asprintf(&output, _("expected %s but got %s"), expected_address, address);
182 }
184 /* check if authoritative */
185 if (result == STATE_OK && expect_authority && non_authoritative) {
186 result = STATE_CRITICAL;
187 asprintf(&output, _("server %s is not authoritative for %s"), dns_server, query_address);
188 }
190 microsec = deltime (tv);
191 elapsed_time = (double)microsec / 1.0e6;
193 if (result == STATE_OK) {
194 if (strchr (address, ',') == NULL)
195 multi_address = FALSE;
196 else
197 multi_address = TRUE;
199 printf ("DNS %s: ", _("OK"));
200 printf (ngettext("%.3f second response time, ", "%.3f seconds response time, ", elapsed_time), elapsed_time);
201 printf (_("%s returns %s"), query_address, address);
202 printf ("|%s\n", fperfdata ("time", elapsed_time, "s", FALSE, 0, FALSE, 0, TRUE, 0, FALSE, 0));
203 }
204 else if (result == STATE_WARNING)
205 printf (_("DNS WARNING - %s\n"),
206 !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output);
207 else if (result == STATE_CRITICAL)
208 printf (_("DNS CRITICAL - %s\n"),
209 !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output);
210 else
211 printf (_("DNS UNKNOW - %s\n"),
212 !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output);
214 return result;
215 }
219 int
220 error_scan (char *input_buffer)
221 {
223 /* the DNS lookup timed out */
224 if (strstr (input_buffer, "Note: nslookup is deprecated and may be removed from future releases.") ||
225 strstr (input_buffer, "Consider using the `dig' or `host' programs instead. Run nslookup with") ||
226 strstr (input_buffer, "the `-sil[ent]' option to prevent this message from appearing."))
227 return STATE_OK;
229 /* DNS server is not running... */
230 else if (strstr (input_buffer, "No response from server"))
231 die (STATE_CRITICAL, _("No response from DNS %s\n"), dns_server);
233 /* Host name is valid, but server doesn't have records... */
234 else if (strstr (input_buffer, "No records"))
235 die (STATE_CRITICAL, _("DNS %s has no records\n"), dns_server);
237 /* Connection was refused */
238 else if (strstr (input_buffer, "Connection refused") ||
239 strstr (input_buffer, "Refused") ||
240 (strstr (input_buffer, "** server can't find") &&
241 strstr (input_buffer, ": REFUSED")))
242 die (STATE_CRITICAL, _("Connection to DNS %s was refused\n"), dns_server);
244 /* Host or domain name does not exist */
245 else if (strstr (input_buffer, "Non-existent") ||
246 strstr (input_buffer, "** server can't find") ||
247 strstr (input_buffer,"NXDOMAIN"))
248 die (STATE_CRITICAL, _("Domain %s was not found by the server\n"), query_address);
250 /* Network is unreachable */
251 else if (strstr (input_buffer, "Network is unreachable"))
252 die (STATE_CRITICAL, _("Network is unreachable\n"));
254 /* Internal server failure */
255 else if (strstr (input_buffer, "Server failure"))
256 die (STATE_CRITICAL, _("DNS failure for %s\n"), dns_server);
258 /* Request error or the DNS lookup timed out */
259 else if (strstr (input_buffer, "Format error") ||
260 strstr (input_buffer, "Timed out"))
261 return STATE_WARNING;
263 return STATE_OK;
265 }
269 /* process command-line arguments */
270 int
271 process_arguments (int argc, char **argv)
272 {
273 int c;
275 int opt_index = 0;
276 static struct option long_opts[] = {
277 {"help", no_argument, 0, 'h'},
278 {"version", no_argument, 0, 'V'},
279 {"verbose", no_argument, 0, 'v'},
280 {"timeout", required_argument, 0, 't'},
281 {"hostname", required_argument, 0, 'H'},
282 {"server", required_argument, 0, 's'},
283 {"reverse-server", required_argument, 0, 'r'},
284 {"expected-address", required_argument, 0, 'a'},
285 {"expect-authority", no_argument, 0, 'A'},
286 {0, 0, 0, 0}
287 };
289 if (argc < 2)
290 return ERROR;
292 for (c = 1; c < argc; c++)
293 if (strcmp ("-to", argv[c]) == 0)
294 strcpy (argv[c], "-t");
296 while (1) {
297 c = getopt_long (argc, argv, "hVvAt:H:s:r:a:", long_opts, &opt_index);
299 if (c == -1 || c == EOF)
300 break;
302 switch (c) {
303 case '?': /* args not parsable */
304 printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
305 print_usage ();
306 exit (STATE_UNKNOWN);
307 case 'h': /* help */
308 print_help ();
309 exit (STATE_OK);
310 case 'V': /* version */
311 print_revision (progname, revision);
312 exit (STATE_OK);
313 case 'v': /* version */
314 verbose = TRUE;
315 break;
316 case 't': /* timeout period */
317 timeout_interval = atoi (optarg);
318 break;
319 case 'H': /* hostname */
320 if (strlen (optarg) >= ADDRESS_LENGTH)
321 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
322 strcpy (query_address, optarg);
323 break;
324 case 's': /* server name */
325 /* TODO: this is_host check is probably unnecessary. */
326 /* Better to confirm nslookup response matches */
327 if (is_host (optarg) == FALSE) {
328 printf (_("Invalid hostname/address\n\n"));
329 print_usage ();
330 exit (STATE_UNKNOWN);
331 }
332 if (strlen (optarg) >= ADDRESS_LENGTH)
333 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
334 strcpy (dns_server, optarg);
335 break;
336 case 'r': /* reverse server name */
337 /* TODO: Is this is_host necessary? */
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 (ptr_server, optarg);
344 break;
345 case 'a': /* expected address */
346 if (strlen (optarg) >= ADDRESS_LENGTH)
347 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
348 strcpy (expected_address, optarg);
349 match_expected_address = TRUE;
350 break;
351 case 'A': /* expect authority */
352 expect_authority = TRUE;
353 break;
354 }
355 }
357 c = optind;
358 if (strlen(query_address)==0 && c<argc) {
359 if (strlen(argv[c])>=ADDRESS_LENGTH)
360 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
361 strcpy (query_address, argv[c++]);
362 }
364 if (strlen(dns_server)==0 && c<argc) {
365 /* TODO: See -s option */
366 if (is_host(argv[c]) == FALSE) {
367 printf (_("Invalid hostname/address: %s\n\n"), argv[c]);
368 return ERROR;
369 }
370 if (strlen(argv[c]) >= ADDRESS_LENGTH)
371 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
372 strcpy (dns_server, argv[c++]);
373 }
375 return validate_arguments ();
376 }
380 int
381 validate_arguments ()
382 {
383 if (query_address[0] == 0)
384 return ERROR;
385 else
386 return OK;
387 }
391 void
392 print_help (void)
393 {
394 print_revision (progname, revision);
396 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
397 printf (COPYRIGHT, copyright, email);
399 print_usage ();
401 printf (_(UT_HELP_VRSN));
403 printf (_("\
404 -H, --hostname=HOST\n\
405 The name or address you want to query\n\
406 -s, --server=HOST\n\
407 Optional DNS server you want to use for the lookup\n\
408 -a, --expected-address=IP-ADDRESS\n\
409 Optional IP address you expect the DNS server to return\n\
410 -A, --expect-authority\n\
411 Optionally expect the DNS server to be authoritative for the lookup\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 }
426 void
427 print_usage (void)
428 {
429 printf (_("\
430 Usage: %s -H host [-s server] [-a expected-address] [-A] [-t timeout]\n\
431 %s --help\n\
432 %s --version\n"), progname, progname, progname);
433 }