a22a68ac196fbedfd7955d2f42f89e4ca1e82752
1 /*****************************************************************************
2 *
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.
7 *
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.
12 *
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.
16 *
17 *****************************************************************************/
19 #include "config.h"
20 #include "common.h"
21 #include "netutils.h"
22 #include "utils.h"
23 #include "popen.h"
25 int process_arguments (int, char **);
26 int validate_arguments (void);
27 void print_help (void);
28 void print_usage (void);
30 const char *progname = "check_dig";
31 const char *revision = "$Revision$";
32 const char *copyright = "2002-2003";
33 const char *email = "nagiosplug-devel@lists.sourceforge.net";
35 enum {
36 DEFAULT_PORT = 53
37 };
39 void
40 print_usage (void)
41 {
42 printf (_("\
43 Usage: %s -H host -l lookup [-p <server port>] [-w <warning interval>]\n\
44 [-c <critical interval>] [-t <timeout>] [-v]\n"),
45 progname);
46 printf (" %s (-h|--help)\n", progname);
47 printf (" %s (-V|--version)\n", progname);
48 }
50 void
51 print_help (void)
52 {
53 char *myport;
55 asprintf (&myport, "%d", DEFAULT_PORT);
57 print_revision (progname, revision);
59 printf (_(COPYRIGHT), copyright, email);
61 printf (_("Test the DNS service on the specified host using dig\n\n"));
63 print_usage ();
65 printf (_(HELP_VRSN));
67 printf (_(HOST_PORT), 'P', myport);
69 printf (_("\
70 -l, --lookup=STRING\n\
71 machine name to lookup\n"));
73 printf (_(WARN_CRIT_TO), DEFAULT_SOCKET_TIMEOUT);
75 printf (_(VRBS));
77 support ();
78 }
79 \f
81 char *query_address = NULL;
82 char *dns_server = NULL;
83 int verbose = FALSE;
84 int server_port = DEFAULT_PORT;
85 int warning_interval = -1;
86 int critical_interval = -1;
89 int
90 main (int argc, char **argv)
91 {
92 char input_buffer[MAX_INPUT_BUFFER];
93 char *command_line = NULL;
94 char *output = "";
95 int result = STATE_UNKNOWN;
97 /* Set signal handling and alarm */
98 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
99 usage (_("Cannot catch SIGALRM\n"));
101 if (process_arguments (argc, argv) != OK)
102 usage (_("Could not parse arguments\n"));
104 /* get the command to run */
105 asprintf (&command_line, "%s @%s -p %d %s",
106 PATH_TO_DIG, dns_server, server_port, query_address);
108 alarm (timeout_interval);
109 time (&start_time);
111 if (verbose)
112 printf ("%s\n", command_line);
113 /* run the command */
114 child_process = spopen (command_line);
115 if (child_process == NULL) {
116 printf (_("Could not open pipe: %s\n"), command_line);
117 return STATE_UNKNOWN;
118 }
120 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
121 if (child_stderr == NULL)
122 printf (_("Could not open stderr for %s\n"), command_line);
124 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
126 /* the server is responding, we just got the host name... */
127 if (strstr (input_buffer, ";; ANSWER SECTION:")) {
129 /* get the host address */
130 if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
131 break;
133 if (strpbrk (input_buffer, "\r\n"))
134 input_buffer[strcspn (input_buffer, "\r\n")] = '\0';
136 if (strstr (input_buffer, query_address) == input_buffer) {
137 asprintf (&output, input_buffer);
138 result = STATE_OK;
139 }
140 else {
141 asprintf (&output, _("Server not found in ANSWER SECTION"));
142 result = STATE_WARNING;
143 }
145 continue;
146 }
148 }
150 if (result != STATE_OK) {
151 asprintf (&output, _("No ANSWER SECTION found"));
152 }
154 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
155 /* If we get anything on STDERR, at least set warning */
156 result = max_state (result, STATE_WARNING);
157 printf ("%s", input_buffer);
158 if (strlen (output) == 0)
159 asprintf (&output, 1 + index (input_buffer, ':'));
160 }
162 (void) fclose (child_stderr);
164 /* close the pipe */
165 if (spclose (child_process)) {
166 result = max_state (result, STATE_WARNING);
167 if (strlen (output) == 0)
168 asprintf (&output, _("dig returned error status"));
169 }
171 (void) time (&end_time);
173 if (output == NULL || strlen (output) == 0)
174 asprintf (&output, _(" Probably a non-existent host/domain"));
176 if (result == STATE_OK)
177 printf (_("DNS OK - %d seconds response time (%s)\n"),
178 (int) (end_time - start_time), output);
179 else if (result == STATE_WARNING)
180 printf (_("DNS WARNING - %s\n"), output);
181 else if (result == STATE_CRITICAL)
182 printf (_("DNS CRITICAL - %s\n"), output);
183 else
184 printf (_("DNS problem - %s\n"), output);
186 return result;
187 }
189 /* process command-line arguments */
190 int
191 process_arguments (int argc, char **argv)
192 {
193 int c;
195 int option_index = 0;
196 static struct option long_options[] = {
197 {"hostname", required_argument, 0, 'H'},
198 {"query_address", required_argument, 0, 'e'},
199 {"verbose", no_argument, 0, 'v'},
200 {"version", no_argument, 0, 'V'},
201 {"help", no_argument, 0, 'h'},
202 {0, 0, 0, 0}
203 };
205 if (argc < 2)
206 return ERROR;
208 while (1) {
209 c = getopt_long (argc, argv, "hVvt:l:H:", long_options, &option_index);
211 if (c == -1 || c == EOF)
212 break;
214 switch (c) {
215 case '?': /* help */
216 usage3 (_("Unknown argument"), optopt);
217 case 'h': /* help */
218 print_help ();
219 exit (STATE_OK);
220 case 'V': /* version */
221 print_revision (progname, "$Revision$");
222 exit (STATE_OK);
223 case 'H': /* hostname */
224 if (is_host (optarg)) {
225 dns_server = optarg;
226 }
227 else {
228 usage2 (_("Invalid host name"), optarg);
229 }
230 break;
231 case 'p':
232 if (is_intpos (optarg)) {
233 server_port = atoi (optarg);
234 }
235 else {
236 usage2 (_("Server port must be a nonnegative integer\n"), optarg);
237 }
238 break;
239 case 'l': /* username */
240 query_address = optarg;
241 break;
242 case 'w': /* timeout */
243 if (is_intnonneg (optarg)) {
244 warning_interval = atoi (optarg);
245 }
246 else {
247 usage2 (_("Warning interval must be a nonnegative integer\n"), optarg);
248 }
249 break;
250 case 'c': /* timeout */
251 if (is_intnonneg (optarg)) {
252 critical_interval = atoi (optarg);
253 }
254 else {
255 usage2 (_("Critical interval must be a nonnegative integer\n"), optarg);
256 }
257 break;
258 case 't': /* timeout */
259 if (is_intnonneg (optarg)) {
260 timeout_interval = atoi (optarg);
261 }
262 else {
263 usage2 (_("Time interval must be a nonnegative integer\n"), optarg);
264 }
265 break;
266 case 'v': /* verbose */
267 verbose = TRUE;
268 break;
269 }
270 }
272 c = optind;
273 if (dns_server == NULL) {
274 if (c < argc) {
275 if (is_host (argv[c])) {
276 dns_server = argv[c];
277 }
278 else {
279 usage2 (_("Invalid host name"), argv[c]);
280 }
281 }
282 else {
283 dns_server = strdup ("127.0.0.1");
284 }
285 }
287 return validate_arguments ();
288 }
294 int
295 validate_arguments (void)
296 {
297 return OK;
298 }
299 \f