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.
17 $Id$
19 *****************************************************************************/
21 const char *progname = "check_dig";
22 const char *revision = "$Revision$";
23 const char *copyright = "2002-2004";
24 const char *email = "nagiosplug-devel@lists.sourceforge.net";
26 #include "common.h"
27 #include "netutils.h"
28 #include "utils.h"
29 #include "popen.h"
31 int process_arguments (int, char **);
32 int validate_arguments (void);
33 void print_help (void);
34 void print_usage (void);
36 enum {
37 UNDEFINED = 0,
38 DEFAULT_PORT = 53
39 };
41 char *query_address = NULL;
42 char *record_type = "A";
43 char *expected_address = NULL;
44 char *dns_server = NULL;
45 int verbose = FALSE;
46 int server_port = DEFAULT_PORT;
47 double warning_interval = UNDEFINED;
48 double critical_interval = UNDEFINED;
49 struct timeval tv;
51 int
52 main (int argc, char **argv)
53 {
54 char input_buffer[MAX_INPUT_BUFFER];
55 char *command_line;
56 char *output;
57 long microsec;
58 double elapsed_time;
59 int result = STATE_UNKNOWN;
61 output = strdup ("");
63 setlocale (LC_ALL, "");
64 bindtextdomain (PACKAGE, LOCALEDIR);
65 textdomain (PACKAGE);
67 /* Set signal handling and alarm */
68 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
69 usage4 (_("Cannot catch SIGALRM"));
71 if (process_arguments (argc, argv) == ERROR)
72 usage4 (_("Could not parse arguments"));
74 /* get the command to run */
75 asprintf (&command_line, "%s @%s -p %d %s -t %s",
76 PATH_TO_DIG, dns_server, server_port, query_address, record_type);
78 alarm (timeout_interval);
79 gettimeofday (&tv, NULL);
81 if (verbose) {
82 printf ("%s\n", command_line);
83 if(expected_address != NULL) {
84 printf ("Looking for: '%s'\n", expected_address);
85 } else {
86 printf ("Looking for: '%s'\n", query_address);
87 }
88 }
90 /* run the command */
91 child_process = spopen (command_line);
92 if (child_process == NULL) {
93 printf (_("Could not open pipe: %s\n"), command_line);
94 return STATE_UNKNOWN;
95 }
97 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
98 if (child_stderr == NULL)
99 printf (_("Could not open stderr for %s\n"), command_line);
101 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
103 /* the server is responding, we just got the host name... */
104 if (strstr (input_buffer, ";; ANSWER SECTION:")) {
106 /* loop through the whole 'ANSWER SECTION' */
107 do {
108 /* get the host address */
109 if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
110 break;
112 if (strpbrk (input_buffer, "\r\n"))
113 input_buffer[strcspn (input_buffer, "\r\n")] = '\0';
115 if (verbose && !strstr (input_buffer, ";; "))
116 printf ("%s\n", input_buffer);
118 if (expected_address==NULL && strstr (input_buffer, query_address) != NULL) {
119 output = strdup(input_buffer);
120 result = STATE_OK;
121 }
122 else if (expected_address != NULL && strstr (input_buffer, expected_address) != NULL) {
123 output = strdup(input_buffer);
124 result = STATE_OK;
125 }
127 } while (!strstr (input_buffer, ";; "));
129 if (result == STATE_UNKNOWN) {
130 asprintf (&output, _("Server not found in ANSWER SECTION"));
131 result = STATE_WARNING;
132 }
133 }
135 }
137 if (result == STATE_UNKNOWN) {
138 asprintf (&output, _("No ANSWER SECTION found"));
139 }
141 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
142 /* If we get anything on STDERR, at least set warning */
143 result = max_state (result, STATE_WARNING);
144 printf ("%s", input_buffer);
145 if (strlen (output) == 0)
146 output = strdup (1 + index (input_buffer, ':'));
147 }
149 (void) fclose (child_stderr);
151 /* close the pipe */
152 if (spclose (child_process)) {
153 result = max_state (result, STATE_WARNING);
154 if (strlen (output) == 0)
155 asprintf (&output, _("dig returned an error status"));
156 }
158 microsec = deltime (tv);
159 elapsed_time = (double)microsec / 1.0e6;
161 if (output == NULL || strlen (output) == 0)
162 asprintf (&output, _(" Probably a non-existent host/domain"));
164 if (critical_interval > UNDEFINED && elapsed_time > critical_interval)
165 result = STATE_CRITICAL;
167 else if (warning_interval > UNDEFINED && elapsed_time > warning_interval)
168 result = STATE_WARNING;
170 asprintf (&output, _("%.3f seconds response time (%s)"), elapsed_time, output);
172 printf ("DNS %s - %s|%s\n",
173 state_text (result), output,
174 fperfdata("time", elapsed_time, "s",
175 (warning_interval>UNDEFINED?TRUE:FALSE),
176 warning_interval,
177 (critical_interval>UNDEFINED?TRUE:FALSE),
178 critical_interval,
179 TRUE, 0, FALSE, 0));
180 return result;
181 }
185 /* process command-line arguments */
186 int
187 process_arguments (int argc, char **argv)
188 {
189 int c;
191 int option = 0;
192 static struct option longopts[] = {
193 {"hostname", required_argument, 0, 'H'},
194 {"query_address", required_argument, 0, 'l'},
195 {"warning", required_argument, 0, 'w'},
196 {"critical", required_argument, 0, 'c'},
197 {"timeout", required_argument, 0, 't'},
198 {"verbose", no_argument, 0, 'v'},
199 {"version", no_argument, 0, 'V'},
200 {"help", no_argument, 0, 'h'},
201 {"record_type", required_argument, 0, 'T'},
202 {"expected_address", required_argument, 0, 'a'},
203 {0, 0, 0, 0}
204 };
206 if (argc < 2)
207 return ERROR;
209 while (1) {
210 c = getopt_long (argc, argv, "hVvt:l:H:w:c:T:a:", longopts, &option);
212 if (c == -1 || c == EOF)
213 break;
215 switch (c) {
216 case '?': /* help */
217 printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
218 print_usage ();
219 exit (STATE_UNKNOWN);
220 case 'h': /* help */
221 print_help ();
222 exit (STATE_OK);
223 case 'V': /* version */
224 print_revision (progname, revision);
225 exit (STATE_OK);
226 case 'H': /* hostname */
227 if (is_host (optarg)) {
228 dns_server = optarg;
229 }
230 else {
231 usage2 (_("Invalid hostname/address"), optarg);
232 }
233 break;
234 case 'p': /* server port */
235 if (is_intpos (optarg)) {
236 server_port = atoi (optarg);
237 }
238 else {
239 usage2 (_("Port must be a positive integer"), optarg);
240 }
241 break;
242 case 'l': /* address to lookup */
243 query_address = optarg;
244 break;
245 case 'w': /* warning */
246 if (is_nonnegative (optarg)) {
247 warning_interval = strtod (optarg, NULL);
248 }
249 else {
250 usage2 (_("Warning interval must be a positive integer"), optarg);
251 }
252 break;
253 case 'c': /* critical */
254 if (is_nonnegative (optarg)) {
255 critical_interval = strtod (optarg, NULL);
256 }
257 else {
258 usage2 (_("Critical interval must be a positive integer"), optarg);
259 }
260 break;
261 case 't': /* timeout */
262 if (is_intnonneg (optarg)) {
263 timeout_interval = atoi (optarg);
264 }
265 else {
266 usage2 (_("Timeout interval must be a positive integer"), optarg);
267 }
268 break;
269 case 'v': /* verbose */
270 verbose = TRUE;
271 break;
272 case 'T':
273 record_type = optarg;
274 break;
275 case 'a':
276 expected_address = optarg;
277 break;
278 }
279 }
281 c = optind;
282 if (dns_server == NULL) {
283 if (c < argc) {
284 if (is_host (argv[c])) {
285 dns_server = argv[c];
286 }
287 else {
288 usage2 (_("Invalid hostname/address"), argv[c]);
289 }
290 }
291 else {
292 dns_server = strdup ("127.0.0.1");
293 }
294 }
296 return validate_arguments ();
297 }
301 int
302 validate_arguments (void)
303 {
304 return OK;
305 }
309 void
310 print_help (void)
311 {
312 char *myport;
314 asprintf (&myport, "%d", DEFAULT_PORT);
316 print_revision (progname, revision);
318 printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
319 printf (COPYRIGHT, copyright, email);
321 printf (_("Test the DNS service on the specified host using dig\n\n"));
323 print_usage ();
325 printf (_(UT_HELP_VRSN));
327 printf (_(UT_HOST_PORT), 'P', myport);
329 printf (_("\
330 -l, --lookup=STRING\n\
331 machine name to lookup\n"));
333 printf (_("\
334 -T, --record_type=STRING\n\
335 record type to lookup (default: A)\n"));
337 printf (_("\
338 -a, --expected_address=STRING\n\
339 an address expected to be in the asnwer section.\n\
340 if not set, uses whatever was in -l\n"));
342 printf (_(UT_WARN_CRIT));
344 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
346 printf (_(UT_VERBOSE));
348 printf (_(UT_SUPPORT));
349 }
353 void
354 print_usage (void)
355 {
356 printf ("\
357 Usage: %s -H host -l lookup [-p <server port>] [-T <query type>]\n\
358 [-w <warning interval>] [-c <critical interval>] [-t <timeout>]\n\
359 [-a <expected answer address>] [-v]\n", progname);
360 }