diff options
-rw-r--r-- | plugins/Makefile.am | 1 | ||||
-rw-r--r-- | plugins/check_ntp_time.c | 210 | ||||
-rw-r--r-- | plugins/check_ntp_time.d/config.h | 28 |
3 files changed, 146 insertions, 93 deletions
diff --git a/plugins/Makefile.am b/plugins/Makefile.am index a6c0ba65..9ea6e85e 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am | |||
@@ -66,6 +66,7 @@ EXTRA_DIST = t \ | |||
66 | check_by_ssh.d \ | 66 | check_by_ssh.d \ |
67 | check_smtp.d \ | 67 | check_smtp.d \ |
68 | check_mysql.d \ | 68 | check_mysql.d \ |
69 | check_ntp_time.d \ | ||
69 | check_dig.d \ | 70 | check_dig.d \ |
70 | check_cluster.d \ | 71 | check_cluster.d \ |
71 | check_fping.d | 72 | check_fping.d |
diff --git a/plugins/check_ntp_time.c b/plugins/check_ntp_time.c index 703b69df..31162883 100644 --- a/plugins/check_ntp_time.c +++ b/plugins/check_ntp_time.c | |||
@@ -41,17 +41,18 @@ const char *email = "devel@monitoring-plugins.org"; | |||
41 | #include "common.h" | 41 | #include "common.h" |
42 | #include "netutils.h" | 42 | #include "netutils.h" |
43 | #include "utils.h" | 43 | #include "utils.h" |
44 | #include "states.h" | ||
45 | #include "thresholds.h" | ||
46 | #include "check_ntp_time.d/config.h" | ||
44 | 47 | ||
45 | static char *server_address = NULL; | ||
46 | static char *port = "123"; | ||
47 | static int verbose = 0; | 48 | static int verbose = 0; |
48 | static bool quiet = false; | ||
49 | static char *owarn = "60"; | ||
50 | static char *ocrit = "120"; | ||
51 | static int time_offset = 0; | ||
52 | 49 | ||
53 | static int process_arguments(int, char **); | 50 | typedef struct { |
54 | static thresholds *offset_thresholds = NULL; | 51 | int errorcode; |
52 | check_ntp_time_config config; | ||
53 | } check_ntp_time_config_wrapper; | ||
54 | static check_ntp_time_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
55 | |||
55 | static void print_help(void); | 56 | static void print_help(void); |
56 | void print_usage(void); | 57 | void print_usage(void); |
57 | 58 | ||
@@ -159,7 +160,7 @@ typedef struct { | |||
159 | #define EPOCHDIFF 0x83aa7e80UL | 160 | #define EPOCHDIFF 0x83aa7e80UL |
160 | 161 | ||
161 | /* extract a 32-bit ntp fixed point number into a double */ | 162 | /* extract a 32-bit ntp fixed point number into a double */ |
162 | #define NTP32asDOUBLE(x) (ntohs(L16(x)) + (double)ntohs(R16(x)) / 65536.0) | 163 | #define NTP32asDOUBLE(x) (ntohs(L16(x)) + ((double)ntohs(R16(x)) / 65536.0)) |
163 | 164 | ||
164 | /* likewise for a 64-bit ntp fp number */ | 165 | /* likewise for a 64-bit ntp fp number */ |
165 | #define NTP64asDOUBLE(n) \ | 166 | #define NTP64asDOUBLE(n) \ |
@@ -208,56 +209,52 @@ typedef struct { | |||
208 | } while (0); | 209 | } while (0); |
209 | 210 | ||
210 | /* calculate the offset of the local clock */ | 211 | /* calculate the offset of the local clock */ |
211 | static inline double calc_offset(const ntp_message *m, const struct timeval *t) { | 212 | static inline double calc_offset(const ntp_message *message, const struct timeval *time_value) { |
212 | double client_tx = NTP64asDOUBLE(m->origts); | 213 | double client_tx = NTP64asDOUBLE(message->origts); |
213 | double peer_rx = NTP64asDOUBLE(m->rxts); | 214 | double peer_rx = NTP64asDOUBLE(message->rxts); |
214 | double peer_tx = NTP64asDOUBLE(m->txts); | 215 | double peer_tx = NTP64asDOUBLE(message->txts); |
215 | double client_rx = TVasDOUBLE((*t)); | 216 | double client_rx = TVasDOUBLE((*time_value)); |
216 | return (.5 * ((peer_tx - client_rx) + (peer_rx - client_tx))); | 217 | return (((peer_tx - client_rx) + (peer_rx - client_tx)) / 2); |
217 | } | 218 | } |
218 | 219 | ||
219 | /* print out a ntp packet in human readable/debuggable format */ | 220 | /* print out a ntp packet in human readable/debuggable format */ |
220 | void print_ntp_message(const ntp_message *p) { | 221 | void print_ntp_message(const ntp_message *message) { |
221 | struct timeval ref; | 222 | struct timeval ref; |
222 | struct timeval orig; | 223 | struct timeval orig; |
223 | struct timeval rx; | ||
224 | struct timeval tx; | ||
225 | 224 | ||
226 | NTP64toTV(p->refts, ref); | 225 | NTP64toTV(message->refts, ref); |
227 | NTP64toTV(p->origts, orig); | 226 | NTP64toTV(message->origts, orig); |
228 | NTP64toTV(p->rxts, rx); | ||
229 | NTP64toTV(p->txts, tx); | ||
230 | 227 | ||
231 | printf("packet contents:\n"); | 228 | printf("packet contents:\n"); |
232 | printf("\tflags: 0x%.2x\n", p->flags); | 229 | printf("\tflags: 0x%.2x\n", message->flags); |
233 | printf("\t li=%d (0x%.2x)\n", LI(p->flags), p->flags & LI_MASK); | 230 | printf("\t li=%d (0x%.2x)\n", LI(message->flags), message->flags & LI_MASK); |
234 | printf("\t vn=%d (0x%.2x)\n", VN(p->flags), p->flags & VN_MASK); | 231 | printf("\t vn=%d (0x%.2x)\n", VN(message->flags), message->flags & VN_MASK); |
235 | printf("\t mode=%d (0x%.2x)\n", MODE(p->flags), p->flags & MODE_MASK); | 232 | printf("\t mode=%d (0x%.2x)\n", MODE(message->flags), message->flags & MODE_MASK); |
236 | printf("\tstratum = %d\n", p->stratum); | 233 | printf("\tstratum = %d\n", message->stratum); |
237 | printf("\tpoll = %g\n", pow(2, p->poll)); | 234 | printf("\tpoll = %g\n", pow(2, message->poll)); |
238 | printf("\tprecision = %g\n", pow(2, p->precision)); | 235 | printf("\tprecision = %g\n", pow(2, message->precision)); |
239 | printf("\trtdelay = %-.16g\n", NTP32asDOUBLE(p->rtdelay)); | 236 | printf("\trtdelay = %-.16g\n", NTP32asDOUBLE(message->rtdelay)); |
240 | printf("\trtdisp = %-.16g\n", NTP32asDOUBLE(p->rtdisp)); | 237 | printf("\trtdisp = %-.16g\n", NTP32asDOUBLE(message->rtdisp)); |
241 | printf("\trefid = %x\n", p->refid); | 238 | printf("\trefid = %x\n", message->refid); |
242 | printf("\trefts = %-.16g\n", NTP64asDOUBLE(p->refts)); | 239 | printf("\trefts = %-.16g\n", NTP64asDOUBLE(message->refts)); |
243 | printf("\torigts = %-.16g\n", NTP64asDOUBLE(p->origts)); | 240 | printf("\torigts = %-.16g\n", NTP64asDOUBLE(message->origts)); |
244 | printf("\trxts = %-.16g\n", NTP64asDOUBLE(p->rxts)); | 241 | printf("\trxts = %-.16g\n", NTP64asDOUBLE(message->rxts)); |
245 | printf("\ttxts = %-.16g\n", NTP64asDOUBLE(p->txts)); | 242 | printf("\ttxts = %-.16g\n", NTP64asDOUBLE(message->txts)); |
246 | } | 243 | } |
247 | 244 | ||
248 | void setup_request(ntp_message *p) { | 245 | void setup_request(ntp_message *message) { |
249 | memset(p, 0, sizeof(ntp_message)); | 246 | memset(message, 0, sizeof(ntp_message)); |
250 | LI_SET(p->flags, LI_ALARM); | 247 | LI_SET(message->flags, LI_ALARM); |
251 | VN_SET(p->flags, 4); | 248 | VN_SET(message->flags, 4); |
252 | MODE_SET(p->flags, MODE_CLIENT); | 249 | MODE_SET(message->flags, MODE_CLIENT); |
253 | p->poll = 4; | 250 | message->poll = 4; |
254 | p->precision = (int8_t)0xfa; | 251 | message->precision = (int8_t)0xfa; |
255 | L16(p->rtdelay) = htons(1); | 252 | L16(message->rtdelay) = htons(1); |
256 | L16(p->rtdisp) = htons(1); | 253 | L16(message->rtdisp) = htons(1); |
257 | 254 | ||
258 | struct timeval t; | 255 | struct timeval t; |
259 | gettimeofday(&t, NULL); | 256 | gettimeofday(&t, NULL); |
260 | TVtoNTP64(t, p->txts); | 257 | TVtoNTP64(t, message->txts); |
261 | } | 258 | } |
262 | 259 | ||
263 | /* select the "best" server from a list of servers, and return its index. | 260 | /* select the "best" server from a list of servers, and return its index. |
@@ -273,14 +270,16 @@ int best_offset_server(const ntp_server_results *slist, int nservers) { | |||
273 | * stratum 0 is for reference clocks so no NTP server should ever report | 270 | * stratum 0 is for reference clocks so no NTP server should ever report |
274 | * a stratum 0 */ | 271 | * a stratum 0 */ |
275 | if (slist[cserver].stratum == 0) { | 272 | if (slist[cserver].stratum == 0) { |
276 | if (verbose) | 273 | if (verbose) { |
277 | printf("discarding peer %d: stratum=%d\n", cserver, slist[cserver].stratum); | 274 | printf("discarding peer %d: stratum=%d\n", cserver, slist[cserver].stratum); |
275 | } | ||
278 | continue; | 276 | continue; |
279 | } | 277 | } |
280 | /* Sort out servers with error flags */ | 278 | /* Sort out servers with error flags */ |
281 | if (LI(slist[cserver].flags) == LI_ALARM) { | 279 | if (LI(slist[cserver].flags) == LI_ALARM) { |
282 | if (verbose) | 280 | if (verbose) { |
283 | printf("discarding peer %d: flags=%d\n", cserver, LI(slist[cserver].flags)); | 281 | printf("discarding peer %d: flags=%d\n", cserver, LI(slist[cserver].flags)); |
282 | } | ||
284 | continue; | 283 | continue; |
285 | } | 284 | } |
286 | 285 | ||
@@ -322,7 +321,7 @@ int best_offset_server(const ntp_server_results *slist, int nservers) { | |||
322 | * we don't waste time sitting around waiting for single packets. | 321 | * we don't waste time sitting around waiting for single packets. |
323 | * - we also "manually" handle resolving host names and connecting, because | 322 | * - we also "manually" handle resolving host names and connecting, because |
324 | * we have to do it in a way that our lazy macros don't handle currently :( */ | 323 | * we have to do it in a way that our lazy macros don't handle currently :( */ |
325 | double offset_request(const char *host, int *status) { | 324 | double offset_request(const char *host, const char *port, mp_state_enum *status, int time_offset) { |
326 | /* setup hints to only return results from getaddrinfo that we'd like */ | 325 | /* setup hints to only return results from getaddrinfo that we'd like */ |
327 | struct addrinfo hints; | 326 | struct addrinfo hints; |
328 | memset(&hints, 0, sizeof(struct addrinfo)); | 327 | memset(&hints, 0, sizeof(struct addrinfo)); |
@@ -331,39 +330,43 @@ double offset_request(const char *host, int *status) { | |||
331 | hints.ai_socktype = SOCK_DGRAM; | 330 | hints.ai_socktype = SOCK_DGRAM; |
332 | 331 | ||
333 | /* fill in ai with the list of hosts resolved by the host name */ | 332 | /* fill in ai with the list of hosts resolved by the host name */ |
334 | struct addrinfo *ai = NULL; | 333 | struct addrinfo *addresses = NULL; |
335 | int ga_result = getaddrinfo(host, port, &hints, &ai); | 334 | int ga_result = getaddrinfo(host, port, &hints, &addresses); |
336 | if (ga_result != 0) { | 335 | if (ga_result != 0) { |
337 | die(STATE_UNKNOWN, "error getting address for %s: %s\n", host, gai_strerror(ga_result)); | 336 | die(STATE_UNKNOWN, "error getting address for %s: %s\n", host, gai_strerror(ga_result)); |
338 | } | 337 | } |
339 | 338 | ||
340 | /* count the number of returned hosts, and allocate stuff accordingly */ | 339 | /* count the number of returned hosts, and allocate stuff accordingly */ |
341 | int num_hosts = 0; | 340 | size_t num_hosts = 0; |
342 | for (struct addrinfo *ai_tmp = ai; ai_tmp != NULL; ai_tmp = ai_tmp->ai_next) { | 341 | for (struct addrinfo *ai_tmp = addresses; ai_tmp != NULL; ai_tmp = ai_tmp->ai_next) { |
343 | num_hosts++; | 342 | num_hosts++; |
344 | } | 343 | } |
345 | 344 | ||
346 | ntp_message *req = (ntp_message *)malloc(sizeof(ntp_message) * num_hosts); | 345 | ntp_message *req = (ntp_message *)malloc(sizeof(ntp_message) * num_hosts); |
347 | 346 | ||
348 | if (req == NULL) | 347 | if (req == NULL) { |
349 | die(STATE_UNKNOWN, "can not allocate ntp message array"); | 348 | die(STATE_UNKNOWN, "can not allocate ntp message array"); |
349 | } | ||
350 | int *socklist = (int *)malloc(sizeof(int) * num_hosts); | 350 | int *socklist = (int *)malloc(sizeof(int) * num_hosts); |
351 | 351 | ||
352 | if (socklist == NULL) | 352 | if (socklist == NULL) { |
353 | die(STATE_UNKNOWN, "can not allocate socket array"); | 353 | die(STATE_UNKNOWN, "can not allocate socket array"); |
354 | } | ||
354 | 355 | ||
355 | struct pollfd *ufds = (struct pollfd *)malloc(sizeof(struct pollfd) * num_hosts); | 356 | struct pollfd *ufds = (struct pollfd *)malloc(sizeof(struct pollfd) * num_hosts); |
356 | if (ufds == NULL) | 357 | if (ufds == NULL) { |
357 | die(STATE_UNKNOWN, "can not allocate socket array"); | 358 | die(STATE_UNKNOWN, "can not allocate socket array"); |
359 | } | ||
358 | 360 | ||
359 | ntp_server_results *servers = (ntp_server_results *)malloc(sizeof(ntp_server_results) * num_hosts); | 361 | ntp_server_results *servers = (ntp_server_results *)malloc(sizeof(ntp_server_results) * num_hosts); |
360 | if (servers == NULL) | 362 | if (servers == NULL) { |
361 | die(STATE_UNKNOWN, "can not allocate server array"); | 363 | die(STATE_UNKNOWN, "can not allocate server array"); |
364 | } | ||
362 | memset(servers, 0, sizeof(ntp_server_results) * num_hosts); | 365 | memset(servers, 0, sizeof(ntp_server_results) * num_hosts); |
363 | DBG(printf("Found %d peers to check\n", num_hosts)); | 366 | DBG(printf("Found %zu peers to check\n", num_hosts)); |
364 | 367 | ||
365 | /* setup each socket for writing, and the corresponding struct pollfd */ | 368 | /* setup each socket for writing, and the corresponding struct pollfd */ |
366 | struct addrinfo *ai_tmp = ai; | 369 | struct addrinfo *ai_tmp = addresses; |
367 | for (int i = 0; ai_tmp; i++) { | 370 | for (int i = 0; ai_tmp; i++) { |
368 | socklist[i] = socket(ai_tmp->ai_family, SOCK_DGRAM, IPPROTO_UDP); | 371 | socklist[i] = socket(ai_tmp->ai_family, SOCK_DGRAM, IPPROTO_UDP); |
369 | if (socklist[i] == -1) { | 372 | if (socklist[i] == -1) { |
@@ -389,7 +392,7 @@ double offset_request(const char *host, int *status) { | |||
389 | time_t start_ts = 0; | 392 | time_t start_ts = 0; |
390 | time_t now_time = 0; | 393 | time_t now_time = 0; |
391 | now_time = start_ts = time(NULL); | 394 | now_time = start_ts = time(NULL); |
392 | int servers_completed = 0; | 395 | size_t servers_completed = 0; |
393 | bool one_read = false; | 396 | bool one_read = false; |
394 | while (servers_completed < num_hosts && now_time - start_ts <= socket_timeout / 2) { | 397 | while (servers_completed < num_hosts && now_time - start_ts <= socket_timeout / 2) { |
395 | /* loop through each server and find each one which hasn't | 398 | /* loop through each server and find each one which hasn't |
@@ -398,12 +401,14 @@ double offset_request(const char *host, int *status) { | |||
398 | * and update the "waiting" timestamp with the current time. */ | 401 | * and update the "waiting" timestamp with the current time. */ |
399 | now_time = time(NULL); | 402 | now_time = time(NULL); |
400 | 403 | ||
401 | for (int i = 0; i < num_hosts; i++) { | 404 | for (size_t i = 0; i < num_hosts; i++) { |
402 | if (servers[i].waiting < now_time && servers[i].num_responses < AVG_NUM) { | 405 | if (servers[i].waiting < now_time && servers[i].num_responses < AVG_NUM) { |
403 | if (verbose && servers[i].waiting != 0) | 406 | if (verbose && servers[i].waiting != 0) { |
404 | printf("re-"); | 407 | printf("re-"); |
405 | if (verbose) | 408 | } |
406 | printf("sending request to peer %d\n", i); | 409 | if (verbose) { |
410 | printf("sending request to peer %zu\n", i); | ||
411 | } | ||
407 | setup_request(&req[i]); | 412 | setup_request(&req[i]); |
408 | write(socklist[i], &req[i], sizeof(ntp_message)); | 413 | write(socklist[i], &req[i], sizeof(ntp_message)); |
409 | servers[i].waiting = now_time; | 414 | servers[i].waiting = now_time; |
@@ -419,10 +424,10 @@ double offset_request(const char *host, int *status) { | |||
419 | } | 424 | } |
420 | 425 | ||
421 | /* read from any sockets with pending data */ | 426 | /* read from any sockets with pending data */ |
422 | for (int i = 0; servers_readable && i < num_hosts; i++) { | 427 | for (size_t i = 0; servers_readable && i < num_hosts; i++) { |
423 | if (ufds[i].revents & POLLIN && servers[i].num_responses < AVG_NUM) { | 428 | if (ufds[i].revents & POLLIN && servers[i].num_responses < AVG_NUM) { |
424 | if (verbose) { | 429 | if (verbose) { |
425 | printf("response from peer %d: ", i); | 430 | printf("response from peer %zu: ", i); |
426 | } | 431 | } |
427 | 432 | ||
428 | read(ufds[i].fd, &req[i], sizeof(ntp_message)); | 433 | read(ufds[i].fd, &req[i], sizeof(ntp_message)); |
@@ -442,14 +447,15 @@ double offset_request(const char *host, int *status) { | |||
442 | servers[i].flags = req[i].flags; | 447 | servers[i].flags = req[i].flags; |
443 | servers_readable--; | 448 | servers_readable--; |
444 | one_read = true; | 449 | one_read = true; |
445 | if (servers[i].num_responses == AVG_NUM) | 450 | if (servers[i].num_responses == AVG_NUM) { |
446 | servers_completed++; | 451 | servers_completed++; |
452 | } | ||
447 | } | 453 | } |
448 | } | 454 | } |
449 | /* lather, rinse, repeat. */ | 455 | /* lather, rinse, repeat. */ |
450 | } | 456 | } |
451 | 457 | ||
452 | if (one_read == false) { | 458 | if (!one_read) { |
453 | die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n"); | 459 | die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n"); |
454 | } | 460 | } |
455 | 461 | ||
@@ -467,21 +473,22 @@ double offset_request(const char *host, int *status) { | |||
467 | } | 473 | } |
468 | 474 | ||
469 | /* cleanup */ | 475 | /* cleanup */ |
470 | for (int j = 0; j < num_hosts; j++) { | 476 | for (size_t j = 0; j < num_hosts; j++) { |
471 | close(socklist[j]); | 477 | close(socklist[j]); |
472 | } | 478 | } |
473 | free(socklist); | 479 | free(socklist); |
474 | free(ufds); | 480 | free(ufds); |
475 | free(servers); | 481 | free(servers); |
476 | free(req); | 482 | free(req); |
477 | freeaddrinfo(ai); | 483 | freeaddrinfo(addresses); |
478 | 484 | ||
479 | if (verbose) | 485 | if (verbose) { |
480 | printf("overall average offset: %.10g\n", avg_offset); | 486 | printf("overall average offset: %.10g\n", avg_offset); |
487 | } | ||
481 | return avg_offset; | 488 | return avg_offset; |
482 | } | 489 | } |
483 | 490 | ||
484 | int process_arguments(int argc, char **argv) { | 491 | check_ntp_time_config_wrapper process_arguments(int argc, char **argv) { |
485 | static struct option longopts[] = {{"version", no_argument, 0, 'V'}, | 492 | static struct option longopts[] = {{"version", no_argument, 0, 'V'}, |
486 | {"help", no_argument, 0, 'h'}, | 493 | {"help", no_argument, 0, 'h'}, |
487 | {"verbose", no_argument, 0, 'v'}, | 494 | {"verbose", no_argument, 0, 'v'}, |
@@ -496,14 +503,24 @@ int process_arguments(int argc, char **argv) { | |||
496 | {"port", required_argument, 0, 'p'}, | 503 | {"port", required_argument, 0, 'p'}, |
497 | {0, 0, 0, 0}}; | 504 | {0, 0, 0, 0}}; |
498 | 505 | ||
499 | if (argc < 2) | 506 | if (argc < 2) { |
500 | usage("\n"); | 507 | usage("\n"); |
508 | } | ||
509 | |||
510 | check_ntp_time_config_wrapper result = { | ||
511 | .errorcode = OK, | ||
512 | .config = check_ntp_time_config_init(), | ||
513 | }; | ||
514 | |||
515 | char *owarn = "60"; | ||
516 | char *ocrit = "120"; | ||
501 | 517 | ||
502 | while (true) { | 518 | while (true) { |
503 | int option = 0; | 519 | int option = 0; |
504 | int option_char = getopt_long(argc, argv, "Vhv46qw:c:t:H:p:o:", longopts, &option); | 520 | int option_char = getopt_long(argc, argv, "Vhv46qw:c:t:H:p:o:", longopts, &option); |
505 | if (option_char == -1 || option_char == EOF || option_char == 1) | 521 | if (option_char == -1 || option_char == EOF || option_char == 1) { |
506 | break; | 522 | break; |
523 | } | ||
507 | 524 | ||
508 | switch (option_char) { | 525 | switch (option_char) { |
509 | case 'h': | 526 | case 'h': |
@@ -518,7 +535,7 @@ int process_arguments(int argc, char **argv) { | |||
518 | verbose++; | 535 | verbose++; |
519 | break; | 536 | break; |
520 | case 'q': | 537 | case 'q': |
521 | quiet = true; | 538 | result.config.quiet = true; |
522 | break; | 539 | break; |
523 | case 'w': | 540 | case 'w': |
524 | owarn = optarg; | 541 | owarn = optarg; |
@@ -527,18 +544,19 @@ int process_arguments(int argc, char **argv) { | |||
527 | ocrit = optarg; | 544 | ocrit = optarg; |
528 | break; | 545 | break; |
529 | case 'H': | 546 | case 'H': |
530 | if (!is_host(optarg)) | 547 | if (!is_host(optarg)) { |
531 | usage2(_("Invalid hostname/address"), optarg); | 548 | usage2(_("Invalid hostname/address"), optarg); |
532 | server_address = strdup(optarg); | 549 | } |
550 | result.config.server_address = strdup(optarg); | ||
533 | break; | 551 | break; |
534 | case 'p': | 552 | case 'p': |
535 | port = strdup(optarg); | 553 | result.config.port = strdup(optarg); |
536 | break; | 554 | break; |
537 | case 't': | 555 | case 't': |
538 | socket_timeout = atoi(optarg); | 556 | socket_timeout = atoi(optarg); |
539 | break; | 557 | break; |
540 | case 'o': | 558 | case 'o': |
541 | time_offset = atoi(optarg); | 559 | result.config.time_offset = atoi(optarg); |
542 | break; | 560 | break; |
543 | case '4': | 561 | case '4': |
544 | address_family = AF_INET; | 562 | address_family = AF_INET; |
@@ -557,14 +575,16 @@ int process_arguments(int argc, char **argv) { | |||
557 | } | 575 | } |
558 | } | 576 | } |
559 | 577 | ||
560 | if (server_address == NULL) { | 578 | if (result.config.server_address == NULL) { |
561 | usage4(_("Hostname was not supplied")); | 579 | usage4(_("Hostname was not supplied")); |
562 | } | 580 | } |
563 | 581 | ||
564 | return 0; | 582 | set_thresholds(&result.config.offset_thresholds, owarn, ocrit); |
583 | |||
584 | return result; | ||
565 | } | 585 | } |
566 | 586 | ||
567 | char *perfd_offset(double offset) { | 587 | char *perfd_offset(double offset, thresholds *offset_thresholds) { |
568 | return fperfdata("offset", offset, "s", true, offset_thresholds->warning->end, true, offset_thresholds->critical->end, false, 0, false, | 588 | return fperfdata("offset", offset, "s", true, offset_thresholds->warning->end, true, offset_thresholds->critical->end, false, 0, false, |
569 | 0); | 589 | 0); |
570 | } | 590 | } |
@@ -577,10 +597,13 @@ int main(int argc, char *argv[]) { | |||
577 | /* Parse extra opts if any */ | 597 | /* Parse extra opts if any */ |
578 | argv = np_extra_opts(&argc, argv, progname); | 598 | argv = np_extra_opts(&argc, argv, progname); |
579 | 599 | ||
580 | if (process_arguments(argc, argv) == ERROR) | 600 | check_ntp_time_config_wrapper tmp_config = process_arguments(argc, argv); |
601 | |||
602 | if (tmp_config.errorcode == ERROR) { | ||
581 | usage4(_("Could not parse arguments")); | 603 | usage4(_("Could not parse arguments")); |
604 | } | ||
582 | 605 | ||
583 | set_thresholds(&offset_thresholds, owarn, ocrit); | 606 | const check_ntp_time_config config = tmp_config.config; |
584 | 607 | ||
585 | /* initialize alarm signal handling */ | 608 | /* initialize alarm signal handling */ |
586 | signal(SIGALRM, socket_timeout_alarm_handler); | 609 | signal(SIGALRM, socket_timeout_alarm_handler); |
@@ -588,13 +611,13 @@ int main(int argc, char *argv[]) { | |||
588 | /* set socket timeout */ | 611 | /* set socket timeout */ |
589 | alarm(socket_timeout); | 612 | alarm(socket_timeout); |
590 | 613 | ||
591 | int offset_result = STATE_OK; | 614 | mp_state_enum offset_result = STATE_OK; |
592 | int result = STATE_OK; | 615 | mp_state_enum result = STATE_OK; |
593 | double offset = offset_request(server_address, &offset_result); | 616 | double offset = offset_request(config.server_address, config.port, &offset_result, config.time_offset); |
594 | if (offset_result == STATE_UNKNOWN) { | 617 | if (offset_result == STATE_UNKNOWN) { |
595 | result = ((!quiet) ? STATE_UNKNOWN : STATE_CRITICAL); | 618 | result = ((!config.quiet) ? STATE_UNKNOWN : STATE_CRITICAL); |
596 | } else { | 619 | } else { |
597 | result = get_status(fabs(offset), offset_thresholds); | 620 | result = get_status(fabs(offset), config.offset_thresholds); |
598 | } | 621 | } |
599 | 622 | ||
600 | char *result_line; | 623 | char *result_line; |
@@ -619,13 +642,14 @@ int main(int argc, char *argv[]) { | |||
619 | xasprintf(&perfdata_line, ""); | 642 | xasprintf(&perfdata_line, ""); |
620 | } else { | 643 | } else { |
621 | xasprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset); | 644 | xasprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset); |
622 | xasprintf(&perfdata_line, "%s", perfd_offset(offset)); | 645 | xasprintf(&perfdata_line, "%s", perfd_offset(offset, config.offset_thresholds)); |
623 | } | 646 | } |
624 | printf("%s|%s\n", result_line, perfdata_line); | 647 | printf("%s|%s\n", result_line, perfdata_line); |
625 | 648 | ||
626 | if (server_address != NULL) | 649 | if (config.server_address != NULL) { |
627 | free(server_address); | 650 | free(config.server_address); |
628 | return result; | 651 | } |
652 | exit(result); | ||
629 | } | 653 | } |
630 | 654 | ||
631 | void print_help(void) { | 655 | void print_help(void) { |
diff --git a/plugins/check_ntp_time.d/config.h b/plugins/check_ntp_time.d/config.h new file mode 100644 index 00000000..99dabbbd --- /dev/null +++ b/plugins/check_ntp_time.d/config.h | |||
@@ -0,0 +1,28 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include "../../config.h" | ||
4 | #include "thresholds.h" | ||
5 | #include <stddef.h> | ||
6 | |||
7 | typedef struct { | ||
8 | char *server_address; | ||
9 | char *port; | ||
10 | |||
11 | bool quiet; | ||
12 | int time_offset; | ||
13 | |||
14 | thresholds *offset_thresholds; | ||
15 | } check_ntp_time_config; | ||
16 | |||
17 | check_ntp_time_config check_ntp_time_config_init() { | ||
18 | check_ntp_time_config tmp = { | ||
19 | .server_address = NULL, | ||
20 | .port = "123", | ||
21 | |||
22 | .quiet = false, | ||
23 | .time_offset = 0, | ||
24 | |||
25 | .offset_thresholds = NULL, | ||
26 | }; | ||
27 | return tmp; | ||
28 | } | ||