[monitoring-plugins] check_ntp_time: actually implement polling delay ...

GitHub git at monitoring-plugins.org
Thu Aug 20 09:10:14 CEST 2026


    Module: monitoring-plugins
    Branch: master
    Commit: fa36a83c7ab1dd66114294c6c30fac532f9eb90e
    Author: Lorenz Kästle <12514511+RincewindsHat at users.noreply.github.com>
 Committer: GitHub <noreply at github.com>
      Date: Thu Aug 20 09:09:21 2026 +0200
       URL: https://www.monitoring-plugins.org/repositories/monitoring-plugins/commit/?id=fa36a83c

check_ntp_time: actually implement polling delay (#2320)

check_ntp_time: actually implement polling delay

Apparently I forgot the actual implementaion of the
polling delay feature in 1372654e8a2d392db35aae8f62586d55319ccb3c

Therefor this patch adds it....

---

 plugins/check_ntp_time.c          | 31 ++++++++++++++++++++++++++++---
 plugins/check_ntp_time.d/config.h |  6 ++++--
 2 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/plugins/check_ntp_time.c b/plugins/check_ntp_time.c
index 5ad615c2..53fcbf46 100644
--- a/plugins/check_ntp_time.c
+++ b/plugins/check_ntp_time.c
@@ -45,6 +45,7 @@
 #include <netinet/in.h>
 #include <string.h>
 #include <sys/socket.h>
+#include <time.h>
 
 static int verbose = 0;
 
@@ -331,7 +332,8 @@ typedef struct {
 	mp_state_enum offset_result;
 	double offset;
 } offset_request_wrapper;
-static offset_request_wrapper offset_request(const char *host, const char *port, int time_offset) {
+static offset_request_wrapper offset_request(const char *host, const char *port, int time_offset,
+											 const struct timespec poll_delay) {
 	/* setup hints to only return results from getaddrinfo that we'd like */
 	struct addrinfo hints;
 	memset(&hints, 0, sizeof(struct addrinfo));
@@ -458,6 +460,14 @@ static offset_request_wrapper offset_request(const char *host, const char *port,
 					printf("sending request to peer %zu\n", i);
 				}
 				setup_request(&req[i]);
+
+				// Delay sending a request to avoid triggering flooding mechanisms
+				struct timespec remainder = {};
+				int sleep_ret = nanosleep(&poll_delay, &remainder);
+				while (sleep_ret != 0) {
+					sleep_ret = nanosleep(&remainder, &remainder);
+				}
+
 				write(socklist[i], &req[i], sizeof(ntp_message));
 				servers[i].waiting = now_time;
 				break;
@@ -477,7 +487,6 @@ static offset_request_wrapper offset_request(const char *host, const char *port,
 				if (verbose) {
 					printf("response from peer %zu: ", i);
 				}
-
 				read(ufds[i].fd, &req[i], sizeof(ntp_message));
 
 				struct timeval recv_time;
@@ -488,6 +497,7 @@ static offset_request_wrapper offset_request(const char *host, const char *port,
 				if (verbose) {
 					printf("offset %.10g\n", servers[i].offset[respnum]);
 				}
+
 				servers[i].stratum = req[i].stratum;
 				servers[i].rtdisp = NTP32asDOUBLE(req[i].rtdisp);
 				servers[i].rtdelay = NTP32asDOUBLE(req[i].rtdelay);
@@ -548,6 +558,7 @@ static check_ntp_time_config_wrapper process_arguments(int argc, char **argv) {
 
 	enum {
 		output_format_index = CHAR_MAX + 1,
+		polling_delay_index,
 	};
 
 	static struct option longopts[] = {{"version", no_argument, 0, 'V'},
@@ -562,6 +573,7 @@ static check_ntp_time_config_wrapper process_arguments(int argc, char **argv) {
 									   {"timeout", required_argument, 0, 't'},
 									   {"hostname", required_argument, 0, 'H'},
 									   {"port", required_argument, 0, 'p'},
+									   {"poll-delay", required_argument, 0, polling_delay_index},
 									   {"output-format", required_argument, 0, output_format_index},
 									   {0, 0, 0, 0}};
 
@@ -640,6 +652,17 @@ static check_ntp_time_config_wrapper process_arguments(int argc, char **argv) {
 		case 'o':
 			result.config.time_offset = atoi(optarg);
 			break;
+		case polling_delay_index: {
+			long tmp_time = (long)(1.0e9 * atof(optarg));
+			if (tmp_time < 0 || tmp_time > MAX_POLL) {
+				usage2(_("Invalid time"), optarg);
+			} else {
+				DBG(printf("Polling delay: %lu\n", tmp_time));
+
+				result.config.poll_delay.tv_sec = (tmp_time / 1000000000);
+				result.config.poll_delay.tv_nsec = (tmp_time % 1000000000);
+			}
+		} break;
 		case '4':
 			address_family = AF_INET;
 			break;
@@ -704,7 +727,7 @@ int main(int argc, char *argv[]) {
 
 	mp_subcheck sc_offset = mp_subcheck_init();
 	offset_request_wrapper offset_result =
-		offset_request(config.server_address, config.port, config.time_offset);
+		offset_request(config.server_address, config.port, config.time_offset, config.poll_delay);
 
 	if (offset_result.offset_result == STATE_UNKNOWN) {
 		sc_offset =
@@ -756,6 +779,8 @@ void print_help(void) {
 	printf("    %s\n", _("Offset to result in critical status (seconds)"));
 	printf(" %s\n", "-o, --time-offset=INTEGER");
 	printf("    %s\n", _("Expected offset of the ntp server relative to local server (seconds)"));
+	printf(" %s\n", " --poll-delay=DELAY");
+	printf("    %s\n", _("Delay between polling to avoid KOD response (seconds, 0.0 to 5.0, default 0.5 seconds)"));
 	printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
 	printf(UT_VERBOSE);
 	printf(UT_OUTPUT_FORMAT);
diff --git a/plugins/check_ntp_time.d/config.h b/plugins/check_ntp_time.d/config.h
index c1aa142c..81194a95 100644
--- a/plugins/check_ntp_time.d/config.h
+++ b/plugins/check_ntp_time.d/config.h
@@ -4,9 +4,11 @@
 #include "output.h"
 #include "thresholds.h"
 #include <stddef.h>
+#include <time.h>
 
 /* Time in microseconds to delay between polling to avoid a blocking response. */
-const long default_polling_delay = 500000L;
+const struct timespec default_polling_delay = {.tv_nsec = 500000000L, .tv_sec = 0};
+const long MAX_POLL = 5000000000L; // nanoseconds, 5 seconds
 
 typedef struct {
 	char *server_address;
@@ -18,7 +20,7 @@ typedef struct {
 	mp_thresholds offset_thresholds;
 
 	bool output_format_is_set;
-	long poll_delay;
+	struct timespec poll_delay;
 	mp_output_format output_format;
 } check_ntp_time_config;
 



More information about the Commits mailing list