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;
46 int
47 main (int argc, char **argv)
48 {
49 char *command_line = NULL;
50 char input_buffer[MAX_INPUT_BUFFER];
51 char *output = NULL;
52 char *address = NULL;
53 char *temp_buffer = NULL;
54 int result = STATE_UNKNOWN;
55 double elapsed_time;
56 long microsec;
57 struct timeval tv;
58 int multi_address;
59 int parse_address = FALSE; /* This flag scans for Address: but only after Name: */
61 setlocale (LC_ALL, "");
62 bindtextdomain (PACKAGE, LOCALEDIR);
63 textdomain (PACKAGE);
65 /* Set signal handling and alarm */
66 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
67 printf (_("Cannot catch SIGALRM"));
68 return STATE_UNKNOWN;
69 }
71 if (process_arguments (argc, argv) != OK) {
72 print_usage ();
73 return STATE_UNKNOWN;
74 }
76 /* get the command to run */
77 asprintf (&command_line, "%s %s %s", NSLOOKUP_COMMAND, query_address, dns_server);
79 alarm (timeout_interval);
80 gettimeofday (&tv, NULL);
82 if (verbose)
83 printf ("%s\n", command_line);
85 /* run the command */
86 child_process = spopen (command_line);
87 if (child_process == NULL) {
88 printf (_("Could not open pipe: %s\n"), command_line);
89 return STATE_UNKNOWN;
90 }
92 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
93 if (child_stderr == NULL)
94 printf (_("Could not open stderr for %s\n"), command_line);
96 /* scan stdout */
97 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
99 if (verbose)
100 printf ("%s", input_buffer);
102 if (strstr (input_buffer, ".in-addr.arpa")) {
103 if ((temp_buffer = strstr (input_buffer, "name = ")))
104 address = strdup (temp_buffer + 7);
105 else {
106 output = strdup (_("Unknown error (plugin)"));
107 result = STATE_WARNING;
108 }
109 }
111 /* the server is responding, we just got the host name... */
112 if (strstr (input_buffer, "Name:"))
113 parse_address = TRUE;
114 else if (parse_address == TRUE && (strstr (input_buffer, "Address:") || strstr (input_buffer, "Addresses:"))) {
115 temp_buffer = index (input_buffer, ':');
116 temp_buffer++;
118 /* Strip leading spaces */
119 for (; *temp_buffer != '\0' && *temp_buffer == ' '; temp_buffer++)
120 /* NOOP */;
122 strip(temp_buffer);
123 if (temp_buffer==NULL || strlen(temp_buffer)==0) {
124 die (STATE_CRITICAL, _("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 result = error_scan (input_buffer);
135 if (result != STATE_OK) {
136 output = strdup (1 + index (input_buffer, ':'));
137 strip (output);
138 break;
139 }
141 }
143 /* scan stderr */
144 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
145 if (error_scan (input_buffer) != STATE_OK) {
146 result = max_state (result, error_scan (input_buffer));
147 output = strdup (1 + index (input_buffer, ':'));
148 strip (output);
149 }
150 }
152 /* close stderr */
153 (void) fclose (child_stderr);
155 /* close stdout */
156 if (spclose (child_process)) {
157 result = max_state (result, STATE_WARNING);
158 if (!strcmp (output, ""))
159 output = strdup (_("nslookup returned error status"));
160 }
162 /* If we got here, we should have an address string,
163 and we can segfault if we do not */
164 if (address==NULL || strlen(address)==0)
165 die (STATE_CRITICAL,
166 _("DNS CRITICAL - '%s' output parsing exited with no address\n"),
167 NSLOOKUP_COMMAND);
169 /* compare to expected address */
170 if (result == STATE_OK && match_expected_address && strcmp(address, expected_address)) {
171 result = STATE_CRITICAL;
172 asprintf(&output, _("expected %s but got %s"), expected_address, address);
173 }
175 microsec = deltime (tv);
176 elapsed_time = (double)microsec / 1.0e6;
178 if (result == STATE_OK) {
179 if (strchr (address, ',') == NULL)
180 multi_address = FALSE;
181 else
182 multi_address = TRUE;
184 printf ("%s %s: ", _("DNS"), _("OK"));
185 printf (ngettext("%.3f second response time, ", "%.3f seconds response time, ", elapsed_time), elapsed_time);
186 printf (_("%s returns %s"), query_address, address);
187 printf ("|%s\n", perfdata ("time", microsec, "us", FALSE, 0, FALSE, 0, TRUE, 0, FALSE, 0));
188 }
189 else if (result == STATE_WARNING)
190 printf (_("DNS WARNING - %s\n"),
191 !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output);
192 else if (result == STATE_CRITICAL)
193 printf (_("DNS CRITICAL - %s\n"),
194 !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output);
195 else
196 printf (_("DNS problem - %s\n"),
197 !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output);
199 return result;
200 }
202 int
203 error_scan (char *input_buffer)
204 {
206 /* the DNS lookup timed out */
207 if (strstr (input_buffer, "Note: nslookup is deprecated and may be removed from future releases.") ||
208 strstr (input_buffer, "Consider using the `dig' or `host' programs instead. Run nslookup with") ||
209 strstr (input_buffer, "the `-sil[ent]' option to prevent this message from appearing."))
210 return STATE_OK;
212 /* DNS server is not running... */
213 else if (strstr (input_buffer, "No response from server"))
214 die (STATE_CRITICAL, _("No response from name server %s\n"), dns_server);
216 /* Host name is valid, but server doesn't have records... */
217 else if (strstr (input_buffer, "No records"))
218 die (STATE_CRITICAL, _("Name server %s has no records\n"), dns_server);
220 /* Connection was refused */
221 else if (strstr (input_buffer, "Connection refused") ||
222 (strstr (input_buffer, "** server can't find") &&
223 strstr (input_buffer, ": REFUSED")) ||
224 (strstr (input_buffer, "Refused")))
225 die (STATE_CRITICAL, _("Connection to name server %s was refused\n"), dns_server);
227 /* Host or domain name does not exist */
228 else if (strstr (input_buffer, "Non-existent") ||
229 strstr (input_buffer, "** server can't find") ||
230 strstr (input_buffer,"NXDOMAIN"))
231 die (STATE_CRITICAL, _("Domain %s was not found by the server\n"), query_address);
233 /* Network is unreachable */
234 else if (strstr (input_buffer, "Network is unreachable"))
235 die (STATE_CRITICAL, _("Network is unreachable\n"));
237 /* Internal server failure */
238 else if (strstr (input_buffer, "Server failure"))
239 die (STATE_CRITICAL, _("Server failure for %s\n"), dns_server);
241 /* Request error or the DNS lookup timed out */
242 else if (strstr (input_buffer, "Format error") ||
243 strstr (input_buffer, "Timed out"))
244 return STATE_WARNING;
246 return STATE_OK;
248 }
250 /* process command-line arguments */
251 int
252 process_arguments (int argc, char **argv)
253 {
254 int c;
256 int opt_index = 0;
257 static struct option long_opts[] = {
258 {"help", no_argument, 0, 'h'},
259 {"version", no_argument, 0, 'V'},
260 {"verbose", no_argument, 0, 'v'},
261 {"timeout", required_argument, 0, 't'},
262 {"hostname", required_argument, 0, 'H'},
263 {"server", required_argument, 0, 's'},
264 {"reverse-server", required_argument, 0, 'r'},
265 {"expected-address", required_argument, 0, 'a'},
266 {0, 0, 0, 0}
267 };
269 if (argc < 2)
270 return ERROR;
272 for (c = 1; c < argc; c++)
273 if (strcmp ("-to", argv[c]) == 0)
274 strcpy (argv[c], "-t");
276 while (1) {
277 c = getopt_long (argc, argv, "hVvt:H:s:r:a:", long_opts, &opt_index);
279 if (c == -1 || c == EOF)
280 break;
282 switch (c) {
283 case '?': /* args not parsable */
284 printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
285 print_usage ();
286 exit (STATE_UNKNOWN);
287 case 'h': /* help */
288 print_help ();
289 exit (STATE_OK);
290 case 'V': /* version */
291 print_revision (progname, revision);
292 exit (STATE_OK);
293 case 'v': /* version */
294 verbose = TRUE;
295 break;
296 case 't': /* timeout period */
297 timeout_interval = atoi (optarg);
298 break;
299 case 'H': /* hostname */
300 if (strlen (optarg) >= ADDRESS_LENGTH)
301 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
302 strcpy (query_address, optarg);
303 break;
304 case 's': /* server name */
305 /* TODO: this is_host check is probably unnecessary. Better to confirm nslookup
306 response matches */
307 if (is_host (optarg) == FALSE) {
308 printf (_("Invalid server name/address\n\n"));
309 print_usage ();
310 exit (STATE_UNKNOWN);
311 }
312 if (strlen (optarg) >= ADDRESS_LENGTH)
313 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
314 strcpy (dns_server, optarg);
315 break;
316 case 'r': /* reverse server name */
317 /* TODO: Is this is_host necessary? */
318 if (is_host (optarg) == FALSE) {
319 printf (_("Invalid host name/address\n\n"));
320 print_usage ();
321 exit (STATE_UNKNOWN);
322 }
323 if (strlen (optarg) >= ADDRESS_LENGTH)
324 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
325 strcpy (ptr_server, optarg);
326 break;
327 case 'a': /* expected address */
328 if (strlen (optarg) >= ADDRESS_LENGTH)
329 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
330 strcpy (expected_address, optarg);
331 match_expected_address = TRUE;
332 break;
333 }
334 }
336 c = optind;
337 if (strlen(query_address)==0 && c<argc) {
338 if (strlen(argv[c])>=ADDRESS_LENGTH)
339 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
340 strcpy (query_address, argv[c++]);
341 }
343 if (strlen(dns_server)==0 && c<argc) {
344 /* TODO: See -s option */
345 if (is_host(argv[c]) == FALSE) {
346 printf (_("Invalid name/address: %s\n\n"), argv[c]);
347 return ERROR;
348 }
349 if (strlen(argv[c]) >= ADDRESS_LENGTH)
350 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
351 strcpy (dns_server, argv[c++]);
352 }
354 return validate_arguments ();
355 }
357 int
358 validate_arguments ()
359 {
360 if (query_address[0] == 0)
361 return ERROR;
362 else
363 return OK;
364 }
370 \f
371 void
372 print_help (void)
373 {
374 print_revision (progname, revision);
376 printf (_("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"));
377 printf (_(COPYRIGHT), copyright, email);
379 print_usage ();
381 printf (_(UT_HELP_VRSN));
383 printf (_("\
384 -H, --hostname=HOST\n\
385 The name or address you want to query\n\
386 -s, --server=HOST\n\
387 Optional DNS server you want to use for the lookup\n\
388 -a, --expected-address=IP-ADDRESS\n\
389 Optional IP address you expect the DNS server to return\n"));
391 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
393 printf (_("\n\
394 This plugin uses the nslookup program to obtain the IP address\n\
395 for the given host/domain query. A optional DNS server to use may\n\
396 be specified. If no DNS server is specified, the default server(s)\n\
397 specified in /etc/resolv.conf will be used.\n"));
399 printf (_(UT_SUPPORT));
400 }
405 void
406 print_usage (void)
407 {
408 printf (_("\
409 Usage: %s -H host [-s server] [-a expected-address] [-t timeout]\n\
410 %s --help\n\
411 %s --version\n"),
412 progname, progname, progname);
413 }