1 /*****************************************************************************
2 *
3 * Monitoring check_dig plugin
4 *
5 * License: GPL
6 * Copyright (c) 2002-2008 Monitoring Plugins Development Team
7 *
8 * Description:
9 *
10 * This file contains the check_dig plugin
11 *
12 *
13 * This program is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation, either version 3 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25 *
26 *
27 *****************************************************************************/
28 
29 /* Hackers note:
30  *  There are typecasts to (char *) from _("foo bar") in this file.
31  *  They prevent compiler warnings. Never (ever), permute strings obtained
32  *  that are typecast from (const char *) (which happens when --disable-nls)
33  *  because on some architectures those strings are in non-writable memory */
34 
35 const char *progname = "check_dig";
36 const char *copyright = "2002-2008";
37 const char *email = "devel@monitoring-plugins.org";
38 
39 #include "common.h"
40 #include "netutils.h"
41 #include "utils.h"
42 #include "runcmd.h"
43 
44 int process_arguments (int, char **);
45 int validate_arguments (void);
46 void print_help (void);
47 void print_usage (void);
48 
49 #define UNDEFINED 0
50 #define DEFAULT_PORT 53
51 #define DEFAULT_TRIES 2
52 
53 char *query_address = NULL;
54 char *record_type = "A";
55 char *expected_address = NULL;
56 char *dns_server = NULL;
57 char *dig_args = "";
58 char *query_transport = "";
59 int verbose = FALSE;
60 int server_port = DEFAULT_PORT;
61 int number_tries = DEFAULT_TRIES;
62 double warning_interval = UNDEFINED;
63 double critical_interval = UNDEFINED;
64 struct timeval tv;
65 
66 int
main(int argc,char ** argv)67 main (int argc, char **argv)
68 {
69   char *command_line;
70   output chld_out, chld_err;
71   char *msg = NULL;
72   size_t i;
73   char *t;
74   long microsec;
75   double elapsed_time;
76   int result = STATE_UNKNOWN;
77   int timeout_interval_dig;
78 
79   setlocale (LC_ALL, "");
80   bindtextdomain (PACKAGE, LOCALEDIR);
81   textdomain (PACKAGE);
82 
83   /* Set signal handling and alarm */
84   if (signal (SIGALRM, runcmd_timeout_alarm_handler) == SIG_ERR)
85     usage_va(_("Cannot catch SIGALRM"));
86 
87   /* Parse extra opts if any */
88   argv=np_extra_opts (&argc, argv, progname);
89 
90   if (process_arguments (argc, argv) == ERROR)
91     usage_va(_("Could not parse arguments"));
92 
93   /* dig applies the timeout to each try, so we need to work around this */
94   timeout_interval_dig = timeout_interval / number_tries + number_tries;
95 
96   /* get the command to run */
97   xasprintf (&command_line, "%s %s %s -p %d @%s %s %s",
98             PATH_TO_DIG, dig_args, query_transport, server_port, dns_server, query_address, record_type);
99 
100   alarm (timeout_interval);
101   gettimeofday (&tv, NULL);
102 
103   if (verbose) {
104     printf ("%s\n", command_line);
105     if(expected_address != NULL) {
106       printf (_("Looking for: '%s'\n"), expected_address);
107     } else {
108       printf (_("Looking for: '%s'\n"), query_address);
109     }
110   }
111 
112   /* run the command */
113   if(np_runcmd(command_line, &chld_out, &chld_err, 0) != 0) {
114     result = STATE_WARNING;
115     msg = (char *)_("dig returned an error status");
116   }
117 
118   for(i = 0; i < chld_out.lines; i++) {
119     /* the server is responding, we just got the host name... */
120     if (strstr (chld_out.line[i], ";; ANSWER SECTION:")) {
121 
122       /* loop through the whole 'ANSWER SECTION' */
123       for(; i < chld_out.lines; i++) {
124         /* get the host address */
125         if (verbose)
126           printf ("%s\n", chld_out.line[i]);
127 
128         if (strcasestr (chld_out.line[i], (expected_address == NULL ? query_address : expected_address)) != NULL) {
129           msg = chld_out.line[i];
130           result = STATE_OK;
131 
132           /* Translate output TAB -> SPACE */
133           t = msg;
134           while ((t = strchr(t, '\t')) != NULL) *t = ' ';
135           break;
136         }
137       }
138 
139       if (result == STATE_UNKNOWN) {
140         msg = (char *)_("Server not found in ANSWER SECTION");
141         result = STATE_WARNING;
142       }
143 
144       /* we found the answer section, so break out of the loop */
145       break;
146     }
147   }
148 
149   if (result == STATE_UNKNOWN) {
150     msg = (char *)_("No ANSWER SECTION found");
151     result = STATE_CRITICAL;
152   }
153 
154   /* If we get anything on STDERR, at least set warning */
155   if(chld_err.buflen > 0) {
156     result = max_state(result, STATE_WARNING);
157     if(!msg) for(i = 0; i < chld_err.lines; i++) {
158       msg = strchr(chld_err.line[0], ':');
159       if(msg) {
160         msg++;
161         break;
162       }
163     }
164   }
165 
166   microsec = deltime (tv);
167   elapsed_time = (double)microsec / 1.0e6;
168 
169   if (critical_interval > UNDEFINED && elapsed_time > critical_interval)
170     result = STATE_CRITICAL;
171 
172   else if (warning_interval > UNDEFINED && elapsed_time > warning_interval)
173     result = STATE_WARNING;
174 
175   printf ("DNS %s - %.3f seconds response time (%s)|%s\n",
176           state_text (result), elapsed_time,
177           msg ? msg : _("Probably a non-existent host/domain"),
178           fperfdata("time", elapsed_time, "s",
179                     (warning_interval>UNDEFINED?TRUE:FALSE),
180                     warning_interval,
181                     (critical_interval>UNDEFINED?TRUE:FALSE),
182             critical_interval,
183             TRUE, 0, FALSE, 0));
184   return result;
185 }
186 
187 
188 
189 /* process command-line arguments */
190 int
process_arguments(int argc,char ** argv)191 process_arguments (int argc, char **argv)
192 {
193   int c;
194 
195   int option = 0;
196   static struct option longopts[] = {
197     {"hostname", required_argument, 0, 'H'},
198     {"query_address", required_argument, 0, 'l'},
199     {"warning", required_argument, 0, 'w'},
200     {"critical", required_argument, 0, 'c'},
201     {"timeout", required_argument, 0, 't'},
202     {"dig-arguments", required_argument, 0, 'A'},
203     {"verbose", no_argument, 0, 'v'},
204     {"version", no_argument, 0, 'V'},
205     {"help", no_argument, 0, 'h'},
206     {"record_type", required_argument, 0, 'T'},
207     {"expected_address", required_argument, 0, 'a'},
208     {"port", required_argument, 0, 'p'},
209     {"use-ipv4", no_argument, 0, '4'},
210     {"use-ipv6", no_argument, 0, '6'},
211     {0, 0, 0, 0}
212   };
213 
214   if (argc < 2)
215     return ERROR;
216 
217   while (1) {
218     c = getopt_long (argc, argv, "hVvt:l:H:w:c:T:p:a:A:46", longopts, &option);
219 
220     if (c == -1 || c == EOF)
221       break;
222 
223     switch (c) {
224     case 'h':                 /* help */
225       print_help ();
226       exit (STATE_UNKNOWN);
227     case 'V':                 /* version */
228       print_revision (progname, NP_VERSION);
229       exit (STATE_UNKNOWN);
230     case 'H':                 /* hostname */
231       host_or_die(optarg);
232       dns_server = optarg;
233       break;
234     case 'p':                 /* server port */
235       if (is_intpos (optarg)) {
236         server_port = atoi (optarg);
237       }
238       else {
239         usage_va(_("Port must be a positive integer - %s"), 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         usage_va(_("Warning interval must be a positive integer - %s"), optarg);
251       }
252       break;
253     case 'c':                 /* critical */
254       if (is_nonnegative (optarg)) {
255         critical_interval = strtod (optarg, NULL);
256       }
257       else {
258         usage_va(_("Critical interval must be a positive integer - %s"), optarg);
259       }
260       break;
261     case 't':                 /* timeout */
262       if (is_intnonneg (optarg)) {
263         timeout_interval = atoi (optarg);
264       }
265       else {
266         usage_va(_("Timeout interval must be a positive integer - %s"), optarg);
267       }
268       break;
269     case 'A':                 /* dig arguments */
270       dig_args = strdup(optarg);
271       break;
272     case 'v':                 /* verbose */
273       verbose = TRUE;
274       break;
275     case 'T':
276       record_type = optarg;
277       break;
278     case 'a':
279       expected_address = optarg;
280       break;
281     case '4':
282       query_transport = "-4";
283       break;
284     case '6':
285       query_transport = "-6";
286       break;
287     default:                  /* usage5 */
288       usage5();
289     }
290   }
291 
292   c = optind;
293   if (dns_server == NULL) {
294     if (c < argc) {
295       host_or_die(argv[c]);
296       dns_server = argv[c];
297     }
298     else {
299       if (strcmp(query_transport,"-6") == 0)
300         dns_server = strdup("::1");
301       else
302         dns_server = strdup ("127.0.0.1");
303     }
304   }
305 
306   return validate_arguments ();
307 }
308 
309 
310 
311 int
validate_arguments(void)312 validate_arguments (void)
313 {
314   if (query_address != NULL)
315     return OK;
316   else
317     return ERROR;
318 }
319 
320 
321 
322 void
print_help(void)323 print_help (void)
324 {
325   char *myport;
326 
327   xasprintf (&myport, "%d", DEFAULT_PORT);
328 
329   print_revision (progname, NP_VERSION);
330 
331   printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
332   printf (COPYRIGHT, copyright, email);
333 
334   printf (_("This plugin tests the DNS service on the specified host using dig"));
335 
336   printf ("\n\n");
337 
338   print_usage ();
339 
340   printf (UT_HELP_VRSN);
341 
342   printf (UT_EXTRA_OPTS);
343 
344   printf (UT_HOST_PORT, 'p', myport);
345 
346   printf (" %s\n","-4, --use-ipv4");
347   printf ("    %s\n",_("Force dig to only use IPv4 query transport"));
348   printf (" %s\n","-6, --use-ipv6");
349   printf ("    %s\n",_("Force dig to only use IPv6 query transport"));
350   printf (" %s\n","-l, --query_address=STRING");
351   printf ("    %s\n",_("Machine name to lookup"));
352   printf (" %s\n","-T, --record_type=STRING");
353   printf ("    %s\n",_("Record type to lookup (default: A)"));
354   printf (" %s\n","-a, --expected_address=STRING");
355   printf ("    %s\n",_("An address expected to be in the answer section. If not set, uses whatever"));
356   printf ("    %s\n",_("was in -l"));
357   printf (" %s\n","-A, --dig-arguments=STRING");
358   printf ("    %s\n",_("Pass STRING as argument(s) to dig"));
359   printf (UT_WARN_CRIT);
360   printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
361   printf (UT_VERBOSE);
362 
363   printf ("\n");
364   printf ("%s\n", _("Examples:"));
365   printf (" %s\n", "check_dig -H DNSSERVER -l www.example.com -A \"+tcp\"");
366   printf (" %s\n", "This will send a tcp query to DNSSERVER for www.example.com");
367 
368   printf (UT_SUPPORT);
369 }
370 
371 
372 
373 void
print_usage(void)374 print_usage (void)
375 {
376   printf ("%s\n", _("Usage:"));
377   printf ("%s -l <query_address> [-H <host>] [-p <server port>]\n", progname);
378   printf (" [-T <query type>] [-w <warning interval>] [-c <critical interval>]\n");
379   printf (" [-t <timeout>] [-a <expected answer address>] [-v]\n");
380 }
381