Merge pull request #2045 from RincewindsHat/fix/calloc_argument_order
[monitoring-plugins.git] / plugins / check_dig.c
blob2bbd1e053637020899844171294ea13b8ba1a2ac
1 /*****************************************************************************
3 * Monitoring check_dig plugin
5 * License: GPL
6 * Copyright (c) 2002-2024 Monitoring Plugins Development Team
8 * Description:
10 * This file contains the check_dig plugin
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.
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.
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/>.
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-2024";
37 const char *email = "devel@monitoring-plugins.org";
39 #include "common.h"
40 #include "netutils.h"
41 #include "utils.h"
42 #include "runcmd.h"
44 static int process_arguments(int /*argc*/, char ** /*argv*/);
45 static int validate_arguments(void);
46 static void print_help(void);
47 void print_usage(void);
49 #define UNDEFINED 0
50 #define DEFAULT_PORT 53
51 #define DEFAULT_TRIES 2
53 static char *query_address = NULL;
54 static char *record_type = "A";
55 static char *expected_address = NULL;
56 static char *dns_server = NULL;
57 static char *dig_args = "";
58 static char *query_transport = "";
59 static bool verbose = false;
60 static int server_port = DEFAULT_PORT;
61 static int number_tries = DEFAULT_TRIES;
62 static double warning_interval = UNDEFINED;
63 static double critical_interval = UNDEFINED;
64 static struct timeval tv;
66 int main(int argc, char **argv) {
67 char *command_line;
68 output chld_out;
69 output chld_err;
70 char *msg = NULL;
71 size_t i;
72 char *t;
73 long microsec;
74 double elapsed_time;
75 int result = STATE_UNKNOWN;
76 int timeout_interval_dig;
78 setlocale(LC_ALL, "");
79 bindtextdomain(PACKAGE, LOCALEDIR);
80 textdomain(PACKAGE);
82 /* Set signal handling and alarm */
83 if (signal(SIGALRM, runcmd_timeout_alarm_handler) == SIG_ERR)
84 usage_va(_("Cannot catch SIGALRM"));
86 /* Parse extra opts if any */
87 argv = np_extra_opts(&argc, argv, progname);
89 if (process_arguments(argc, argv) == ERROR)
90 usage_va(_("Could not parse arguments"));
92 /* dig applies the timeout to each try, so we need to work around this */
93 timeout_interval_dig = timeout_interval / number_tries + number_tries;
95 /* get the command to run */
96 xasprintf(&command_line, "%s %s %s -p %d @%s %s %s +retry=%d +time=%d", PATH_TO_DIG, dig_args, query_transport, server_port, dns_server,
97 query_address, record_type, number_tries, timeout_interval_dig);
99 alarm(timeout_interval);
100 gettimeofday(&tv, NULL);
102 if (verbose) {
103 printf("%s\n", command_line);
104 if (expected_address != NULL) {
105 printf(_("Looking for: '%s'\n"), expected_address);
106 } else {
107 printf(_("Looking for: '%s'\n"), query_address);
111 /* run the command */
112 if (np_runcmd(command_line, &chld_out, &chld_err, 0) != 0) {
113 result = STATE_WARNING;
114 msg = (char *)_("dig returned an error status");
117 for (i = 0; i < chld_out.lines; i++) {
118 /* the server is responding, we just got the host name... */
119 if (strstr(chld_out.line[i], ";; ANSWER SECTION:")) {
121 /* loop through the whole 'ANSWER SECTION' */
122 for (; i < chld_out.lines; i++) {
123 /* get the host address */
124 if (verbose)
125 printf("%s\n", chld_out.line[i]);
127 if (strcasestr(chld_out.line[i], (expected_address == NULL ? query_address : expected_address)) != NULL) {
128 msg = chld_out.line[i];
129 result = STATE_OK;
131 /* Translate output TAB -> SPACE */
132 t = msg;
133 while ((t = strchr(t, '\t')) != NULL)
134 *t = ' ';
135 break;
139 if (result == STATE_UNKNOWN) {
140 msg = (char *)_("Server not found in ANSWER SECTION");
141 result = STATE_WARNING;
144 /* we found the answer section, so break out of the loop */
145 break;
149 if (result == STATE_UNKNOWN) {
150 msg = (char *)_("No ANSWER SECTION found");
151 result = STATE_CRITICAL;
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)
158 for (i = 0; i < chld_err.lines; i++) {
159 msg = strchr(chld_err.line[0], ':');
160 if (msg) {
161 msg++;
162 break;
167 microsec = deltime(tv);
168 elapsed_time = (double)microsec / 1.0e6;
170 if (critical_interval > UNDEFINED && elapsed_time > critical_interval)
171 result = STATE_CRITICAL;
173 else if (warning_interval > UNDEFINED && elapsed_time > warning_interval)
174 result = STATE_WARNING;
176 printf("DNS %s - %.3f seconds response time (%s)|%s\n", state_text(result), elapsed_time,
177 msg ? msg : _("Probably a non-existent host/domain"),
178 fperfdata("time", elapsed_time, "s", (warning_interval > UNDEFINED ? true : false), warning_interval,
179 (critical_interval > UNDEFINED ? true : false), critical_interval, true, 0, false, 0));
180 return result;
183 /* process command-line arguments */
184 int process_arguments(int argc, char **argv) {
185 int c;
187 int option = 0;
188 static struct option longopts[] = {{"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 {"dig-arguments", required_argument, 0, 'A'},
194 {"verbose", no_argument, 0, 'v'},
195 {"version", no_argument, 0, 'V'},
196 {"help", no_argument, 0, 'h'},
197 {"record_type", required_argument, 0, 'T'},
198 {"expected_address", required_argument, 0, 'a'},
199 {"port", required_argument, 0, 'p'},
200 {"use-ipv4", no_argument, 0, '4'},
201 {"use-ipv6", no_argument, 0, '6'},
202 {0, 0, 0, 0}};
204 if (argc < 2)
205 return ERROR;
207 while (1) {
208 c = getopt_long(argc, argv, "hVvt:l:H:w:c:T:p:a:A:46", longopts, &option);
210 if (c == -1 || c == EOF)
211 break;
213 switch (c) {
214 case 'h': /* help */
215 print_help();
216 exit(STATE_UNKNOWN);
217 case 'V': /* version */
218 print_revision(progname, NP_VERSION);
219 exit(STATE_UNKNOWN);
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 } else {
228 usage_va(_("Port must be a positive integer - %s"), optarg);
230 break;
231 case 'l': /* address to lookup */
232 query_address = optarg;
233 break;
234 case 'w': /* warning */
235 if (is_nonnegative(optarg)) {
236 warning_interval = strtod(optarg, NULL);
237 } else {
238 usage_va(_("Warning interval must be a positive integer - %s"), optarg);
240 break;
241 case 'c': /* critical */
242 if (is_nonnegative(optarg)) {
243 critical_interval = strtod(optarg, NULL);
244 } else {
245 usage_va(_("Critical interval must be a positive integer - %s"), optarg);
247 break;
248 case 't': /* timeout */
249 if (is_intnonneg(optarg)) {
250 timeout_interval = atoi(optarg);
251 } else {
252 usage_va(_("Timeout interval must be a positive integer - %s"), optarg);
254 break;
255 case 'A': /* dig arguments */
256 dig_args = strdup(optarg);
257 break;
258 case 'v': /* verbose */
259 verbose = true;
260 break;
261 case 'T':
262 record_type = optarg;
263 break;
264 case 'a':
265 expected_address = optarg;
266 break;
267 case '4':
268 query_transport = "-4";
269 break;
270 case '6':
271 query_transport = "-6";
272 break;
273 default: /* usage5 */
274 usage5();
278 c = optind;
279 if (dns_server == NULL) {
280 if (c < argc) {
281 host_or_die(argv[c]);
282 dns_server = argv[c];
283 } else {
284 if (strcmp(query_transport, "-6") == 0)
285 dns_server = strdup("::1");
286 else
287 dns_server = strdup("127.0.0.1");
291 return validate_arguments();
294 int validate_arguments(void) {
295 if (query_address != NULL)
296 return OK;
297 return ERROR;
300 void print_help(void) {
301 char *myport;
303 xasprintf(&myport, "%d", DEFAULT_PORT);
305 print_revision(progname, NP_VERSION);
307 printf("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
308 printf(COPYRIGHT, copyright, email);
310 printf(_("This plugin tests the DNS service on the specified host using dig"));
312 printf("\n\n");
314 print_usage();
316 printf(UT_HELP_VRSN);
318 printf(UT_EXTRA_OPTS);
320 printf(UT_HOST_PORT, 'p', myport);
322 printf(" %s\n", "-4, --use-ipv4");
323 printf(" %s\n", _("Force dig to only use IPv4 query transport"));
324 printf(" %s\n", "-6, --use-ipv6");
325 printf(" %s\n", _("Force dig to only use IPv6 query transport"));
326 printf(" %s\n", "-l, --query_address=STRING");
327 printf(" %s\n", _("Machine name to lookup"));
328 printf(" %s\n", "-T, --record_type=STRING");
329 printf(" %s\n", _("Record type to lookup (default: A)"));
330 printf(" %s\n", "-a, --expected_address=STRING");
331 printf(" %s\n", _("An address expected to be in the answer section. If not set, uses whatever"));
332 printf(" %s\n", _("was in -l"));
333 printf(" %s\n", "-A, --dig-arguments=STRING");
334 printf(" %s\n", _("Pass STRING as argument(s) to dig"));
335 printf(UT_WARN_CRIT);
336 printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
337 printf(UT_VERBOSE);
339 printf("\n");
340 printf("%s\n", _("Examples:"));
341 printf(" %s\n", "check_dig -H DNSSERVER -l www.example.com -A \"+tcp\"");
342 printf(" %s\n", "This will send a tcp query to DNSSERVER for www.example.com");
344 printf(UT_SUPPORT);
347 void print_usage(void) {
348 printf("%s\n", _("Usage:"));
349 printf("%s -l <query_address> [-H <host>] [-p <server port>]\n", progname);
350 printf(" [-T <query type>] [-w <warning interval>] [-c <critical interval>]\n");
351 printf(" [-t <timeout>] [-a <expected answer address>] [-v]\n");