Code

Fix translations when extra-opts aren't enabled
[nagiosplug.git] / plugins / check_dig.c
1 /*****************************************************************************
2
3 * Nagios check_dig plugin
4
5 * License: GPL
6 * Copyright (c) 2002-2008 Nagios 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 *****************************************************************************/
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 */
35 const char *progname = "check_dig";
36 const char *copyright = "2002-2008";
37 const char *email = "nagiosplug-devel@lists.sourceforge.net";
39 #include "common.h"
40 #include "netutils.h"
41 #include "utils.h"
42 #include "runcmd.h"
44 int process_arguments (int, char **);
45 int validate_arguments (void);
46 void print_help (void);
47 void print_usage (void);
49 #define UNDEFINED 0
50 #define DEFAULT_PORT 53
52 char *query_address = NULL;
53 char *record_type = "A";
54 char *expected_address = NULL;
55 char *dns_server = NULL;
56 char *dig_args = "";
57 int verbose = FALSE;
58 int server_port = DEFAULT_PORT;
59 double warning_interval = UNDEFINED;
60 double critical_interval = UNDEFINED;
61 struct timeval tv;
63 int
64 main (int argc, char **argv)
65 {
66   char *command_line;
67   output chld_out, chld_err;
68   char *msg = NULL;
69   size_t i;
70   char *t;
71   long microsec;
72   double elapsed_time;
73   int result = STATE_UNKNOWN;
75   setlocale (LC_ALL, "");
76   bindtextdomain (PACKAGE, LOCALEDIR);
77   textdomain (PACKAGE);
79   /* Set signal handling and alarm */
80   if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
81     usage_va(_("Cannot catch SIGALRM"));
83   /* Parse extra opts if any */
84   argv=np_extra_opts (&argc, argv, progname);
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 %s",
91             PATH_TO_DIG, dns_server, server_port, query_address, record_type, dig_args);
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");
144     result = STATE_CRITICAL;
145   }
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     {"dig-arguments", required_argument, 0, 'A'},
196     {"verbose", no_argument, 0, 'v'},
197     {"version", no_argument, 0, 'V'},
198     {"help", no_argument, 0, 'h'},
199     {"record_type", required_argument, 0, 'T'},
200     {"expected_address", required_argument, 0, 'a'},
201     {"port", required_argument, 0, 'p'},
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:w:c:T:p:a:A:", longopts, &option);
211     if (c == -1 || c == EOF)
212       break;
214     switch (c) {
215     case 'h':                 /* help */
216       print_help ();
217       exit (STATE_OK);
218     case 'V':                 /* version */
219       print_revision (progname, NP_VERSION);
220       exit (STATE_OK);
221     case 'H':                 /* hostname */
222       host_or_die(optarg);
223       dns_server = optarg;
224       break;
225     case 'p':                 /* server port */
226       if (is_intpos (optarg)) {
227         server_port = atoi (optarg);
228       }
229       else {
230         usage_va(_("Port must be a positive integer - %s"), optarg);
231       }
232       break;
233     case 'l':                 /* address to lookup */
234       query_address = optarg;
235       break;
236     case 'w':                 /* warning */
237       if (is_nonnegative (optarg)) {
238         warning_interval = strtod (optarg, NULL);
239       }
240       else {
241         usage_va(_("Warning interval must be a positive integer - %s"), optarg);
242       }
243       break;
244     case 'c':                 /* critical */
245       if (is_nonnegative (optarg)) {
246         critical_interval = strtod (optarg, NULL);
247       }
248       else {
249         usage_va(_("Critical interval must be a positive integer - %s"), optarg);
250       }
251       break;
252     case 't':                 /* timeout */
253       if (is_intnonneg (optarg)) {
254         timeout_interval = atoi (optarg);
255       }
256       else {
257         usage_va(_("Timeout interval must be a positive integer - %s"), optarg);
258       }
259       break;
260     case 'A':                 /* dig arguments */
261       dig_args = strdup(optarg);
262       break;
263     case 'v':                 /* verbose */
264       verbose = TRUE;
265       break;
266     case 'T':
267       record_type = optarg;
268       break;
269     case 'a':
270       expected_address = optarg;
271       break;
272     default:                  /* usage5 */
273       usage5();
274     }
275   }
277   c = optind;
278   if (dns_server == NULL) {
279     if (c < argc) {
280       host_or_die(argv[c]);
281       dns_server = argv[c];
282     }
283     else {
284       dns_server = strdup ("127.0.0.1");
285     }
286   }
288   return validate_arguments ();
293 int
294 validate_arguments (void)
296   if (query_address != NULL)
297     return OK;
298   else
299     return ERROR;
304 void
305 print_help (void)
307   char *myport;
309   asprintf (&myport, "%d", DEFAULT_PORT);
311   print_revision (progname, NP_VERSION);
313   printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
314   printf (COPYRIGHT, copyright, email);
316   printf (_("This plugin test the DNS service on the specified host using dig"));
318   printf ("\n\n");
320   print_usage ();
322   printf (UT_HELP_VRSN);
324   printf (UT_EXTRA_OPTS);
326   printf (UT_HOST_PORT, 'p', myport);
328   printf (" %s\n","-l, --query_address=STRING");
329   printf ("    %s\n",_("Machine name to lookup"));
330   printf (" %s\n","-T, --record_type=STRING");
331   printf ("    %s\n",_("Record type to lookup (default: A)"));
332   printf (" %s\n","-a, --expected_address=STRING");
333   printf ("    %s\n",_("An address expected to be in the answer section. If not set, uses whatever"));
334   printf ("    %s\n",_("was in -l"));
335   printf (" %s\n","-A, --dig-arguments=STRING");
336   printf ("    %s\n",_("Pass STRING as argument(s) to dig"));
337   printf (UT_WARN_CRIT);
338   printf (UT_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
339   printf (UT_VERBOSE);
341   printf ("\n");
342   printf ("%s\n", _("Examples:"));
343   printf (" %s\n", "check_dig -H DNSSERVER -l www.example.com -A \"+tcp\"");
344   printf (" %s\n", "This will send a tcp query to DNSSERVER for www.example.com");
346 #ifdef NP_EXTRA_OPTS
347   printf ("\n");
348   printf ("%s\n", _("Notes:"));
349   printf (UT_EXTRA_OPTS_NOTES);
350 #endif
352   printf (UT_SUPPORT);
357 void
358 print_usage (void)
360   printf (_("Usage:"));
361   printf ("%s -l <query_address> [-H <host>] [-p <server port>]\n", progname);
362   printf (" [-T <query type>] [-w <warning interval>] [-c <critical interval>]\n");
363   printf (" [-t <timeout>] [-a <expected answer address>] [-v]\n");