[monitoring-plugins] Split DBG_PRINT functions into printf and print ...
GitHub
git at monitoring-plugins.org
Wed Oct 21 11:50:13 CEST 2026
Module: monitoring-plugins
Branch: master
Commit: 161b760ff01c7d61f0fc2225e6074a6bb1214c9d
Author: Lorenz Kästle <12514511+RincewindsHat at users.noreply.github.com>
Committer: GitHub <noreply at github.com>
Date: Wed Oct 21 11:45:30 2026 +0200
URL: https://www.monitoring-plugins.org/repositories/monitoring-plugins/commit/?id=161b760f
Split DBG_PRINT functions into printf and print (#2378)
* Split DBG_PRINT functions into printf and print
This commit fixes problems with the previously too complex DBG_PRINT
macros with broke on some systems.
The fix is replacing them with two versions, where one is printf like
and expects a format string and arguments and a plain PRINT version
which just takes a string.
---------
Co-authored-by: Lorenz Kästle <lorenz.kaestle+gitub-mopl at mailbox.org>
Co-authored-by: Alvar <github.jyf300hj at 0x21.biz>
---
plugins/check_ntp_peer.c | 10 +++++-----
plugins/check_ntp_time.c | 34 +++++++++++++++++-----------------
plugins/utils.h | 18 +++++++++++-------
3 files changed, 33 insertions(+), 29 deletions(-)
diff --git a/plugins/check_ntp_peer.c b/plugins/check_ntp_peer.c
index 71d71d20..10017968 100644
--- a/plugins/check_ntp_peer.c
+++ b/plugins/check_ntp_peer.c
@@ -297,7 +297,7 @@ ntp_request_result ntp_request(const check_ntp_peer_config config) {
}
}
- DBG_PRINT_1("%d candidate peers available\n", num_candidates);
+ DBG_PRINTF_1("%d candidate peers available\n", num_candidates);
if (result.syncsource_found) {
DBG_PRINT_1("synchronization source found\n");
}
@@ -366,7 +366,7 @@ ntp_request_result ntp_request(const check_ntp_peer_config config) {
}
}
- DBG_PRINT_1("Server responded: >>>%s<<<\n", data);
+ DBG_PRINTF_1("Server responded: >>>%s<<<\n", data);
double tmp_offset = 0;
char *value;
@@ -385,7 +385,7 @@ ntp_request_result ntp_request(const check_ntp_peer_config config) {
if (value == NULL || value == nptr) {
DBG_PRINT_1("error: unable to read server offset response.\n");
} else {
- DBG_PRINT_1("%.10g\n", tmp_offset);
+ DBG_PRINTF_1("%.10g\n", tmp_offset);
if (result.offset_result == STATE_UNKNOWN ||
fabs(tmp_offset) < fabs(result.offset)) {
result.offset = tmp_offset;
@@ -413,7 +413,7 @@ ntp_request_result ntp_request(const check_ntp_peer_config config) {
DBG_PRINT_1("error: unable to read server jitter/dispersion response.\n");
result.jitter = -1;
} else {
- DBG_PRINT_1("%.10g\n", result.jitter);
+ DBG_PRINTF_1("%.10g\n", result.jitter);
}
}
@@ -430,7 +430,7 @@ ntp_request_result ntp_request(const check_ntp_peer_config config) {
DBG_PRINT_1("error: unable to read server stratum response.\n");
result.stratum = -1;
} else {
- DBG_PRINT_1("%li\n", result.stratum);
+ DBG_PRINTF_1("%li\n", result.stratum);
}
}
} /* if (PEER_SEL(peers[i].status) >= min_peer_sel) */
diff --git a/plugins/check_ntp_time.c b/plugins/check_ntp_time.c
index 60c24d31..4972fac8 100644
--- a/plugins/check_ntp_time.c
+++ b/plugins/check_ntp_time.c
@@ -270,42 +270,42 @@ static int best_offset_server(const ntp_server_results *slist, int nservers) {
* stratum 0 is for reference clocks so no NTP server should ever report
* a stratum 0 */
if (slist[cserver].stratum == 0) {
- DBG_PRINT_1("discarding peer %d: stratum=%d\n", cserver, slist[cserver].stratum);
+ DBG_PRINTF_1("discarding peer %d: stratum=%d\n", cserver, slist[cserver].stratum);
continue;
}
/* Sort out servers with error flags */
if (LI(slist[cserver].flags) == LI_ALARM) {
- DBG_PRINT_1("discarding peer %d: flags=%d\n", cserver, LI(slist[cserver].flags));
+ DBG_PRINTF_1("discarding peer %d: flags=%d\n", cserver, LI(slist[cserver].flags));
continue;
}
/* If we don't have a server yet, use the first one */
if (best_server_index == -1) {
best_server_index = cserver;
- DBG_PRINT_1("using peer %d as our first candidate\n", best_server_index);
+ DBG_PRINTF_1("using peer %d as our first candidate\n", best_server_index);
continue;
}
/* compare the server to the best one we've seen so far */
/* does it have an equal or better stratum? */
- DBG_PRINT_1("comparing peer %d with peer %d\n", cserver, best_server_index);
+ DBG_PRINTF_1("comparing peer %d with peer %d\n", cserver, best_server_index);
if (slist[cserver].stratum <= slist[best_server_index].stratum) {
- DBG_PRINT_1("stratum for peer %d <= peer %d\n", cserver, best_server_index);
+ DBG_PRINTF_1("stratum for peer %d <= peer %d\n", cserver, best_server_index);
/* does it have an equal or better dispersion? */
if (slist[cserver].rtdisp <= slist[best_server_index].rtdisp) {
- DBG_PRINT_1("dispersion for peer %d <= peer %d\n", cserver, best_server_index);
+ DBG_PRINTF_1("dispersion for peer %d <= peer %d\n", cserver, best_server_index);
/* does it have a better rtdelay? */
if (slist[cserver].rtdelay < slist[best_server_index].rtdelay) {
- DBG_PRINT_1("rtdelay for peer %d < peer %d\n", cserver, best_server_index);
+ DBG_PRINTF_1("rtdelay for peer %d < peer %d\n", cserver, best_server_index);
best_server_index = cserver;
- DBG_PRINT_1("peer %d is now our best candidate\n", best_server_index);
+ DBG_PRINTF_1("peer %d is now our best candidate\n", best_server_index);
}
}
}
}
if (best_server_index >= 0) {
- DBG_PRINT_1("best server selected: peer %d\n", best_server_index);
+ DBG_PRINTF_1("best server selected: peer %d\n", best_server_index);
return best_server_index;
}
DBG_PRINT_1("no peers meeting synchronization criteria :(\n");
@@ -374,13 +374,13 @@ static offset_request_wrapper offset_request(const char *host, const char *port,
die(STATE_UNKNOWN, "can not allocate server array");
}
memset(servers, 0, sizeof(ntp_server_results) * num_hosts);
- DBG_PRINT_1("Found %zu peers to check\n", num_hosts);
+ DBG_PRINTF_1("Found %zu peers to check\n", num_hosts);
/* setup each socket for writing, and the corresponding struct pollfd */
if (is_socket) {
socklist[0] = socket(AF_UNIX, SOCK_STREAM, 0);
if (socklist[0] == -1) {
- DBG_PRINT_1("can't create socket: %s\n", strerror(errno));
+ DBG_PRINTF_1("can't create socket: %s\n", strerror(errno));
die(STATE_UNKNOWN, "can not create new socket\n");
}
@@ -398,7 +398,7 @@ static offset_request_wrapper offset_request(const char *host, const char *port,
answering in time. This also would break for dual ipv4/6 stacked
ntp servers when the client only supports on of them.
*/
- DBG_PRINT_1("can't create socket connection on peer %i: %s\n", 0, strerror(errno));
+ DBG_PRINTF_1("can't create socket connection on peer %i: %s\n", 0, strerror(errno));
} else {
ufds[0].fd = socklist[0];
ufds[0].events = POLLIN;
@@ -417,7 +417,7 @@ static offset_request_wrapper offset_request(const char *host, const char *port,
answering in time. This also would break for dual ipv4/6 stacked
ntp servers when the client only supports on of them.
*/
- DBG_PRINT_1("can't create socket connection on peer %i: %s\n", i, strerror(errno));
+ DBG_PRINTF_1("can't create socket connection on peer %i: %s\n", i, strerror(errno));
} else {
ufds[i].fd = socklist[i];
ufds[i].events = POLLIN;
@@ -446,7 +446,7 @@ static offset_request_wrapper offset_request(const char *host, const char *port,
if (servers[i].waiting != 0) {
DBG_PRINT_1("re-");
}
- DBG_PRINT_1("sending request to peer %zu\n", i);
+ DBG_PRINTF_1("sending request to peer %zu\n", i);
setup_request(&req[i]);
// Delay sending a request to avoid triggering flooding mechanisms
@@ -472,7 +472,7 @@ static offset_request_wrapper offset_request(const char *host, const char *port,
/* read from any sockets with pending data */
for (size_t i = 0; servers_readable && i < num_hosts; i++) {
if (ufds[i].revents & POLLIN && servers[i].num_responses < AVG_NUM) {
- DBG_PRINT_1("response from peer %zu: ", i);
+ DBG_PRINTF_1("response from peer %zu: ", i);
read(ufds[i].fd, &req[i], sizeof(ntp_message));
@@ -482,7 +482,7 @@ static offset_request_wrapper offset_request(const char *host, const char *port,
int respnum = servers[i].num_responses++;
servers[i].offset[respnum] = calc_offset(&req[i], &recv_time) + time_offset;
- DBG_PRINT_1("offset %.10g\n", servers[i].offset[respnum]);
+ DBG_PRINTF_1("offset %.10g\n", servers[i].offset[respnum]);
servers[i].stratum = req[i].stratum;
servers[i].rtdisp = NTP32asDOUBLE(req[i].rtdisp);
@@ -535,7 +535,7 @@ static offset_request_wrapper offset_request(const char *host, const char *port,
free(req);
freeaddrinfo(addresses);
- DBG_PRINT_1("overall average offset: %.10g\n", avg_offset);
+ DBG_PRINTF_1("overall average offset: %.10g\n", avg_offset);
result.offset = avg_offset;
return result;
diff --git a/plugins/utils.h b/plugins/utils.h
index bc9847b1..09e66e36 100644
--- a/plugins/utils.h
+++ b/plugins/utils.h
@@ -193,18 +193,22 @@ For more information about these matters, see the file named COPYING.\n")
/* finally, a little helper or two for debugging: */
#define DBG(x) \
do { \
- if (verbose > 1) { \
+ if (verbose >= 1) { \
x; \
} \
} while (0);
-#define DBG_PRINT(x, ...) \
+#define DBG_PRINTF(verbosity, format, ...) \
do { \
- if (verbose > x) { \
- printf(0 __VA_OPT__(, ) __VA_ARGS__); \
+ if (verbose >= verbosity) { \
+ printf(format, __VA_ARGS__); \
} \
} while (0);
-#define DBG_PRINT_1(...) DBG_PRINT(1, 0 __VA_OPT__(, ) __VA_ARGS__);
-#define DBG_PRINT_2(...) DBG_PRINT(2, 0 __VA_OPT__(, ) __VA_ARGS__);
-#define DBG_PRINT_3(...) DBG_PRINT(3, 0 __VA_OPT__(, ) __VA_ARGS__);
+#define DBG_PRINTF_1(format, ...) DBG_PRINTF(1, format, __VA_ARGS__);
+#define DBG_PRINTF_2(format, ...) DBG_PRINTF(2, format, __VA_ARGS__);
+#define DBG_PRINTF_3(format, ...) DBG_PRINTF(3, format, __VA_ARGS__);
+
+#define DBG_PRINT_1(...) DBG_PRINTF(1, "%s", __VA_ARGS__);
+#define DBG_PRINT_2(...) DBG_PRINTF(2, "%s", __VA_ARGS__);
+#define DBG_PRINT_3(...) DBG_PRINTF(3, "%s", __VA_ARGS__);
More information about the Commits
mailing list