1 /*****************************************************************************
3 * Monitoring check_dig plugin
6 * Copyright (c) 2002-2008 Monitoring Plugins Development Team
10 * This file contains the check_dig plugin
13 * This program is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation, either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 *****************************************************************************/
30 * There are typecasts to (char *) from _("foo bar") in this file.
31 * They prevent compiler warnings. Never (ever), permute strings obtained
32 * that are typecast from (const char *) (which happens when --disable-nls)
33 * because on some architectures those strings are in non-writable memory */
35 const char *progname
= "check_dig";
36 const char *copyright
= "2002-2008";
37 const char *email
= "devel@monitoring-plugins.org";
44 int process_arguments (int, char **);
45 int validate_arguments (void);
46 void print_help (void);
47 void print_usage (void);
50 #define DEFAULT_PORT 53
51 #define DEFAULT_TRIES 3
53 char *query_address
= NULL
;
54 char *record_type
= "A";
55 char *expected_address
= NULL
;
56 char *dns_server
= NULL
;
58 char *query_transport
= "";
60 int server_port
= DEFAULT_PORT
;
61 int number_tries
= DEFAULT_TRIES
;
62 double warning_interval
= UNDEFINED
;
63 double critical_interval
= UNDEFINED
;
67 main (int argc
, char **argv
)
70 output chld_out
, chld_err
;
76 int result
= STATE_UNKNOWN
;
77 int timeout_interval_dig
;
79 setlocale (LC_ALL
, "");
80 bindtextdomain (PACKAGE
, LOCALEDIR
);
83 /* Set signal handling and alarm */
84 if (signal (SIGALRM
, runcmd_timeout_alarm_handler
) == SIG_ERR
)
85 usage_va(_("Cannot catch SIGALRM"));
87 /* Parse extra opts if any */
88 argv
=np_extra_opts (&argc
, argv
, progname
);
90 if (process_arguments (argc
, argv
) == ERROR
)
91 usage_va(_("Could not parse arguments"));
93 /* dig applies the timeout to each try, so we need to work around this */
94 timeout_interval_dig
= timeout_interval
/ number_tries
+ number_tries
;
96 /* get the command to run */
97 xasprintf (&command_line
, "%s %s %s -p %d @%s %s %s +tries=%d +time=%d",
98 PATH_TO_DIG
, dig_args
, query_transport
, server_port
, dns_server
, query_address
, record_type
, number_tries
, timeout_interval_dig
);
100 alarm (timeout_interval
);
101 gettimeofday (&tv
, NULL
);
104 printf ("%s\n", command_line
);
105 if(expected_address
!= NULL
) {
106 printf (_("Looking for: '%s'\n"), expected_address
);
108 printf (_("Looking for: '%s'\n"), query_address
);
112 /* run the command */
113 if(np_runcmd(command_line
, &chld_out
, &chld_err
, 0) != 0) {
114 result
= STATE_WARNING
;
115 msg
= (char *)_("dig returned an error status");
118 for(i
= 0; i
< chld_out
.lines
; i
++) {
119 /* the server is responding, we just got the host name... */
120 if (strstr (chld_out
.line
[i
], ";; ANSWER SECTION:")) {
122 /* loop through the whole 'ANSWER SECTION' */
123 for(; i
< chld_out
.lines
; i
++) {
124 /* get the host address */
126 printf ("%s\n", chld_out
.line
[i
]);
128 if (strstr (chld_out
.line
[i
], (expected_address
== NULL
? query_address
: expected_address
)) != NULL
) {
129 msg
= chld_out
.line
[i
];
132 /* Translate output TAB -> SPACE */
134 while ((t
= strchr(t
, '\t')) != NULL
) *t
= ' ';
139 if (result
== STATE_UNKNOWN
) {
140 msg
= (char *)_("Server not found in ANSWER SECTION");
141 result
= STATE_WARNING
;
144 /* we found the answer section, so break out of the loop */
149 if (result
== STATE_UNKNOWN
) {
150 msg
= (char *)_("No ANSWER SECTION found");
151 result
= STATE_CRITICAL
;
154 /* If we get anything on STDERR, at least set warning */
155 if(chld_err
.buflen
> 0) {
156 result
= max_state(result
, STATE_WARNING
);
157 if(!msg
) for(i
= 0; i
< chld_err
.lines
; i
++) {
158 msg
= strchr(chld_err
.line
[0], ':');
166 microsec
= deltime (tv
);
167 elapsed_time
= (double)microsec
/ 1.0e6
;
169 if (critical_interval
> UNDEFINED
&& elapsed_time
> critical_interval
)
170 result
= STATE_CRITICAL
;
172 else if (warning_interval
> UNDEFINED
&& elapsed_time
> warning_interval
)
173 result
= STATE_WARNING
;
175 printf ("DNS %s - %.3f seconds response time (%s)|%s\n",
176 state_text (result
), elapsed_time
,
177 msg
? msg
: _("Probably a non-existent host/domain"),
178 fperfdata("time", elapsed_time
, "s",
179 (warning_interval
>UNDEFINED
?TRUE
:FALSE
),
181 (critical_interval
>UNDEFINED
?TRUE
:FALSE
),
189 /* process command-line arguments */
191 process_arguments (int argc
, char **argv
)
196 static struct option longopts
[] = {
197 {"hostname", required_argument
, 0, 'H'},
198 {"query_address", required_argument
, 0, 'l'},
199 {"warning", required_argument
, 0, 'w'},
200 {"critical", required_argument
, 0, 'c'},
201 {"timeout", required_argument
, 0, 't'},
202 {"dig-arguments", required_argument
, 0, 'A'},
203 {"verbose", no_argument
, 0, 'v'},
204 {"version", no_argument
, 0, 'V'},
205 {"help", no_argument
, 0, 'h'},
206 {"record_type", required_argument
, 0, 'T'},
207 {"expected_address", required_argument
, 0, 'a'},
208 {"port", required_argument
, 0, 'p'},
209 {"use-ipv4", no_argument
, 0, '4'},
210 {"use-ipv6", no_argument
, 0, '6'},
218 c
= getopt_long (argc
, argv
, "hVvt:l:H:w:c:T:p:a:A:46", longopts
, &option
);
220 if (c
== -1 || c
== EOF
)
227 case 'V': /* version */
228 print_revision (progname
, NP_VERSION
);
230 case 'H': /* hostname */
234 case 'p': /* server port */
235 if (is_intpos (optarg
)) {
236 server_port
= atoi (optarg
);
239 usage_va(_("Port must be a positive integer - %s"), optarg
);
242 case 'l': /* address to lookup */
243 query_address
= optarg
;
245 case 'w': /* warning */
246 if (is_nonnegative (optarg
)) {
247 warning_interval
= strtod (optarg
, NULL
);
250 usage_va(_("Warning interval must be a positive integer - %s"), optarg
);
253 case 'c': /* critical */
254 if (is_nonnegative (optarg
)) {
255 critical_interval
= strtod (optarg
, NULL
);
258 usage_va(_("Critical interval must be a positive integer - %s"), optarg
);
261 case 't': /* timeout */
262 if (is_intnonneg (optarg
)) {
263 timeout_interval
= atoi (optarg
);
266 usage_va(_("Timeout interval must be a positive integer - %s"), optarg
);
269 case 'A': /* dig arguments */
270 dig_args
= strdup(optarg
);
272 case 'v': /* verbose */
276 record_type
= optarg
;
279 expected_address
= optarg
;
282 query_transport
= "-4";
285 query_transport
= "-6";
287 default: /* usage5 */
293 if (dns_server
== NULL
) {
295 host_or_die(argv
[c
]);
296 dns_server
= argv
[c
];
299 if (strcmp(query_transport
,"-6") == 0)
300 dns_server
= strdup("::1");
302 dns_server
= strdup ("127.0.0.1");
306 return validate_arguments ();
312 validate_arguments (void)
314 if (query_address
!= NULL
)
327 xasprintf (&myport
, "%d", DEFAULT_PORT
);
329 print_revision (progname
, NP_VERSION
);
331 printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
332 printf (COPYRIGHT
, copyright
, email
);
334 printf (_("This plugin test the DNS service on the specified host using dig"));
340 printf (UT_HELP_VRSN
);
342 printf (UT_EXTRA_OPTS
);
344 printf (UT_HOST_PORT
, 'p', myport
);
346 printf (" %s\n","-4, --use-ipv4");
347 printf (" %s\n",_("Force dig to only use IPv4 query transport"));
348 printf (" %s\n","-6, --use-ipv6");
349 printf (" %s\n",_("Force dig to only use IPv6 query transport"));
350 printf (" %s\n","-l, --query_address=STRING");
351 printf (" %s\n",_("Machine name to lookup"));
352 printf (" %s\n","-T, --record_type=STRING");
353 printf (" %s\n",_("Record type to lookup (default: A)"));
354 printf (" %s\n","-a, --expected_address=STRING");
355 printf (" %s\n",_("An address expected to be in the answer section. If not set, uses whatever"));
356 printf (" %s\n",_("was in -l"));
357 printf (" %s\n","-A, --dig-arguments=STRING");
358 printf (" %s\n",_("Pass STRING as argument(s) to dig"));
359 printf (UT_WARN_CRIT
);
360 printf (UT_CONN_TIMEOUT
, DEFAULT_SOCKET_TIMEOUT
);
364 printf ("%s\n", _("Examples:"));
365 printf (" %s\n", "check_dig -H DNSSERVER -l www.example.com -A \"+tcp\"");
366 printf (" %s\n", "This will send a tcp query to DNSSERVER for www.example.com");
376 printf ("%s\n", _("Usage:"));
377 printf ("%s -l <query_address> [-H <host>] [-p <server port>]\n", progname
);
378 printf (" [-T <query type>] [-w <warning interval>] [-c <critical interval>]\n");
379 printf (" [-t <timeout>] [-a <expected answer address>] [-v]\n");