summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-10-31 18:13:32 +0100
committerLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-10-31 18:13:32 +0100
commite867c2ebd3eeea5726136dc8e7227b256b5e117d (patch)
tree3b449401437d48c385d47de8e11c90a0e8ec314d
parentbb9fcf5bfaf34aeffd651919b5a14b631b9fceb4 (diff)
downloadmonitoring-plugins-e867c2ebd3eeea5726136dc8e7227b256b5e117d.tar.gz
check_ntp_peer: small refactoring
-rw-r--r--plugins/check_ntp_peer.c46
1 files changed, 23 insertions, 23 deletions
diff --git a/plugins/check_ntp_peer.c b/plugins/check_ntp_peer.c
index 24d1c9b5..37e481c7 100644
--- a/plugins/check_ntp_peer.c
+++ b/plugins/check_ntp_peer.c
@@ -35,11 +35,11 @@
35 * 35 *
36 *****************************************************************************/ 36 *****************************************************************************/
37 37
38#include "thresholds.h"
39const char *progname = "check_ntp_peer"; 38const char *progname = "check_ntp_peer";
40const char *copyright = "2006-2024"; 39const char *copyright = "2006-2024";
41const char *email = "devel@monitoring-plugins.org"; 40const char *email = "devel@monitoring-plugins.org";
42 41
42#include "thresholds.h"
43#include "common.h" 43#include "common.h"
44#include "netutils.h" 44#include "netutils.h"
45#include "utils.h" 45#include "utils.h"
@@ -47,8 +47,6 @@ const char *email = "devel@monitoring-plugins.org";
47#include "check_ntp_peer.d/config.h" 47#include "check_ntp_peer.d/config.h"
48 48
49static int verbose = 0; 49static int verbose = 0;
50static bool syncsource_found = false;
51static bool li_alarm = false;
52 50
53typedef struct { 51typedef struct {
54 int errorcode; 52 int errorcode;
@@ -198,9 +196,7 @@ void setup_control_request(ntp_control_message *message, uint8_t opcode, uint16_
198 * positive value means a success retrieving the value. 196 * positive value means a success retrieving the value.
199 * - status is set to WARNING if there's no sync.peer (otherwise OK) and is 197 * - status is set to WARNING if there's no sync.peer (otherwise OK) and is
200 * the return value of the function. 198 * the return value of the function.
201 * status is pretty much useless as syncsource_found is a global variable 199 */
202 * used later in main to check is the server was synchronized. It works
203 * so I left it alone */
204typedef struct { 200typedef struct {
205 mp_state_enum state; 201 mp_state_enum state;
206 mp_state_enum offset_result; 202 mp_state_enum offset_result;
@@ -208,6 +204,8 @@ typedef struct {
208 double jitter; 204 double jitter;
209 long stratum; 205 long stratum;
210 int num_truechimers; 206 int num_truechimers;
207 bool syncsource_found;
208 bool li_alarm;
211} ntp_request_result; 209} ntp_request_result;
212ntp_request_result ntp_request(const check_ntp_peer_config config) { 210ntp_request_result ntp_request(const check_ntp_peer_config config) {
213 211
@@ -217,6 +215,8 @@ ntp_request_result ntp_request(const check_ntp_peer_config config) {
217 .jitter = -1, 215 .jitter = -1,
218 .stratum = -1, 216 .stratum = -1,
219 .num_truechimers = 0, 217 .num_truechimers = 0,
218 .syncsource_found = false,
219 .li_alarm = false,
220 }; 220 };
221 221
222 /* Long-winded explanation: 222 /* Long-winded explanation:
@@ -235,19 +235,16 @@ ntp_request_result ntp_request(const check_ntp_peer_config config) {
235 * 4) Extract the offset, jitter and stratum value from the data[] 235 * 4) Extract the offset, jitter and stratum value from the data[]
236 * (it's ASCII) 236 * (it's ASCII)
237 */ 237 */
238 int min_peer_sel = PEER_INCLUDED;
239 int num_candidates = 0;
240 void *tmp;
241 ntp_assoc_status_pair *peers = NULL;
242 int peer_offset = 0;
243 size_t peers_size = 0;
244 size_t npeers = 0;
245 int conn = -1; 238 int conn = -1;
246 my_udp_connect(config.server_address, config.port, &conn); 239 my_udp_connect(config.server_address, config.port, &conn);
247 240
248 /* keep sending requests until the server stops setting the 241 /* keep sending requests until the server stops setting the
249 * REM_MORE bit, though usually this is only 1 packet. */ 242 * REM_MORE bit, though usually this is only 1 packet. */
250 ntp_control_message req; 243 ntp_control_message req;
244 ntp_assoc_status_pair *peers = NULL;
245 int peer_offset = 0;
246 size_t peers_size = 0;
247 size_t npeers = 0;
251 do { 248 do {
252 setup_control_request(&req, OP_READSTAT, 1); 249 setup_control_request(&req, OP_READSTAT, 1);
253 DBG(printf("sending READSTAT request")); 250 DBG(printf("sending READSTAT request"));
@@ -269,12 +266,13 @@ ntp_request_result ntp_request(const check_ntp_peer_config config) {
269 } while (!(req.op & OP_READSTAT && ntohs(req.seq) == 1)); 266 } while (!(req.op & OP_READSTAT && ntohs(req.seq) == 1));
270 267
271 if (LI(req.flags) == LI_ALARM) { 268 if (LI(req.flags) == LI_ALARM) {
272 li_alarm = true; 269 result.li_alarm = true;
273 } 270 }
274 /* Each peer identifier is 4 bytes in the data section, which 271 /* Each peer identifier is 4 bytes in the data section, which
275 * we represent as a ntp_assoc_status_pair datatype. 272 * we represent as a ntp_assoc_status_pair datatype.
276 */ 273 */
277 peers_size += ntohs(req.count); 274 peers_size += ntohs(req.count);
275 void *tmp;
278 if ((tmp = realloc(peers, peers_size)) == NULL) { 276 if ((tmp = realloc(peers, peers_size)) == NULL) {
279 free(peers), die(STATE_UNKNOWN, "can not (re)allocate 'peers' buffer\n"); 277 free(peers), die(STATE_UNKNOWN, "can not (re)allocate 'peers' buffer\n");
280 } 278 }
@@ -287,13 +285,15 @@ ntp_request_result ntp_request(const check_ntp_peer_config config) {
287 /* first, let's find out if we have a sync source, or if there are 285 /* first, let's find out if we have a sync source, or if there are
288 * at least some candidates. In the latter case we'll issue 286 * at least some candidates. In the latter case we'll issue
289 * a warning but go ahead with the check on them. */ 287 * a warning but go ahead with the check on them. */
288 int min_peer_sel = PEER_INCLUDED;
289 int num_candidates = 0;
290 for (size_t i = 0; i < npeers; i++) { 290 for (size_t i = 0; i < npeers; i++) {
291 if (PEER_SEL(peers[i].status) >= PEER_TRUECHIMER) { 291 if (PEER_SEL(peers[i].status) >= PEER_TRUECHIMER) {
292 result.num_truechimers++; 292 result.num_truechimers++;
293 if (PEER_SEL(peers[i].status) >= PEER_INCLUDED) { 293 if (PEER_SEL(peers[i].status) >= PEER_INCLUDED) {
294 num_candidates++; 294 num_candidates++;
295 if (PEER_SEL(peers[i].status) >= PEER_SYNCSOURCE) { 295 if (PEER_SEL(peers[i].status) >= PEER_SYNCSOURCE) {
296 syncsource_found = true; 296 result.syncsource_found = true;
297 min_peer_sel = PEER_SYNCSOURCE; 297 min_peer_sel = PEER_SYNCSOURCE;
298 } 298 }
299 } 299 }
@@ -302,18 +302,18 @@ ntp_request_result ntp_request(const check_ntp_peer_config config) {
302 302
303 if (verbose) { 303 if (verbose) {
304 printf("%d candidate peers available\n", num_candidates); 304 printf("%d candidate peers available\n", num_candidates);
305 } 305 if (result.syncsource_found) {
306 if (verbose && syncsource_found) { 306 printf("synchronization source found\n");
307 printf("synchronization source found\n"); 307 }
308 } 308 }
309 309
310 if (!syncsource_found) { 310 if (!result.syncsource_found) {
311 result.state = STATE_WARNING; 311 result.state = STATE_WARNING;
312 if (verbose) { 312 if (verbose) {
313 printf("warning: no synchronization source found\n"); 313 printf("warning: no synchronization source found\n");
314 } 314 }
315 } 315 }
316 if (li_alarm) { 316 if (result.li_alarm) {
317 result.state = STATE_WARNING; 317 result.state = STATE_WARNING;
318 if (verbose) { 318 if (verbose) {
319 printf("warning: LI_ALARM bit is set\n"); 319 printf("warning: LI_ALARM bit is set\n");
@@ -634,7 +634,7 @@ int main(int argc, char *argv[]) {
634 alarm(socket_timeout); 634 alarm(socket_timeout);
635 635
636 /* This returns either OK or WARNING (See comment preceding ntp_request) */ 636 /* This returns either OK or WARNING (See comment preceding ntp_request) */
637 ntp_request_result ntp_res = ntp_request(config); 637 const ntp_request_result ntp_res = ntp_request(config);
638 mp_state_enum result = STATE_UNKNOWN; 638 mp_state_enum result = STATE_UNKNOWN;
639 639
640 if (ntp_res.offset_result == STATE_UNKNOWN) { 640 if (ntp_res.offset_result == STATE_UNKNOWN) {
@@ -686,9 +686,9 @@ int main(int argc, char *argv[]) {
686 break; 686 break;
687 } 687 }
688 688
689 if (!syncsource_found) { 689 if (!ntp_res.syncsource_found) {
690 xasprintf(&result_line, "%s %s,", result_line, _("Server not synchronized")); 690 xasprintf(&result_line, "%s %s,", result_line, _("Server not synchronized"));
691 } else if (li_alarm) { 691 } else if (ntp_res.li_alarm) {
692 xasprintf(&result_line, "%s %s,", result_line, _("Server has the LI_ALARM bit set")); 692 xasprintf(&result_line, "%s %s,", result_line, _("Server has the LI_ALARM bit set"));
693 } 693 }
694 694