From 59156e79c97c1f425142e6ddc7983d4364534cef Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 12 Mar 2025 01:08:18 +0100 Subject: check_ntp_time: clang-format --- plugins/check_ntp_time.c | 45 ++++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/plugins/check_ntp_time.c b/plugins/check_ntp_time.c index 703b69df..05c3d1e4 100644 --- a/plugins/check_ntp_time.c +++ b/plugins/check_ntp_time.c @@ -273,14 +273,16 @@ 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) { - if (verbose) + if (verbose) { printf("discarding peer %d: stratum=%d\n", cserver, slist[cserver].stratum); + } continue; } /* Sort out servers with error flags */ if (LI(slist[cserver].flags) == LI_ALARM) { - if (verbose) + if (verbose) { printf("discarding peer %d: flags=%d\n", cserver, LI(slist[cserver].flags)); + } continue; } @@ -345,20 +347,24 @@ double offset_request(const char *host, int *status) { ntp_message *req = (ntp_message *)malloc(sizeof(ntp_message) * num_hosts); - if (req == NULL) + if (req == NULL) { die(STATE_UNKNOWN, "can not allocate ntp message array"); + } int *socklist = (int *)malloc(sizeof(int) * num_hosts); - if (socklist == NULL) + if (socklist == NULL) { die(STATE_UNKNOWN, "can not allocate socket array"); + } struct pollfd *ufds = (struct pollfd *)malloc(sizeof(struct pollfd) * num_hosts); - if (ufds == NULL) + if (ufds == NULL) { die(STATE_UNKNOWN, "can not allocate socket array"); + } ntp_server_results *servers = (ntp_server_results *)malloc(sizeof(ntp_server_results) * num_hosts); - if (servers == NULL) + if (servers == NULL) { die(STATE_UNKNOWN, "can not allocate server array"); + } memset(servers, 0, sizeof(ntp_server_results) * num_hosts); DBG(printf("Found %d peers to check\n", num_hosts)); @@ -400,10 +406,12 @@ double offset_request(const char *host, int *status) { for (int i = 0; i < num_hosts; i++) { if (servers[i].waiting < now_time && servers[i].num_responses < AVG_NUM) { - if (verbose && servers[i].waiting != 0) + if (verbose && servers[i].waiting != 0) { printf("re-"); - if (verbose) + } + if (verbose) { printf("sending request to peer %d\n", i); + } setup_request(&req[i]); write(socklist[i], &req[i], sizeof(ntp_message)); servers[i].waiting = now_time; @@ -442,8 +450,9 @@ double offset_request(const char *host, int *status) { servers[i].flags = req[i].flags; servers_readable--; one_read = true; - if (servers[i].num_responses == AVG_NUM) + if (servers[i].num_responses == AVG_NUM) { servers_completed++; + } } } /* lather, rinse, repeat. */ @@ -476,8 +485,9 @@ double offset_request(const char *host, int *status) { free(req); freeaddrinfo(ai); - if (verbose) + if (verbose) { printf("overall average offset: %.10g\n", avg_offset); + } return avg_offset; } @@ -496,14 +506,16 @@ int process_arguments(int argc, char **argv) { {"port", required_argument, 0, 'p'}, {0, 0, 0, 0}}; - if (argc < 2) + if (argc < 2) { usage("\n"); + } while (true) { int option = 0; int option_char = getopt_long(argc, argv, "Vhv46qw:c:t:H:p:o:", longopts, &option); - if (option_char == -1 || option_char == EOF || option_char == 1) + if (option_char == -1 || option_char == EOF || option_char == 1) { break; + } switch (option_char) { case 'h': @@ -527,8 +539,9 @@ int process_arguments(int argc, char **argv) { ocrit = optarg; break; case 'H': - if (!is_host(optarg)) + if (!is_host(optarg)) { usage2(_("Invalid hostname/address"), optarg); + } server_address = strdup(optarg); break; case 'p': @@ -577,8 +590,9 @@ int main(int argc, char *argv[]) { /* Parse extra opts if any */ argv = np_extra_opts(&argc, argv, progname); - if (process_arguments(argc, argv) == ERROR) + if (process_arguments(argc, argv) == ERROR) { usage4(_("Could not parse arguments")); + } set_thresholds(&offset_thresholds, owarn, ocrit); @@ -623,8 +637,9 @@ int main(int argc, char *argv[]) { } printf("%s|%s\n", result_line, perfdata_line); - if (server_address != NULL) + if (server_address != NULL) { free(server_address); + } return result; } -- cgit v1.2.3-74-g34f1 From b770cc0f42a78a5bb934b7cf757a04f132b1e2de Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 12 Mar 2025 01:23:50 +0100 Subject: check_ntp_time: general refactoring --- plugins/check_ntp_time.c | 105 +++++++++++++++++++++++------------------------ 1 file changed, 51 insertions(+), 54 deletions(-) diff --git a/plugins/check_ntp_time.c b/plugins/check_ntp_time.c index 05c3d1e4..c757bc08 100644 --- a/plugins/check_ntp_time.c +++ b/plugins/check_ntp_time.c @@ -41,6 +41,7 @@ const char *email = "devel@monitoring-plugins.org"; #include "common.h" #include "netutils.h" #include "utils.h" +#include "states.h" static char *server_address = NULL; static char *port = "123"; @@ -50,7 +51,7 @@ static char *owarn = "60"; static char *ocrit = "120"; static int time_offset = 0; -static int process_arguments(int, char **); +static int process_arguments(int /*argc*/, char ** /*argv*/); static thresholds *offset_thresholds = NULL; static void print_help(void); void print_usage(void); @@ -159,7 +160,7 @@ typedef struct { #define EPOCHDIFF 0x83aa7e80UL /* extract a 32-bit ntp fixed point number into a double */ -#define NTP32asDOUBLE(x) (ntohs(L16(x)) + (double)ntohs(R16(x)) / 65536.0) +#define NTP32asDOUBLE(x) (ntohs(L16(x)) + ((double)ntohs(R16(x)) / 65536.0)) /* likewise for a 64-bit ntp fp number */ #define NTP64asDOUBLE(n) \ @@ -208,56 +209,52 @@ typedef struct { } while (0); /* calculate the offset of the local clock */ -static inline double calc_offset(const ntp_message *m, const struct timeval *t) { - double client_tx = NTP64asDOUBLE(m->origts); - double peer_rx = NTP64asDOUBLE(m->rxts); - double peer_tx = NTP64asDOUBLE(m->txts); - double client_rx = TVasDOUBLE((*t)); - return (.5 * ((peer_tx - client_rx) + (peer_rx - client_tx))); +static inline double calc_offset(const ntp_message *message, const struct timeval *time_value) { + double client_tx = NTP64asDOUBLE(message->origts); + double peer_rx = NTP64asDOUBLE(message->rxts); + double peer_tx = NTP64asDOUBLE(message->txts); + double client_rx = TVasDOUBLE((*time_value)); + return (((peer_tx - client_rx) + (peer_rx - client_tx)) / 2); } /* print out a ntp packet in human readable/debuggable format */ -void print_ntp_message(const ntp_message *p) { +void print_ntp_message(const ntp_message *message) { struct timeval ref; struct timeval orig; - struct timeval rx; - struct timeval tx; - NTP64toTV(p->refts, ref); - NTP64toTV(p->origts, orig); - NTP64toTV(p->rxts, rx); - NTP64toTV(p->txts, tx); + NTP64toTV(message->refts, ref); + NTP64toTV(message->origts, orig); printf("packet contents:\n"); - printf("\tflags: 0x%.2x\n", p->flags); - printf("\t li=%d (0x%.2x)\n", LI(p->flags), p->flags & LI_MASK); - printf("\t vn=%d (0x%.2x)\n", VN(p->flags), p->flags & VN_MASK); - printf("\t mode=%d (0x%.2x)\n", MODE(p->flags), p->flags & MODE_MASK); - printf("\tstratum = %d\n", p->stratum); - printf("\tpoll = %g\n", pow(2, p->poll)); - printf("\tprecision = %g\n", pow(2, p->precision)); - printf("\trtdelay = %-.16g\n", NTP32asDOUBLE(p->rtdelay)); - printf("\trtdisp = %-.16g\n", NTP32asDOUBLE(p->rtdisp)); - printf("\trefid = %x\n", p->refid); - printf("\trefts = %-.16g\n", NTP64asDOUBLE(p->refts)); - printf("\torigts = %-.16g\n", NTP64asDOUBLE(p->origts)); - printf("\trxts = %-.16g\n", NTP64asDOUBLE(p->rxts)); - printf("\ttxts = %-.16g\n", NTP64asDOUBLE(p->txts)); + printf("\tflags: 0x%.2x\n", message->flags); + printf("\t li=%d (0x%.2x)\n", LI(message->flags), message->flags & LI_MASK); + printf("\t vn=%d (0x%.2x)\n", VN(message->flags), message->flags & VN_MASK); + printf("\t mode=%d (0x%.2x)\n", MODE(message->flags), message->flags & MODE_MASK); + printf("\tstratum = %d\n", message->stratum); + printf("\tpoll = %g\n", pow(2, message->poll)); + printf("\tprecision = %g\n", pow(2, message->precision)); + printf("\trtdelay = %-.16g\n", NTP32asDOUBLE(message->rtdelay)); + printf("\trtdisp = %-.16g\n", NTP32asDOUBLE(message->rtdisp)); + printf("\trefid = %x\n", message->refid); + printf("\trefts = %-.16g\n", NTP64asDOUBLE(message->refts)); + printf("\torigts = %-.16g\n", NTP64asDOUBLE(message->origts)); + printf("\trxts = %-.16g\n", NTP64asDOUBLE(message->rxts)); + printf("\ttxts = %-.16g\n", NTP64asDOUBLE(message->txts)); } -void setup_request(ntp_message *p) { - memset(p, 0, sizeof(ntp_message)); - LI_SET(p->flags, LI_ALARM); - VN_SET(p->flags, 4); - MODE_SET(p->flags, MODE_CLIENT); - p->poll = 4; - p->precision = (int8_t)0xfa; - L16(p->rtdelay) = htons(1); - L16(p->rtdisp) = htons(1); +void setup_request(ntp_message *message) { + memset(message, 0, sizeof(ntp_message)); + LI_SET(message->flags, LI_ALARM); + VN_SET(message->flags, 4); + MODE_SET(message->flags, MODE_CLIENT); + message->poll = 4; + message->precision = (int8_t)0xfa; + L16(message->rtdelay) = htons(1); + L16(message->rtdisp) = htons(1); struct timeval t; gettimeofday(&t, NULL); - TVtoNTP64(t, p->txts); + TVtoNTP64(t, message->txts); } /* select the "best" server from a list of servers, and return its index. @@ -324,7 +321,7 @@ int best_offset_server(const ntp_server_results *slist, int nservers) { * we don't waste time sitting around waiting for single packets. * - we also "manually" handle resolving host names and connecting, because * we have to do it in a way that our lazy macros don't handle currently :( */ -double offset_request(const char *host, int *status) { +double offset_request(const char *host, mp_state_enum *status) { /* setup hints to only return results from getaddrinfo that we'd like */ struct addrinfo hints; memset(&hints, 0, sizeof(struct addrinfo)); @@ -333,15 +330,15 @@ double offset_request(const char *host, int *status) { hints.ai_socktype = SOCK_DGRAM; /* fill in ai with the list of hosts resolved by the host name */ - struct addrinfo *ai = NULL; - int ga_result = getaddrinfo(host, port, &hints, &ai); + struct addrinfo *addresses = NULL; + int ga_result = getaddrinfo(host, port, &hints, &addresses); if (ga_result != 0) { die(STATE_UNKNOWN, "error getting address for %s: %s\n", host, gai_strerror(ga_result)); } /* count the number of returned hosts, and allocate stuff accordingly */ - int num_hosts = 0; - for (struct addrinfo *ai_tmp = ai; ai_tmp != NULL; ai_tmp = ai_tmp->ai_next) { + size_t num_hosts = 0; + for (struct addrinfo *ai_tmp = addresses; ai_tmp != NULL; ai_tmp = ai_tmp->ai_next) { num_hosts++; } @@ -366,10 +363,10 @@ double offset_request(const char *host, int *status) { die(STATE_UNKNOWN, "can not allocate server array"); } memset(servers, 0, sizeof(ntp_server_results) * num_hosts); - DBG(printf("Found %d peers to check\n", num_hosts)); + DBG(printf("Found %zu peers to check\n", num_hosts)); /* setup each socket for writing, and the corresponding struct pollfd */ - struct addrinfo *ai_tmp = ai; + struct addrinfo *ai_tmp = addresses; for (int i = 0; ai_tmp; i++) { socklist[i] = socket(ai_tmp->ai_family, SOCK_DGRAM, IPPROTO_UDP); if (socklist[i] == -1) { @@ -427,10 +424,10 @@ double offset_request(const char *host, int *status) { } /* read from any sockets with pending data */ - for (int i = 0; servers_readable && i < num_hosts; i++) { + for (size_t i = 0; servers_readable && i < num_hosts; i++) { if (ufds[i].revents & POLLIN && servers[i].num_responses < AVG_NUM) { if (verbose) { - printf("response from peer %d: ", i); + printf("response from peer %zu: ", i); } read(ufds[i].fd, &req[i], sizeof(ntp_message)); @@ -458,7 +455,7 @@ double offset_request(const char *host, int *status) { /* lather, rinse, repeat. */ } - if (one_read == false) { + if (!one_read) { die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n"); } @@ -476,14 +473,14 @@ double offset_request(const char *host, int *status) { } /* cleanup */ - for (int j = 0; j < num_hosts; j++) { + for (size_t j = 0; j < num_hosts; j++) { close(socklist[j]); } free(socklist); free(ufds); free(servers); free(req); - freeaddrinfo(ai); + freeaddrinfo(addresses); if (verbose) { printf("overall average offset: %.10g\n", avg_offset); @@ -602,8 +599,8 @@ int main(int argc, char *argv[]) { /* set socket timeout */ alarm(socket_timeout); - int offset_result = STATE_OK; - int result = STATE_OK; + mp_state_enum offset_result = STATE_OK; + mp_state_enum result = STATE_OK; double offset = offset_request(server_address, &offset_result); if (offset_result == STATE_UNKNOWN) { result = ((!quiet) ? STATE_UNKNOWN : STATE_CRITICAL); @@ -640,7 +637,7 @@ int main(int argc, char *argv[]) { if (server_address != NULL) { free(server_address); } - return result; + exit(result); } void print_help(void) { -- cgit v1.2.3-74-g34f1 From 9ea0bf23df484cdb223595e02211aa9145dffb34 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 12 Mar 2025 01:41:48 +0100 Subject: Refactor check_ntp_time --- plugins/Makefile.am | 1 + plugins/check_ntp_time.c | 68 +++++++++++++++++++++++---------------- plugins/check_ntp_time.d/config.h | 28 ++++++++++++++++ 3 files changed, 69 insertions(+), 28 deletions(-) create mode 100644 plugins/check_ntp_time.d/config.h diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 5d4449bf..bb117881 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -64,6 +64,7 @@ EXTRA_DIST = t \ check_by_ssh.d \ check_smtp.d \ check_mysql.d \ + check_ntp_time.d \ check_dig.d \ check_cluster.d \ check_fping.d diff --git a/plugins/check_ntp_time.c b/plugins/check_ntp_time.c index c757bc08..31162883 100644 --- a/plugins/check_ntp_time.c +++ b/plugins/check_ntp_time.c @@ -42,17 +42,17 @@ const char *email = "devel@monitoring-plugins.org"; #include "netutils.h" #include "utils.h" #include "states.h" +#include "thresholds.h" +#include "check_ntp_time.d/config.h" -static char *server_address = NULL; -static char *port = "123"; static int verbose = 0; -static bool quiet = false; -static char *owarn = "60"; -static char *ocrit = "120"; -static int time_offset = 0; -static int process_arguments(int /*argc*/, char ** /*argv*/); -static thresholds *offset_thresholds = NULL; +typedef struct { + int errorcode; + check_ntp_time_config config; +} check_ntp_time_config_wrapper; +static check_ntp_time_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); + static void print_help(void); void print_usage(void); @@ -321,7 +321,7 @@ int best_offset_server(const ntp_server_results *slist, int nservers) { * we don't waste time sitting around waiting for single packets. * - we also "manually" handle resolving host names and connecting, because * we have to do it in a way that our lazy macros don't handle currently :( */ -double offset_request(const char *host, mp_state_enum *status) { +double offset_request(const char *host, const char *port, mp_state_enum *status, int time_offset) { /* setup hints to only return results from getaddrinfo that we'd like */ struct addrinfo hints; memset(&hints, 0, sizeof(struct addrinfo)); @@ -392,7 +392,7 @@ double offset_request(const char *host, mp_state_enum *status) { time_t start_ts = 0; time_t now_time = 0; now_time = start_ts = time(NULL); - int servers_completed = 0; + size_t servers_completed = 0; bool one_read = false; while (servers_completed < num_hosts && now_time - start_ts <= socket_timeout / 2) { /* loop through each server and find each one which hasn't @@ -401,13 +401,13 @@ double offset_request(const char *host, mp_state_enum *status) { * and update the "waiting" timestamp with the current time. */ now_time = time(NULL); - for (int i = 0; i < num_hosts; i++) { + for (size_t i = 0; i < num_hosts; i++) { if (servers[i].waiting < now_time && servers[i].num_responses < AVG_NUM) { if (verbose && servers[i].waiting != 0) { printf("re-"); } if (verbose) { - printf("sending request to peer %d\n", i); + printf("sending request to peer %zu\n", i); } setup_request(&req[i]); write(socklist[i], &req[i], sizeof(ntp_message)); @@ -488,7 +488,7 @@ double offset_request(const char *host, mp_state_enum *status) { return avg_offset; } -int process_arguments(int argc, char **argv) { +check_ntp_time_config_wrapper process_arguments(int argc, char **argv) { static struct option longopts[] = {{"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {"verbose", no_argument, 0, 'v'}, @@ -507,6 +507,14 @@ int process_arguments(int argc, char **argv) { usage("\n"); } + check_ntp_time_config_wrapper result = { + .errorcode = OK, + .config = check_ntp_time_config_init(), + }; + + char *owarn = "60"; + char *ocrit = "120"; + while (true) { int option = 0; int option_char = getopt_long(argc, argv, "Vhv46qw:c:t:H:p:o:", longopts, &option); @@ -527,7 +535,7 @@ int process_arguments(int argc, char **argv) { verbose++; break; case 'q': - quiet = true; + result.config.quiet = true; break; case 'w': owarn = optarg; @@ -539,16 +547,16 @@ int process_arguments(int argc, char **argv) { if (!is_host(optarg)) { usage2(_("Invalid hostname/address"), optarg); } - server_address = strdup(optarg); + result.config.server_address = strdup(optarg); break; case 'p': - port = strdup(optarg); + result.config.port = strdup(optarg); break; case 't': socket_timeout = atoi(optarg); break; case 'o': - time_offset = atoi(optarg); + result.config.time_offset = atoi(optarg); break; case '4': address_family = AF_INET; @@ -567,14 +575,16 @@ int process_arguments(int argc, char **argv) { } } - if (server_address == NULL) { + if (result.config.server_address == NULL) { usage4(_("Hostname was not supplied")); } - return 0; + set_thresholds(&result.config.offset_thresholds, owarn, ocrit); + + return result; } -char *perfd_offset(double offset) { +char *perfd_offset(double offset, thresholds *offset_thresholds) { return fperfdata("offset", offset, "s", true, offset_thresholds->warning->end, true, offset_thresholds->critical->end, false, 0, false, 0); } @@ -587,11 +597,13 @@ int main(int argc, char *argv[]) { /* Parse extra opts if any */ argv = np_extra_opts(&argc, argv, progname); - if (process_arguments(argc, argv) == ERROR) { + check_ntp_time_config_wrapper tmp_config = process_arguments(argc, argv); + + if (tmp_config.errorcode == ERROR) { usage4(_("Could not parse arguments")); } - set_thresholds(&offset_thresholds, owarn, ocrit); + const check_ntp_time_config config = tmp_config.config; /* initialize alarm signal handling */ signal(SIGALRM, socket_timeout_alarm_handler); @@ -601,11 +613,11 @@ int main(int argc, char *argv[]) { mp_state_enum offset_result = STATE_OK; mp_state_enum result = STATE_OK; - double offset = offset_request(server_address, &offset_result); + double offset = offset_request(config.server_address, config.port, &offset_result, config.time_offset); if (offset_result == STATE_UNKNOWN) { - result = ((!quiet) ? STATE_UNKNOWN : STATE_CRITICAL); + result = ((!config.quiet) ? STATE_UNKNOWN : STATE_CRITICAL); } else { - result = get_status(fabs(offset), offset_thresholds); + result = get_status(fabs(offset), config.offset_thresholds); } char *result_line; @@ -630,12 +642,12 @@ int main(int argc, char *argv[]) { xasprintf(&perfdata_line, ""); } else { xasprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset); - xasprintf(&perfdata_line, "%s", perfd_offset(offset)); + xasprintf(&perfdata_line, "%s", perfd_offset(offset, config.offset_thresholds)); } printf("%s|%s\n", result_line, perfdata_line); - if (server_address != NULL) { - free(server_address); + if (config.server_address != NULL) { + free(config.server_address); } exit(result); } 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 @@ +#pragma once + +#include "../../config.h" +#include "thresholds.h" +#include + +typedef struct { + char *server_address; + char *port; + + bool quiet; + int time_offset; + + thresholds *offset_thresholds; +} check_ntp_time_config; + +check_ntp_time_config check_ntp_time_config_init() { + check_ntp_time_config tmp = { + .server_address = NULL, + .port = "123", + + .quiet = false, + .time_offset = 0, + + .offset_thresholds = NULL, + }; + return tmp; +} -- cgit v1.2.3-74-g34f1