1 /*****************************************************************************
3 * Nagios check_dig plugin
6 * Copyright (c) 2002-2007 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-2007";
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
;
61 int server_port
= DEFAULT_PORT
;
62 double warning_interval
= UNDEFINED
;
63 double critical_interval
= UNDEFINED
;
67 main (int argc
, char **argv
)
70 output chld_out
, chld_err
;
76 int result
= STATE_UNKNOWN
;
78 setlocale (LC_ALL
, "");
79 bindtextdomain (PACKAGE
, LOCALEDIR
);
82 /* Set signal handling and alarm */
83 if (signal (SIGALRM
, popen_timeout_alarm_handler
) == SIG_ERR
)
84 usage_va(_("Cannot catch SIGALRM"));
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",
91 PATH_TO_DIG
, dns_server
, server_port
, query_address
, record_type
);
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");
145 /* If we get anything on STDERR, at least set warning */
146 if(chld_err
.buflen
> 0) {
147 result
= max_state(result
, STATE_WARNING
);
148 if(!msg
) for(i
= 0; i
< chld_err
.lines
; i
++) {
149 msg
= strchr(chld_err
.line
[0], ':');
157 microsec
= deltime (tv
);
158 elapsed_time
= (double)microsec
/ 1.0e6
;
160 if (critical_interval
> UNDEFINED
&& elapsed_time
> critical_interval
)
161 result
= STATE_CRITICAL
;
163 else if (warning_interval
> UNDEFINED
&& elapsed_time
> warning_interval
)
164 result
= STATE_WARNING
;
166 printf ("DNS %s - %.3f seconds response time (%s)|%s\n",
167 state_text (result
), elapsed_time
,
168 msg
? msg
: _("Probably a non-existent host/domain"),
169 fperfdata("time", elapsed_time
, "s",
170 (warning_interval
>UNDEFINED
?TRUE
:FALSE
),
172 (critical_interval
>UNDEFINED
?TRUE
:FALSE
),
180 /* process command-line arguments */
182 process_arguments (int argc
, char **argv
)
187 static struct option longopts
[] = {
188 {"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 {"verbose", no_argument
, 0, 'v'},
194 {"version", no_argument
, 0, 'V'},
195 {"help", no_argument
, 0, 'h'},
196 {"record_type", required_argument
, 0, 'T'},
197 {"expected_address", required_argument
, 0, 'a'},
198 {"port", required_argument
, 0, 'p'},
206 c
= getopt_long (argc
, argv
, "hVvt:l:H:w:c:T:p:a:", longopts
, &option
);
208 if (c
== -1 || c
== EOF
)
215 case 'V': /* version */
216 print_revision (progname
, revision
);
218 case 'H': /* hostname */
222 case 'p': /* server port */
223 if (is_intpos (optarg
)) {
224 server_port
= atoi (optarg
);
227 usage_va(_("Port must be a positive integer - %s"), optarg
);
230 case 'l': /* address to lookup */
231 query_address
= optarg
;
233 case 'w': /* warning */
234 if (is_nonnegative (optarg
)) {
235 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
);
246 usage_va(_("Critical interval must be a positive integer - %s"), optarg
);
249 case 't': /* timeout */
250 if (is_intnonneg (optarg
)) {
251 timeout_interval
= atoi (optarg
);
254 usage_va(_("Timeout interval must be a positive integer - %s"), optarg
);
257 case 'v': /* verbose */
261 record_type
= optarg
;
264 expected_address
= optarg
;
266 default: /* usage5 */
272 if (dns_server
== NULL
) {
274 host_or_die(argv
[c
]);
275 dns_server
= argv
[c
];
278 dns_server
= strdup ("127.0.0.1");
282 return validate_arguments ();
288 validate_arguments (void)
300 asprintf (&myport
, "%d", DEFAULT_PORT
);
302 print_revision (progname
, revision
);
304 printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
305 printf (COPYRIGHT
, copyright
, email
);
307 printf (_("This plugin test the DNS service on the specified host using dig"));
313 printf (_(UT_HELP_VRSN
));
315 printf (_(UT_HOST_PORT
), 'p', myport
);
317 printf (" %s\n","-l, --lookup=STRING");
318 printf (" %s\n",_("machine name to lookup"));
319 printf (" %s\n","-T, --record_type=STRING");
320 printf (" %s\n",_("record type to lookup (default: A)"));
321 printf (" %s\n","-a, --expected_address=STRING");
322 printf (" %s\n",_("an address expected to be in the answer section.if not set, uses whatever was in -l"));
323 printf (_(UT_WARN_CRIT
));
324 printf (_(UT_TIMEOUT
), DEFAULT_SOCKET_TIMEOUT
);
325 printf (_(UT_VERBOSE
));
326 printf (_(UT_SUPPORT
));
334 printf (_("Usage:"));
335 printf ("%s -H host -l lookup [-p <server port>] [-T <query type>]", progname
);
336 printf (" [-w <warning interval>] [-c <critical interval>] [-t <timeout>]");
337 printf (" [-a <expected answer address>] [-v]\n");