1 /*****************************************************************************
2 *
3 * Nagios check_dig plugin
4 *
5 * License: GPL
6 * Copyright (c) 2002-2008 Nagios Plugins Development Team
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
11 *
12 * This file contains the check_dig plugin
13 *
14 *
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.
19 *
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.
24 *
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/>.
27 *
28 * $Id$
29 *
30 *****************************************************************************/
32 /* Hackers note:
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";
43 #include "common.h"
44 #include "netutils.h"
45 #include "utils.h"
46 #include "runcmd.h"
48 int process_arguments (int, char **);
49 int validate_arguments (void);
50 void print_help (void);
51 void print_usage (void);
53 #define UNDEFINED 0
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;
60 char *dig_args = "";
61 int verbose = FALSE;
62 int server_port = DEFAULT_PORT;
63 double warning_interval = UNDEFINED;
64 double critical_interval = UNDEFINED;
65 struct timeval tv;
67 int
68 main (int argc, char **argv)
69 {
70 char *command_line;
71 output chld_out, chld_err;
72 char *msg = NULL;
73 size_t i;
74 char *t;
75 long microsec;
76 double elapsed_time;
77 int result = STATE_UNKNOWN;
79 setlocale (LC_ALL, "");
80 bindtextdomain (PACKAGE, LOCALEDIR);
81 textdomain (PACKAGE);
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);
100 if (verbose) {
101 printf ("%s\n", command_line);
102 if(expected_address != NULL) {
103 printf (_("Looking for: '%s'\n"), expected_address);
104 } else {
105 printf (_("Looking for: '%s'\n"), query_address);
106 }
107 }
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");
113 }
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 */
122 if (verbose)
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];
127 result = STATE_OK;
129 /* Translate output TAB -> SPACE */
130 t = msg;
131 while ((t = strchr(t, '\t')) != NULL) *t = ' ';
132 break;
133 }
134 }
136 if (result == STATE_UNKNOWN) {
137 msg = (char *)_("Server not found in ANSWER SECTION");
138 result = STATE_WARNING;
139 }
141 /* we found the answer section, so break out of the loop */
142 break;
143 }
144 }
146 if (result == STATE_UNKNOWN) {
147 msg = (char *)_("No ANSWER SECTION found");
148 result = STATE_CRITICAL;
149 }
151 /* If we get anything on STDERR, at least set warning */
152 if(chld_err.buflen > 0) {
153 result = max_state(result, STATE_WARNING);
154 if(!msg) for(i = 0; i < chld_err.lines; i++) {
155 msg = strchr(chld_err.line[0], ':');
156 if(msg) {
157 msg++;
158 break;
159 }
160 }
161 }
163 microsec = deltime (tv);
164 elapsed_time = (double)microsec / 1.0e6;
166 if (critical_interval > UNDEFINED && elapsed_time > critical_interval)
167 result = STATE_CRITICAL;
169 else if (warning_interval > UNDEFINED && elapsed_time > warning_interval)
170 result = STATE_WARNING;
172 printf ("DNS %s - %.3f seconds response time (%s)|%s\n",
173 state_text (result), elapsed_time,
174 msg ? msg : _("Probably a non-existent host/domain"),
175 fperfdata("time", elapsed_time, "s",
176 (warning_interval>UNDEFINED?TRUE:FALSE),
177 warning_interval,
178 (critical_interval>UNDEFINED?TRUE:FALSE),
179 critical_interval,
180 TRUE, 0, FALSE, 0));
181 return result;
182 }
186 /* process command-line arguments */
187 int
188 process_arguments (int argc, char **argv)
189 {
190 int c;
192 int option = 0;
193 static struct option longopts[] = {
194 {"hostname", required_argument, 0, 'H'},
195 {"query_address", required_argument, 0, 'l'},
196 {"warning", required_argument, 0, 'w'},
197 {"critical", required_argument, 0, 'c'},
198 {"timeout", required_argument, 0, 't'},
199 {"dig-arguments", required_argument, 0, 'A'},
200 {"verbose", no_argument, 0, 'v'},
201 {"version", no_argument, 0, 'V'},
202 {"help", no_argument, 0, 'h'},
203 {"record_type", required_argument, 0, 'T'},
204 {"expected_address", required_argument, 0, 'a'},
205 {"port", required_argument, 0, 'p'},
206 {0, 0, 0, 0}
207 };
209 if (argc < 2)
210 return ERROR;
212 while (1) {
213 c = getopt_long (argc, argv, "hVvt:l:H:w:c:T:p:a:A:", longopts, &option);
215 if (c == -1 || c == EOF)
216 break;
218 switch (c) {
219 case 'h': /* help */
220 print_help ();
221 exit (STATE_OK);
222 case 'V': /* version */
223 print_revision (progname, revision);
224 exit (STATE_OK);
225 case 'H': /* hostname */
226 host_or_die(optarg);
227 dns_server = optarg;
228 break;
229 case 'p': /* server port */
230 if (is_intpos (optarg)) {
231 server_port = atoi (optarg);
232 }
233 else {
234 usage_va(_("Port must be a positive integer - %s"), optarg);
235 }
236 break;
237 case 'l': /* address to lookup */
238 query_address = optarg;
239 break;
240 case 'w': /* warning */
241 if (is_nonnegative (optarg)) {
242 warning_interval = strtod (optarg, NULL);
243 }
244 else {
245 usage_va(_("Warning interval must be a positive integer - %s"), optarg);
246 }
247 break;
248 case 'c': /* critical */
249 if (is_nonnegative (optarg)) {
250 critical_interval = strtod (optarg, NULL);
251 }
252 else {
253 usage_va(_("Critical interval must be a positive integer - %s"), optarg);
254 }
255 break;
256 case 't': /* timeout */
257 if (is_intnonneg (optarg)) {
258 timeout_interval = atoi (optarg);
259 }
260 else {
261 usage_va(_("Timeout interval must be a positive integer - %s"), optarg);
262 }
263 break;
264 case 'A': /* dig arguments */
265 dig_args = strdup(optarg);
266 break;
267 case 'v': /* verbose */
268 verbose = TRUE;
269 break;
270 case 'T':
271 record_type = optarg;
272 break;
273 case 'a':
274 expected_address = optarg;
275 break;
276 default: /* usage5 */
277 usage5();
278 }
279 }
281 c = optind;
282 if (dns_server == NULL) {
283 if (c < argc) {
284 host_or_die(argv[c]);
285 dns_server = argv[c];
286 }
287 else {
288 dns_server = strdup ("127.0.0.1");
289 }
290 }
292 return validate_arguments ();
293 }
297 int
298 validate_arguments (void)
299 {
300 if (query_address != NULL)
301 return OK;
302 else
303 return ERROR;
304 }
308 void
309 print_help (void)
310 {
311 char *myport;
313 asprintf (&myport, "%d", DEFAULT_PORT);
315 print_revision (progname, revision);
317 printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
318 printf (COPYRIGHT, copyright, email);
320 printf (_("This plugin test the DNS service on the specified host using dig"));
322 printf ("\n\n");
324 print_usage ();
326 printf (_(UT_HELP_VRSN));
328 printf (_(UT_EXTRA_OPTS));
330 printf (_(UT_HOST_PORT), 'p', myport);
332 printf (" %s\n","-l, --query_address=STRING");
333 printf (" %s\n",_("Machine name to lookup"));
334 printf (" %s\n","-T, --record_type=STRING");
335 printf (" %s\n",_("Record type to lookup (default: A)"));
336 printf (" %s\n","-a, --expected_address=STRING");
337 printf (" %s\n",_("An address expected to be in the answer section. If not set, uses whatever"));
338 printf (" %s\n",_("was in -l"));
339 printf (" %s\n","-A, --dig-arguments=STRING");
340 printf (" %s\n",_("Pass STRING as argument(s) to dig"));
341 printf (_(UT_WARN_CRIT));
342 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
343 printf (_(UT_VERBOSE));
345 printf ("\n");
346 printf ("%s\n", _("Examples:"));
347 printf (" %s\n", "check_dig -H DNSSERVER -l www.example.com -A \"+tcp\"");
348 printf (" %s\n", "This will send a tcp query to DNSSERVER for www.example.com");
350 #ifdef NP_EXTRA_OPTS
351 printf ("\n");
352 printf ("%s\n", _("Notes:"));
353 printf (_(UT_EXTRA_OPTS_NOTES));
354 #endif
356 printf (_(UT_SUPPORT));
357 }
361 void
362 print_usage (void)
363 {
364 printf (_("Usage:"));
365 printf ("%s -l <query_address> [-H <host>] [-p <server port>]\n", progname);
366 printf (" [-T <query type>] [-w <warning interval>] [-c <critical interval>]\n");
367 printf (" [-t <timeout>] [-a <expected answer address>] [-v]\n");
368 }