Code

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