Reverted check_procs for solaris back to using pst3 due to truncation
[monitoring-plugins.git] / plugins / check_dns.c
blob9322aef9ec5b23017cebc66a4d0d08297d6dae79
1 /*****************************************************************************
2 *
3 * Nagios check_dns plugin
4 *
5 * License: GPL
6 * Copyright (c) 2000-2008 Nagios Plugins Development Team
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
12 * This file contains the check_dns plugin
14 * LIMITATION: nslookup on Solaris 7 can return output over 2 lines, which
15 * will not be picked up by this plugin
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 * $Id$
33 *****************************************************************************/
35 const char *progname = "check_dns";
36 const char *revision = "$Revision$";
37 const char *copyright = "2000-2008";
38 const char *email = "nagiosplug-devel@lists.sourceforge.net";
40 #include "common.h"
41 #include "utils.h"
42 #include "utils_base.h"
43 #include "netutils.h"
44 #include "runcmd.h"
46 int process_arguments (int, char **);
47 int validate_arguments (void);
48 int error_scan (char *);
49 void print_help (void);
50 void print_usage (void);
52 #define ADDRESS_LENGTH 256
53 char query_address[ADDRESS_LENGTH] = "";
54 char dns_server[ADDRESS_LENGTH] = "";
55 char ptr_server[ADDRESS_LENGTH] = "";
56 int verbose = FALSE;
57 char expected_address[ADDRESS_LENGTH] = "";
58 int match_expected_address = FALSE;
59 int expect_authority = FALSE;
60 thresholds *time_thresholds = NULL;
62 static int
63 qstrcmp(const void *p1, const void *p2)
65 /* The actual arguments to this function are "pointers to
66 pointers to char", but strcmp() arguments are "pointers
67 to char", hence the following cast plus dereference */
68 return strcmp(* (char * const *) p1, * (char * const *) p2);
72 int
73 main (int argc, char **argv)
75 char *command_line = NULL;
76 char input_buffer[MAX_INPUT_BUFFER];
77 char *address = NULL; /* comma seperated str with addrs/ptrs (sorted) */
78 char **addresses = NULL;
79 int n_addresses = 0;
80 char *msg = NULL;
81 char *temp_buffer = NULL;
82 int non_authoritative = FALSE;
83 int result = STATE_UNKNOWN;
84 double elapsed_time;
85 long microsec;
86 struct timeval tv;
87 int multi_address;
88 int parse_address = FALSE; /* This flag scans for Address: but only after Name: */
89 output chld_out, chld_err;
90 size_t i;
92 setlocale (LC_ALL, "");
93 bindtextdomain (PACKAGE, LOCALEDIR);
94 textdomain (PACKAGE);
96 /* Set signal handling and alarm */
97 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
98 usage_va(_("Cannot catch SIGALRM"));
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);
111 if (verbose)
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;
120 /* scan stdout */
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));
127 if (verbose)
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);
133 else {
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], ':');
145 temp_buffer++;
147 /* Strip leading spaces */
148 for (; *temp_buffer != '\0' && *temp_buffer == ' '; temp_buffer++)
149 /* NOOP */;
151 strip(temp_buffer);
152 if (temp_buffer==NULL || strlen(temp_buffer)==0) {
153 die (STATE_CRITICAL,
154 _("DNS CRITICAL - '%s' returned empty host name string\n"),
155 NSLOOKUP_COMMAND);
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], ':');
168 if(msg) msg++;
169 break;
173 /* scan stderr */
174 for(i = 0; i < chld_err.lines; i++) {
175 if (verbose)
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, ':');
181 if(msg) msg++;
185 if (addresses) {
186 int i,slen;
187 char *adrp;
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]);
198 *adrp = 0;
199 } else
200 die (STATE_CRITICAL,
201 _("DNS CRITICAL - '%s' msg parsing exited with no address\n"),
202 NSLOOKUP_COMMAND);
204 /* compare to expected address */
205 if (result == STATE_OK && match_expected_address && strcmp(address, expected_address)) {
206 result = STATE_CRITICAL;
207 asprintf(&msg, _("expected '%s' but got '%s'"), expected_address, address);
210 /* check if authoritative */
211 if (result == STATE_OK && expect_authority && non_authoritative) {
212 result = STATE_CRITICAL;
213 asprintf(&msg, _("server %s is not authoritative for %s"), dns_server, query_address);
216 microsec = deltime (tv);
217 elapsed_time = (double)microsec / 1.0e6;
219 if (result == STATE_OK) {
220 if (strchr (address, ',') == NULL)
221 multi_address = FALSE;
222 else
223 multi_address = TRUE;
225 result = get_status(elapsed_time, time_thresholds);
226 if (result == STATE_OK) {
227 printf ("DNS %s: ", _("OK"));
228 } else if (result == STATE_WARNING) {
229 printf ("DNS %s: ", _("WARNING"));
230 } else if (result == STATE_CRITICAL) {
231 printf ("DNS %s: ", _("CRITICAL"));
233 printf (ngettext("%.3f second response time", "%.3f seconds response time", elapsed_time), elapsed_time);
234 printf (_(". %s returns %s"), query_address, address);
235 printf ("|%s\n", fperfdata ("time", elapsed_time, "s", FALSE, 0, FALSE, 0, TRUE, 0, FALSE, 0));
237 else if (result == STATE_WARNING)
238 printf (_("DNS WARNING - %s\n"),
239 !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
240 else if (result == STATE_CRITICAL)
241 printf (_("DNS CRITICAL - %s\n"),
242 !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
243 else
244 printf (_("DNS UNKNOW - %s\n"),
245 !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
247 return result;
253 error_scan (char *input_buffer)
256 /* the DNS lookup timed out */
257 if (strstr (input_buffer, _("Note: nslookup is deprecated and may be removed from future releases.")) ||
258 strstr (input_buffer, _("Consider using the `dig' or `host' programs instead. Run nslookup with")) ||
259 strstr (input_buffer, _("the `-sil[ent]' option to prevent this message from appearing.")))
260 return STATE_OK;
262 /* DNS server is not running... */
263 else if (strstr (input_buffer, "No response from server"))
264 die (STATE_CRITICAL, _("No response from DNS %s\n"), dns_server);
266 /* Host name is valid, but server doesn't have records... */
267 else if (strstr (input_buffer, "No records"))
268 die (STATE_CRITICAL, _("DNS %s has no records\n"), dns_server);
270 /* Connection was refused */
271 else if (strstr (input_buffer, "Connection refused") ||
272 strstr (input_buffer, "Couldn't find server") ||
273 strstr (input_buffer, "Refused") ||
274 (strstr (input_buffer, "** server can't find") &&
275 strstr (input_buffer, ": REFUSED")))
276 die (STATE_CRITICAL, _("Connection to DNS %s was refused\n"), dns_server);
278 /* Query refused (usually by an ACL in the namserver) */
279 else if (strstr (input_buffer, "Query refused"))
280 die (STATE_CRITICAL, _("Query was refused by DNS server at %s\n"), dns_server);
282 /* No information (e.g. nameserver IP has two PTR records) */
283 else if (strstr (input_buffer, "No information"))
284 die (STATE_CRITICAL, _("No information returned by DNS server at %s\n"), dns_server);
286 /* Host or domain name does not exist */
287 else if (strstr (input_buffer, "Non-existent") ||
288 strstr (input_buffer, "** server can't find") ||
289 strstr (input_buffer,"NXDOMAIN"))
290 die (STATE_CRITICAL, _("Domain %s was not found by the server\n"), query_address);
292 /* Network is unreachable */
293 else if (strstr (input_buffer, "Network is unreachable"))
294 die (STATE_CRITICAL, _("Network is unreachable\n"));
296 /* Internal server failure */
297 else if (strstr (input_buffer, "Server failure"))
298 die (STATE_CRITICAL, _("DNS failure for %s\n"), dns_server);
300 /* Request error or the DNS lookup timed out */
301 else if (strstr (input_buffer, "Format error") ||
302 strstr (input_buffer, "Timed out"))
303 return STATE_WARNING;
305 return STATE_OK;
310 /* process command-line arguments */
312 process_arguments (int argc, char **argv)
314 int c;
315 char *warning = NULL;
316 char *critical = NULL;
318 int opt_index = 0;
319 static struct option long_opts[] = {
320 {"help", no_argument, 0, 'h'},
321 {"version", no_argument, 0, 'V'},
322 {"verbose", no_argument, 0, 'v'},
323 {"timeout", required_argument, 0, 't'},
324 {"hostname", required_argument, 0, 'H'},
325 {"server", required_argument, 0, 's'},
326 {"reverse-server", required_argument, 0, 'r'},
327 {"expected-address", required_argument, 0, 'a'},
328 {"expect-authority", no_argument, 0, 'A'},
329 {"warning", no_argument, 0, 'w'},
330 {"critical", no_argument, 0, 'c'},
331 {0, 0, 0, 0}
334 if (argc < 2)
335 return ERROR;
337 for (c = 1; c < argc; c++)
338 if (strcmp ("-to", argv[c]) == 0)
339 strcpy (argv[c], "-t");
341 while (1) {
342 c = getopt_long (argc, argv, "hVvAt:H:s:r:a:w:c:", long_opts, &opt_index);
344 if (c == -1 || c == EOF)
345 break;
347 switch (c) {
348 case 'h': /* help */
349 print_help ();
350 exit (STATE_OK);
351 case 'V': /* version */
352 print_revision (progname, revision);
353 exit (STATE_OK);
354 case 'v': /* version */
355 verbose = TRUE;
356 break;
357 case 't': /* timeout period */
358 timeout_interval = atoi (optarg);
359 break;
360 case 'H': /* hostname */
361 if (strlen (optarg) >= ADDRESS_LENGTH)
362 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
363 strcpy (query_address, optarg);
364 break;
365 case 's': /* server name */
366 /* TODO: this host_or_die check is probably unnecessary.
367 * Better to confirm nslookup response matches */
368 host_or_die(optarg);
369 if (strlen (optarg) >= ADDRESS_LENGTH)
370 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
371 strcpy (dns_server, optarg);
372 break;
373 case 'r': /* reverse server name */
374 /* TODO: Is this host_or_die necessary? */
375 host_or_die(optarg);
376 if (strlen (optarg) >= ADDRESS_LENGTH)
377 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
378 strcpy (ptr_server, optarg);
379 break;
380 case 'a': /* expected address */
381 if (strlen (optarg) >= ADDRESS_LENGTH)
382 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
383 strcpy (expected_address, optarg);
384 match_expected_address = TRUE;
385 break;
386 case 'A': /* expect authority */
387 expect_authority = TRUE;
388 break;
389 case 'w':
390 warning = optarg;
391 break;
392 case 'c':
393 critical = optarg;
394 break;
395 default: /* args not parsable */
396 usage5();
400 c = optind;
401 if (strlen(query_address)==0 && c<argc) {
402 if (strlen(argv[c])>=ADDRESS_LENGTH)
403 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
404 strcpy (query_address, argv[c++]);
407 if (strlen(dns_server)==0 && c<argc) {
408 /* TODO: See -s option */
409 host_or_die(argv[c]);
410 if (strlen(argv[c]) >= ADDRESS_LENGTH)
411 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
412 strcpy (dns_server, argv[c++]);
415 set_thresholds(&time_thresholds, warning, critical);
417 return validate_arguments ();
422 validate_arguments ()
424 if (query_address[0] == 0)
425 return ERROR;
427 return OK;
431 void
432 print_help (void)
434 print_revision (progname, revision);
436 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
437 printf (COPYRIGHT, copyright, email);
439 printf ("%s\n", _("This plugin uses the nslookup program to obtain the IP address for the given host/domain query."));
440 printf ("%s\n", _("An optional DNS server to use may be specified."));
441 printf ("%s\n", _("If no DNS server is specified, the default server(s) specified in /etc/resolv.conf will be used."));
443 printf ("\n\n");
445 print_usage ();
447 printf (_(UT_HELP_VRSN));
449 printf (" -H, --hostname=HOST\n");
450 printf (" %s\n", _("The name or address you want to query"));
451 printf (" -s, --server=HOST\n");
452 printf (" %s\n", _("Optional DNS server you want to use for the lookup"));
453 printf (" -a, --expected-address=IP-ADDRESS|HOST\n");
454 printf (" %s\n", _("Optional IP-ADDRESS you expect the DNS server to return. HOST must end with ."));
455 printf (" %s\n", _("Multiple addresses can be separated with commas, and need to be sorted."));
456 printf (" -A, --expect-authority\n");
457 printf (" %s\n", _("Optionally expect the DNS server to be authoritative for the lookup"));
458 printf (" -w, --warning=seconds\n");
459 printf (" %s\n", _("Return warning if elapsed time exceeds value. Default off"));
460 printf (" -c, --critical=seconds\n");
461 printf (" %s\n", _("Return critical if elapsed time exceeds value. Default off"));
463 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
464 printf (_(UT_SUPPORT));
468 void
469 print_usage (void)
471 printf (_("Usage:"));
472 printf ("%s -H host [-s server] [-a expected-address] [-A] [-t timeout] [-w warn] [-c crit]\n", progname);