1 /*****************************************************************************
3 * Monitoring check_dig plugin
6 * Copyright (c) 2002-2024 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-2024";
37 const char *email
= "devel@monitoring-plugins.org";
44 static int process_arguments(int /*argc*/, char ** /*argv*/);
45 static int validate_arguments(void);
46 static void print_help(void);
47 void print_usage(void);
50 #define DEFAULT_PORT 53
51 #define DEFAULT_TRIES 2
53 static char *query_address
= NULL
;
54 static char *record_type
= "A";
55 static char *expected_address
= NULL
;
56 static char *dns_server
= NULL
;
57 static char *dig_args
= "";
58 static char *query_transport
= "";
59 static bool verbose
= false;
60 static int server_port
= DEFAULT_PORT
;
61 static int number_tries
= DEFAULT_TRIES
;
62 static double warning_interval
= UNDEFINED
;
63 static double critical_interval
= UNDEFINED
;
64 static struct timeval tv
;
66 int main(int argc
, char **argv
) {
75 int result
= STATE_UNKNOWN
;
76 int timeout_interval_dig
;
78 setlocale(LC_ALL
, "");
79 bindtextdomain(PACKAGE
, LOCALEDIR
);
82 /* Set signal handling and alarm */
83 if (signal(SIGALRM
, runcmd_timeout_alarm_handler
) == SIG_ERR
)
84 usage_va(_("Cannot catch SIGALRM"));
86 /* Parse extra opts if any */
87 argv
= np_extra_opts(&argc
, argv
, progname
);
89 if (process_arguments(argc
, argv
) == ERROR
)
90 usage_va(_("Could not parse arguments"));
92 /* dig applies the timeout to each try, so we need to work around this */
93 timeout_interval_dig
= timeout_interval
/ number_tries
+ number_tries
;
95 /* get the command to run */
96 xasprintf(&command_line
, "%s %s %s -p %d @%s %s %s +retry=%d +time=%d", PATH_TO_DIG
, dig_args
, query_transport
, server_port
, dns_server
,
97 query_address
, record_type
, number_tries
, timeout_interval_dig
);
99 alarm(timeout_interval
);
100 gettimeofday(&tv
, NULL
);
103 printf("%s\n", command_line
);
104 if (expected_address
!= NULL
) {
105 printf(_("Looking for: '%s'\n"), expected_address
);
107 printf(_("Looking for: '%s'\n"), query_address
);
111 /* run the command */
112 if (np_runcmd(command_line
, &chld_out
, &chld_err
, 0) != 0) {
113 result
= STATE_WARNING
;
114 msg
= (char *)_("dig returned an error status");
117 for (i
= 0; i
< chld_out
.lines
; i
++) {
118 /* the server is responding, we just got the host name... */
119 if (strstr(chld_out
.line
[i
], ";; ANSWER SECTION:")) {
121 /* loop through the whole 'ANSWER SECTION' */
122 for (; i
< chld_out
.lines
; i
++) {
123 /* get the host address */
125 printf("%s\n", chld_out
.line
[i
]);
127 if (strcasestr(chld_out
.line
[i
], (expected_address
== NULL
? query_address
: expected_address
)) != NULL
) {
128 msg
= chld_out
.line
[i
];
131 /* Translate output TAB -> SPACE */
133 while ((t
= strchr(t
, '\t')) != NULL
)
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
);
158 for (i
= 0; i
< chld_err
.lines
; i
++) {
159 msg
= strchr(chld_err
.line
[0], ':');
167 microsec
= deltime(tv
);
168 elapsed_time
= (double)microsec
/ 1.0e6
;
170 if (critical_interval
> UNDEFINED
&& elapsed_time
> critical_interval
)
171 result
= STATE_CRITICAL
;
173 else if (warning_interval
> UNDEFINED
&& elapsed_time
> warning_interval
)
174 result
= STATE_WARNING
;
176 printf("DNS %s - %.3f seconds response time (%s)|%s\n", state_text(result
), elapsed_time
,
177 msg
? msg
: _("Probably a non-existent host/domain"),
178 fperfdata("time", elapsed_time
, "s", (warning_interval
> UNDEFINED
? true : false), warning_interval
,
179 (critical_interval
> UNDEFINED
? true : false), critical_interval
, true, 0, false, 0));
183 /* process command-line arguments */
184 int process_arguments(int argc
, char **argv
) {
188 static struct option longopts
[] = {{"hostname", required_argument
, 0, 'H'},
189 {"query_address", required_argument
, 0, 'l'},
190 {"warning", required_argument
, 0, 'w'},
191 {"critical", required_argument
, 0, 'c'},
192 {"timeout", required_argument
, 0, 't'},
193 {"dig-arguments", required_argument
, 0, 'A'},
194 {"verbose", no_argument
, 0, 'v'},
195 {"version", no_argument
, 0, 'V'},
196 {"help", no_argument
, 0, 'h'},
197 {"record_type", required_argument
, 0, 'T'},
198 {"expected_address", required_argument
, 0, 'a'},
199 {"port", required_argument
, 0, 'p'},
200 {"use-ipv4", no_argument
, 0, '4'},
201 {"use-ipv6", no_argument
, 0, '6'},
208 c
= getopt_long(argc
, argv
, "hVvt:l:H:w:c:T:p:a:A:46", longopts
, &option
);
210 if (c
== -1 || c
== EOF
)
217 case 'V': /* version */
218 print_revision(progname
, NP_VERSION
);
220 case 'H': /* hostname */
224 case 'p': /* server port */
225 if (is_intpos(optarg
)) {
226 server_port
= atoi(optarg
);
228 usage_va(_("Port must be a positive integer - %s"), optarg
);
231 case 'l': /* address to lookup */
232 query_address
= optarg
;
234 case 'w': /* warning */
235 if (is_nonnegative(optarg
)) {
236 warning_interval
= strtod(optarg
, NULL
);
238 usage_va(_("Warning interval must be a positive integer - %s"), optarg
);
241 case 'c': /* critical */
242 if (is_nonnegative(optarg
)) {
243 critical_interval
= strtod(optarg
, NULL
);
245 usage_va(_("Critical interval must be a positive integer - %s"), optarg
);
248 case 't': /* timeout */
249 if (is_intnonneg(optarg
)) {
250 timeout_interval
= atoi(optarg
);
252 usage_va(_("Timeout interval must be a positive integer - %s"), optarg
);
255 case 'A': /* dig arguments */
256 dig_args
= strdup(optarg
);
258 case 'v': /* verbose */
262 record_type
= optarg
;
265 expected_address
= optarg
;
268 query_transport
= "-4";
271 query_transport
= "-6";
273 default: /* usage5 */
279 if (dns_server
== NULL
) {
281 host_or_die(argv
[c
]);
282 dns_server
= argv
[c
];
284 if (strcmp(query_transport
, "-6") == 0)
285 dns_server
= strdup("::1");
287 dns_server
= strdup("127.0.0.1");
291 return validate_arguments();
294 int validate_arguments(void) {
295 if (query_address
!= NULL
)
300 void print_help(void) {
303 xasprintf(&myport
, "%d", DEFAULT_PORT
);
305 print_revision(progname
, NP_VERSION
);
307 printf("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
308 printf(COPYRIGHT
, copyright
, email
);
310 printf(_("This plugin tests the DNS service on the specified host using dig"));
316 printf(UT_HELP_VRSN
);
318 printf(UT_EXTRA_OPTS
);
320 printf(UT_HOST_PORT
, 'p', myport
);
322 printf(" %s\n", "-4, --use-ipv4");
323 printf(" %s\n", _("Force dig to only use IPv4 query transport"));
324 printf(" %s\n", "-6, --use-ipv6");
325 printf(" %s\n", _("Force dig to only use IPv6 query transport"));
326 printf(" %s\n", "-l, --query_address=STRING");
327 printf(" %s\n", _("Machine name to lookup"));
328 printf(" %s\n", "-T, --record_type=STRING");
329 printf(" %s\n", _("Record type to lookup (default: A)"));
330 printf(" %s\n", "-a, --expected_address=STRING");
331 printf(" %s\n", _("An address expected to be in the answer section. If not set, uses whatever"));
332 printf(" %s\n", _("was in -l"));
333 printf(" %s\n", "-A, --dig-arguments=STRING");
334 printf(" %s\n", _("Pass STRING as argument(s) to dig"));
335 printf(UT_WARN_CRIT
);
336 printf(UT_CONN_TIMEOUT
, DEFAULT_SOCKET_TIMEOUT
);
340 printf("%s\n", _("Examples:"));
341 printf(" %s\n", "check_dig -H DNSSERVER -l www.example.com -A \"+tcp\"");
342 printf(" %s\n", "This will send a tcp query to DNSSERVER for www.example.com");
347 void print_usage(void) {
348 printf("%s\n", _("Usage:"));
349 printf("%s -l <query_address> [-H <host>] [-p <server port>]\n", progname
);
350 printf(" [-T <query type>] [-w <warning interval>] [-c <critical interval>]\n");
351 printf(" [-t <timeout>] [-a <expected answer address>] [-v]\n");