1 /*****************************************************************************
3 * Nagios check_dns plugin
6 * Copyright (c) 2000-2008 Nagios Plugins Development Team
10 * This file contains the check_dns plugin
12 * LIMITATION: nslookup on Solaris 7 can return output over 2 lines, which
13 * will not be picked up by this plugin
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 *****************************************************************************/
32 const char *progname
= "check_dns";
33 const char *copyright
= "2000-2008";
34 const char *email
= "nagiosplug-devel@lists.sourceforge.net";
38 #include "utils_base.h"
42 int process_arguments (int, char **);
43 int validate_arguments (void);
44 int error_scan (char *);
45 void print_help (void);
46 void print_usage (void);
48 #define ADDRESS_LENGTH 256
49 char query_address
[ADDRESS_LENGTH
] = "";
50 char dns_server
[ADDRESS_LENGTH
] = "";
51 char ptr_server
[ADDRESS_LENGTH
] = "";
53 char **expected_address
= NULL
;
54 int expected_address_cnt
= 0;
56 int expect_authority
= FALSE
;
57 thresholds
*time_thresholds
= NULL
;
60 qstrcmp(const void *p1
, const void *p2
)
62 /* The actual arguments to this function are "pointers to
63 pointers to char", but strcmp() arguments are "pointers
64 to char", hence the following cast plus dereference */
65 return strcmp(* (char * const *) p1
, * (char * const *) p2
);
70 main (int argc
, char **argv
)
72 char *command_line
= NULL
;
73 char input_buffer
[MAX_INPUT_BUFFER
];
74 char *address
= NULL
; /* comma seperated str with addrs/ptrs (sorted) */
75 char **addresses
= NULL
;
78 char *temp_buffer
= NULL
;
79 int non_authoritative
= FALSE
;
80 int result
= STATE_UNKNOWN
;
85 int parse_address
= FALSE
; /* This flag scans for Address: but only after Name: */
86 output chld_out
, chld_err
;
89 setlocale (LC_ALL
, "");
90 bindtextdomain (PACKAGE
, LOCALEDIR
);
93 /* Set signal handling and alarm */
94 if (signal (SIGALRM
, popen_timeout_alarm_handler
) == SIG_ERR
) {
95 usage_va(_("Cannot catch SIGALRM"));
98 /* Parse extra opts if any */
99 argv
=np_extra_opts (&argc
, argv
, progname
);
101 if (process_arguments (argc
, argv
) == ERROR
) {
102 usage_va(_("Could not parse arguments"));
105 /* get the command to run */
106 asprintf (&command_line
, "%s %s %s", NSLOOKUP_COMMAND
, query_address
, dns_server
);
108 alarm (timeout_interval
);
109 gettimeofday (&tv
, NULL
);
112 printf ("%s\n", command_line
);
114 /* run the command */
115 if((np_runcmd(command_line
, &chld_out
, &chld_err
, 0)) != 0) {
116 msg
= (char *)_("nslookup returned an error status");
117 result
= STATE_WARNING
;
121 for(i
= 0; i
< chld_out
.lines
; i
++) {
122 if (addresses
== NULL
)
123 addresses
= malloc(sizeof(*addresses
)*10);
124 else if (!(n_addresses
% 10))
125 addresses
= realloc(addresses
,sizeof(*addresses
) * (n_addresses
+ 10));
128 puts(chld_out
.line
[i
]);
130 if (strstr (chld_out
.line
[i
], ".in-addr.arpa")) {
131 if ((temp_buffer
= strstr (chld_out
.line
[i
], "name = ")))
132 addresses
[n_addresses
++] = strdup (temp_buffer
+ 7);
134 msg
= (char *)_("Warning plugin error");
135 result
= STATE_WARNING
;
139 /* the server is responding, we just got the host name... */
140 if (strstr (chld_out
.line
[i
], "Name:"))
141 parse_address
= TRUE
;
142 else if (parse_address
== TRUE
&& (strstr (chld_out
.line
[i
], "Address:") ||
143 strstr (chld_out
.line
[i
], "Addresses:"))) {
144 temp_buffer
= index (chld_out
.line
[i
], ':');
147 /* Strip leading spaces */
148 for (; *temp_buffer
!= '\0' && *temp_buffer
== ' '; temp_buffer
++)
152 if (temp_buffer
==NULL
|| strlen(temp_buffer
)==0) {
154 _("DNS CRITICAL - '%s' returned empty host name string\n"),
158 addresses
[n_addresses
++] = strdup(temp_buffer
);
160 else if (strstr (chld_out
.line
[i
], _("Non-authoritative answer:"))) {
161 non_authoritative
= TRUE
;
165 result
= error_scan (chld_out
.line
[i
]);
166 if (result
!= STATE_OK
) {
167 msg
= strchr (chld_out
.line
[i
], ':');
174 for(i
= 0; i
< chld_err
.lines
; i
++) {
176 puts(chld_err
.line
[i
]);
178 if (error_scan (chld_err
.line
[i
]) != STATE_OK
) {
179 result
= max_state (result
, error_scan (chld_err
.line
[i
]));
180 msg
= strchr(input_buffer
, ':');
188 qsort(addresses
, n_addresses
, sizeof(*addresses
), qstrcmp
);
189 for(i
=0, slen
=1; i
< n_addresses
; i
++) {
190 slen
+= strlen(addresses
[i
])+1;
192 adrp
= address
= malloc(slen
);
193 for(i
=0; i
< n_addresses
; i
++) {
194 if (i
) *adrp
++ = ',';
195 strcpy(adrp
, addresses
[i
]);
196 adrp
+= strlen(addresses
[i
]);
201 _("DNS CRITICAL - '%s' msg parsing exited with no address\n"),
204 /* compare to expected address */
205 if (result
== STATE_OK
&& expected_address_cnt
> 0) {
206 result
= STATE_CRITICAL
;
208 for (i
=0; i
<expected_address_cnt
; i
++) {
209 /* check if we get a match and prepare an error string */
210 if (strcmp(address
, expected_address
[i
]) == 0) result
= STATE_OK
;
211 asprintf(&temp_buffer
, "%s%s; ", temp_buffer
, expected_address
[i
]);
213 if (result
== STATE_CRITICAL
) {
214 /* Strip off last semicolon... */
215 temp_buffer
[strlen(temp_buffer
)-2] = '\0';
216 asprintf(&msg
, _("expected '%s' but got '%s'"), temp_buffer
, address
);
220 /* check if authoritative */
221 if (result
== STATE_OK
&& expect_authority
&& non_authoritative
) {
222 result
= STATE_CRITICAL
;
223 asprintf(&msg
, _("server %s is not authoritative for %s"), dns_server
, query_address
);
226 microsec
= deltime (tv
);
227 elapsed_time
= (double)microsec
/ 1.0e6
;
229 if (result
== STATE_OK
) {
230 if (strchr (address
, ',') == NULL
)
231 multi_address
= FALSE
;
233 multi_address
= TRUE
;
235 result
= get_status(elapsed_time
, time_thresholds
);
236 if (result
== STATE_OK
) {
237 printf ("DNS %s: ", _("OK"));
238 } else if (result
== STATE_WARNING
) {
239 printf ("DNS %s: ", _("WARNING"));
240 } else if (result
== STATE_CRITICAL
) {
241 printf ("DNS %s: ", _("CRITICAL"));
243 printf (ngettext("%.3f second response time", "%.3f seconds response time", elapsed_time
), elapsed_time
);
244 printf (_(". %s returns %s"), query_address
, address
);
245 printf ("|%s\n", fperfdata ("time", elapsed_time
, "s", FALSE
, 0, FALSE
, 0, TRUE
, 0, FALSE
, 0));
247 else if (result
== STATE_WARNING
)
248 printf (_("DNS WARNING - %s\n"),
249 !strcmp (msg
, "") ? _(" Probably a non-existent host/domain") : msg
);
250 else if (result
== STATE_CRITICAL
)
251 printf (_("DNS CRITICAL - %s\n"),
252 !strcmp (msg
, "") ? _(" Probably a non-existent host/domain") : msg
);
254 printf (_("DNS UNKNOW - %s\n"),
255 !strcmp (msg
, "") ? _(" Probably a non-existent host/domain") : msg
);
263 error_scan (char *input_buffer
)
266 /* the DNS lookup timed out */
267 if (strstr (input_buffer
, _("Note: nslookup is deprecated and may be removed from future releases.")) ||
268 strstr (input_buffer
, _("Consider using the `dig' or `host' programs instead. Run nslookup with")) ||
269 strstr (input_buffer
, _("the `-sil[ent]' option to prevent this message from appearing.")))
272 /* DNS server is not running... */
273 else if (strstr (input_buffer
, "No response from server"))
274 die (STATE_CRITICAL
, _("No response from DNS %s\n"), dns_server
);
276 /* Host name is valid, but server doesn't have records... */
277 else if (strstr (input_buffer
, "No records"))
278 die (STATE_CRITICAL
, _("DNS %s has no records\n"), dns_server
);
280 /* Connection was refused */
281 else if (strstr (input_buffer
, "Connection refused") ||
282 strstr (input_buffer
, "Couldn't find server") ||
283 strstr (input_buffer
, "Refused") ||
284 (strstr (input_buffer
, "** server can't find") &&
285 strstr (input_buffer
, ": REFUSED")))
286 die (STATE_CRITICAL
, _("Connection to DNS %s was refused\n"), dns_server
);
288 /* Query refused (usually by an ACL in the namserver) */
289 else if (strstr (input_buffer
, "Query refused"))
290 die (STATE_CRITICAL
, _("Query was refused by DNS server at %s\n"), dns_server
);
292 /* No information (e.g. nameserver IP has two PTR records) */
293 else if (strstr (input_buffer
, "No information"))
294 die (STATE_CRITICAL
, _("No information returned by DNS server at %s\n"), dns_server
);
296 /* Host or domain name does not exist */
297 else if (strstr (input_buffer
, "Non-existent") ||
298 strstr (input_buffer
, "** server can't find") ||
299 strstr (input_buffer
,"NXDOMAIN"))
300 die (STATE_CRITICAL
, _("Domain %s was not found by the server\n"), query_address
);
302 /* Network is unreachable */
303 else if (strstr (input_buffer
, "Network is unreachable"))
304 die (STATE_CRITICAL
, _("Network is unreachable\n"));
306 /* Internal server failure */
307 else if (strstr (input_buffer
, "Server failure"))
308 die (STATE_CRITICAL
, _("DNS failure for %s\n"), dns_server
);
310 /* Request error or the DNS lookup timed out */
311 else if (strstr (input_buffer
, "Format error") ||
312 strstr (input_buffer
, "Timed out"))
313 return STATE_WARNING
;
320 /* process command-line arguments */
322 process_arguments (int argc
, char **argv
)
325 char *warning
= NULL
;
326 char *critical
= NULL
;
329 static struct option long_opts
[] = {
330 {"help", no_argument
, 0, 'h'},
331 {"version", no_argument
, 0, 'V'},
332 {"verbose", no_argument
, 0, 'v'},
333 {"timeout", required_argument
, 0, 't'},
334 {"hostname", required_argument
, 0, 'H'},
335 {"server", required_argument
, 0, 's'},
336 {"reverse-server", required_argument
, 0, 'r'},
337 {"expected-address", required_argument
, 0, 'a'},
338 {"expect-authority", no_argument
, 0, 'A'},
339 {"warning", required_argument
, 0, 'w'},
340 {"critical", required_argument
, 0, 'c'},
347 for (c
= 1; c
< argc
; c
++)
348 if (strcmp ("-to", argv
[c
]) == 0)
349 strcpy (argv
[c
], "-t");
352 c
= getopt_long (argc
, argv
, "hVvAt:H:s:r:a:w:c:", long_opts
, &opt_index
);
354 if (c
== -1 || c
== EOF
)
361 case 'V': /* version */
362 print_revision (progname
, NP_VERSION
);
364 case 'v': /* version */
367 case 't': /* timeout period */
368 timeout_interval
= atoi (optarg
);
370 case 'H': /* hostname */
371 if (strlen (optarg
) >= ADDRESS_LENGTH
)
372 die (STATE_UNKNOWN
, _("Input buffer overflow\n"));
373 strcpy (query_address
, optarg
);
375 case 's': /* server name */
376 /* TODO: this host_or_die check is probably unnecessary.
377 * Better to confirm nslookup response matches */
379 if (strlen (optarg
) >= ADDRESS_LENGTH
)
380 die (STATE_UNKNOWN
, _("Input buffer overflow\n"));
381 strcpy (dns_server
, optarg
);
383 case 'r': /* reverse server name */
384 /* TODO: Is this host_or_die necessary? */
386 if (strlen (optarg
) >= ADDRESS_LENGTH
)
387 die (STATE_UNKNOWN
, _("Input buffer overflow\n"));
388 strcpy (ptr_server
, optarg
);
390 case 'a': /* expected address */
391 if (strlen (optarg
) >= ADDRESS_LENGTH
)
392 die (STATE_UNKNOWN
, _("Input buffer overflow\n"));
393 expected_address
= (char **)realloc(expected_address
, (expected_address_cnt
+1) * sizeof(char**));
394 expected_address
[expected_address_cnt
] = strdup(optarg
);
395 expected_address_cnt
++;
397 case 'A': /* expect authority */
398 expect_authority
= TRUE
;
406 default: /* args not parsable */
412 if (strlen(query_address
)==0 && c
<argc
) {
413 if (strlen(argv
[c
])>=ADDRESS_LENGTH
)
414 die (STATE_UNKNOWN
, _("Input buffer overflow\n"));
415 strcpy (query_address
, argv
[c
++]);
418 if (strlen(dns_server
)==0 && c
<argc
) {
419 /* TODO: See -s option */
420 host_or_die(argv
[c
]);
421 if (strlen(argv
[c
]) >= ADDRESS_LENGTH
)
422 die (STATE_UNKNOWN
, _("Input buffer overflow\n"));
423 strcpy (dns_server
, argv
[c
++]);
426 set_thresholds(&time_thresholds
, warning
, critical
);
428 return validate_arguments ();
433 validate_arguments ()
435 if (query_address
[0] == 0)
445 print_revision (progname
, NP_VERSION
);
447 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
448 printf (COPYRIGHT
, copyright
, email
);
450 printf ("%s\n", _("This plugin uses the nslookup program to obtain the IP address for the given host/domain query."));
451 printf ("%s\n", _("An optional DNS server to use may be specified."));
452 printf ("%s\n", _("If no DNS server is specified, the default server(s) specified in /etc/resolv.conf will be used."));
458 printf (_(UT_HELP_VRSN
));
459 printf (_(UT_EXTRA_OPTS
));
461 printf (" -H, --hostname=HOST\n");
462 printf (" %s\n", _("The name or address you want to query"));
463 printf (" -s, --server=HOST\n");
464 printf (" %s\n", _("Optional DNS server you want to use for the lookup"));
465 printf (" -a, --expected-address=IP-ADDRESS|HOST\n");
466 printf (" %s\n", _("Optional IP-ADDRESS you expect the DNS server to return. HOST must end with"));
467 printf (" %s\n", _("a dot (.). This option can be repeated multiple times (Returns OK if any"));
468 printf (" %s\n", _("value match). If multiple addresses are returned at once, you have to match"));
469 printf (" %s\n", _("the whole string of addresses separated with commas (sorted alphabetically)."));
470 printf (" -A, --expect-authority\n");
471 printf (" %s\n", _("Optionally expect the DNS server to be authoritative for the lookup"));
472 printf (" -w, --warning=seconds\n");
473 printf (" %s\n", _("Return warning if elapsed time exceeds value. Default off"));
474 printf (" -c, --critical=seconds\n");
475 printf (" %s\n", _("Return critical if elapsed time exceeds value. Default off"));
477 printf (_(UT_TIMEOUT
), DEFAULT_SOCKET_TIMEOUT
);
481 printf ("%s\n", _("Notes:"));
482 printf (_(UT_EXTRA_OPTS_NOTES
));
485 printf (_(UT_SUPPORT
));
492 printf (_("Usage:"));
493 printf ("%s -H host [-s server] [-a expected-address] [-A] [-t timeout] [-w warn] [-c crit]\n", progname
);