Code

If the TMPDIR environment variable is set, use that instead of "/tmp" as
[nagiosplug.git] / plugins / check_dig.c
1 /*****************************************************************************
2 *
3 * Nagios check_dig plugin
4 *
5 * License: GPL
6 * Copyright (c) 1999-2006 nagios-plugins team
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
11 *
12 * This file contains the check_dig plugin
13 *
14 * License Information:
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 *
30 * $Id$
31 *
32 *****************************************************************************/
34 /* Hackers note:
35  *  There are typecasts to (char *) from _("foo bar") in this file.
36  *  They prevent compiler warnings. Never (ever), permute strings obtained
37  *  that are typecast from (const char *) (which happens when --disable-nls)
38  *  because on some architectures those strings are in non-writable memory */
40 const char *progname = "check_dig";
41 const char *revision = "$Revision$";
42 const char *copyright = "2002-2006";
43 const char *email = "nagiosplug-devel@lists.sourceforge.net";
45 #include "common.h"
46 #include "netutils.h"
47 #include "utils.h"
48 #include "runcmd.h"
50 int process_arguments (int, char **);
51 int validate_arguments (void);
52 void print_help (void);
53 void print_usage (void);
55 #define UNDEFINED 0
56 #define DEFAULT_PORT 53
58 char *query_address = NULL;
59 char *record_type = "A";
60 char *expected_address = NULL;
61 char *dns_server = NULL;
62 int verbose = FALSE;
63 int server_port = DEFAULT_PORT;
64 double warning_interval = UNDEFINED;
65 double critical_interval = UNDEFINED;
66 struct timeval tv;
68 int
69 main (int argc, char **argv)
70 {
71   char *command_line;
72   output chld_out, chld_err;
73   char *msg = NULL;
74   size_t i;
75   char *t;
76   long microsec;
77   double elapsed_time;
78   int result = STATE_UNKNOWN;
80   setlocale (LC_ALL, "");
81   bindtextdomain (PACKAGE, LOCALEDIR);
82   textdomain (PACKAGE);
84   /* Set signal handling and alarm */
85   if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
86     usage_va(_("Cannot catch SIGALRM"));
88   if (process_arguments (argc, argv) == ERROR)
89     usage_va(_("Could not parse arguments"));
91   /* get the command to run */
92   asprintf (&command_line, "%s @%s -p %d %s -t %s",
93             PATH_TO_DIG, dns_server, server_port, query_address, record_type);
95   alarm (timeout_interval);
96   gettimeofday (&tv, NULL);
98   if (verbose) {
99     printf ("%s\n", command_line);
100     if(expected_address != NULL) {
101       printf (_("Looking for: '%s'\n"), expected_address);
102     } else {
103       printf (_("Looking for: '%s'\n"), query_address);
104     }
105   }
107   /* run the command */
108   if(np_runcmd(command_line, &chld_out, &chld_err, 0) != 0) {
109     result = STATE_WARNING;
110     msg = (char *)_("dig returned an error status");
111   }
113   for(i = 0; i < chld_out.lines; i++) {
114     /* the server is responding, we just got the host name... */
115     if (strstr (chld_out.line[i], ";; ANSWER SECTION:")) {
117       /* loop through the whole 'ANSWER SECTION' */
118       for(; i < chld_out.lines; i++) {
119         /* get the host address */
120         if (verbose)
121           printf ("%s\n", chld_out.line[i]);
123         if (strstr (chld_out.line[i], (expected_address == NULL ? query_address : expected_address)) != NULL) {
124           msg = chld_out.line[i];
125           result = STATE_OK;
127           /* Translate output TAB -> SPACE */
128           t = msg;
129           while ((t = strchr(t, '\t')) != NULL) *t = ' ';
130           break;
131         }
132       }
134       if (result == STATE_UNKNOWN) {
135         msg = (char *)_("Server not found in ANSWER SECTION");
136         result = STATE_WARNING;
137       }
139       /* we found the answer section, so break out of the loop */
140       break;
141     }
142   }
144   if (result == STATE_UNKNOWN)
145     msg = (char *)_("No ANSWER SECTION found");
147   /* If we get anything on STDERR, at least set warning */
148   if(chld_err.buflen > 0) {
149     result = max_state(result, STATE_WARNING);
150     if(!msg) for(i = 0; i < chld_err.lines; i++) {
151       msg = strchr(chld_err.line[0], ':');
152       if(msg) {
153         msg++;
154         break;
155       }
156     }
157   }
159   microsec = deltime (tv);
160   elapsed_time = (double)microsec / 1.0e6;
162   if (critical_interval > UNDEFINED && elapsed_time > critical_interval)
163     result = STATE_CRITICAL;
165   else if (warning_interval > UNDEFINED && elapsed_time > warning_interval)
166     result = STATE_WARNING;
168   printf ("DNS %s - %.3f seconds response time (%s)|%s\n",
169           state_text (result), elapsed_time,
170           msg ? msg : _("Probably a non-existent host/domain"),
171           fperfdata("time", elapsed_time, "s",
172                     (warning_interval>UNDEFINED?TRUE:FALSE),
173                     warning_interval,
174                     (critical_interval>UNDEFINED?TRUE:FALSE),
175             critical_interval,
176             TRUE, 0, FALSE, 0));
177   return result;
182 /* process command-line arguments */
183 int
184 process_arguments (int argc, char **argv)
186   int c;
188   int option = 0;
189   static struct option longopts[] = {
190     {"hostname", required_argument, 0, 'H'},
191     {"query_address", required_argument, 0, 'l'},
192     {"warning", required_argument, 0, 'w'},
193     {"critical", required_argument, 0, 'c'},
194     {"timeout", required_argument, 0, 't'},
195     {"verbose", no_argument, 0, 'v'},
196     {"version", no_argument, 0, 'V'},
197     {"help", no_argument, 0, 'h'},
198     {"record_type", required_argument, 0, 'T'},
199     {"expected_address", required_argument, 0, 'a'},
200     {"port", required_argument, 0, 'p'},
201     {0, 0, 0, 0}
202   };
204   if (argc < 2)
205     return ERROR;
207   while (1) {
208     c = getopt_long (argc, argv, "hVvt:l:H:w:c:T:p:a:", longopts, &option);
210     if (c == -1 || c == EOF)
211       break;
213     switch (c) {
214     case 'h':                 /* help */
215       print_help ();
216       exit (STATE_OK);
217     case 'V':                 /* version */
218       print_revision (progname, revision);
219       exit (STATE_OK);
220     case 'H':                 /* hostname */
221       host_or_die(optarg);
222       dns_server = optarg;
223       break;
224     case 'p':                 /* server port */
225       if (is_intpos (optarg)) {
226         server_port = atoi (optarg);
227       }
228       else {
229         usage_va(_("Port must be a positive integer - %s"), optarg);
230       }
231       break;
232     case 'l':                 /* address to lookup */
233       query_address = optarg;
234       break;
235     case 'w':                 /* warning */
236       if (is_nonnegative (optarg)) {
237         warning_interval = strtod (optarg, NULL);
238       }
239       else {
240         usage_va(_("Warning interval must be a positive integer - %s"), optarg);
241       }
242       break;
243     case 'c':                 /* critical */
244       if (is_nonnegative (optarg)) {
245         critical_interval = strtod (optarg, NULL);
246       }
247       else {
248         usage_va(_("Critical interval must be a positive integer - %s"), optarg);
249       }
250       break;
251     case 't':                 /* timeout */
252       if (is_intnonneg (optarg)) {
253         timeout_interval = atoi (optarg);
254       }
255       else {
256         usage_va(_("Timeout interval must be a positive integer - %s"), optarg);
257       }
258       break;
259     case 'v':                 /* verbose */
260       verbose = TRUE;
261       break;
262     case 'T':
263       record_type = optarg;
264       break;
265     case 'a':
266       expected_address = optarg;
267       break;
268     default:                  /* usage5 */
269       usage5();
270     }
271   }
273   c = optind;
274   if (dns_server == NULL) {
275     if (c < argc) {
276       host_or_die(argv[c]);
277       dns_server = argv[c];
278     }
279     else {
280       dns_server = strdup ("127.0.0.1");
281     }
282   }
284   return validate_arguments ();
289 int
290 validate_arguments (void)
292   return OK;
297 void
298 print_help (void)
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 (_("This plugin test the DNS service on the specified host using dig"));
311   printf ("\n\n");
313   print_usage ();
315   printf (_(UT_HELP_VRSN));
317   printf (_(UT_HOST_PORT), 'p', myport);
319   printf (" %s\n","-l, --lookup=STRING");
320   printf ("    %s\n",_("machine name to lookup"));
321   printf (" %s\n","-T, --record_type=STRING");
322   printf ("    %s\n",_("record type to lookup (default: A)"));
323   printf (" %s\n","-a, --expected_address=STRING");
324   printf ("    %s\n",_("an address expected to be in the answer section.if not set, uses whatever was in -l"));
325   printf (_(UT_WARN_CRIT));
326   printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
327   printf (_(UT_VERBOSE));
328   printf (_(UT_SUPPORT));
333 void
334 print_usage (void)
336   printf (_("Usage:"));
337   printf ("%s -H host -l lookup [-p <server port>] [-T <query type>]", progname);
338   printf (" [-w <warning interval>] [-c <critical interval>] [-t <timeout>]");
339   printf (" [-a <expected answer address>] [-v]\n");