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 if (process_arguments (argc, argv) == ERROR)
88 usage_va(_("Could not parse arguments"));
90 /* get the command to run */
91 asprintf (&command_line, "%s @%s -p %d %s -t %s %s",
92 PATH_TO_DIG, dns_server, server_port, query_address, record_type, dig_args);
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);
103 }
104 }
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");
110 }
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;
130 }
131 }
133 if (result == STATE_UNKNOWN) {
134 msg = (char *)_("Server not found in ANSWER SECTION");
135 result = STATE_WARNING;
136 }
138 /* we found the answer section, so break out of the loop */
139 break;
140 }
141 }
143 if (result == STATE_UNKNOWN)
144 msg = (char *)_("No ANSWER SECTION found");
146 /* If we get anything on STDERR, at least set warning */
147 if(chld_err.buflen > 0) {
148 result = max_state(result, STATE_WARNING);
149 if(!msg) for(i = 0; i < chld_err.lines; i++) {
150 msg = strchr(chld_err.line[0], ':');
151 if(msg) {
152 msg++;
153 break;
154 }
155 }
156 }
158 microsec = deltime (tv);
159 elapsed_time = (double)microsec / 1.0e6;
161 if (critical_interval > UNDEFINED && elapsed_time > critical_interval)
162 result = STATE_CRITICAL;
164 else if (warning_interval > UNDEFINED && elapsed_time > warning_interval)
165 result = STATE_WARNING;
167 printf ("DNS %s - %.3f seconds response time (%s)|%s\n",
168 state_text (result), elapsed_time,
169 msg ? msg : _("Probably a non-existent host/domain"),
170 fperfdata("time", elapsed_time, "s",
171 (warning_interval>UNDEFINED?TRUE:FALSE),
172 warning_interval,
173 (critical_interval>UNDEFINED?TRUE:FALSE),
174 critical_interval,
175 TRUE, 0, FALSE, 0));
176 return result;
177 }
181 /* process command-line arguments */
182 int
183 process_arguments (int argc, char **argv)
184 {
185 int c;
187 int option = 0;
188 static struct option longopts[] = {
189 {"hostname", required_argument, 0, 'H'},
190 {"query_address", required_argument, 0, 'l'},
191 {"warning", required_argument, 0, 'w'},
192 {"critical", required_argument, 0, 'c'},
193 {"timeout", required_argument, 0, 't'},
194 {"dig-arguments", required_argument, 0, 'A'},
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'},
201 {0, 0, 0, 0}
202 };
204 if (argc < 2)
205 return ERROR;
207 while (1) {
208 c = getopt_long (argc, argv, "hVvt:l:H:w:c:T:p:a:A:", longopts, &option);
210 if (c == -1 || c == EOF)
211 break;
213 switch (c) {
214 case 'h': /* help */
215 print_help ();
216 exit (STATE_OK);
217 case 'V': /* version */
218 print_revision (progname, revision);
219 exit (STATE_OK);
220 case 'H': /* hostname */
221 host_or_die(optarg);
222 dns_server = optarg;
223 break;
224 case 'p': /* server port */
225 if (is_intpos (optarg)) {
226 server_port = atoi (optarg);
227 }
228 else {
229 usage_va(_("Port must be a positive integer - %s"), optarg);
230 }
231 break;
232 case 'l': /* address to lookup */
233 query_address = optarg;
234 break;
235 case 'w': /* warning */
236 if (is_nonnegative (optarg)) {
237 warning_interval = strtod (optarg, NULL);
238 }
239 else {
240 usage_va(_("Warning interval must be a positive integer - %s"), optarg);
241 }
242 break;
243 case 'c': /* critical */
244 if (is_nonnegative (optarg)) {
245 critical_interval = strtod (optarg, NULL);
246 }
247 else {
248 usage_va(_("Critical interval must be a positive integer - %s"), optarg);
249 }
250 break;
251 case 't': /* timeout */
252 if (is_intnonneg (optarg)) {
253 timeout_interval = atoi (optarg);
254 }
255 else {
256 usage_va(_("Timeout interval must be a positive integer - %s"), optarg);
257 }
258 break;
259 case 'A': /* dig arguments */
260 dig_args = strdup(optarg);
261 break;
262 case 'v': /* verbose */
263 verbose = TRUE;
264 break;
265 case 'T':
266 record_type = optarg;
267 break;
268 case 'a':
269 expected_address = optarg;
270 break;
271 default: /* usage5 */
272 usage5();
273 }
274 }
276 c = optind;
277 if (dns_server == NULL) {
278 if (c < argc) {
279 host_or_die(argv[c]);
280 dns_server = argv[c];
281 }
282 else {
283 dns_server = strdup ("127.0.0.1");
284 }
285 }
287 return validate_arguments ();
288 }
292 int
293 validate_arguments (void)
294 {
295 return OK;
296 }
300 void
301 print_help (void)
302 {
303 char *myport;
305 asprintf (&myport, "%d", DEFAULT_PORT);
307 print_revision (progname, revision);
309 printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
310 printf (COPYRIGHT, copyright, email);
312 printf (_("This plugin test the DNS service on the specified host using dig"));
314 printf ("\n\n");
316 print_usage ();
318 printf (_(UT_HELP_VRSN));
320 printf (_(UT_HOST_PORT), 'p', myport);
322 printf (" %s\n","-l, --query_address=STRING");
323 printf (" %s\n",_("Machine name to lookup"));
324 printf (" %s\n","-T, --record_type=STRING");
325 printf (" %s\n",_("Record type to lookup (default: A)"));
326 printf (" %s\n","-a, --expected_address=STRING");
327 printf (" %s\n",_("An address expected to be in the answer section. If not set, uses whatever"));
328 printf (" %s\n",_("was in -l"));
329 printf (" %s\n","-A, --dig-arguments=STRING");
330 printf (" %s\n",_("Pass STRING as argument(s) to dig"));
331 printf (_(UT_WARN_CRIT));
332 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
333 printf (_(UT_VERBOSE));
335 printf("\n");
336 printf ("%s\n", _("Examples:"));
337 printf (" %s\n", "check_dig -H DNSSERVER -l www.example.com -A \"+tcp\"");
338 printf (" %s\n", "This will send a tcp query to DNSSERVER for www.example.com");
340 printf (_(UT_SUPPORT));
341 }
345 void
346 print_usage (void)
347 {
348 printf (_("Usage:"));
349 printf ("%s -H <host> -l <query_address> [-p <server port>]\n", progname);
350 printf (" [-T <query type>] [-w <warning interval>] [-c <critical interval>]\n");
351 printf (" [-t <timeout>] [-a <expected answer address>] [-v]\n");
352 }