ecd80a2be68db5ba9af451a2a8652631c1724e79
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 DEFAULT_PORT = 53
36 };
38 char *query_address = NULL;
39 char *dns_server = NULL;
40 int verbose = FALSE;
41 int server_port = DEFAULT_PORT;
42 int warning_interval = -1;
43 int critical_interval = -1;
44 struct timeval tv;
46 int
47 main (int argc, char **argv)
48 {
49 char input_buffer[MAX_INPUT_BUFFER];
50 char *command_line;
51 char *output;
52 long microsec;
53 double elapsed_time;
54 int result = STATE_UNKNOWN;
56 output = strdup ("");
58 setlocale (LC_ALL, "");
59 bindtextdomain (PACKAGE, LOCALEDIR);
60 textdomain (PACKAGE);
62 /* Set signal handling and alarm */
63 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
64 usage (_("Cannot catch SIGALRM\n"));
66 if (process_arguments (argc, argv) != OK)
67 usage (_("Could not parse arguments\n"));
69 /* get the command to run */
70 asprintf (&command_line, "%s @%s -p %d %s",
71 PATH_TO_DIG, dns_server, server_port, query_address);
73 alarm (timeout_interval);
74 gettimeofday (&tv, NULL);
76 if (verbose)
77 printf ("%s\n", command_line);
79 /* run the command */
80 child_process = spopen (command_line);
81 if (child_process == NULL) {
82 printf (_("Could not open pipe: %s\n"), command_line);
83 return STATE_UNKNOWN;
84 }
86 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
87 if (child_stderr == NULL)
88 printf (_("Could not open stderr for %s\n"), command_line);
90 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
92 /* the server is responding, we just got the host name... */
93 if (strstr (input_buffer, ";; ANSWER SECTION:")) {
95 /* get the host address */
96 if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
97 break;
99 if (strpbrk (input_buffer, "\r\n"))
100 input_buffer[strcspn (input_buffer, "\r\n")] = '\0';
102 if (strstr (input_buffer, query_address) == input_buffer) {
103 output = strdup(input_buffer);
104 result = STATE_OK;
105 }
106 else {
107 asprintf (&output, _("Server not found in ANSWER SECTION"));
108 result = STATE_WARNING;
109 }
111 continue;
112 }
114 }
116 if (result != STATE_OK) {
117 asprintf (&output, _("No ANSWER SECTION found"));
118 }
120 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
121 /* If we get anything on STDERR, at least set warning */
122 result = max_state (result, STATE_WARNING);
123 printf ("%s", input_buffer);
124 if (strlen (output) == 0)
125 output = strdup (1 + index (input_buffer, ':'));
126 }
128 (void) fclose (child_stderr);
130 /* close the pipe */
131 if (spclose (child_process)) {
132 result = max_state (result, STATE_WARNING);
133 if (strlen (output) == 0)
134 asprintf (&output, _("dig returned error status"));
135 }
137 microsec = deltime (tv);
138 elapsed_time = (double)microsec / 1.0e6;
140 if (output == NULL || strlen (output) == 0)
141 asprintf (&output, _(" Probably a non-existent host/domain"));
143 if (critical_interval > 0 && elapsed_time > critical_interval)
144 printf (_("DNS OK - %.3f seconds response time (%s)"), elapsed_time, output);
146 else if (result == STATE_CRITICAL)
147 printf (_("DNS CRITICAL - %s"), output);
149 else if (warning_interval > 0 && elapsed_time > warning_interval)
150 printf (_("DNS OK - %.3f seconds response time (%s)"), elapsed_time, output);
152 else if (result == STATE_WARNING)
153 printf (_("DNS WARNING - %s"), output);
155 else if (result == STATE_OK)
156 printf (_("DNS OK - %.3f seconds response time (%s)"),
157 elapsed_time, output);
159 else
160 printf (_("DNS problem - %s"), output);
162 printf ("|%s\n",
163 perfdata("time", microsec, "us",
164 (warning_interval>0?TRUE:FALSE),
165 (int)1e6*warning_interval,
166 (critical_interval>0?TRUE:FALSE),
167 (int)1e6*critical_interval,
168 TRUE, 0, FALSE, 0));
169 return result;
170 }
176 \f
177 /* process command-line arguments */
178 int
179 process_arguments (int argc, char **argv)
180 {
181 int c;
183 int option = 0;
184 static struct option longopts[] = {
185 {"hostname", required_argument, 0, 'H'},
186 {"query_address", required_argument, 0, 'l'},
187 {"warning", required_argument, 0, 'w'},
188 {"critical", required_argument, 0, 'c'},
189 {"timeout", required_argument, 0, 't'},
190 {"verbose", no_argument, 0, 'v'},
191 {"version", no_argument, 0, 'V'},
192 {"help", no_argument, 0, 'h'},
193 {0, 0, 0, 0}
194 };
196 if (argc < 2)
197 return ERROR;
199 while (1) {
200 c = getopt_long (argc, argv, "hVvt:l:H:w:c:", longopts, &option);
202 if (c == -1 || c == EOF)
203 break;
205 switch (c) {
206 case '?': /* help */
207 usage3 (_("Unknown argument"), optopt);
208 case 'h': /* help */
209 print_help ();
210 exit (STATE_OK);
211 case 'V': /* version */
212 print_revision (progname, "$Revision$");
213 exit (STATE_OK);
214 case 'H': /* hostname */
215 if (is_host (optarg)) {
216 dns_server = optarg;
217 }
218 else {
219 usage2 (_("Invalid host name"), optarg);
220 }
221 break;
222 case 'p': /* server port */
223 if (is_intpos (optarg)) {
224 server_port = atoi (optarg);
225 }
226 else {
227 usage2 (_("Server port must be a nonnegative integer\n"), optarg);
228 }
229 break;
230 case 'l': /* address to lookup */
231 query_address = optarg;
232 break;
233 case 'w': /* warning */
234 if (is_intnonneg (optarg)) {
235 warning_interval = atoi (optarg);
236 }
237 else {
238 usage2 (_("Warning interval must be a nonnegative integer\n"), optarg);
239 }
240 break;
241 case 'c': /* critical */
242 if (is_intnonneg (optarg)) {
243 critical_interval = atoi (optarg);
244 }
245 else {
246 usage2 (_("Critical interval must be a nonnegative integer\n"), optarg);
247 }
248 break;
249 case 't': /* timeout */
250 if (is_intnonneg (optarg)) {
251 timeout_interval = atoi (optarg);
252 }
253 else {
254 usage2 (_("Time interval must be a nonnegative integer\n"), optarg);
255 }
256 break;
257 case 'v': /* verbose */
258 verbose = TRUE;
259 break;
260 }
261 }
263 c = optind;
264 if (dns_server == NULL) {
265 if (c < argc) {
266 if (is_host (argv[c])) {
267 dns_server = argv[c];
268 }
269 else {
270 usage2 (_("Invalid host name"), argv[c]);
271 }
272 }
273 else {
274 dns_server = strdup ("127.0.0.1");
275 }
276 }
278 return validate_arguments ();
279 }
285 int
286 validate_arguments (void)
287 {
288 return OK;
289 }
296 \f
297 void
298 print_help (void)
299 {
300 char *myport;
302 asprintf (&myport, "%d", DEFAULT_PORT);
304 print_revision (progname, revision);
306 printf (_("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n"));
307 printf (_(COPYRIGHT), copyright, email);
309 printf (_("Test the DNS service on the specified host using dig\n\n"));
311 print_usage ();
313 printf (_(UT_HELP_VRSN));
315 printf (_(UT_HOST_PORT), 'P', myport);
317 printf (_("\
318 -l, --lookup=STRING\n\
319 machine name to lookup\n"));
321 printf (_(UT_WARN_CRIT));
323 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
325 printf (_(UT_VERBOSE));
327 printf (_(UT_SUPPORT));
328 }
333 void
334 print_usage (void)
335 {
336 printf (_("\
337 Usage: %s -H host -l lookup [-p <server port>] [-w <warning interval>]\n\
338 [-c <critical interval>] [-t <timeout>] [-v]\n"),
339 progname);
340 printf (" %s (-h|--help)\n", progname);
341 printf (" %s (-V|--version)\n", progname);
342 }