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.c189
1 files changed, 100 insertions, 89 deletions
diff --git a/plugins/check_dig.c b/plugins/check_dig.c
index 2bbd1e05..c27e5f13 100644
--- a/plugins/check_dig.c
+++ b/plugins/check_dig.c
@@ -41,97 +41,95 @@ 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
44static int process_arguments(int /*argc*/, char ** /*argv*/); 44#include "check_dig.d/config.h"
45static int validate_arguments(void); 45#include "states.h"
46
47typedef struct {
48 int errorcode;
49 check_dig_config config;
50} check_dig_config_wrapper;
51static check_dig_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
52static check_dig_config_wrapper validate_arguments(check_dig_config_wrapper /*config_wrapper*/);
53
46static void print_help(void); 54static void print_help(void);
47void print_usage(void); 55void print_usage(void);
48 56
49#define UNDEFINED 0 57static int verbose = 0;
50#define DEFAULT_PORT 53
51#define DEFAULT_TRIES 2
52
53static char *query_address = NULL;
54static char *record_type = "A";
55static char *expected_address = NULL;
56static char *dns_server = NULL;
57static char *dig_args = "";
58static char *query_transport = "";
59static bool verbose = false;
60static int server_port = DEFAULT_PORT;
61static int number_tries = DEFAULT_TRIES;
62static double warning_interval = UNDEFINED;
63static double critical_interval = UNDEFINED;
64static struct timeval tv;
65 58
66int main(int argc, char **argv) { 59int 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;
77
78 setlocale(LC_ALL, ""); 60 setlocale(LC_ALL, "");
79 bindtextdomain(PACKAGE, LOCALEDIR); 61 bindtextdomain(PACKAGE, LOCALEDIR);
80 textdomain(PACKAGE); 62 textdomain(PACKAGE);
81 63
82 /* Set signal handling and alarm */ 64 /* Set signal handling and alarm */
83 if (signal(SIGALRM, runcmd_timeout_alarm_handler) == SIG_ERR) 65 if (signal(SIGALRM, runcmd_timeout_alarm_handler) == SIG_ERR) {
84 usage_va(_("Cannot catch SIGALRM")); 66 usage_va(_("Cannot catch SIGALRM"));
67 }
85 68
86 /* Parse extra opts if any */ 69 /* Parse extra opts if any */
87 argv = np_extra_opts(&argc, argv, progname); 70 argv = np_extra_opts(&argc, argv, progname);
88 71
89 if (process_arguments(argc, argv) == ERROR) 72 check_dig_config_wrapper tmp_config = process_arguments(argc, argv);
73 if (tmp_config.errorcode == ERROR) {
90 usage_va(_("Could not parse arguments")); 74 usage_va(_("Could not parse arguments"));
75 }
76
77 const check_dig_config config = tmp_config.config;
91 78
92 /* dig applies the timeout to each try, so we need to work around this */ 79 /* dig applies the timeout to each try, so we need to work around this */
93 timeout_interval_dig = timeout_interval / number_tries + number_tries; 80 int timeout_interval_dig = ((int)timeout_interval / config.number_tries) + config.number_tries;
94 81
82 char *command_line;
95 /* get the command to run */ 83 /* 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, 84 xasprintf(&command_line, "%s %s %s -p %d @%s %s %s +retry=%d +time=%d", PATH_TO_DIG,
97 query_address, record_type, number_tries, timeout_interval_dig); 85 config.dig_args, config.query_transport, config.server_port, config.dns_server,
86 config.query_address, config.record_type, config.number_tries, timeout_interval_dig);
98 87
99 alarm(timeout_interval); 88 alarm(timeout_interval);
100 gettimeofday(&tv, NULL); 89 struct timeval start_time;
90 gettimeofday(&start_time, NULL);
101 91
102 if (verbose) { 92 if (verbose) {
103 printf("%s\n", command_line); 93 printf("%s\n", command_line);
104 if (expected_address != NULL) { 94 if (config.expected_address != NULL) {
105 printf(_("Looking for: '%s'\n"), expected_address); 95 printf(_("Looking for: '%s'\n"), config.expected_address);
106 } else { 96 } else {
107 printf(_("Looking for: '%s'\n"), query_address); 97 printf(_("Looking for: '%s'\n"), config.query_address);
108 } 98 }
109 } 99 }
110 100
101 output chld_out;
102 output chld_err;
103 char *msg = NULL;
104 mp_state_enum result = STATE_UNKNOWN;
111 /* run the command */ 105 /* run the command */
112 if (np_runcmd(command_line, &chld_out, &chld_err, 0) != 0) { 106 if (np_runcmd(command_line, &chld_out, &chld_err, 0) != 0) {
113 result = STATE_WARNING; 107 result = STATE_WARNING;
114 msg = (char *)_("dig returned an error status"); 108 msg = (char *)_("dig returned an error status");
115 } 109 }
116 110
117 for (i = 0; i < chld_out.lines; i++) { 111 for (size_t i = 0; i < chld_out.lines; i++) {
118 /* the server is responding, we just got the host name... */ 112 /* the server is responding, we just got the host name... */
119 if (strstr(chld_out.line[i], ";; ANSWER SECTION:")) { 113 if (strstr(chld_out.line[i], ";; ANSWER SECTION:")) {
120 114
121 /* loop through the whole 'ANSWER SECTION' */ 115 /* loop through the whole 'ANSWER SECTION' */
122 for (; i < chld_out.lines; i++) { 116 for (; i < chld_out.lines; i++) {
123 /* get the host address */ 117 /* get the host address */
124 if (verbose) 118 if (verbose) {
125 printf("%s\n", chld_out.line[i]); 119 printf("%s\n", chld_out.line[i]);
120 }
126 121
127 if (strcasestr(chld_out.line[i], (expected_address == NULL ? query_address : expected_address)) != NULL) { 122 if (strcasestr(chld_out.line[i], (config.expected_address == NULL
123 ? config.query_address
124 : config.expected_address)) != NULL) {
128 msg = chld_out.line[i]; 125 msg = chld_out.line[i];
129 result = STATE_OK; 126 result = STATE_OK;
130 127
131 /* Translate output TAB -> SPACE */ 128 /* Translate output TAB -> SPACE */
132 t = msg; 129 char *temp = msg;
133 while ((t = strchr(t, '\t')) != NULL) 130 while ((temp = strchr(temp, '\t')) != NULL) {
134 *t = ' '; 131 *temp = ' ';
132 }
135 break; 133 break;
136 } 134 }
137 } 135 }
@@ -154,37 +152,38 @@ int main(int argc, char **argv) {
154 /* If we get anything on STDERR, at least set warning */ 152 /* If we get anything on STDERR, at least set warning */
155 if (chld_err.buflen > 0) { 153 if (chld_err.buflen > 0) {
156 result = max_state(result, STATE_WARNING); 154 result = max_state(result, STATE_WARNING);
157 if (!msg) 155 if (!msg) {
158 for (i = 0; i < chld_err.lines; i++) { 156 for (size_t i = 0; i < chld_err.lines; i++) {
159 msg = strchr(chld_err.line[0], ':'); 157 msg = strchr(chld_err.line[0], ':');
160 if (msg) { 158 if (msg) {
161 msg++; 159 msg++;
162 break; 160 break;
163 } 161 }
164 } 162 }
163 }
165 } 164 }
166 165
167 microsec = deltime(tv); 166 long microsec = deltime(start_time);
168 elapsed_time = (double)microsec / 1.0e6; 167 double elapsed_time = (double)microsec / 1.0e6;
169 168
170 if (critical_interval > UNDEFINED && elapsed_time > critical_interval) 169 if (config.critical_interval > UNDEFINED && elapsed_time > config.critical_interval) {
171 result = STATE_CRITICAL; 170 result = STATE_CRITICAL;
171 }
172 172
173 else if (warning_interval > UNDEFINED && elapsed_time > warning_interval) 173 else if (config.warning_interval > UNDEFINED && elapsed_time > config.warning_interval) {
174 result = STATE_WARNING; 174 result = STATE_WARNING;
175 }
175 176
176 printf("DNS %s - %.3f seconds response time (%s)|%s\n", state_text(result), elapsed_time, 177 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 msg ? msg : _("Probably a non-existent host/domain"),
178 fperfdata("time", elapsed_time, "s", (warning_interval > UNDEFINED ? true : false), warning_interval, 179 fperfdata("time", elapsed_time, "s", (config.warning_interval > UNDEFINED),
179 (critical_interval > UNDEFINED ? true : false), critical_interval, true, 0, false, 0)); 180 config.warning_interval, (config.critical_interval > UNDEFINED),
180 return result; 181 config.critical_interval, true, 0, false, 0));
182 exit(result);
181} 183}
182 184
183/* process command-line arguments */ 185/* process command-line arguments */
184int process_arguments(int argc, char **argv) { 186check_dig_config_wrapper process_arguments(int argc, char **argv) {
185 int c;
186
187 int option = 0;
188 static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, 187 static struct option longopts[] = {{"hostname", required_argument, 0, 'H'},
189 {"query_address", required_argument, 0, 'l'}, 188 {"query_address", required_argument, 0, 'l'},
190 {"warning", required_argument, 0, 'w'}, 189 {"warning", required_argument, 0, 'w'},
@@ -201,16 +200,25 @@ int process_arguments(int argc, char **argv) {
201 {"use-ipv6", no_argument, 0, '6'}, 200 {"use-ipv6", no_argument, 0, '6'},
202 {0, 0, 0, 0}}; 201 {0, 0, 0, 0}};
203 202
204 if (argc < 2) 203 check_dig_config_wrapper result = {
205 return ERROR; 204 .errorcode = OK,
205 .config = check_dig_config_init(),
206 };
206 207
207 while (1) { 208 if (argc < 2) {
208 c = getopt_long(argc, argv, "hVvt:l:H:w:c:T:p:a:A:46", longopts, &option); 209 result.errorcode = ERROR;
210 return result;
211 }
209 212
210 if (c == -1 || c == EOF) 213 int option = 0;
214 while (true) {
215 int option_index = getopt_long(argc, argv, "hVvt:l:H:w:c:T:p:a:A:46", longopts, &option);
216
217 if (option_index == -1 || option_index == EOF) {
211 break; 218 break;
219 }
212 220
213 switch (c) { 221 switch (option_index) {
214 case 'h': /* help */ 222 case 'h': /* help */
215 print_help(); 223 print_help();
216 exit(STATE_UNKNOWN); 224 exit(STATE_UNKNOWN);
@@ -219,28 +227,28 @@ int process_arguments(int argc, char **argv) {
219 exit(STATE_UNKNOWN); 227 exit(STATE_UNKNOWN);
220 case 'H': /* hostname */ 228 case 'H': /* hostname */
221 host_or_die(optarg); 229 host_or_die(optarg);
222 dns_server = optarg; 230 result.config.dns_server = optarg;
223 break; 231 break;
224 case 'p': /* server port */ 232 case 'p': /* server port */
225 if (is_intpos(optarg)) { 233 if (is_intpos(optarg)) {
226 server_port = atoi(optarg); 234 result.config.server_port = atoi(optarg);
227 } else { 235 } else {
228 usage_va(_("Port must be a positive integer - %s"), optarg); 236 usage_va(_("Port must be a positive integer - %s"), optarg);
229 } 237 }
230 break; 238 break;
231 case 'l': /* address to lookup */ 239 case 'l': /* address to lookup */
232 query_address = optarg; 240 result.config.query_address = optarg;
233 break; 241 break;
234 case 'w': /* warning */ 242 case 'w': /* warning */
235 if (is_nonnegative(optarg)) { 243 if (is_nonnegative(optarg)) {
236 warning_interval = strtod(optarg, NULL); 244 result.config.warning_interval = strtod(optarg, NULL);
237 } else { 245 } else {
238 usage_va(_("Warning interval must be a positive integer - %s"), optarg); 246 usage_va(_("Warning interval must be a positive integer - %s"), optarg);
239 } 247 }
240 break; 248 break;
241 case 'c': /* critical */ 249 case 'c': /* critical */
242 if (is_nonnegative(optarg)) { 250 if (is_nonnegative(optarg)) {
243 critical_interval = strtod(optarg, NULL); 251 result.config.critical_interval = strtod(optarg, NULL);
244 } else { 252 } else {
245 usage_va(_("Critical interval must be a positive integer - %s"), optarg); 253 usage_va(_("Critical interval must be a positive integer - %s"), optarg);
246 } 254 }
@@ -253,48 +261,50 @@ int process_arguments(int argc, char **argv) {
253 } 261 }
254 break; 262 break;
255 case 'A': /* dig arguments */ 263 case 'A': /* dig arguments */
256 dig_args = strdup(optarg); 264 result.config.dig_args = strdup(optarg);
257 break; 265 break;
258 case 'v': /* verbose */ 266 case 'v': /* verbose */
259 verbose = true; 267 verbose++;
260 break; 268 break;
261 case 'T': 269 case 'T':
262 record_type = optarg; 270 result.config.record_type = optarg;
263 break; 271 break;
264 case 'a': 272 case 'a':
265 expected_address = optarg; 273 result.config.expected_address = optarg;
266 break; 274 break;
267 case '4': 275 case '4':
268 query_transport = "-4"; 276 result.config.query_transport = "-4";
269 break; 277 break;
270 case '6': 278 case '6':
271 query_transport = "-6"; 279 result.config.query_transport = "-6";
272 break; 280 break;
273 default: /* usage5 */ 281 default: /* usage5 */
274 usage5(); 282 usage5();
275 } 283 }
276 } 284 }
277 285
278 c = optind; 286 int index = optind;
279 if (dns_server == NULL) { 287 if (result.config.dns_server == NULL) {
280 if (c < argc) { 288 if (index < argc) {
281 host_or_die(argv[c]); 289 host_or_die(argv[index]);
282 dns_server = argv[c]; 290 result.config.dns_server = argv[index];
283 } else { 291 } else {
284 if (strcmp(query_transport, "-6") == 0) 292 if (strcmp(result.config.query_transport, "-6") == 0) {
285 dns_server = strdup("::1"); 293 result.config.dns_server = strdup("::1");
286 else 294 } else {
287 dns_server = strdup("127.0.0.1"); 295 result.config.dns_server = strdup("127.0.0.1");
296 }
288 } 297 }
289 } 298 }
290 299
291 return validate_arguments(); 300 return validate_arguments(result);
292} 301}
293 302
294int validate_arguments(void) { 303check_dig_config_wrapper validate_arguments(check_dig_config_wrapper config_wrapper) {
295 if (query_address != NULL) 304 if (config_wrapper.config.query_address == NULL) {
296 return OK; 305 config_wrapper.errorcode = ERROR;
297 return ERROR; 306 }
307 return config_wrapper;
298} 308}
299 309
300void print_help(void) { 310void print_help(void) {
@@ -328,7 +338,8 @@ void print_help(void) {
328 printf(" %s\n", "-T, --record_type=STRING"); 338 printf(" %s\n", "-T, --record_type=STRING");
329 printf(" %s\n", _("Record type to lookup (default: A)")); 339 printf(" %s\n", _("Record type to lookup (default: A)"));
330 printf(" %s\n", "-a, --expected_address=STRING"); 340 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")); 341 printf(" %s\n",
342 _("An address expected to be in the answer section. If not set, uses whatever"));
332 printf(" %s\n", _("was in -l")); 343 printf(" %s\n", _("was in -l"));
333 printf(" %s\n", "-A, --dig-arguments=STRING"); 344 printf(" %s\n", "-A, --dig-arguments=STRING");
334 printf(" %s\n", _("Pass STRING as argument(s) to dig")); 345 printf(" %s\n", _("Pass STRING as argument(s) to dig"));