1 /*****************************************************************************
3 * Nagios check_dig plugin
6 * Copyright (c) 2002-2008 Nagios Plugins Development Team
8 * Last Modified: $Date$
12 * This file contains the check_dig plugin
15 * This program is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 *****************************************************************************/
33 * There are typecasts to (char *) from _("foo bar") in this file.
34 * They prevent compiler warnings. Never (ever), permute strings obtained
35 * that are typecast from (const char *) (which happens when --disable-nls)
36 * because on some architectures those strings are in non-writable memory */
38 const char *progname
= "check_dig";
39 const char *revision
= "$Revision$";
40 const char *copyright
= "2002-2008";
41 const char *email
= "nagiosplug-devel@lists.sourceforge.net";
48 int process_arguments (int, char **);
49 int validate_arguments (void);
50 void print_help (void);
51 void print_usage (void);
54 #define DEFAULT_PORT 53
56 char *query_address
= NULL
;
57 char *record_type
= "A";
58 char *expected_address
= NULL
;
59 char *dns_server
= NULL
;
62 int server_port
= DEFAULT_PORT
;
63 double warning_interval
= UNDEFINED
;
64 double critical_interval
= UNDEFINED
;
68 main (int argc
, char **argv
)
71 output chld_out
, chld_err
;
77 int result
= STATE_UNKNOWN
;
79 setlocale (LC_ALL
, "");
80 bindtextdomain (PACKAGE
, LOCALEDIR
);
83 /* Set signal handling and alarm */
84 if (signal (SIGALRM
, popen_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 /* get the command to run */
94 asprintf (&command_line
, "%s @%s -p %d %s -t %s %s",
95 PATH_TO_DIG
, dns_server
, server_port
, query_address
, record_type
, dig_args
);
97 alarm (timeout_interval
);
98 gettimeofday (&tv
, NULL
);
101 printf ("%s\n", command_line
);
102 if(expected_address
!= NULL
) {
103 printf (_("Looking for: '%s'\n"), expected_address
);
105 printf (_("Looking for: '%s'\n"), query_address
);
109 /* run the command */
110 if(np_runcmd(command_line
, &chld_out
, &chld_err
, 0) != 0) {
111 result
= STATE_WARNING
;
112 msg
= (char *)_("dig returned an error status");
115 for(i
= 0; i
< chld_out
.lines
; i
++) {
116 /* the server is responding, we just got the host name... */
117 if (strstr (chld_out
.line
[i
], ";; ANSWER SECTION:")) {
119 /* loop through the whole 'ANSWER SECTION' */
120 for(; i
< chld_out
.lines
; i
++) {
121 /* get the host address */
123 printf ("%s\n", chld_out
.line
[i
]);
125 if (strstr (chld_out
.line
[i
], (expected_address
== NULL
? query_address
: expected_address
)) != NULL
) {
126 msg
= chld_out
.line
[i
];
129 /* Translate output TAB -> SPACE */
131 while ((t
= strchr(t
, '\t')) != NULL
) *t
= ' ';
136 if (result
== STATE_UNKNOWN
) {
137 msg
= (char *)_("Server not found in ANSWER SECTION");
138 result
= STATE_WARNING
;
141 /* we found the answer section, so break out of the loop */
146 if (result
== STATE_UNKNOWN
)
147 msg
= (char *)_("No ANSWER SECTION found");
149 /* If we get anything on STDERR, at least set warning */
150 if(chld_err
.buflen
> 0) {
151 result
= max_state(result
, STATE_WARNING
);
152 if(!msg
) for(i
= 0; i
< chld_err
.lines
; i
++) {
153 msg
= strchr(chld_err
.line
[0], ':');
161 microsec
= deltime (tv
);
162 elapsed_time
= (double)microsec
/ 1.0e6
;
164 if (critical_interval
> UNDEFINED
&& elapsed_time
> critical_interval
)
165 result
= STATE_CRITICAL
;
167 else if (warning_interval
> UNDEFINED
&& elapsed_time
> warning_interval
)
168 result
= STATE_WARNING
;
170 printf ("DNS %s - %.3f seconds response time (%s)|%s\n",
171 state_text (result
), elapsed_time
,
172 msg
? msg
: _("Probably a non-existent host/domain"),
173 fperfdata("time", elapsed_time
, "s",
174 (warning_interval
>UNDEFINED
?TRUE
:FALSE
),
176 (critical_interval
>UNDEFINED
?TRUE
:FALSE
),
184 /* process command-line arguments */
186 process_arguments (int argc
, char **argv
)
191 static struct option longopts
[] = {
192 {"hostname", required_argument
, 0, 'H'},
193 {"query_address", required_argument
, 0, 'l'},
194 {"warning", required_argument
, 0, 'w'},
195 {"critical", required_argument
, 0, 'c'},
196 {"timeout", required_argument
, 0, 't'},
197 {"dig-arguments", required_argument
, 0, 'A'},
198 {"verbose", no_argument
, 0, 'v'},
199 {"version", no_argument
, 0, 'V'},
200 {"help", no_argument
, 0, 'h'},
201 {"record_type", required_argument
, 0, 'T'},
202 {"expected_address", required_argument
, 0, 'a'},
203 {"port", required_argument
, 0, 'p'},
211 c
= getopt_long (argc
, argv
, "hVvt:l:H:w:c:T:p:a:A:", longopts
, &option
);
213 if (c
== -1 || c
== EOF
)
220 case 'V': /* version */
221 print_revision (progname
, revision
);
223 case 'H': /* hostname */
227 case 'p': /* server port */
228 if (is_intpos (optarg
)) {
229 server_port
= atoi (optarg
);
232 usage_va(_("Port must be a positive integer - %s"), optarg
);
235 case 'l': /* address to lookup */
236 query_address
= optarg
;
238 case 'w': /* warning */
239 if (is_nonnegative (optarg
)) {
240 warning_interval
= strtod (optarg
, NULL
);
243 usage_va(_("Warning interval must be a positive integer - %s"), optarg
);
246 case 'c': /* critical */
247 if (is_nonnegative (optarg
)) {
248 critical_interval
= strtod (optarg
, NULL
);
251 usage_va(_("Critical interval must be a positive integer - %s"), optarg
);
254 case 't': /* timeout */
255 if (is_intnonneg (optarg
)) {
256 timeout_interval
= atoi (optarg
);
259 usage_va(_("Timeout interval must be a positive integer - %s"), optarg
);
262 case 'A': /* dig arguments */
263 dig_args
= strdup(optarg
);
265 case 'v': /* verbose */
269 record_type
= optarg
;
272 expected_address
= optarg
;
274 default: /* usage5 */
280 if (dns_server
== NULL
) {
282 host_or_die(argv
[c
]);
283 dns_server
= argv
[c
];
286 dns_server
= strdup ("127.0.0.1");
290 return validate_arguments ();
296 validate_arguments (void)
308 asprintf (&myport
, "%d", DEFAULT_PORT
);
310 print_revision (progname
, revision
);
312 printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
313 printf (COPYRIGHT
, copyright
, email
);
315 printf (_("This plugin test the DNS service on the specified host using dig"));
321 printf (_(UT_HELP_VRSN
));
323 printf (_(UT_EXTRA_OPTS
));
325 printf (_(UT_HOST_PORT
), 'p', myport
);
327 printf (" %s\n","-l, --query_address=STRING");
328 printf (" %s\n",_("Machine name to lookup"));
329 printf (" %s\n","-T, --record_type=STRING");
330 printf (" %s\n",_("Record type to lookup (default: A)"));
331 printf (" %s\n","-a, --expected_address=STRING");
332 printf (" %s\n",_("An address expected to be in the answer section. If not set, uses whatever"));
333 printf (" %s\n",_("was in -l"));
334 printf (" %s\n","-A, --dig-arguments=STRING");
335 printf (" %s\n",_("Pass STRING as argument(s) to dig"));
336 printf (_(UT_WARN_CRIT
));
337 printf (_(UT_TIMEOUT
), DEFAULT_SOCKET_TIMEOUT
);
338 printf (_(UT_VERBOSE
));
341 printf ("%s\n", _("Examples:"));
342 printf (" %s\n", "check_dig -H DNSSERVER -l www.example.com -A \"+tcp\"");
343 printf (" %s\n", "This will send a tcp query to DNSSERVER for www.example.com");
347 printf ("%s\n", _("Notes:"));
348 printf (_(UT_EXTRA_OPTS_NOTES
));
351 printf (_(UT_SUPPORT
));
359 printf (_("Usage:"));
360 printf ("%s -H <host> -l <query_address> [-p <server port>]\n", progname
);
361 printf (" [-T <query type>] [-w <warning interval>] [-c <critical interval>]\n");
362 printf (" [-t <timeout>] [-a <expected answer address>] [-v]\n");