d6e8ca29d51d9fd5cfd1cb944ee6ac68f8267101
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 const char *progname = "check_dns";
25 const char *revision = "$Revision$";
26 const char *copyright = "2000-2004";
27 const char *email = "nagiosplug-devel@lists.sourceforge.net";
29 #include "common.h"
30 #include "utils.h"
31 #include "netutils.h"
32 #include "runcmd.h"
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 *address = NULL;
55 char *msg = 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: */
64 output chld_out, chld_err;
65 size_t i;
67 setlocale (LC_ALL, "");
68 bindtextdomain (PACKAGE, LOCALEDIR);
69 textdomain (PACKAGE);
71 /* Set signal handling and alarm */
72 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
73 usage_va(_("Cannot catch SIGALRM"));
74 }
76 if (process_arguments (argc, argv) == ERROR) {
77 usage_va(_("Could not parse arguments"));
78 }
80 /* get the command to run */
81 asprintf (&command_line, "%s %s %s", NSLOOKUP_COMMAND, query_address, dns_server);
83 alarm (timeout_interval);
84 gettimeofday (&tv, NULL);
86 if (verbose)
87 printf ("%s\n", command_line);
89 /* run the command */
90 if((np_runcmd(command_line, &chld_out, &chld_err, 0)) != 0) {
91 msg = (char *)_("nslookup returned error status");
92 result = STATE_WARNING;
93 }
95 /* scan stdout */
96 for(i = 0; i < chld_out.lines; i++) {
97 if (verbose)
98 puts(chld_out.line[i]);
100 if (strstr (chld_out.line[i], ".in-addr.arpa")) {
101 if ((temp_buffer = strstr (chld_out.line[i], "name = ")))
102 address = strdup (temp_buffer + 7);
103 else {
104 msg = (char *)_("Warning plugin error");
105 result = STATE_WARNING;
106 }
107 }
109 /* the server is responding, we just got the host name... */
110 if (strstr (chld_out.line[i], "Name:"))
111 parse_address = TRUE;
112 else if (parse_address == TRUE && (strstr (chld_out.line[i], "Address:") ||
113 strstr (chld_out.line[i], "Addresses:"))) {
114 temp_buffer = index (chld_out.line[i], ':');
115 temp_buffer++;
117 /* Strip leading spaces */
118 for (; *temp_buffer != '\0' && *temp_buffer == ' '; temp_buffer++)
119 /* NOOP */;
121 strip(temp_buffer);
122 if (temp_buffer==NULL || strlen(temp_buffer)==0) {
123 die (STATE_CRITICAL,
124 _("DNS CRITICAL - '%s' returned empty host name string\n"),
125 NSLOOKUP_COMMAND);
126 }
128 if (address == NULL)
129 address = strdup (temp_buffer);
130 else
131 asprintf(&address, "%s,%s", address, temp_buffer);
132 }
134 else if (strstr (chld_out.line[i], _("Non-authoritative answer:"))) {
135 non_authoritative = TRUE;
136 }
138 result = error_scan (chld_out.line[i]);
139 if (result != STATE_OK) {
140 msg = strchr (chld_out.line[i], ':');
141 if(msg) msg++;
142 break;
143 }
144 }
146 /* scan stderr */
147 for(i = 0; i < chld_err.lines; i++) {
148 if (verbose)
149 puts(chld_err.line[i]);
151 if (error_scan (chld_err.line[i]) != STATE_OK) {
152 result = max_state (result, error_scan (chld_err.line[i]));
153 msg = strchr(input_buffer, ':');
154 if(msg) msg++;
155 }
156 }
158 /* If we got here, we should have an address string,
159 * and we can segfault if we do not */
160 if (address==NULL || strlen(address)==0)
161 die (STATE_CRITICAL,
162 _("DNS CRITICAL - '%s' msg parsing exited with no address\n"),
163 NSLOOKUP_COMMAND);
165 /* compare to expected address */
166 if (result == STATE_OK && match_expected_address && strcmp(address, expected_address)) {
167 result = STATE_CRITICAL;
168 asprintf(&msg, _("expected %s but got %s"), expected_address, address);
169 }
171 /* check if authoritative */
172 if (result == STATE_OK && expect_authority && non_authoritative) {
173 result = STATE_CRITICAL;
174 asprintf(&msg, _("server %s is not authoritative for %s"), dns_server, query_address);
175 }
177 microsec = deltime (tv);
178 elapsed_time = (double)microsec / 1.0e6;
180 if (result == STATE_OK) {
181 if (strchr (address, ',') == NULL)
182 multi_address = FALSE;
183 else
184 multi_address = TRUE;
186 printf ("DNS %s: ", _("OK"));
187 printf (ngettext("%.3f second response time", "%.3f seconds response time", elapsed_time), elapsed_time);
188 printf (_(". %s returns %s"), query_address, address);
189 printf ("|%s\n", fperfdata ("time", elapsed_time, "s", FALSE, 0, FALSE, 0, TRUE, 0, FALSE, 0));
190 }
191 else if (result == STATE_WARNING)
192 printf (_("DNS WARNING - %s\n"),
193 !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
194 else if (result == STATE_CRITICAL)
195 printf (_("DNS CRITICAL - %s\n"),
196 !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
197 else
198 printf (_("DNS UNKNOW - %s\n"),
199 !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
201 return result;
202 }
206 int
207 error_scan (char *input_buffer)
208 {
210 /* the DNS lookup timed out */
211 if (strstr (input_buffer, _("Note: nslookup is deprecated and may be removed from future releases.")) ||
212 strstr (input_buffer, _("Consider using the `dig' or `host' programs instead. Run nslookup with")) ||
213 strstr (input_buffer, _("the `-sil[ent]' option to prevent this message from appearing.")))
214 return STATE_OK;
216 /* DNS server is not running... */
217 else if (strstr (input_buffer, "No response from server"))
218 die (STATE_CRITICAL, _("No response from DNS %s\n"), dns_server);
220 /* Host name is valid, but server doesn't have records... */
221 else if (strstr (input_buffer, "No records"))
222 die (STATE_CRITICAL, _("DNS %s has no records\n"), dns_server);
224 /* Connection was refused */
225 else if (strstr (input_buffer, "Connection refused") ||
226 strstr (input_buffer, "Couldn't find server") ||
227 strstr (input_buffer, "Refused") ||
228 (strstr (input_buffer, "** server can't find") &&
229 strstr (input_buffer, ": REFUSED")))
230 die (STATE_CRITICAL, _("Connection to DNS %s was refused\n"), dns_server);
232 /* Query refused (usually by an ACL in the namserver) */
233 else if (strstr (input_buffer, "Query refused"))
234 die (STATE_CRITICAL, _("Query was refused by DNS server at %s\n"), dns_server);
236 /* No information (e.g. nameserver IP has two PTR records) */
237 else if (strstr (input_buffer, "No information"))
238 die (STATE_CRITICAL, _("No information returned by DNS server at %s\n"), dns_server);
240 /* Host or domain name does not exist */
241 else if (strstr (input_buffer, "Non-existent") ||
242 strstr (input_buffer, "** server can't find") ||
243 strstr (input_buffer,"NXDOMAIN"))
244 die (STATE_CRITICAL, _("Domain %s was not found by the server\n"), query_address);
246 /* Network is unreachable */
247 else if (strstr (input_buffer, "Network is unreachable"))
248 die (STATE_CRITICAL, _("Network is unreachable\n"));
250 /* Internal server failure */
251 else if (strstr (input_buffer, "Server failure"))
252 die (STATE_CRITICAL, _("DNS failure for %s\n"), dns_server);
254 /* Request error or the DNS lookup timed out */
255 else if (strstr (input_buffer, "Format error") ||
256 strstr (input_buffer, "Timed out"))
257 return STATE_WARNING;
259 return STATE_OK;
261 }
264 /* process command-line arguments */
265 int
266 process_arguments (int argc, char **argv)
267 {
268 int c;
270 int opt_index = 0;
271 static struct option long_opts[] = {
272 {"help", no_argument, 0, 'h'},
273 {"version", no_argument, 0, 'V'},
274 {"verbose", no_argument, 0, 'v'},
275 {"timeout", required_argument, 0, 't'},
276 {"hostname", required_argument, 0, 'H'},
277 {"server", required_argument, 0, 's'},
278 {"reverse-server", required_argument, 0, 'r'},
279 {"expected-address", required_argument, 0, 'a'},
280 {"expect-authority", no_argument, 0, 'A'},
281 {0, 0, 0, 0}
282 };
284 if (argc < 2)
285 return ERROR;
287 for (c = 1; c < argc; c++)
288 if (strcmp ("-to", argv[c]) == 0)
289 strcpy (argv[c], "-t");
291 while (1) {
292 c = getopt_long (argc, argv, "hVvAt:H:s:r:a:", long_opts, &opt_index);
294 if (c == -1 || c == EOF)
295 break;
297 switch (c) {
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 (strlen (optarg) >= ADDRESS_LENGTH)
312 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
313 strcpy (query_address, optarg);
314 break;
315 case 's': /* server name */
316 /* TODO: this host_or_die check is probably unnecessary.
317 * Better to confirm nslookup response matches */
318 host_or_die(optarg);
319 if (strlen (optarg) >= ADDRESS_LENGTH)
320 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
321 strcpy (dns_server, optarg);
322 break;
323 case 'r': /* reverse server name */
324 /* TODO: Is this host_or_die necessary? */
325 host_or_die(optarg);
326 if (strlen (optarg) >= ADDRESS_LENGTH)
327 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
328 strcpy (ptr_server, optarg);
329 break;
330 case 'a': /* expected address */
331 if (strlen (optarg) >= ADDRESS_LENGTH)
332 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
333 strcpy (expected_address, optarg);
334 match_expected_address = TRUE;
335 break;
336 case 'A': /* expect authority */
337 expect_authority = TRUE;
338 break;
339 default: /* args not parsable */
340 usage_va(_("Unknown argument - %s"), optarg);
341 }
342 }
344 c = optind;
345 if (strlen(query_address)==0 && c<argc) {
346 if (strlen(argv[c])>=ADDRESS_LENGTH)
347 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
348 strcpy (query_address, argv[c++]);
349 }
351 if (strlen(dns_server)==0 && c<argc) {
352 /* TODO: See -s option */
353 host_or_die(argv[c]);
354 if (strlen(argv[c]) >= ADDRESS_LENGTH)
355 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
356 strcpy (dns_server, argv[c++]);
357 }
359 return validate_arguments ();
360 }
363 int
364 validate_arguments ()
365 {
366 if (query_address[0] == 0)
367 return ERROR;
369 return OK;
370 }
373 void
374 print_help (void)
375 {
376 print_revision (progname, revision);
378 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
379 printf (COPYRIGHT, copyright, email);
381 printf (_("\
382 This plugin uses the nslookup program to obtain the IP address\n\
383 for the given host/domain query. A optional DNS server to use may\n\
384 be specified. If no DNS server is specified, the default server(s)\n\
385 specified in /etc/resolv.conf will be used.\n\n"));
387 print_usage ();
389 printf (_(UT_HELP_VRSN));
391 printf (_("\
392 -H, --hostname=HOST\n\
393 The name or address you want to query\n\
394 -s, --server=HOST\n\
395 Optional DNS server you want to use for the lookup\n\
396 -a, --expected-address=IP-ADDRESS\n\
397 Optional IP address you expect the DNS server to return\n\
398 -A, --expect-authority\n\
399 Optionally expect the DNS server to be authoritative for the lookup\n"));
401 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
403 printf (_(UT_SUPPORT));
404 }
407 void
408 print_usage (void)
409 {
410 printf ("\
411 Usage: %s -H host [-s server] [-a expected-address] [-A] [-t timeout]\n", progname);
412 }