Add new Gnulib files to .gitignore
[monitoring-plugins.git] / plugins / check_dig.c
blobc113d87b10867b72a1ee4243253b1c31dadd7274
1 /*****************************************************************************
2 *
3 * Nagios check_dig plugin
4 *
5 * License: GPL
6 * Copyright (c) 2002-2008 Nagios Plugins Development Team
7 *
8 * Description:
9 *
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 *****************************************************************************/
29 /* Hackers note:
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";
39 #include "common.h"
40 #include "netutils.h"
41 #include "utils.h"
42 #include "runcmd.h"
44 int process_arguments (int, char **);
45 int validate_arguments (void);
46 void print_help (void);
47 void print_usage (void);
49 #define UNDEFINED 0
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;
56 char *dig_args = "";
57 char *query_transport = "";
58 int verbose = FALSE;
59 int server_port = DEFAULT_PORT;
60 double warning_interval = UNDEFINED;
61 double critical_interval = UNDEFINED;
62 struct timeval tv;
64 int
65 main (int argc, char **argv)
67 char *command_line;
68 output chld_out, chld_err;
69 char *msg = NULL;
70 size_t i;
71 char *t;
72 long microsec;
73 double elapsed_time;
74 int result = STATE_UNKNOWN;
76 setlocale (LC_ALL, "");
77 bindtextdomain (PACKAGE, LOCALEDIR);
78 textdomain (PACKAGE);
80 /* Set signal handling and alarm */
81 if (signal (SIGALRM, runcmd_timeout_alarm_handler) == SIG_ERR)
82 usage_va(_("Cannot catch SIGALRM"));
84 /* Parse extra opts if any */
85 argv=np_extra_opts (&argc, argv, progname);
87 if (process_arguments (argc, argv) == ERROR)
88 usage_va(_("Could not parse arguments"));
90 /* get the command to run */
91 xasprintf (&command_line, "%s @%s -p %d %s -t %s %s %s",
92 PATH_TO_DIG, dns_server, server_port, query_address, record_type, dig_args, query_transport);
94 alarm (timeout_interval);
95 gettimeofday (&tv, NULL);
97 if (verbose) {
98 printf ("%s\n", command_line);
99 if(expected_address != NULL) {
100 printf (_("Looking for: '%s'\n"), expected_address);
101 } else {
102 printf (_("Looking for: '%s'\n"), query_address);
106 /* run the command */
107 if(np_runcmd(command_line, &chld_out, &chld_err, 0) != 0) {
108 result = STATE_WARNING;
109 msg = (char *)_("dig returned an error status");
112 for(i = 0; i < chld_out.lines; i++) {
113 /* the server is responding, we just got the host name... */
114 if (strstr (chld_out.line[i], ";; ANSWER SECTION:")) {
116 /* loop through the whole 'ANSWER SECTION' */
117 for(; i < chld_out.lines; i++) {
118 /* get the host address */
119 if (verbose)
120 printf ("%s\n", chld_out.line[i]);
122 if (strstr (chld_out.line[i], (expected_address == NULL ? query_address : expected_address)) != NULL) {
123 msg = chld_out.line[i];
124 result = STATE_OK;
126 /* Translate output TAB -> SPACE */
127 t = msg;
128 while ((t = strchr(t, '\t')) != NULL) *t = ' ';
129 break;
133 if (result == STATE_UNKNOWN) {
134 msg = (char *)_("Server not found in ANSWER SECTION");
135 result = STATE_WARNING;
138 /* we found the answer section, so break out of the loop */
139 break;
143 if (result == STATE_UNKNOWN) {
144 msg = (char *)_("No ANSWER SECTION found");
145 result = STATE_CRITICAL;
148 /* If we get anything on STDERR, at least set warning */
149 if(chld_err.buflen > 0) {
150 result = max_state(result, STATE_WARNING);
151 if(!msg) for(i = 0; i < chld_err.lines; i++) {
152 msg = strchr(chld_err.line[0], ':');
153 if(msg) {
154 msg++;
155 break;
160 microsec = deltime (tv);
161 elapsed_time = (double)microsec / 1.0e6;
163 if (critical_interval > UNDEFINED && elapsed_time > critical_interval)
164 result = STATE_CRITICAL;
166 else if (warning_interval > UNDEFINED && elapsed_time > warning_interval)
167 result = STATE_WARNING;
169 printf ("DNS %s - %.3f seconds response time (%s)|%s\n",
170 state_text (result), elapsed_time,
171 msg ? msg : _("Probably a non-existent host/domain"),
172 fperfdata("time", elapsed_time, "s",
173 (warning_interval>UNDEFINED?TRUE:FALSE),
174 warning_interval,
175 (critical_interval>UNDEFINED?TRUE:FALSE),
176 critical_interval,
177 TRUE, 0, FALSE, 0));
178 return result;
183 /* process command-line arguments */
185 process_arguments (int argc, char **argv)
187 int c;
189 int option = 0;
190 static struct option longopts[] = {
191 {"hostname", required_argument, 0, 'H'},
192 {"query_address", required_argument, 0, 'l'},
193 {"warning", required_argument, 0, 'w'},
194 {"critical", required_argument, 0, 'c'},
195 {"timeout", required_argument, 0, 't'},
196 {"dig-arguments", required_argument, 0, 'A'},
197 {"verbose", no_argument, 0, 'v'},
198 {"version", no_argument, 0, 'V'},
199 {"help", no_argument, 0, 'h'},
200 {"record_type", required_argument, 0, 'T'},
201 {"expected_address", required_argument, 0, 'a'},
202 {"port", required_argument, 0, 'p'},
203 {"use-ipv4", no_argument, 0, '4'},
204 {"use-ipv6", no_argument, 0, '6'},
205 {0, 0, 0, 0}
208 if (argc < 2)
209 return ERROR;
211 while (1) {
212 c = getopt_long (argc, argv, "hVvt:l:H:w:c:T:p:a:A:46", longopts, &option);
214 if (c == -1 || c == EOF)
215 break;
217 switch (c) {
218 case 'h': /* help */
219 print_help ();
220 exit (STATE_OK);
221 case 'V': /* version */
222 print_revision (progname, NP_VERSION);
223 exit (STATE_OK);
224 case 'H': /* hostname */
225 host_or_die(optarg);
226 dns_server = optarg;
227 break;
228 case 'p': /* server port */
229 if (is_intpos (optarg)) {
230 server_port = atoi (optarg);
232 else {
233 usage_va(_("Port must be a positive integer - %s"), optarg);
235 break;
236 case 'l': /* address to lookup */
237 query_address = optarg;
238 break;
239 case 'w': /* warning */
240 if (is_nonnegative (optarg)) {
241 warning_interval = strtod (optarg, NULL);
243 else {
244 usage_va(_("Warning interval must be a positive integer - %s"), optarg);
246 break;
247 case 'c': /* critical */
248 if (is_nonnegative (optarg)) {
249 critical_interval = strtod (optarg, NULL);
251 else {
252 usage_va(_("Critical interval must be a positive integer - %s"), optarg);
254 break;
255 case 't': /* timeout */
256 if (is_intnonneg (optarg)) {
257 timeout_interval = atoi (optarg);
259 else {
260 usage_va(_("Timeout interval must be a positive integer - %s"), optarg);
262 break;
263 case 'A': /* dig arguments */
264 dig_args = strdup(optarg);
265 break;
266 case 'v': /* verbose */
267 verbose = TRUE;
268 break;
269 case 'T':
270 record_type = optarg;
271 break;
272 case 'a':
273 expected_address = optarg;
274 break;
275 case '4':
276 query_transport = "-4";
277 break;
278 case '6':
279 query_transport = "-6";
280 break;
281 default: /* usage5 */
282 usage5();
286 c = optind;
287 if (dns_server == NULL) {
288 if (c < argc) {
289 host_or_die(argv[c]);
290 dns_server = argv[c];
292 else {
293 dns_server = strdup ("127.0.0.1");
297 return validate_arguments ();
303 validate_arguments (void)
305 if (query_address != NULL)
306 return OK;
307 else
308 return ERROR;
313 void
314 print_help (void)
316 char *myport;
318 xasprintf (&myport, "%d", DEFAULT_PORT);
320 print_revision (progname, NP_VERSION);
322 printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
323 printf (COPYRIGHT, copyright, email);
325 printf (_("This plugin test the DNS service on the specified host using dig"));
327 printf ("\n\n");
329 print_usage ();
331 printf (UT_HELP_VRSN);
333 printf (UT_EXTRA_OPTS);
335 printf (UT_HOST_PORT, 'p', myport);
337 printf (" %s\n","-4, --use-ipv4");
338 printf (" %s\n",_("Force dig to only use IPv4 query transport"));
339 printf (" %s\n","-6, --use-ipv6");
340 printf (" %s\n",_("Force dig to only use IPv6 query transport"));
341 printf (" %s\n","-l, --query_address=STRING");
342 printf (" %s\n",_("Machine name to lookup"));
343 printf (" %s\n","-T, --record_type=STRING");
344 printf (" %s\n",_("Record type to lookup (default: A)"));
345 printf (" %s\n","-a, --expected_address=STRING");
346 printf (" %s\n",_("An address expected to be in the answer section. If not set, uses whatever"));
347 printf (" %s\n",_("was in -l"));
348 printf (" %s\n","-A, --dig-arguments=STRING");
349 printf (" %s\n",_("Pass STRING as argument(s) to dig"));
350 printf (UT_WARN_CRIT);
351 printf (UT_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
352 printf (UT_VERBOSE);
354 printf ("\n");
355 printf ("%s\n", _("Examples:"));
356 printf (" %s\n", "check_dig -H DNSSERVER -l www.example.com -A \"+tcp\"");
357 printf (" %s\n", "This will send a tcp query to DNSSERVER for www.example.com");
359 printf (UT_SUPPORT);
364 void
365 print_usage (void)
367 printf ("%s\n", _("Usage:"));
368 printf ("%s -l <query_address> [-H <host>] [-p <server port>]\n", progname);
369 printf (" [-T <query type>] [-w <warning interval>] [-c <critical interval>]\n");
370 printf (" [-t <timeout>] [-a <expected answer address>] [-v]\n");