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 /* Hackers note:
22 * There are typecasts to (char *) from _("foo bar") in this file.
23 * They prevent compiler warnings. Never (ever), permute strings obtained
24 * that are typecast from (const char *) (which happens when --disable-nls)
25 * because on some architectures those strings are in non-writable memory */
27 const char *progname = "check_dig";
28 const char *revision = "$Revision$";
29 const char *copyright = "2002-2005";
30 const char *email = "nagiosplug-devel@lists.sourceforge.net";
32 #include "common.h"
33 #include "netutils.h"
34 #include "utils.h"
35 #include "runcmd.h"
37 int process_arguments (int, char **);
38 int validate_arguments (void);
39 void print_help (void);
40 void print_usage (void);
42 #define UNDEFINED 0
43 #define DEFAULT_PORT 53
45 char *query_address = NULL;
46 char *record_type = "A";
47 char *expected_address = NULL;
48 char *dns_server = NULL;
49 int verbose = FALSE;
50 int server_port = DEFAULT_PORT;
51 double warning_interval = UNDEFINED;
52 double critical_interval = UNDEFINED;
53 struct timeval tv;
55 int
56 main (int argc, char **argv)
57 {
58 char *command_line;
59 output chld_out, chld_err;
60 char *msg = NULL;
61 size_t i;
62 char *t;
63 long microsec;
64 double elapsed_time;
65 int result = STATE_UNKNOWN;
67 setlocale (LC_ALL, "");
68 bindtextdomain (PACKAGE, LOCALEDIR);
69 textdomain (PACKAGE);
71 /* Set signal handling and alarm */
72 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
73 usage_va(_("Cannot catch SIGALRM"));
75 if (process_arguments (argc, argv) == ERROR)
76 usage_va(_("Could not parse arguments"));
78 /* get the command to run */
79 asprintf (&command_line, "%s @%s -p %d %s -t %s",
80 PATH_TO_DIG, dns_server, server_port, query_address, record_type);
82 alarm (timeout_interval);
83 gettimeofday (&tv, NULL);
85 if (verbose) {
86 printf ("%s\n", command_line);
87 if(expected_address != NULL) {
88 printf (_("Looking for: '%s'\n"), expected_address);
89 } else {
90 printf (_("Looking for: '%s'\n"), query_address);
91 }
92 }
94 /* run the command */
95 if(np_runcmd(command_line, &chld_out, &chld_err, 0) != 0) {
96 result = STATE_WARNING;
97 msg = (char *)_("dig returned an error status");
98 }
100 for(i = 0; i < chld_out.lines; i++) {
101 /* the server is responding, we just got the host name... */
102 if (strstr (chld_out.line[i], ";; ANSWER SECTION:")) {
104 /* loop through the whole 'ANSWER SECTION' */
105 for(; i < chld_out.lines; i++) {
106 /* get the host address */
107 if (verbose)
108 printf ("%s\n", chld_out.line[i]);
110 if (strstr (chld_out.line[i], (expected_address == NULL ? query_address : expected_address)) != NULL) {
111 msg = chld_out.line[i];
112 result = STATE_OK;
114 /* Translate output TAB -> SPACE */
115 t = msg;
116 while ((t = strchr(t, '\t')) != NULL) *t = ' ';
117 break;
118 }
119 }
121 if (result == STATE_UNKNOWN) {
122 msg = (char *)_("Server not found in ANSWER SECTION");
123 result = STATE_WARNING;
124 }
126 /* we found the answer section, so break out of the loop */
127 break;
128 }
129 }
131 if (result == STATE_UNKNOWN)
132 msg = (char *)_("No ANSWER SECTION found");
134 /* If we get anything on STDERR, at least set warning */
135 if(chld_err.buflen > 0) {
136 result = max_state(result, STATE_WARNING);
137 if(!msg) for(i = 0; i < chld_err.lines; i++) {
138 msg = strchr(chld_err.line[0], ':');
139 if(msg) {
140 msg++;
141 break;
142 }
143 }
144 }
146 microsec = deltime (tv);
147 elapsed_time = (double)microsec / 1.0e6;
149 if (critical_interval > UNDEFINED && elapsed_time > critical_interval)
150 result = STATE_CRITICAL;
152 else if (warning_interval > UNDEFINED && elapsed_time > warning_interval)
153 result = STATE_WARNING;
155 printf ("DNS %s - %.3f seconds response time (%s)|%s\n",
156 state_text (result), elapsed_time,
157 msg ? msg : _("Probably a non-existent host/domain"),
158 fperfdata("time", elapsed_time, "s",
159 (warning_interval>UNDEFINED?TRUE:FALSE),
160 warning_interval,
161 (critical_interval>UNDEFINED?TRUE:FALSE),
162 critical_interval,
163 TRUE, 0, FALSE, 0));
164 return result;
165 }
169 /* process command-line arguments */
170 int
171 process_arguments (int argc, char **argv)
172 {
173 int c;
175 int option = 0;
176 static struct option longopts[] = {
177 {"hostname", required_argument, 0, 'H'},
178 {"query_address", required_argument, 0, 'l'},
179 {"warning", required_argument, 0, 'w'},
180 {"critical", required_argument, 0, 'c'},
181 {"timeout", required_argument, 0, 't'},
182 {"verbose", no_argument, 0, 'v'},
183 {"version", no_argument, 0, 'V'},
184 {"help", no_argument, 0, 'h'},
185 {"record_type", required_argument, 0, 'T'},
186 {"expected_address", required_argument, 0, 'a'},
187 {"port", required_argument, 0, 'p'},
188 {0, 0, 0, 0}
189 };
191 if (argc < 2)
192 return ERROR;
194 while (1) {
195 c = getopt_long (argc, argv, "hVvt:l:H:w:c:T:p:a:", longopts, &option);
197 if (c == -1 || c == EOF)
198 break;
200 switch (c) {
201 case 'h': /* help */
202 print_help ();
203 exit (STATE_OK);
204 case 'V': /* version */
205 print_revision (progname, revision);
206 exit (STATE_OK);
207 case 'H': /* hostname */
208 host_or_die(optarg);
209 dns_server = optarg;
210 break;
211 case 'p': /* server port */
212 if (is_intpos (optarg)) {
213 server_port = atoi (optarg);
214 }
215 else {
216 usage_va(_("Port must be a positive integer - %s"), optarg);
217 }
218 break;
219 case 'l': /* address to lookup */
220 query_address = optarg;
221 break;
222 case 'w': /* warning */
223 if (is_nonnegative (optarg)) {
224 warning_interval = strtod (optarg, NULL);
225 }
226 else {
227 usage_va(_("Warning interval must be a positive integer - %s"), optarg);
228 }
229 break;
230 case 'c': /* critical */
231 if (is_nonnegative (optarg)) {
232 critical_interval = strtod (optarg, NULL);
233 }
234 else {
235 usage_va(_("Critical interval must be a positive integer - %s"), optarg);
236 }
237 break;
238 case 't': /* timeout */
239 if (is_intnonneg (optarg)) {
240 timeout_interval = atoi (optarg);
241 }
242 else {
243 usage_va(_("Timeout interval must be a positive integer - %s"), optarg);
244 }
245 break;
246 case 'v': /* verbose */
247 verbose = TRUE;
248 break;
249 case 'T':
250 record_type = optarg;
251 break;
252 case 'a':
253 expected_address = optarg;
254 break;
255 default: /* usage_va */
256 usage_va(_("Unknown argument - %s"), optarg);
257 }
258 }
260 c = optind;
261 if (dns_server == NULL) {
262 if (c < argc) {
263 host_or_die(argv[c]);
264 dns_server = argv[c];
265 }
266 else {
267 dns_server = strdup ("127.0.0.1");
268 }
269 }
271 return validate_arguments ();
272 }
276 int
277 validate_arguments (void)
278 {
279 return OK;
280 }
284 void
285 print_help (void)
286 {
287 char *myport;
289 asprintf (&myport, "%d", DEFAULT_PORT);
291 print_revision (progname, revision);
293 printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
294 printf (COPYRIGHT, copyright, email);
296 printf (_("This plugin test the DNS service on the specified host using dig"));
298 printf ("\n\n");
300 print_usage ();
302 printf (_(UT_HELP_VRSN));
304 printf (_(UT_HOST_PORT), 'p', myport);
306 printf (" %s\n","-l, --lookup=STRING");
307 printf (" %s\n",_("machine name to lookup"));
308 printf (" %s\n","-T, --record_type=STRING");
309 printf (" %s\n",_("record type to lookup (default: A)"));
310 printf (" %s\n","-a, --expected_address=STRING");
311 printf (" %s\n",_("an address expected to be in the answer section.if not set, uses whatever was in -l"));
312 printf (_(UT_WARN_CRIT));
313 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
314 printf (_(UT_VERBOSE));
315 printf (_(UT_SUPPORT));
316 }
320 void
321 print_usage (void)
322 {
323 printf (_("Usage:"));
324 printf ("%s -H host -l lookup [-p <server port>] [-T <query type>]", progname);
325 printf (" [-w <warning interval>] [-c <critical interval>] [-t <timeout>]");
326 printf (" [-a <expected answer address>] [-v]\n");
327 }