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
;
58 int server_port
= DEFAULT_PORT
;
59 double warning_interval
= UNDEFINED
;
60 double critical_interval
= UNDEFINED
;
64 main (int argc
, char **argv
)
67 output chld_out
, chld_err
;
73 int result
= STATE_UNKNOWN
;
75 setlocale (LC_ALL
, "");
76 bindtextdomain (PACKAGE
, LOCALEDIR
);
79 /* Set signal handling and alarm */
80 if (signal (SIGALRM
, popen_timeout_alarm_handler
) == SIG_ERR
)
81 usage_va(_("Cannot catch SIGALRM"));
83 /* Parse extra opts if any */
84 argv
=np_extra_opts (&argc
, argv
, progname
);
86 if (process_arguments (argc
, argv
) == ERROR
)
87 usage_va(_("Could not parse arguments"));
89 /* get the command to run */
90 asprintf (&command_line
, "%s @%s -p %d %s -t %s %s",
91 PATH_TO_DIG
, dns_server
, server_port
, query_address
, record_type
, dig_args
);
93 alarm (timeout_interval
);
94 gettimeofday (&tv
, NULL
);
97 printf ("%s\n", command_line
);
98 if(expected_address
!= NULL
) {
99 printf (_("Looking for: '%s'\n"), expected_address
);
101 printf (_("Looking for: '%s'\n"), query_address
);
105 /* run the command */
106 if(np_runcmd(command_line
, &chld_out
, &chld_err
, 0) != 0) {
107 result
= STATE_WARNING
;
108 msg
= (char *)_("dig returned an error status");
111 for(i
= 0; i
< chld_out
.lines
; i
++) {
112 /* the server is responding, we just got the host name... */
113 if (strstr (chld_out
.line
[i
], ";; ANSWER SECTION:")) {
115 /* loop through the whole 'ANSWER SECTION' */
116 for(; i
< chld_out
.lines
; i
++) {
117 /* get the host address */
119 printf ("%s\n", chld_out
.line
[i
]);
121 if (strstr (chld_out
.line
[i
], (expected_address
== NULL
? query_address
: expected_address
)) != NULL
) {
122 msg
= chld_out
.line
[i
];
125 /* Translate output TAB -> SPACE */
127 while ((t
= strchr(t
, '\t')) != NULL
) *t
= ' ';
132 if (result
== STATE_UNKNOWN
) {
133 msg
= (char *)_("Server not found in ANSWER SECTION");
134 result
= STATE_WARNING
;
137 /* we found the answer section, so break out of the loop */
142 if (result
== STATE_UNKNOWN
) {
143 msg
= (char *)_("No ANSWER SECTION found");
144 result
= STATE_CRITICAL
;
147 /* If we get anything on STDERR, at least set warning */
148 if(chld_err
.buflen
> 0) {
149 result
= max_state(result
, STATE_WARNING
);
150 if(!msg
) for(i
= 0; i
< chld_err
.lines
; i
++) {
151 msg
= strchr(chld_err
.line
[0], ':');
159 microsec
= deltime (tv
);
160 elapsed_time
= (double)microsec
/ 1.0e6
;
162 if (critical_interval
> UNDEFINED
&& elapsed_time
> critical_interval
)
163 result
= STATE_CRITICAL
;
165 else if (warning_interval
> UNDEFINED
&& elapsed_time
> warning_interval
)
166 result
= STATE_WARNING
;
168 printf ("DNS %s - %.3f seconds response time (%s)|%s\n",
169 state_text (result
), elapsed_time
,
170 msg
? msg
: _("Probably a non-existent host/domain"),
171 fperfdata("time", elapsed_time
, "s",
172 (warning_interval
>UNDEFINED
?TRUE
:FALSE
),
174 (critical_interval
>UNDEFINED
?TRUE
:FALSE
),
182 /* process command-line arguments */
184 process_arguments (int argc
, char **argv
)
189 static struct option longopts
[] = {
190 {"hostname", required_argument
, 0, 'H'},
191 {"query_address", required_argument
, 0, 'l'},
192 {"warning", required_argument
, 0, 'w'},
193 {"critical", required_argument
, 0, 'c'},
194 {"timeout", required_argument
, 0, 't'},
195 {"dig-arguments", required_argument
, 0, 'A'},
196 {"verbose", no_argument
, 0, 'v'},
197 {"version", no_argument
, 0, 'V'},
198 {"help", no_argument
, 0, 'h'},
199 {"record_type", required_argument
, 0, 'T'},
200 {"expected_address", required_argument
, 0, 'a'},
201 {"port", required_argument
, 0, 'p'},
209 c
= getopt_long (argc
, argv
, "hVvt:l:H:w:c:T:p:a:A:", longopts
, &option
);
211 if (c
== -1 || c
== EOF
)
218 case 'V': /* version */
219 print_revision (progname
, NP_VERSION
);
221 case 'H': /* hostname */
225 case 'p': /* server port */
226 if (is_intpos (optarg
)) {
227 server_port
= atoi (optarg
);
230 usage_va(_("Port must be a positive integer - %s"), optarg
);
233 case 'l': /* address to lookup */
234 query_address
= optarg
;
236 case 'w': /* warning */
237 if (is_nonnegative (optarg
)) {
238 warning_interval
= strtod (optarg
, NULL
);
241 usage_va(_("Warning interval must be a positive integer - %s"), optarg
);
244 case 'c': /* critical */
245 if (is_nonnegative (optarg
)) {
246 critical_interval
= strtod (optarg
, NULL
);
249 usage_va(_("Critical interval must be a positive integer - %s"), optarg
);
252 case 't': /* timeout */
253 if (is_intnonneg (optarg
)) {
254 timeout_interval
= atoi (optarg
);
257 usage_va(_("Timeout interval must be a positive integer - %s"), optarg
);
260 case 'A': /* dig arguments */
261 dig_args
= strdup(optarg
);
263 case 'v': /* verbose */
267 record_type
= optarg
;
270 expected_address
= optarg
;
272 default: /* usage5 */
278 if (dns_server
== NULL
) {
280 host_or_die(argv
[c
]);
281 dns_server
= argv
[c
];
284 dns_server
= strdup ("127.0.0.1");
288 return validate_arguments ();
294 validate_arguments (void)
296 if (query_address
!= NULL
)
309 asprintf (&myport
, "%d", DEFAULT_PORT
);
311 print_revision (progname
, NP_VERSION
);
313 printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
314 printf (COPYRIGHT
, copyright
, email
);
316 printf (_("This plugin test the DNS service on the specified host using dig"));
322 printf (_(UT_HELP_VRSN
));
324 printf (_(UT_EXTRA_OPTS
));
326 printf (_(UT_HOST_PORT
), 'p', myport
);
328 printf (" %s\n","-l, --query_address=STRING");
329 printf (" %s\n",_("Machine name to lookup"));
330 printf (" %s\n","-T, --record_type=STRING");
331 printf (" %s\n",_("Record type to lookup (default: A)"));
332 printf (" %s\n","-a, --expected_address=STRING");
333 printf (" %s\n",_("An address expected to be in the answer section. If not set, uses whatever"));
334 printf (" %s\n",_("was in -l"));
335 printf (" %s\n","-A, --dig-arguments=STRING");
336 printf (" %s\n",_("Pass STRING as argument(s) to dig"));
337 printf (_(UT_WARN_CRIT
));
338 printf (_(UT_TIMEOUT
), DEFAULT_SOCKET_TIMEOUT
);
339 printf (_(UT_VERBOSE
));
342 printf ("%s\n", _("Examples:"));
343 printf (" %s\n", "check_dig -H DNSSERVER -l www.example.com -A \"+tcp\"");
344 printf (" %s\n", "This will send a tcp query to DNSSERVER for www.example.com");
348 printf ("%s\n", _("Notes:"));
349 printf (_(UT_EXTRA_OPTS_NOTES
));
352 printf (_(UT_SUPPORT
));
360 printf (_("Usage:"));
361 printf ("%s -l <query_address> [-H <host>] [-p <server port>]\n", progname
);
362 printf (" [-T <query type>] [-w <warning interval>] [-c <critical interval>]\n");
363 printf (" [-t <timeout>] [-a <expected answer address>] [-v]\n");