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