1 /*****************************************************************************
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *****************************************************************************/
22 * There are typecasts to (char *) from _("foo bar") in this file.
23 * They prevent compiler warnings. Never (ever), permute strings obtained
24 * that are typecast from (const char *) (which happens when --disable-nls)
25 * because on some architectures those strings are in non-writable memory */
27 const char *progname
= "check_dig";
28 const char *revision
= "$Revision$";
29 const char *copyright
= "2002-2005";
30 const char *email
= "nagiosplug-devel@lists.sourceforge.net";
37 int process_arguments (int, char **);
38 int validate_arguments (void);
39 void print_help (void);
40 void print_usage (void);
43 #define DEFAULT_PORT 53
45 char *query_address
= NULL
;
46 char *record_type
= "A";
47 char *expected_address
= NULL
;
48 char *dns_server
= NULL
;
50 int server_port
= DEFAULT_PORT
;
51 double warning_interval
= UNDEFINED
;
52 double critical_interval
= UNDEFINED
;
56 main (int argc
, char **argv
)
59 output chld_out
, chld_err
;
65 int result
= STATE_UNKNOWN
;
67 setlocale (LC_ALL
, "");
68 bindtextdomain (PACKAGE
, LOCALEDIR
);
71 /* Set signal handling and alarm */
72 if (signal (SIGALRM
, popen_timeout_alarm_handler
) == SIG_ERR
)
73 usage_va(_("Cannot catch SIGALRM"));
75 if (process_arguments (argc
, argv
) == ERROR
)
76 usage_va(_("Could not parse arguments"));
78 /* get the command to run */
79 asprintf (&command_line
, "%s @%s -p %d %s -t %s",
80 PATH_TO_DIG
, dns_server
, server_port
, query_address
, record_type
);
82 alarm (timeout_interval
);
83 gettimeofday (&tv
, NULL
);
86 printf ("%s\n", command_line
);
87 if(expected_address
!= NULL
) {
88 printf (_("Looking for: '%s'\n"), expected_address
);
90 printf (_("Looking for: '%s'\n"), query_address
);
95 if(np_runcmd(command_line
, &chld_out
, &chld_err
, 0) != 0) {
96 result
= STATE_WARNING
;
97 msg
= (char *)_("dig returned an error status");
100 for(i
= 0; i
< chld_out
.lines
; i
++) {
101 /* the server is responding, we just got the host name... */
102 if (strstr (chld_out
.line
[i
], ";; ANSWER SECTION:")) {
104 /* loop through the whole 'ANSWER SECTION' */
105 for(; i
< chld_out
.lines
; i
++) {
106 /* get the host address */
108 printf ("%s\n", chld_out
.line
[i
]);
110 if (strstr (chld_out
.line
[i
], (expected_address
== NULL
? query_address
: expected_address
)) != NULL
) {
111 msg
= chld_out
.line
[i
];
114 /* Translate output TAB -> SPACE */
116 while ((t
= strchr(t
, '\t')) != NULL
) *t
= ' ';
121 if (result
== STATE_UNKNOWN
) {
122 msg
= (char *)_("Server not found in ANSWER SECTION");
123 result
= STATE_WARNING
;
126 /* we found the answer section, so break out of the loop */
131 if (result
== STATE_UNKNOWN
)
132 msg
= (char *)_("No ANSWER SECTION found");
134 /* If we get anything on STDERR, at least set warning */
135 if(chld_err
.buflen
> 0) {
136 result
= max_state(result
, STATE_WARNING
);
137 if(!msg
) for(i
= 0; i
< chld_err
.lines
; i
++) {
138 msg
= strchr(chld_err
.line
[0], ':');
146 microsec
= deltime (tv
);
147 elapsed_time
= (double)microsec
/ 1.0e6
;
149 if (critical_interval
> UNDEFINED
&& elapsed_time
> critical_interval
)
150 result
= STATE_CRITICAL
;
152 else if (warning_interval
> UNDEFINED
&& elapsed_time
> warning_interval
)
153 result
= STATE_WARNING
;
155 printf ("DNS %s - %.3f seconds response time (%s)|%s\n",
156 state_text (result
), elapsed_time
,
157 msg
? msg
: _("Probably a non-existent host/domain"),
158 fperfdata("time", elapsed_time
, "s",
159 (warning_interval
>UNDEFINED
?TRUE
:FALSE
),
161 (critical_interval
>UNDEFINED
?TRUE
:FALSE
),
169 /* process command-line arguments */
171 process_arguments (int argc
, char **argv
)
176 static struct option longopts
[] = {
177 {"hostname", required_argument
, 0, 'H'},
178 {"query_address", required_argument
, 0, 'l'},
179 {"warning", required_argument
, 0, 'w'},
180 {"critical", required_argument
, 0, 'c'},
181 {"timeout", required_argument
, 0, 't'},
182 {"verbose", no_argument
, 0, 'v'},
183 {"version", no_argument
, 0, 'V'},
184 {"help", no_argument
, 0, 'h'},
185 {"record_type", required_argument
, 0, 'T'},
186 {"expected_address", required_argument
, 0, 'a'},
194 c
= getopt_long (argc
, argv
, "hVvt:l:H:w:c:T:a:", longopts
, &option
);
196 if (c
== -1 || c
== EOF
)
203 case 'V': /* version */
204 print_revision (progname
, revision
);
206 case 'H': /* hostname */
210 case 'p': /* server port */
211 if (is_intpos (optarg
)) {
212 server_port
= atoi (optarg
);
215 usage_va(_("Port must be a positive integer - %s"), optarg
);
218 case 'l': /* address to lookup */
219 query_address
= optarg
;
221 case 'w': /* warning */
222 if (is_nonnegative (optarg
)) {
223 warning_interval
= strtod (optarg
, NULL
);
226 usage_va(_("Warning interval must be a positive integer - %s"), optarg
);
229 case 'c': /* critical */
230 if (is_nonnegative (optarg
)) {
231 critical_interval
= strtod (optarg
, NULL
);
234 usage_va(_("Critical interval must be a positive integer - %s"), optarg
);
237 case 't': /* timeout */
238 if (is_intnonneg (optarg
)) {
239 timeout_interval
= atoi (optarg
);
242 usage_va(_("Timeout interval must be a positive integer - %s"), optarg
);
245 case 'v': /* verbose */
249 record_type
= optarg
;
252 expected_address
= optarg
;
254 default: /* usage_va */
255 usage_va(_("Unknown argument - %s"), optarg
);
260 if (dns_server
== NULL
) {
262 host_or_die(argv
[c
]);
263 dns_server
= argv
[c
];
266 dns_server
= strdup ("127.0.0.1");
270 return validate_arguments ();
276 validate_arguments (void)
288 asprintf (&myport
, "%d", DEFAULT_PORT
);
290 print_revision (progname
, revision
);
292 printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
293 printf (COPYRIGHT
, copyright
, email
);
295 printf (_("This plugin test the DNS service on the specified host using dig"));
301 printf (_(UT_HELP_VRSN
));
303 printf (_(UT_HOST_PORT
), 'P', myport
);
305 printf ("-l, --lookup=STRING");
309 printf (_("machine name to lookup"));
313 printf ("-T, --record_type=STRING");
317 printf (_("record type to lookup (default: A)"));
321 printf ("-a, --expected_address=STRING");
325 printf (_("an address expected to be in the answer section.if not set, uses whatever was in -l"));
329 printf (_(UT_WARN_CRIT
));
331 printf (_(UT_TIMEOUT
), DEFAULT_SOCKET_TIMEOUT
);
333 printf (_(UT_VERBOSE
));
335 printf (_(UT_SUPPORT
));
343 printf (_("Usage:"));
344 printf ("%s -H host -l lookup [-p <server port>] [-T <query type>]", progname
);
345 printf (" [-w <warning interval>] [-c <critical interval>] [-t <timeout>]");
346 printf (" [-a <expected answer address>] [-v]\n");