summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'plugins')
-rw-r--r--plugins/Makefile.am3
-rw-r--r--plugins/check_dig.c162
-rw-r--r--plugins/check_dig.d/config.h40
3 files changed, 121 insertions, 84 deletions
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index 3d5ad1a9..643a4992 100644
--- a/plugins/Makefile.am
+++ b/plugins/Makefile.am
@@ -55,7 +55,8 @@ EXTRA_DIST = t \
55 check_ssh.d \ 55 check_ssh.d \
56 check_dns.d \ 56 check_dns.d \
57 check_apt.d \ 57 check_apt.d \
58 check_by_ssh.d 58 check_by_ssh.d \
59 check_dig.d
59 60
60PLUGINHDRS = common.h 61PLUGINHDRS = common.h
61 62
diff --git a/plugins/check_dig.c b/plugins/check_dig.c
index abca6b24..d0903be2 100644
--- a/plugins/check_dig.c
+++ b/plugins/check_dig.c
@@ -41,40 +41,22 @@ 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);
@@ -87,36 +69,45 @@ int main(int argc, char **argv) {
87 /* Parse extra opts if any */ 69 /* Parse extra opts if any */
88 argv = np_extra_opts(&argc, argv, progname); 70 argv = np_extra_opts(&argc, argv, progname);
89 71
90 if (process_arguments(argc, argv) == ERROR) { 72 check_dig_config_wrapper tmp_config = process_arguments(argc, argv);
73 if (tmp_config.errorcode == ERROR) {
91 usage_va(_("Could not parse arguments")); 74 usage_va(_("Could not parse arguments"));
92 } 75 }
93 76
77 const check_dig_config config = tmp_config.config;
78
94 /* 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 */
95 timeout_interval_dig = timeout_interval / number_tries + number_tries; 80 int timeout_interval_dig = ((int)timeout_interval / config.number_tries) + config.number_tries;
96 81
82 char *command_line;
97 /* get the command to run */ 83 /* get the command to run */
98 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,
99 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);
100 86
101 alarm(timeout_interval); 87 alarm(timeout_interval);
102 gettimeofday(&tv, NULL); 88 struct timeval start_time;
89 gettimeofday(&start_time, NULL);
103 90
104 if (verbose) { 91 if (verbose) {
105 printf("%s\n", command_line); 92 printf("%s\n", command_line);
106 if (expected_address != NULL) { 93 if (config.expected_address != NULL) {
107 printf(_("Looking for: '%s'\n"), expected_address); 94 printf(_("Looking for: '%s'\n"), config.expected_address);
108 } else { 95 } else {
109 printf(_("Looking for: '%s'\n"), query_address); 96 printf(_("Looking for: '%s'\n"), config.query_address);
110 } 97 }
111 } 98 }
112 99
100 output chld_out;
101 output chld_err;
102 char *msg = NULL;
103 mp_state_enum result = STATE_UNKNOWN;
113 /* run the command */ 104 /* run the command */
114 if (np_runcmd(command_line, &chld_out, &chld_err, 0) != 0) { 105 if (np_runcmd(command_line, &chld_out, &chld_err, 0) != 0) {
115 result = STATE_WARNING; 106 result = STATE_WARNING;
116 msg = (char *)_("dig returned an error status"); 107 msg = (char *)_("dig returned an error status");
117 } 108 }
118 109
119 for (i = 0; i < chld_out.lines; i++) { 110 for (size_t i = 0; i < chld_out.lines; i++) {
120 /* the server is responding, we just got the host name... */ 111 /* the server is responding, we just got the host name... */
121 if (strstr(chld_out.line[i], ";; ANSWER SECTION:")) { 112 if (strstr(chld_out.line[i], ";; ANSWER SECTION:")) {
122 113
@@ -127,14 +118,15 @@ int main(int argc, char **argv) {
127 printf("%s\n", chld_out.line[i]); 118 printf("%s\n", chld_out.line[i]);
128 } 119 }
129 120
130 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) {
131 msg = chld_out.line[i]; 123 msg = chld_out.line[i];
132 result = STATE_OK; 124 result = STATE_OK;
133 125
134 /* Translate output TAB -> SPACE */ 126 /* Translate output TAB -> SPACE */
135 t = msg; 127 char *temp = msg;
136 while ((t = strchr(t, '\t')) != NULL) { 128 while ((temp = strchr(temp, '\t')) != NULL) {
137 *t = ' '; 129 *temp = ' ';
138 } 130 }
139 break; 131 break;
140 } 132 }
@@ -159,7 +151,7 @@ int main(int argc, char **argv) {
159 if (chld_err.buflen > 0) { 151 if (chld_err.buflen > 0) {
160 result = max_state(result, STATE_WARNING); 152 result = max_state(result, STATE_WARNING);
161 if (!msg) { 153 if (!msg) {
162 for (i = 0; i < chld_err.lines; i++) { 154 for (size_t i = 0; i < chld_err.lines; i++) {
163 msg = strchr(chld_err.line[0], ':'); 155 msg = strchr(chld_err.line[0], ':');
164 if (msg) { 156 if (msg) {
165 msg++; 157 msg++;
@@ -169,29 +161,26 @@ int main(int argc, char **argv) {
169 } 161 }
170 } 162 }
171 163
172 microsec = deltime(tv); 164 long microsec = deltime(start_time);
173 elapsed_time = (double)microsec / 1.0e6; 165 double elapsed_time = (double)microsec / 1.0e6;
174 166
175 if (critical_interval > UNDEFINED && elapsed_time > critical_interval) { 167 if (config.critical_interval > UNDEFINED && elapsed_time > config.critical_interval) {
176 result = STATE_CRITICAL; 168 result = STATE_CRITICAL;
177 } 169 }
178 170
179 else if (warning_interval > UNDEFINED && elapsed_time > warning_interval) { 171 else if (config.warning_interval > UNDEFINED && elapsed_time > config.warning_interval) {
180 result = STATE_WARNING; 172 result = STATE_WARNING;
181 } 173 }
182 174
183 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,
184 msg ? msg : _("Probably a non-existent host/domain"), 176 msg ? msg : _("Probably a non-existent host/domain"),
185 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,
186 (critical_interval > UNDEFINED ? true : false), critical_interval, true, 0, false, 0)); 178 (config.critical_interval > UNDEFINED), config.critical_interval, true, 0, false, 0));
187 return result; 179 exit(result);
188} 180}
189 181
190/* process command-line arguments */ 182/* process command-line arguments */
191int process_arguments(int argc, char **argv) { 183check_dig_config_wrapper process_arguments(int argc, char **argv) {
192 int c;
193
194 int option = 0;
195 static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, 184 static struct option longopts[] = {{"hostname", required_argument, 0, 'H'},
196 {"query_address", required_argument, 0, 'l'}, 185 {"query_address", required_argument, 0, 'l'},
197 {"warning", required_argument, 0, 'w'}, 186 {"warning", required_argument, 0, 'w'},
@@ -208,18 +197,25 @@ int process_arguments(int argc, char **argv) {
208 {"use-ipv6", no_argument, 0, '6'}, 197 {"use-ipv6", no_argument, 0, '6'},
209 {0, 0, 0, 0}}; 198 {0, 0, 0, 0}};
210 199
200 check_dig_config_wrapper result = {
201 .errorcode = OK,
202 .config = check_dig_config_init(),
203 };
204
211 if (argc < 2) { 205 if (argc < 2) {
212 return ERROR; 206 result.errorcode = ERROR;
207 return result;
213 } 208 }
214 209
215 while (1) { 210 int option = 0;
216 c = getopt_long(argc, argv, "hVvt:l:H:w:c:T:p:a:A:46", longopts, &option); 211 while (true) {
212 int option_index = getopt_long(argc, argv, "hVvt:l:H:w:c:T:p:a:A:46", longopts, &option);
217 213
218 if (c == -1 || c == EOF) { 214 if (option_index == -1 || option_index == EOF) {
219 break; 215 break;
220 } 216 }
221 217
222 switch (c) { 218 switch (option_index) {
223 case 'h': /* help */ 219 case 'h': /* help */
224 print_help(); 220 print_help();
225 exit(STATE_UNKNOWN); 221 exit(STATE_UNKNOWN);
@@ -228,28 +224,28 @@ int process_arguments(int argc, char **argv) {
228 exit(STATE_UNKNOWN); 224 exit(STATE_UNKNOWN);
229 case 'H': /* hostname */ 225 case 'H': /* hostname */
230 host_or_die(optarg); 226 host_or_die(optarg);
231 dns_server = optarg; 227 result.config.dns_server = optarg;
232 break; 228 break;
233 case 'p': /* server port */ 229 case 'p': /* server port */
234 if (is_intpos(optarg)) { 230 if (is_intpos(optarg)) {
235 server_port = atoi(optarg); 231 result.config.server_port = atoi(optarg);
236 } else { 232 } else {
237 usage_va(_("Port must be a positive integer - %s"), optarg); 233 usage_va(_("Port must be a positive integer - %s"), optarg);
238 } 234 }
239 break; 235 break;
240 case 'l': /* address to lookup */ 236 case 'l': /* address to lookup */
241 query_address = optarg; 237 result.config.query_address = optarg;
242 break; 238 break;
243 case 'w': /* warning */ 239 case 'w': /* warning */
244 if (is_nonnegative(optarg)) { 240 if (is_nonnegative(optarg)) {
245 warning_interval = strtod(optarg, NULL); 241 result.config.warning_interval = strtod(optarg, NULL);
246 } else { 242 } else {
247 usage_va(_("Warning interval must be a positive integer - %s"), optarg); 243 usage_va(_("Warning interval must be a positive integer - %s"), optarg);
248 } 244 }
249 break; 245 break;
250 case 'c': /* critical */ 246 case 'c': /* critical */
251 if (is_nonnegative(optarg)) { 247 if (is_nonnegative(optarg)) {
252 critical_interval = strtod(optarg, NULL); 248 result.config.critical_interval = strtod(optarg, NULL);
253 } else { 249 } else {
254 usage_va(_("Critical interval must be a positive integer - %s"), optarg); 250 usage_va(_("Critical interval must be a positive integer - %s"), optarg);
255 } 251 }
@@ -262,50 +258,50 @@ int process_arguments(int argc, char **argv) {
262 } 258 }
263 break; 259 break;
264 case 'A': /* dig arguments */ 260 case 'A': /* dig arguments */
265 dig_args = strdup(optarg); 261 result.config.dig_args = strdup(optarg);
266 break; 262 break;
267 case 'v': /* verbose */ 263 case 'v': /* verbose */
268 verbose = true; 264 verbose++;
269 break; 265 break;
270 case 'T': 266 case 'T':
271 record_type = optarg; 267 result.config.record_type = optarg;
272 break; 268 break;
273 case 'a': 269 case 'a':
274 expected_address = optarg; 270 result.config.expected_address = optarg;
275 break; 271 break;
276 case '4': 272 case '4':
277 query_transport = "-4"; 273 result.config.query_transport = "-4";
278 break; 274 break;
279 case '6': 275 case '6':
280 query_transport = "-6"; 276 result.config.query_transport = "-6";
281 break; 277 break;
282 default: /* usage5 */ 278 default: /* usage5 */
283 usage5(); 279 usage5();
284 } 280 }
285 } 281 }
286 282
287 c = optind; 283 int index = optind;
288 if (dns_server == NULL) { 284 if (result.config.dns_server == NULL) {
289 if (c < argc) { 285 if (index < argc) {
290 host_or_die(argv[c]); 286 host_or_die(argv[index]);
291 dns_server = argv[c]; 287 result.config.dns_server = argv[index];
292 } else { 288 } else {
293 if (strcmp(query_transport, "-6") == 0) { 289 if (strcmp(result.config.query_transport, "-6") == 0) {
294 dns_server = strdup("::1"); 290 result.config.dns_server = strdup("::1");
295 } else { 291 } else {
296 dns_server = strdup("127.0.0.1"); 292 result.config.dns_server = strdup("127.0.0.1");
297 } 293 }
298 } 294 }
299 } 295 }
300 296
301 return validate_arguments(); 297 return validate_arguments(result);
302} 298}
303 299
304int validate_arguments(void) { 300check_dig_config_wrapper validate_arguments(check_dig_config_wrapper config_wrapper) {
305 if (query_address != NULL) { 301 if (config_wrapper.config.query_address == NULL) {
306 return OK; 302 config_wrapper.errorcode = ERROR;
307 } 303 }
308 return ERROR; 304 return config_wrapper;
309} 305}
310 306
311void print_help(void) { 307void print_help(void) {
diff --git a/plugins/check_dig.d/config.h b/plugins/check_dig.d/config.h
new file mode 100644
index 00000000..a570b633
--- /dev/null
+++ b/plugins/check_dig.d/config.h
@@ -0,0 +1,40 @@
1#pragma once
2
3#include "../../config.h"
4#include <stddef.h>
5
6#define UNDEFINED 0
7#define DEFAULT_PORT 53
8#define DEFAULT_TRIES 2
9
10typedef struct {
11 char *query_address;
12 char *record_type;
13 char *expected_address;
14 char *dns_server;
15 char *query_transport;
16 int server_port;
17 char *dig_args;
18 int number_tries;
19
20 double warning_interval;
21 double critical_interval;
22} check_dig_config;
23
24check_dig_config check_dig_config_init() {
25 check_dig_config tmp = {
26 .query_address = NULL,
27 .record_type = "A",
28 .expected_address = NULL,
29 .dns_server = NULL,
30 .query_transport = "",
31 .server_port = DEFAULT_PORT,
32 .dig_args = "",
33 .number_tries = DEFAULT_TRIES,
34
35 .warning_interval = UNDEFINED,
36 .critical_interval = UNDEFINED,
37
38 };
39 return tmp;
40}