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.c183
1 files changed, 95 insertions, 88 deletions
diff --git a/plugins/check_dig.c b/plugins/check_dig.c
index 2bbd1e05..d0903be2 100644
--- a/plugins/check_dig.c
+++ b/plugins/check_dig.c
@@ -41,97 +41,93 @@ 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, config.dig_args, config.query_transport,
97 query_address, record_type, number_tries, timeout_interval_dig); 85 config.server_port, config.dns_server, config.query_address, config.record_type, config.number_tries, timeout_interval_dig);
98 86
99 alarm(timeout_interval); 87 alarm(timeout_interval);
100 gettimeofday(&tv, NULL); 88 struct timeval start_time;
89 gettimeofday(&start_time, NULL);
101 90
102 if (verbose) { 91 if (verbose) {
103 printf("%s\n", command_line); 92 printf("%s\n", command_line);
104 if (expected_address != NULL) { 93 if (config.expected_address != NULL) {
105 printf(_("Looking for: '%s'\n"), expected_address); 94 printf(_("Looking for: '%s'\n"), config.expected_address);
106 } else { 95 } else {
107 printf(_("Looking for: '%s'\n"), query_address); 96 printf(_("Looking for: '%s'\n"), config.query_address);
108 } 97 }
109 } 98 }
110 99
100 output chld_out;
101 output chld_err;
102 char *msg = NULL;
103 mp_state_enum result = STATE_UNKNOWN;
111 /* run the command */ 104 /* run the command */
112 if (np_runcmd(command_line, &chld_out, &chld_err, 0) != 0) { 105 if (np_runcmd(command_line, &chld_out, &chld_err, 0) != 0) {
113 result = STATE_WARNING; 106 result = STATE_WARNING;
114 msg = (char *)_("dig returned an error status"); 107 msg = (char *)_("dig returned an error status");
115 } 108 }
116 109
117 for (i = 0; i < chld_out.lines; i++) { 110 for (size_t i = 0; i < chld_out.lines; i++) {
118 /* the server is responding, we just got the host name... */ 111 /* the server is responding, we just got the host name... */
119 if (strstr(chld_out.line[i], ";; ANSWER SECTION:")) { 112 if (strstr(chld_out.line[i], ";; ANSWER SECTION:")) {
120 113
121 /* loop through the whole 'ANSWER SECTION' */ 114 /* loop through the whole 'ANSWER SECTION' */
122 for (; i < chld_out.lines; i++) { 115 for (; i < chld_out.lines; i++) {
123 /* get the host address */ 116 /* get the host address */
124 if (verbose) 117 if (verbose) {
125 printf("%s\n", chld_out.line[i]); 118 printf("%s\n", chld_out.line[i]);
119 }
126 120
127 if (strcasestr(chld_out.line[i], (expected_address == NULL ? query_address : expected_address)) != NULL) { 121 if (strcasestr(chld_out.line[i], (config.expected_address == NULL ? config.query_address : config.expected_address)) !=
122 NULL) {
128 msg = chld_out.line[i]; 123 msg = chld_out.line[i];
129 result = STATE_OK; 124 result = STATE_OK;
130 125
131 /* Translate output TAB -> SPACE */ 126 /* Translate output TAB -> SPACE */
132 t = msg; 127 char *temp = msg;
133 while ((t = strchr(t, '\t')) != NULL) 128 while ((temp = strchr(temp, '\t')) != NULL) {
134 *t = ' '; 129 *temp = ' ';
130 }
135 break; 131 break;
136 } 132 }
137 } 133 }
@@ -154,37 +150,37 @@ int main(int argc, char **argv) {
154 /* If we get anything on STDERR, at least set warning */ 150 /* If we get anything on STDERR, at least set warning */
155 if (chld_err.buflen > 0) { 151 if (chld_err.buflen > 0) {
156 result = max_state(result, STATE_WARNING); 152 result = max_state(result, STATE_WARNING);
157 if (!msg) 153 if (!msg) {
158 for (i = 0; i < chld_err.lines; i++) { 154 for (size_t i = 0; i < chld_err.lines; i++) {
159 msg = strchr(chld_err.line[0], ':'); 155 msg = strchr(chld_err.line[0], ':');
160 if (msg) { 156 if (msg) {
161 msg++; 157 msg++;
162 break; 158 break;
163 } 159 }
164 } 160 }
161 }
165 } 162 }
166 163
167 microsec = deltime(tv); 164 long microsec = deltime(start_time);
168 elapsed_time = (double)microsec / 1.0e6; 165 double elapsed_time = (double)microsec / 1.0e6;
169 166
170 if (critical_interval > UNDEFINED && elapsed_time > critical_interval) 167 if (config.critical_interval > UNDEFINED && elapsed_time > config.critical_interval) {
171 result = STATE_CRITICAL; 168 result = STATE_CRITICAL;
169 }
172 170
173 else if (warning_interval > UNDEFINED && elapsed_time > warning_interval) 171 else if (config.warning_interval > UNDEFINED && elapsed_time > config.warning_interval) {
174 result = STATE_WARNING; 172 result = STATE_WARNING;
173 }
175 174
176 printf("DNS %s - %.3f seconds response time (%s)|%s\n", state_text(result), elapsed_time, 175 printf("DNS %s - %.3f seconds response time (%s)|%s\n", state_text(result), elapsed_time,
177 msg ? msg : _("Probably a non-existent host/domain"), 176 msg ? msg : _("Probably a non-existent host/domain"),
178 fperfdata("time", elapsed_time, "s", (warning_interval > UNDEFINED ? true : false), warning_interval, 177 fperfdata("time", elapsed_time, "s", (config.warning_interval > UNDEFINED), config.warning_interval,
179 (critical_interval > UNDEFINED ? true : false), critical_interval, true, 0, false, 0)); 178 (config.critical_interval > UNDEFINED), config.critical_interval, true, 0, false, 0));
180 return result; 179 exit(result);
181} 180}
182 181
183/* process command-line arguments */ 182/* process command-line arguments */
184int process_arguments(int argc, char **argv) { 183check_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'}, 184 static struct option longopts[] = {{"hostname", required_argument, 0, 'H'},
189 {"query_address", required_argument, 0, 'l'}, 185 {"query_address", required_argument, 0, 'l'},
190 {"warning", required_argument, 0, 'w'}, 186 {"warning", required_argument, 0, 'w'},
@@ -201,16 +197,25 @@ int process_arguments(int argc, char **argv) {
201 {"use-ipv6", no_argument, 0, '6'}, 197 {"use-ipv6", no_argument, 0, '6'},
202 {0, 0, 0, 0}}; 198 {0, 0, 0, 0}};
203 199
204 if (argc < 2) 200 check_dig_config_wrapper result = {
205 return ERROR; 201 .errorcode = OK,
202 .config = check_dig_config_init(),
203 };
206 204
207 while (1) { 205 if (argc < 2) {
208 c = getopt_long(argc, argv, "hVvt:l:H:w:c:T:p:a:A:46", longopts, &option); 206 result.errorcode = ERROR;
207 return result;
208 }
209 209
210 if (c == -1 || c == EOF) 210 int option = 0;
211 while (true) {
212 int option_index = getopt_long(argc, argv, "hVvt:l:H:w:c:T:p:a:A:46", longopts, &option);
213
214 if (option_index == -1 || option_index == EOF) {
211 break; 215 break;
216 }
212 217
213 switch (c) { 218 switch (option_index) {
214 case 'h': /* help */ 219 case 'h': /* help */
215 print_help(); 220 print_help();
216 exit(STATE_UNKNOWN); 221 exit(STATE_UNKNOWN);
@@ -219,28 +224,28 @@ int process_arguments(int argc, char **argv) {
219 exit(STATE_UNKNOWN); 224 exit(STATE_UNKNOWN);
220 case 'H': /* hostname */ 225 case 'H': /* hostname */
221 host_or_die(optarg); 226 host_or_die(optarg);
222 dns_server = optarg; 227 result.config.dns_server = optarg;
223 break; 228 break;
224 case 'p': /* server port */ 229 case 'p': /* server port */
225 if (is_intpos(optarg)) { 230 if (is_intpos(optarg)) {
226 server_port = atoi(optarg); 231 result.config.server_port = atoi(optarg);
227 } else { 232 } else {
228 usage_va(_("Port must be a positive integer - %s"), optarg); 233 usage_va(_("Port must be a positive integer - %s"), optarg);
229 } 234 }
230 break; 235 break;
231 case 'l': /* address to lookup */ 236 case 'l': /* address to lookup */
232 query_address = optarg; 237 result.config.query_address = optarg;
233 break; 238 break;
234 case 'w': /* warning */ 239 case 'w': /* warning */
235 if (is_nonnegative(optarg)) { 240 if (is_nonnegative(optarg)) {
236 warning_interval = strtod(optarg, NULL); 241 result.config.warning_interval = strtod(optarg, NULL);
237 } else { 242 } else {
238 usage_va(_("Warning interval must be a positive integer - %s"), optarg); 243 usage_va(_("Warning interval must be a positive integer - %s"), optarg);
239 } 244 }
240 break; 245 break;
241 case 'c': /* critical */ 246 case 'c': /* critical */
242 if (is_nonnegative(optarg)) { 247 if (is_nonnegative(optarg)) {
243 critical_interval = strtod(optarg, NULL); 248 result.config.critical_interval = strtod(optarg, NULL);
244 } else { 249 } else {
245 usage_va(_("Critical interval must be a positive integer - %s"), optarg); 250 usage_va(_("Critical interval must be a positive integer - %s"), optarg);
246 } 251 }
@@ -253,48 +258,50 @@ int process_arguments(int argc, char **argv) {
253 } 258 }
254 break; 259 break;
255 case 'A': /* dig arguments */ 260 case 'A': /* dig arguments */
256 dig_args = strdup(optarg); 261 result.config.dig_args = strdup(optarg);
257 break; 262 break;
258 case 'v': /* verbose */ 263 case 'v': /* verbose */
259 verbose = true; 264 verbose++;
260 break; 265 break;
261 case 'T': 266 case 'T':
262 record_type = optarg; 267 result.config.record_type = optarg;
263 break; 268 break;
264 case 'a': 269 case 'a':
265 expected_address = optarg; 270 result.config.expected_address = optarg;
266 break; 271 break;
267 case '4': 272 case '4':
268 query_transport = "-4"; 273 result.config.query_transport = "-4";
269 break; 274 break;
270 case '6': 275 case '6':
271 query_transport = "-6"; 276 result.config.query_transport = "-6";
272 break; 277 break;
273 default: /* usage5 */ 278 default: /* usage5 */
274 usage5(); 279 usage5();
275 } 280 }
276 } 281 }
277 282
278 c = optind; 283 int index = optind;
279 if (dns_server == NULL) { 284 if (result.config.dns_server == NULL) {
280 if (c < argc) { 285 if (index < argc) {
281 host_or_die(argv[c]); 286 host_or_die(argv[index]);
282 dns_server = argv[c]; 287 result.config.dns_server = argv[index];
283 } else { 288 } else {
284 if (strcmp(query_transport, "-6") == 0) 289 if (strcmp(result.config.query_transport, "-6") == 0) {
285 dns_server = strdup("::1"); 290 result.config.dns_server = strdup("::1");
286 else 291 } else {
287 dns_server = strdup("127.0.0.1"); 292 result.config.dns_server = strdup("127.0.0.1");
293 }
288 } 294 }
289 } 295 }
290 296
291 return validate_arguments(); 297 return validate_arguments(result);
292} 298}
293 299
294int validate_arguments(void) { 300check_dig_config_wrapper validate_arguments(check_dig_config_wrapper config_wrapper) {
295 if (query_address != NULL) 301 if (config_wrapper.config.query_address == NULL) {
296 return OK; 302 config_wrapper.errorcode = ERROR;
297 return ERROR; 303 }
304 return config_wrapper;
298} 305}
299 306
300void print_help(void) { 307void print_help(void) {