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 *****************************************************************************/
19 #include "common.h"
20 #include "netutils.h"
21 #include "utils.h"
22 #include "popen.h"
24 int process_arguments (int, char **);
25 int validate_arguments (void);
26 void print_help (void);
27 void print_usage (void);
29 const char *progname = "check_dig";
30 const char *revision = "$Revision$";
31 const char *copyright = "2002-2003";
32 const char *email = "nagiosplug-devel@lists.sourceforge.net";
34 enum {
35 UNDEFINED = 0,
36 DEFAULT_PORT = 53
37 };
39 char *query_address = NULL;
40 char *dns_server = NULL;
41 int verbose = FALSE;
42 int server_port = DEFAULT_PORT;
43 double warning_interval = UNDEFINED;
44 double critical_interval = UNDEFINED;
45 struct timeval tv;
47 int
48 main (int argc, char **argv)
49 {
50 char input_buffer[MAX_INPUT_BUFFER];
51 char *command_line;
52 char *output;
53 long microsec;
54 double elapsed_time;
55 int result = STATE_UNKNOWN;
57 output = strdup ("");
59 setlocale (LC_ALL, "");
60 bindtextdomain (PACKAGE, LOCALEDIR);
61 textdomain (PACKAGE);
63 /* Set signal handling and alarm */
64 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
65 usage (_("Cannot catch SIGALRM\n"));
67 if (process_arguments (argc, argv) != OK)
68 usage (_("Could not parse arguments\n"));
70 /* get the command to run */
71 asprintf (&command_line, "%s @%s -p %d %s",
72 PATH_TO_DIG, dns_server, server_port, query_address);
74 alarm (timeout_interval);
75 gettimeofday (&tv, NULL);
77 if (verbose)
78 printf ("%s\n", command_line);
80 /* run the command */
81 child_process = spopen (command_line);
82 if (child_process == NULL) {
83 printf (_("Could not open pipe: %s\n"), command_line);
84 return STATE_UNKNOWN;
85 }
87 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
88 if (child_stderr == NULL)
89 printf (_("Could not open stderr for %s\n"), command_line);
91 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
93 /* the server is responding, we just got the host name... */
94 if (strstr (input_buffer, ";; ANSWER SECTION:")) {
96 /* get the host address */
97 if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
98 break;
100 if (strpbrk (input_buffer, "\r\n"))
101 input_buffer[strcspn (input_buffer, "\r\n")] = '\0';
103 if (strstr (input_buffer, query_address) == input_buffer) {
104 output = strdup(input_buffer);
105 result = STATE_OK;
106 }
107 else {
108 asprintf (&output, _("Server not found in ANSWER SECTION"));
109 result = STATE_WARNING;
110 }
112 continue;
113 }
115 }
117 if (result != STATE_OK) {
118 asprintf (&output, _("No ANSWER SECTION found"));
119 }
121 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
122 /* If we get anything on STDERR, at least set warning */
123 result = max_state (result, STATE_WARNING);
124 printf ("%s", input_buffer);
125 if (strlen (output) == 0)
126 output = strdup (1 + index (input_buffer, ':'));
127 }
129 (void) fclose (child_stderr);
131 /* close the pipe */
132 if (spclose (child_process)) {
133 result = max_state (result, STATE_WARNING);
134 if (strlen (output) == 0)
135 asprintf (&output, _("dig returned error status"));
136 }
138 microsec = deltime (tv);
139 elapsed_time = (double)microsec / 1.0e6;
141 if (output == NULL || strlen (output) == 0)
142 asprintf (&output, _(" Probably a non-existent host/domain"));
144 if (critical_interval != UNDEFINED && elapsed_time > critical_interval)
145 result = STATE_CRITICAL;
147 else if (warning_interval != UNDEFINED && elapsed_time > warning_interval)
148 result = STATE_WARNING;
150 asprintf (&output, _("%.3f seconds response time (%s)"), elapsed_time, output);
152 printf ("DNS %s - %s|%s\n",
153 state_text (result), output,
154 perfdata("time", microsec, "us",
155 (warning_interval!=UNDEFINED?TRUE:FALSE),
156 (int)(1e6*warning_interval),
157 (critical_interval!=UNDEFINED?TRUE:FALSE),
158 (int)(1e6*critical_interval),
159 TRUE, 0, FALSE, 0));
160 return result;
161 }
167 \f
168 /* process command-line arguments */
169 int
170 process_arguments (int argc, char **argv)
171 {
172 int c;
174 int option = 0;
175 static struct option longopts[] = {
176 {"hostname", required_argument, 0, 'H'},
177 {"query_address", required_argument, 0, 'l'},
178 {"warning", required_argument, 0, 'w'},
179 {"critical", required_argument, 0, 'c'},
180 {"timeout", required_argument, 0, 't'},
181 {"verbose", no_argument, 0, 'v'},
182 {"version", no_argument, 0, 'V'},
183 {"help", no_argument, 0, 'h'},
184 {0, 0, 0, 0}
185 };
187 if (argc < 2)
188 return ERROR;
190 while (1) {
191 c = getopt_long (argc, argv, "hVvt:l:H:w:c:", longopts, &option);
193 if (c == -1 || c == EOF)
194 break;
196 switch (c) {
197 case '?': /* help */
198 usage3 (_("Unknown argument"), optopt);
199 case 'h': /* help */
200 print_help ();
201 exit (STATE_OK);
202 case 'V': /* version */
203 print_revision (progname, "$Revision$");
204 exit (STATE_OK);
205 case 'H': /* hostname */
206 if (is_host (optarg)) {
207 dns_server = optarg;
208 }
209 else {
210 usage2 (_("Invalid host name"), optarg);
211 }
212 break;
213 case 'p': /* server port */
214 if (is_intpos (optarg)) {
215 server_port = atoi (optarg);
216 }
217 else {
218 usage2 (_("Server port must be a nonnegative integer"), optarg);
219 }
220 break;
221 case 'l': /* address to lookup */
222 query_address = optarg;
223 break;
224 case 'w': /* warning */
225 if (is_nonnegative (optarg)) {
226 warning_interval = strtod (optarg, NULL);
227 if (warning_interval == HUGE_VAL)
228 usage2 (_("Input causes overflow in warning interval"), optarg);
229 }
230 else {
231 usage2 (_("Warning interval must be a nonnegative integer"), optarg);
232 }
233 break;
234 case 'c': /* critical */
235 if (is_nonnegative (optarg)) {
236 critical_interval = strtod (optarg, NULL);
237 if (critical_interval == HUGE_VAL)
238 usage2 (_("Input causes overflow in critical interval"), optarg);
239 }
240 else {
241 usage2 (_("Critical interval must be a nonnegative integer"), optarg);
242 }
243 break;
244 case 't': /* timeout */
245 if (is_intnonneg (optarg)) {
246 timeout_interval = atoi (optarg);
247 }
248 else {
249 usage2 (_("Time interval must be a nonnegative integer"), optarg);
250 }
251 break;
252 case 'v': /* verbose */
253 verbose = TRUE;
254 break;
255 }
256 }
258 c = optind;
259 if (dns_server == NULL) {
260 if (c < argc) {
261 if (is_host (argv[c])) {
262 dns_server = argv[c];
263 }
264 else {
265 usage2 (_("Invalid host name"), argv[c]);
266 }
267 }
268 else {
269 dns_server = strdup ("127.0.0.1");
270 }
271 }
273 return validate_arguments ();
274 }
280 int
281 validate_arguments (void)
282 {
283 return OK;
284 }
291 \f
292 void
293 print_help (void)
294 {
295 char *myport;
297 asprintf (&myport, "%d", DEFAULT_PORT);
299 print_revision (progname, revision);
301 printf (_("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n"));
302 printf (_(COPYRIGHT), copyright, email);
304 printf (_("Test the DNS service on the specified host using dig\n\n"));
306 print_usage ();
308 printf (_(UT_HELP_VRSN));
310 printf (_(UT_HOST_PORT), 'P', myport);
312 printf (_("\
313 -l, --lookup=STRING\n\
314 machine name to lookup\n"));
316 printf (_(UT_WARN_CRIT));
318 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
320 printf (_(UT_VERBOSE));
322 printf (_(UT_SUPPORT));
323 }
328 void
329 print_usage (void)
330 {
331 printf (_("\
332 Usage: %s -H host -l lookup [-p <server port>] [-w <warning interval>]\n\
333 [-c <critical interval>] [-t <timeout>] [-v]\n"),
334 progname);
335 printf (" %s (-h|--help)\n", progname);
336 printf (" %s (-V|--version)\n", progname);
337 }