1 /*****************************************************************************
3 * Nagios check_dig plugin
6 * Copyright (c) 1999-2006 nagios-plugins team
8 * Last Modified: $Date$
12 * This file contains the check_dig plugin
14 * License Information:
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 *****************************************************************************/
35 * There are typecasts to (char *) from _("foo bar") in this file.
36 * They prevent compiler warnings. Never (ever), permute strings obtained
37 * that are typecast from (const char *) (which happens when --disable-nls)
38 * because on some architectures those strings are in non-writable memory */
40 const char *progname
= "check_dig";
41 const char *revision
= "$Revision$";
42 const char *copyright
= "2002-2006";
43 const char *email
= "nagiosplug-devel@lists.sourceforge.net";
50 int process_arguments (int, char **);
51 int validate_arguments (void);
52 void print_help (void);
53 void print_usage (void);
56 #define DEFAULT_PORT 53
58 char *query_address
= NULL
;
59 char *record_type
= "A";
60 char *expected_address
= NULL
;
61 char *dns_server
= NULL
;
63 int server_port
= DEFAULT_PORT
;
64 double warning_interval
= UNDEFINED
;
65 double critical_interval
= UNDEFINED
;
69 main (int argc
, char **argv
)
72 output chld_out
, chld_err
;
78 int result
= STATE_UNKNOWN
;
80 setlocale (LC_ALL
, "");
81 bindtextdomain (PACKAGE
, LOCALEDIR
);
84 /* Set signal handling and alarm */
85 if (signal (SIGALRM
, popen_timeout_alarm_handler
) == SIG_ERR
)
86 usage_va(_("Cannot catch SIGALRM"));
88 if (process_arguments (argc
, argv
) == ERROR
)
89 usage_va(_("Could not parse arguments"));
91 /* get the command to run */
92 asprintf (&command_line
, "%s @%s -p %d %s -t %s",
93 PATH_TO_DIG
, dns_server
, server_port
, query_address
, record_type
);
95 alarm (timeout_interval
);
96 gettimeofday (&tv
, NULL
);
99 printf ("%s\n", command_line
);
100 if(expected_address
!= NULL
) {
101 printf (_("Looking for: '%s'\n"), expected_address
);
103 printf (_("Looking for: '%s'\n"), query_address
);
107 /* run the command */
108 if(np_runcmd(command_line
, &chld_out
, &chld_err
, 0) != 0) {
109 result
= STATE_WARNING
;
110 msg
= (char *)_("dig returned an error status");
113 for(i
= 0; i
< chld_out
.lines
; i
++) {
114 /* the server is responding, we just got the host name... */
115 if (strstr (chld_out
.line
[i
], ";; ANSWER SECTION:")) {
117 /* loop through the whole 'ANSWER SECTION' */
118 for(; i
< chld_out
.lines
; i
++) {
119 /* get the host address */
121 printf ("%s\n", chld_out
.line
[i
]);
123 if (strstr (chld_out
.line
[i
], (expected_address
== NULL
? query_address
: expected_address
)) != NULL
) {
124 msg
= chld_out
.line
[i
];
127 /* Translate output TAB -> SPACE */
129 while ((t
= strchr(t
, '\t')) != NULL
) *t
= ' ';
134 if (result
== STATE_UNKNOWN
) {
135 msg
= (char *)_("Server not found in ANSWER SECTION");
136 result
= STATE_WARNING
;
139 /* we found the answer section, so break out of the loop */
144 if (result
== STATE_UNKNOWN
)
145 msg
= (char *)_("No ANSWER SECTION found");
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 {"verbose", no_argument
, 0, 'v'},
196 {"version", no_argument
, 0, 'V'},
197 {"help", no_argument
, 0, 'h'},
198 {"record_type", required_argument
, 0, 'T'},
199 {"expected_address", required_argument
, 0, 'a'},
200 {"port", required_argument
, 0, 'p'},
208 c
= getopt_long (argc
, argv
, "hVvt:l:H:w:c:T:p:a:", longopts
, &option
);
210 if (c
== -1 || c
== EOF
)
217 case 'V': /* version */
218 print_revision (progname
, revision
);
220 case 'H': /* hostname */
224 case 'p': /* server port */
225 if (is_intpos (optarg
)) {
226 server_port
= atoi (optarg
);
229 usage_va(_("Port must be a positive integer - %s"), optarg
);
232 case 'l': /* address to lookup */
233 query_address
= optarg
;
235 case 'w': /* warning */
236 if (is_nonnegative (optarg
)) {
237 warning_interval
= strtod (optarg
, NULL
);
240 usage_va(_("Warning interval must be a positive integer - %s"), optarg
);
243 case 'c': /* critical */
244 if (is_nonnegative (optarg
)) {
245 critical_interval
= strtod (optarg
, NULL
);
248 usage_va(_("Critical interval must be a positive integer - %s"), optarg
);
251 case 't': /* timeout */
252 if (is_intnonneg (optarg
)) {
253 timeout_interval
= atoi (optarg
);
256 usage_va(_("Timeout interval must be a positive integer - %s"), optarg
);
259 case 'v': /* verbose */
263 record_type
= optarg
;
266 expected_address
= optarg
;
268 default: /* usage5 */
274 if (dns_server
== NULL
) {
276 host_or_die(argv
[c
]);
277 dns_server
= argv
[c
];
280 dns_server
= strdup ("127.0.0.1");
284 return validate_arguments ();
290 validate_arguments (void)
302 asprintf (&myport
, "%d", DEFAULT_PORT
);
304 print_revision (progname
, revision
);
306 printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
307 printf (COPYRIGHT
, copyright
, email
);
309 printf (_("This plugin test the DNS service on the specified host using dig"));
315 printf (_(UT_HELP_VRSN
));
317 printf (_(UT_HOST_PORT
), 'p', myport
);
319 printf (" %s\n","-l, --lookup=STRING");
320 printf (" %s\n",_("machine name to lookup"));
321 printf (" %s\n","-T, --record_type=STRING");
322 printf (" %s\n",_("record type to lookup (default: A)"));
323 printf (" %s\n","-a, --expected_address=STRING");
324 printf (" %s\n",_("an address expected to be in the answer section.if not set, uses whatever was in -l"));
325 printf (_(UT_WARN_CRIT
));
326 printf (_(UT_TIMEOUT
), DEFAULT_SOCKET_TIMEOUT
);
327 printf (_(UT_VERBOSE
));
328 printf (_(UT_SUPPORT
));
336 printf (_("Usage:"));
337 printf ("%s -H host -l lookup [-p <server port>] [-T <query type>]", progname
);
338 printf (" [-w <warning interval>] [-c <critical interval>] [-t <timeout>]");
339 printf (" [-a <expected answer address>] [-v]\n");