43a38fa0c71ea39787a3311b02ba6f1ca4d07e66
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 ******************************************************************************/
22 #include "common.h"
23 #include "popen.h"
24 #include "utils.h"
25 #include "netutils.h"
27 const char *progname = "check_dns";
28 const char *revision = "$Revision$";
29 const char *copyright = "2000-2004";
30 const char *email = "nagiosplug-devel@lists.sourceforge.net";
32 int process_arguments (int, char **);
33 int validate_arguments (void);
34 int error_scan (char *);
35 void print_help (void);
36 void print_usage (void);
38 #define ADDRESS_LENGTH 256
39 char query_address[ADDRESS_LENGTH] = "";
40 char dns_server[ADDRESS_LENGTH] = "";
41 char ptr_server[ADDRESS_LENGTH] = "";
42 int verbose = FALSE;
43 char expected_address[ADDRESS_LENGTH] = "";
44 int match_expected_address = FALSE;
45 int expect_authority = FALSE;
47 int
48 main (int argc, char **argv)
49 {
50 char *command_line = NULL;
51 char input_buffer[MAX_INPUT_BUFFER];
52 char *output = NULL;
53 char *address = NULL;
54 char *temp_buffer = NULL;
55 int non_authoritative = FALSE;
56 int result = STATE_UNKNOWN;
57 double elapsed_time;
58 long microsec;
59 struct timeval tv;
60 int multi_address;
61 int parse_address = FALSE; /* This flag scans for Address: but only after Name: */
63 setlocale (LC_ALL, "");
64 bindtextdomain (PACKAGE, LOCALEDIR);
65 textdomain (PACKAGE);
67 /* Set signal handling and alarm */
68 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
69 printf (_("Cannot catch SIGALRM"));
70 return STATE_UNKNOWN;
71 }
73 if (process_arguments (argc, argv) != OK) {
74 print_usage ();
75 return STATE_UNKNOWN;
76 }
78 /* get the command to run */
79 asprintf (&command_line, "%s %s %s", NSLOOKUP_COMMAND, query_address, dns_server);
81 alarm (timeout_interval);
82 gettimeofday (&tv, NULL);
84 if (verbose)
85 printf ("%s\n", command_line);
87 /* run the command */
88 child_process = spopen (command_line);
89 if (child_process == NULL) {
90 printf (_("Could not open pipe: %s\n"), command_line);
91 return STATE_UNKNOWN;
92 }
94 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
95 if (child_stderr == NULL)
96 printf (_("Could not open stderr for %s\n"), command_line);
98 /* scan stdout */
99 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
101 if (verbose)
102 printf ("%s", input_buffer);
104 if (strstr (input_buffer, ".in-addr.arpa")) {
105 if ((temp_buffer = strstr (input_buffer, "name = ")))
106 address = strdup (temp_buffer + 7);
107 else {
108 output = strdup (_("Unknown error (plugin)"));
109 result = STATE_WARNING;
110 }
111 }
113 /* the server is responding, we just got the host name... */
114 if (strstr (input_buffer, "Name:"))
115 parse_address = TRUE;
116 else if (parse_address == TRUE && (strstr (input_buffer, "Address:") ||
117 strstr (input_buffer, "Addresses:"))) {
118 temp_buffer = index (input_buffer, ':');
119 temp_buffer++;
121 /* Strip leading spaces */
122 for (; *temp_buffer != '\0' && *temp_buffer == ' '; temp_buffer++)
123 /* NOOP */;
125 strip(temp_buffer);
126 if (temp_buffer==NULL || strlen(temp_buffer)==0) {
127 die (STATE_CRITICAL,
128 _("DNS CRITICAL - '%s' returned empty host name string\n"),
129 NSLOOKUP_COMMAND);
130 }
132 if (address == NULL)
133 address = strdup (temp_buffer);
134 else
135 asprintf(&address, "%s,%s", address, temp_buffer);
136 }
138 else if (strstr (input_buffer, "Non-authoritative answer:")) {
139 non_authoritative = TRUE;
140 }
142 result = error_scan (input_buffer);
143 if (result != STATE_OK) {
144 output = strdup (1 + index (input_buffer, ':'));
145 strip (output);
146 break;
147 }
149 }
151 /* scan stderr */
152 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
153 if (error_scan (input_buffer) != STATE_OK) {
154 result = max_state (result, error_scan (input_buffer));
155 output = strdup (1 + index (input_buffer, ':'));
156 strip (output);
157 }
158 }
160 /* close stderr */
161 (void) fclose (child_stderr);
163 /* close stdout */
164 if (spclose (child_process)) {
165 result = max_state (result, STATE_WARNING);
166 if (!strcmp (output, ""))
167 output = strdup (_("nslookup returned error status"));
168 }
170 /* If we got here, we should have an address string,
171 and we can segfault if we do not */
172 if (address==NULL || strlen(address)==0)
173 die (STATE_CRITICAL,
174 _("DNS CRITICAL - '%s' output parsing exited with no address\n"),
175 NSLOOKUP_COMMAND);
177 /* compare to expected address */
178 if (result == STATE_OK && match_expected_address && strcmp(address, expected_address)) {
179 result = STATE_CRITICAL;
180 asprintf(&output, _("expected %s but got %s"), expected_address, address);
181 }
183 /* check if authoritative */
184 if (result == STATE_OK && expect_authority && non_authoritative) {
185 result = STATE_CRITICAL;
186 asprintf(&output, _("server %s is not authoritative for %s"), dns_server, query_address);
187 }
189 microsec = deltime (tv);
190 elapsed_time = (double)microsec / 1.0e6;
192 if (result == STATE_OK) {
193 if (strchr (address, ',') == NULL)
194 multi_address = FALSE;
195 else
196 multi_address = TRUE;
198 printf ("%s %s: ", _("DNS"), _("OK"));
199 printf (ngettext("%.3f second response time, ", "%.3f seconds response time, ", elapsed_time), elapsed_time);
200 printf (_("%s returns %s"), query_address, address);
201 printf ("|%s\n", fperfdata ("time", elapsed_time, "s", FALSE, 0, FALSE, 0, TRUE, 0, FALSE, 0));
202 }
203 else if (result == STATE_WARNING)
204 printf (_("DNS WARNING - %s\n"),
205 !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output);
206 else if (result == STATE_CRITICAL)
207 printf (_("DNS CRITICAL - %s\n"),
208 !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output);
209 else
210 printf (_("DNS problem - %s\n"),
211 !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output);
213 return result;
214 }
216 int
217 error_scan (char *input_buffer)
218 {
220 /* the DNS lookup timed out */
221 if (strstr (input_buffer, "Note: nslookup is deprecated and may be removed from future releases.") ||
222 strstr (input_buffer, "Consider using the `dig' or `host' programs instead. Run nslookup with") ||
223 strstr (input_buffer, "the `-sil[ent]' option to prevent this message from appearing."))
224 return STATE_OK;
226 /* DNS server is not running... */
227 else if (strstr (input_buffer, "No response from server"))
228 die (STATE_CRITICAL, _("No response from name server %s\n"), dns_server);
230 /* Host name is valid, but server doesn't have records... */
231 else if (strstr (input_buffer, "No records"))
232 die (STATE_CRITICAL, _("Name server %s has no records\n"), dns_server);
234 /* Connection was refused */
235 else if (strstr (input_buffer, "Connection refused") ||
236 strstr (input_buffer, "Refused") ||
237 (strstr (input_buffer, "** server can't find") &&
238 strstr (input_buffer, ": REFUSED")))
239 die (STATE_CRITICAL, _("Connection to name server %s was refused\n"), dns_server);
241 /* Host or domain name does not exist */
242 else if (strstr (input_buffer, "Non-existent") ||
243 strstr (input_buffer, "** server can't find") ||
244 strstr (input_buffer,"NXDOMAIN"))
245 die (STATE_CRITICAL, _("Domain %s was not found by the server\n"), query_address);
247 /* Network is unreachable */
248 else if (strstr (input_buffer, "Network is unreachable"))
249 die (STATE_CRITICAL, _("Network is unreachable\n"));
251 /* Internal server failure */
252 else if (strstr (input_buffer, "Server failure"))
253 die (STATE_CRITICAL, _("Server failure for %s\n"), dns_server);
255 /* Request error or the DNS lookup timed out */
256 else if (strstr (input_buffer, "Format error") ||
257 strstr (input_buffer, "Timed out"))
258 return STATE_WARNING;
260 return STATE_OK;
262 }
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 '?': /* args not parsable */
299 printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
300 print_usage ();
301 exit (STATE_UNKNOWN);
302 case 'h': /* help */
303 print_help ();
304 exit (STATE_OK);
305 case 'V': /* version */
306 print_revision (progname, revision);
307 exit (STATE_OK);
308 case 'v': /* version */
309 verbose = TRUE;
310 break;
311 case 't': /* timeout period */
312 timeout_interval = atoi (optarg);
313 break;
314 case 'H': /* hostname */
315 if (strlen (optarg) >= ADDRESS_LENGTH)
316 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
317 strcpy (query_address, optarg);
318 break;
319 case 's': /* server name */
320 /* TODO: this is_host check is probably unnecessary. */
321 /* Better to confirm nslookup response matches */
322 if (is_host (optarg) == FALSE) {
323 printf (_("Invalid server name/address\n\n"));
324 print_usage ();
325 exit (STATE_UNKNOWN);
326 }
327 if (strlen (optarg) >= ADDRESS_LENGTH)
328 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
329 strcpy (dns_server, optarg);
330 break;
331 case 'r': /* reverse server name */
332 /* TODO: Is this is_host necessary? */
333 if (is_host (optarg) == FALSE) {
334 printf (_("Invalid host name/address\n\n"));
335 print_usage ();
336 exit (STATE_UNKNOWN);
337 }
338 if (strlen (optarg) >= ADDRESS_LENGTH)
339 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
340 strcpy (ptr_server, optarg);
341 break;
342 case 'a': /* expected address */
343 if (strlen (optarg) >= ADDRESS_LENGTH)
344 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
345 strcpy (expected_address, optarg);
346 match_expected_address = TRUE;
347 break;
348 case 'A': /* expect authority */
349 expect_authority = TRUE;
350 break;
351 }
352 }
354 c = optind;
355 if (strlen(query_address)==0 && c<argc) {
356 if (strlen(argv[c])>=ADDRESS_LENGTH)
357 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
358 strcpy (query_address, argv[c++]);
359 }
361 if (strlen(dns_server)==0 && c<argc) {
362 /* TODO: See -s option */
363 if (is_host(argv[c]) == FALSE) {
364 printf (_("Invalid name/address: %s\n\n"), argv[c]);
365 return ERROR;
366 }
367 if (strlen(argv[c]) >= ADDRESS_LENGTH)
368 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
369 strcpy (dns_server, argv[c++]);
370 }
372 return validate_arguments ();
373 }
375 int
376 validate_arguments ()
377 {
378 if (query_address[0] == 0)
379 return ERROR;
380 else
381 return OK;
382 }
388 \f
389 void
390 print_help (void)
391 {
392 print_revision (progname, revision);
394 printf (_("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"));
395 printf (_(COPYRIGHT), copyright, email);
397 print_usage ();
399 printf (_(UT_HELP_VRSN));
401 printf (_("\
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 -A, --expect-authority\n\
409 Optionally expect the DNS server to be authoritative for the lookup\n"));
411 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
413 printf (_("\n\
414 This plugin uses the nslookup program to obtain the IP address\n\
415 for the given host/domain query. A optional DNS server to use may\n\
416 be specified. If no DNS server is specified, the default server(s)\n\
417 specified in /etc/resolv.conf will be used.\n"));
419 printf (_(UT_SUPPORT));
420 }
425 void
426 print_usage (void)
427 {
428 printf (_("\
429 Usage: %s -H host [-s server] [-a expected-address] [-A] [-t timeout]\n\
430 %s --help\n\
431 %s --version\n"), progname, progname, progname);
432 }