1 /*****************************************************************************
3 * Nagios check_dig plugin
6 * Copyright (c) 2002-2008 Nagios 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
= "nagiosplug-devel@lists.sourceforge.net";
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
52 char *query_address
= NULL
;
53 char *record_type
= "A";
54 char *expected_address
= NULL
;
55 char *dns_server
= NULL
;
57 char *query_transport
= "";
59 int server_port
= DEFAULT_PORT
;
60 double warning_interval
= UNDEFINED
;
61 double critical_interval
= UNDEFINED
;
65 main (int argc
, char **argv
)
68 output chld_out
, chld_err
;
74 int result
= STATE_UNKNOWN
;
76 setlocale (LC_ALL
, "");
77 bindtextdomain (PACKAGE
, LOCALEDIR
);
80 /* Set signal handling and alarm */
81 if (signal (SIGALRM
, runcmd_timeout_alarm_handler
) == SIG_ERR
)
82 usage_va(_("Cannot catch SIGALRM"));
84 /* Parse extra opts if any */
85 argv
=np_extra_opts (&argc
, argv
, progname
);
87 if (process_arguments (argc
, argv
) == ERROR
)
88 usage_va(_("Could not parse arguments"));
90 /* get the command to run */
91 xasprintf (&command_line
, "%s @%s -p %d %s -t %s %s %s",
92 PATH_TO_DIG
, dns_server
, server_port
, query_address
, record_type
, dig_args
, query_transport
);
94 alarm (timeout_interval
);
95 gettimeofday (&tv
, NULL
);
98 printf ("%s\n", command_line
);
99 if(expected_address
!= NULL
) {
100 printf (_("Looking for: '%s'\n"), expected_address
);
102 printf (_("Looking for: '%s'\n"), query_address
);
106 /* run the command */
107 if(np_runcmd(command_line
, &chld_out
, &chld_err
, 0) != 0) {
108 result
= STATE_WARNING
;
109 msg
= (char *)_("dig returned an error status");
112 for(i
= 0; i
< chld_out
.lines
; i
++) {
113 /* the server is responding, we just got the host name... */
114 if (strstr (chld_out
.line
[i
], ";; ANSWER SECTION:")) {
116 /* loop through the whole 'ANSWER SECTION' */
117 for(; i
< chld_out
.lines
; i
++) {
118 /* get the host address */
120 printf ("%s\n", chld_out
.line
[i
]);
122 if (strstr (chld_out
.line
[i
], (expected_address
== NULL
? query_address
: expected_address
)) != NULL
) {
123 msg
= chld_out
.line
[i
];
126 /* Translate output TAB -> SPACE */
128 while ((t
= strchr(t
, '\t')) != NULL
) *t
= ' ';
133 if (result
== STATE_UNKNOWN
) {
134 msg
= (char *)_("Server not found in ANSWER SECTION");
135 result
= STATE_WARNING
;
138 /* we found the answer section, so break out of the loop */
143 if (result
== STATE_UNKNOWN
) {
144 msg
= (char *)_("No ANSWER SECTION found");
145 result
= STATE_CRITICAL
;
148 /* If we get anything on STDERR, at least set warning */
149 if(chld_err
.buflen
> 0) {
150 result
= max_state(result
, STATE_WARNING
);
151 if(!msg
) for(i
= 0; i
< chld_err
.lines
; i
++) {
152 msg
= strchr(chld_err
.line
[0], ':');
160 microsec
= deltime (tv
);
161 elapsed_time
= (double)microsec
/ 1.0e6
;
163 if (critical_interval
> UNDEFINED
&& elapsed_time
> critical_interval
)
164 result
= STATE_CRITICAL
;
166 else if (warning_interval
> UNDEFINED
&& elapsed_time
> warning_interval
)
167 result
= STATE_WARNING
;
169 printf ("DNS %s - %.3f seconds response time (%s)|%s\n",
170 state_text (result
), elapsed_time
,
171 msg
? msg
: _("Probably a non-existent host/domain"),
172 fperfdata("time", elapsed_time
, "s",
173 (warning_interval
>UNDEFINED
?TRUE
:FALSE
),
175 (critical_interval
>UNDEFINED
?TRUE
:FALSE
),
183 /* process command-line arguments */
185 process_arguments (int argc
, char **argv
)
190 static struct option longopts
[] = {
191 {"hostname", required_argument
, 0, 'H'},
192 {"query_address", required_argument
, 0, 'l'},
193 {"warning", required_argument
, 0, 'w'},
194 {"critical", required_argument
, 0, 'c'},
195 {"timeout", required_argument
, 0, 't'},
196 {"dig-arguments", required_argument
, 0, 'A'},
197 {"verbose", no_argument
, 0, 'v'},
198 {"version", no_argument
, 0, 'V'},
199 {"help", no_argument
, 0, 'h'},
200 {"record_type", required_argument
, 0, 'T'},
201 {"expected_address", required_argument
, 0, 'a'},
202 {"port", required_argument
, 0, 'p'},
203 {"use-ipv4", no_argument
, 0, '4'},
204 {"use-ipv6", no_argument
, 0, '6'},
212 c
= getopt_long (argc
, argv
, "hVvt:l:H:w:c:T:p:a:A:46", longopts
, &option
);
214 if (c
== -1 || c
== EOF
)
221 case 'V': /* version */
222 print_revision (progname
, NP_VERSION
);
224 case 'H': /* hostname */
228 case 'p': /* server port */
229 if (is_intpos (optarg
)) {
230 server_port
= atoi (optarg
);
233 usage_va(_("Port must be a positive integer - %s"), optarg
);
236 case 'l': /* address to lookup */
237 query_address
= optarg
;
239 case 'w': /* warning */
240 if (is_nonnegative (optarg
)) {
241 warning_interval
= strtod (optarg
, NULL
);
244 usage_va(_("Warning interval must be a positive integer - %s"), optarg
);
247 case 'c': /* critical */
248 if (is_nonnegative (optarg
)) {
249 critical_interval
= strtod (optarg
, NULL
);
252 usage_va(_("Critical interval must be a positive integer - %s"), optarg
);
255 case 't': /* timeout */
256 if (is_intnonneg (optarg
)) {
257 timeout_interval
= atoi (optarg
);
260 usage_va(_("Timeout interval must be a positive integer - %s"), optarg
);
263 case 'A': /* dig arguments */
264 dig_args
= strdup(optarg
);
266 case 'v': /* verbose */
270 record_type
= optarg
;
273 expected_address
= optarg
;
276 query_transport
= "-4";
279 query_transport
= "-6";
281 default: /* usage5 */
287 if (dns_server
== NULL
) {
289 host_or_die(argv
[c
]);
290 dns_server
= argv
[c
];
293 dns_server
= strdup ("127.0.0.1");
297 return validate_arguments ();
303 validate_arguments (void)
305 if (query_address
!= NULL
)
318 xasprintf (&myport
, "%d", DEFAULT_PORT
);
320 print_revision (progname
, NP_VERSION
);
322 printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
323 printf (COPYRIGHT
, copyright
, email
);
325 printf (_("This plugin test the DNS service on the specified host using dig"));
331 printf (UT_HELP_VRSN
);
333 printf (UT_EXTRA_OPTS
);
335 printf (UT_HOST_PORT
, 'p', myport
);
337 printf (" %s\n","-4, --use-ipv4");
338 printf (" %s\n",_("Force dig to only use IPv4 query transport"));
339 printf (" %s\n","-6, --use-ipv6");
340 printf (" %s\n",_("Force dig to only use IPv6 query transport"));
341 printf (" %s\n","-l, --query_address=STRING");
342 printf (" %s\n",_("Machine name to lookup"));
343 printf (" %s\n","-T, --record_type=STRING");
344 printf (" %s\n",_("Record type to lookup (default: A)"));
345 printf (" %s\n","-a, --expected_address=STRING");
346 printf (" %s\n",_("An address expected to be in the answer section. If not set, uses whatever"));
347 printf (" %s\n",_("was in -l"));
348 printf (" %s\n","-A, --dig-arguments=STRING");
349 printf (" %s\n",_("Pass STRING as argument(s) to dig"));
350 printf (UT_WARN_CRIT
);
351 printf (UT_TIMEOUT
, DEFAULT_SOCKET_TIMEOUT
);
355 printf ("%s\n", _("Examples:"));
356 printf (" %s\n", "check_dig -H DNSSERVER -l www.example.com -A \"+tcp\"");
357 printf (" %s\n", "This will send a tcp query to DNSSERVER for www.example.com");
367 printf ("%s\n", _("Usage:"));
368 printf ("%s -l <query_address> [-H <host>] [-p <server port>]\n", progname
);
369 printf (" [-T <query type>] [-w <warning interval>] [-c <critical interval>]\n");
370 printf (" [-t <timeout>] [-a <expected answer address>] [-v]\n");