diff options
author | RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> | 2024-10-31 12:09:24 (GMT) |
---|---|---|
committer | RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> | 2024-10-31 12:09:24 (GMT) |
commit | d0ea64439b73b415c206a23e7498364cd58b3c71 (patch) | |
tree | 329531f22c2afa7dba8fd17fc101ee69df265695 /plugins | |
parent | 4f74229bb3dd9d43dd80a8f396f0905a674c4746 (diff) | |
download | monitoring-plugins-d0ea64439b73b415c206a23e7498364cd58b3c71.tar.gz |
check_ntp_peer: more local variables, linter fixes
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/check_ntp_peer.c | 70 |
1 files changed, 38 insertions, 32 deletions
diff --git a/plugins/check_ntp_peer.c b/plugins/check_ntp_peer.c index 2fac2bb..f99e503 100644 --- a/plugins/check_ntp_peer.c +++ b/plugins/check_ntp_peer.c | |||
@@ -61,7 +61,7 @@ static char *tcrit = "0:"; | |||
61 | static bool syncsource_found = false; | 61 | static bool syncsource_found = false; |
62 | static bool li_alarm = false; | 62 | static bool li_alarm = false; |
63 | 63 | ||
64 | static int process_arguments(int, char **); | 64 | static int process_arguments(int /*argc*/, char ** /*argv*/); |
65 | static thresholds *offset_thresholds = NULL; | 65 | static thresholds *offset_thresholds = NULL; |
66 | static thresholds *jitter_thresholds = NULL; | 66 | static thresholds *jitter_thresholds = NULL; |
67 | static thresholds *stratum_thresholds = NULL; | 67 | static thresholds *stratum_thresholds = NULL; |
@@ -158,9 +158,6 @@ typedef struct { | |||
158 | } while (0); | 158 | } while (0); |
159 | 159 | ||
160 | void print_ntp_control_message(const ntp_control_message *p) { | 160 | void print_ntp_control_message(const ntp_control_message *p) { |
161 | int i = 0, numpeers = 0; | ||
162 | const ntp_assoc_status_pair *peer = NULL; | ||
163 | |||
164 | printf("control packet contents:\n"); | 161 | printf("control packet contents:\n"); |
165 | printf("\tflags: 0x%.2x , 0x%.2x\n", p->flags, p->op); | 162 | printf("\tflags: 0x%.2x , 0x%.2x\n", p->flags, p->op); |
166 | printf("\t li=%d (0x%.2x)\n", LI(p->flags), p->flags & LI_MASK); | 163 | printf("\t li=%d (0x%.2x)\n", LI(p->flags), p->flags & LI_MASK); |
@@ -175,10 +172,11 @@ void print_ntp_control_message(const ntp_control_message *p) { | |||
175 | printf("\tassoc: %d (0x%.2x)\n", ntohs(p->assoc), ntohs(p->assoc)); | 172 | printf("\tassoc: %d (0x%.2x)\n", ntohs(p->assoc), ntohs(p->assoc)); |
176 | printf("\toffset: %d (0x%.2x)\n", ntohs(p->offset), ntohs(p->offset)); | 173 | printf("\toffset: %d (0x%.2x)\n", ntohs(p->offset), ntohs(p->offset)); |
177 | printf("\tcount: %d (0x%.2x)\n", ntohs(p->count), ntohs(p->count)); | 174 | printf("\tcount: %d (0x%.2x)\n", ntohs(p->count), ntohs(p->count)); |
178 | numpeers = ntohs(p->count) / (sizeof(ntp_assoc_status_pair)); | 175 | |
176 | int numpeers = ntohs(p->count) / (sizeof(ntp_assoc_status_pair)); | ||
179 | if (p->op & REM_RESP && p->op & OP_READSTAT) { | 177 | if (p->op & REM_RESP && p->op & OP_READSTAT) { |
180 | peer = (ntp_assoc_status_pair *)p->data; | 178 | const ntp_assoc_status_pair *peer = (ntp_assoc_status_pair *)p->data; |
181 | for (i = 0; i < numpeers; i++) { | 179 | for (int i = 0; i < numpeers; i++) { |
182 | printf("\tpeer id %.2x status %.2x", ntohs(peer[i].assoc), ntohs(peer[i].status)); | 180 | printf("\tpeer id %.2x status %.2x", ntohs(peer[i].assoc), ntohs(peer[i].status)); |
183 | if (PEER_SEL(peer[i].status) >= PEER_SYNCSOURCE) { | 181 | if (PEER_SEL(peer[i].status) >= PEER_SYNCSOURCE) { |
184 | printf(" <-- current sync source"); | 182 | printf(" <-- current sync source"); |
@@ -214,18 +212,6 @@ void setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq) | |||
214 | * used later in main to check is the server was synchronized. It works | 212 | * used later in main to check is the server was synchronized. It works |
215 | * so I left it alone */ | 213 | * so I left it alone */ |
216 | int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum, int *num_truechimers) { | 214 | int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum, int *num_truechimers) { |
217 | int conn = -1, i, npeers = 0, num_candidates = 0; | ||
218 | double tmp_offset = 0; | ||
219 | int min_peer_sel = PEER_INCLUDED; | ||
220 | int peers_size = 0, peer_offset = 0; | ||
221 | int status; | ||
222 | ntp_assoc_status_pair *peers = NULL; | ||
223 | ntp_control_message req; | ||
224 | const char *getvar = "stratum,offset,jitter"; | ||
225 | char *data, *value, *nptr; | ||
226 | void *tmp; | ||
227 | |||
228 | status = STATE_OK; | ||
229 | *offset_result = STATE_UNKNOWN; | 215 | *offset_result = STATE_UNKNOWN; |
230 | *jitter = *stratum = -1; | 216 | *jitter = *stratum = -1; |
231 | *num_truechimers = 0; | 217 | *num_truechimers = 0; |
@@ -246,10 +232,19 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum | |||
246 | * 4) Extract the offset, jitter and stratum value from the data[] | 232 | * 4) Extract the offset, jitter and stratum value from the data[] |
247 | * (it's ASCII) | 233 | * (it's ASCII) |
248 | */ | 234 | */ |
235 | int min_peer_sel = PEER_INCLUDED; | ||
236 | int num_candidates = 0; | ||
237 | void *tmp; | ||
238 | ntp_assoc_status_pair *peers = NULL; | ||
239 | int peer_offset = 0; | ||
240 | int peers_size = 0; | ||
241 | int npeers = 0; | ||
242 | int conn = -1; | ||
249 | my_udp_connect(server_address, port, &conn); | 243 | my_udp_connect(server_address, port, &conn); |
250 | 244 | ||
251 | /* keep sending requests until the server stops setting the | 245 | /* keep sending requests until the server stops setting the |
252 | * REM_MORE bit, though usually this is only 1 packet. */ | 246 | * REM_MORE bit, though usually this is only 1 packet. */ |
247 | ntp_control_message req; | ||
253 | do { | 248 | do { |
254 | setup_control_request(&req, OP_READSTAT, 1); | 249 | setup_control_request(&req, OP_READSTAT, 1); |
255 | DBG(printf("sending READSTAT request")); | 250 | DBG(printf("sending READSTAT request")); |
@@ -285,7 +280,7 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum | |||
285 | /* first, let's find out if we have a sync source, or if there are | 280 | /* first, let's find out if we have a sync source, or if there are |
286 | * at least some candidates. In the latter case we'll issue | 281 | * at least some candidates. In the latter case we'll issue |
287 | * a warning but go ahead with the check on them. */ | 282 | * a warning but go ahead with the check on them. */ |
288 | for (i = 0; i < npeers; i++) { | 283 | for (int i = 0; i < npeers; i++) { |
289 | if (PEER_SEL(peers[i].status) >= PEER_TRUECHIMER) { | 284 | if (PEER_SEL(peers[i].status) >= PEER_TRUECHIMER) { |
290 | (*num_truechimers)++; | 285 | (*num_truechimers)++; |
291 | if (PEER_SEL(peers[i].status) >= PEER_INCLUDED) { | 286 | if (PEER_SEL(peers[i].status) >= PEER_INCLUDED) { |
@@ -297,10 +292,13 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum | |||
297 | } | 292 | } |
298 | } | 293 | } |
299 | } | 294 | } |
295 | |||
300 | if (verbose) | 296 | if (verbose) |
301 | printf("%d candidate peers available\n", num_candidates); | 297 | printf("%d candidate peers available\n", num_candidates); |
302 | if (verbose && syncsource_found) | 298 | if (verbose && syncsource_found) |
303 | printf("synchronization source found\n"); | 299 | printf("synchronization source found\n"); |
300 | |||
301 | int status = STATE_OK; | ||
304 | if (!syncsource_found) { | 302 | if (!syncsource_found) { |
305 | status = STATE_WARNING; | 303 | status = STATE_WARNING; |
306 | if (verbose) | 304 | if (verbose) |
@@ -312,7 +310,9 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum | |||
312 | printf("warning: LI_ALARM bit is set\n"); | 310 | printf("warning: LI_ALARM bit is set\n"); |
313 | } | 311 | } |
314 | 312 | ||
315 | for (i = 0; i < npeers; i++) { | 313 | const char *getvar = "stratum,offset,jitter"; |
314 | char *data; | ||
315 | for (int i = 0; i < npeers; i++) { | ||
316 | /* Only query this server if it is the current sync source */ | 316 | /* Only query this server if it is the current sync source */ |
317 | /* If there's no sync.peer, query all candidates and use the best one */ | 317 | /* If there's no sync.peer, query all candidates and use the best one */ |
318 | if (PEER_SEL(peers[i].status) >= min_peer_sel) { | 318 | if (PEER_SEL(peers[i].status) >= min_peer_sel) { |
@@ -354,7 +354,8 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum | |||
354 | getvar = "stratum,offset,dispersion"; | 354 | getvar = "stratum,offset,dispersion"; |
355 | i--; | 355 | i--; |
356 | continue; | 356 | continue; |
357 | } else if (strlen(getvar)) { | 357 | } |
358 | if (strlen(getvar)) { | ||
358 | if (verbose) | 359 | if (verbose) |
359 | printf("Server didn't like dispersion either; will retrieve everything\n"); | 360 | printf("Server didn't like dispersion either; will retrieve everything\n"); |
360 | getvar = ""; | 361 | getvar = ""; |
@@ -366,6 +367,9 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum | |||
366 | if (verbose > 1) | 367 | if (verbose > 1) |
367 | printf("Server responded: >>>%s<<<\n", data); | 368 | printf("Server responded: >>>%s<<<\n", data); |
368 | 369 | ||
370 | double tmp_offset = 0; | ||
371 | char *value; | ||
372 | char *nptr; | ||
369 | /* get the offset */ | 373 | /* get the offset */ |
370 | if (verbose) | 374 | if (verbose) |
371 | printf("parsing offset from peer %.2x: ", ntohs(peers[i].assoc)); | 375 | printf("parsing offset from peer %.2x: ", ntohs(peers[i].assoc)); |
@@ -442,8 +446,6 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum | |||
442 | } | 446 | } |
443 | 447 | ||
444 | int process_arguments(int argc, char **argv) { | 448 | int process_arguments(int argc, char **argv) { |
445 | int c; | ||
446 | int option = 0; | ||
447 | static struct option longopts[] = { | 449 | static struct option longopts[] = { |
448 | {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {"verbose", no_argument, 0, 'v'}, | 450 | {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {"verbose", no_argument, 0, 'v'}, |
449 | {"use-ipv4", no_argument, 0, '4'}, {"use-ipv6", no_argument, 0, '6'}, {"quiet", no_argument, 0, 'q'}, | 451 | {"use-ipv4", no_argument, 0, '4'}, {"use-ipv6", no_argument, 0, '6'}, {"quiet", no_argument, 0, 'q'}, |
@@ -456,11 +458,12 @@ int process_arguments(int argc, char **argv) { | |||
456 | usage("\n"); | 458 | usage("\n"); |
457 | 459 | ||
458 | while (true) { | 460 | while (true) { |
459 | c = getopt_long(argc, argv, "Vhv46qw:c:W:C:j:k:m:n:t:H:p:", longopts, &option); | 461 | int option = 0; |
460 | if (c == -1 || c == EOF || c == 1) | 462 | int option_char = getopt_long(argc, argv, "Vhv46qw:c:W:C:j:k:m:n:t:H:p:", longopts, &option); |
463 | if (option_char == -1 || option_char == EOF || option_char == 1) | ||
461 | break; | 464 | break; |
462 | 465 | ||
463 | switch (c) { | 466 | switch (option_char) { |
464 | case 'h': | 467 | case 'h': |
465 | print_help(); | 468 | print_help(); |
466 | exit(STATE_UNKNOWN); | 469 | exit(STATE_UNKNOWN); |
@@ -561,10 +564,6 @@ char *perfd_truechimers(int num_truechimers) { | |||
561 | } | 564 | } |
562 | 565 | ||
563 | int main(int argc, char *argv[]) { | 566 | int main(int argc, char *argv[]) { |
564 | int result, offset_result, stratum, num_truechimers; | ||
565 | double offset = 0, jitter = 0; | ||
566 | char *result_line, *perfdata_line; | ||
567 | |||
568 | setlocale(LC_ALL, ""); | 567 | setlocale(LC_ALL, ""); |
569 | bindtextdomain(PACKAGE, LOCALEDIR); | 568 | bindtextdomain(PACKAGE, LOCALEDIR); |
570 | textdomain(PACKAGE); | 569 | textdomain(PACKAGE); |
@@ -586,8 +585,13 @@ int main(int argc, char *argv[]) { | |||
586 | /* set socket timeout */ | 585 | /* set socket timeout */ |
587 | alarm(socket_timeout); | 586 | alarm(socket_timeout); |
588 | 587 | ||
588 | int offset_result; | ||
589 | int stratum; | ||
590 | int num_truechimers; | ||
591 | double offset = 0; | ||
592 | double jitter = 0; | ||
589 | /* This returns either OK or WARNING (See comment preceding ntp_request) */ | 593 | /* This returns either OK or WARNING (See comment preceding ntp_request) */ |
590 | result = ntp_request(&offset, &offset_result, &jitter, &stratum, &num_truechimers); | 594 | int result = ntp_request(&offset, &offset_result, &jitter, &stratum, &num_truechimers); |
591 | 595 | ||
592 | if (offset_result == STATE_UNKNOWN) { | 596 | if (offset_result == STATE_UNKNOWN) { |
593 | /* if there's no sync peer (this overrides ntp_request output): */ | 597 | /* if there's no sync peer (this overrides ntp_request output): */ |
@@ -622,6 +626,7 @@ int main(int argc, char *argv[]) { | |||
622 | result = max_state_alt(result, jresult); | 626 | result = max_state_alt(result, jresult); |
623 | } | 627 | } |
624 | 628 | ||
629 | char *result_line; | ||
625 | switch (result) { | 630 | switch (result) { |
626 | case STATE_CRITICAL: | 631 | case STATE_CRITICAL: |
627 | xasprintf(&result_line, _("NTP CRITICAL:")); | 632 | xasprintf(&result_line, _("NTP CRITICAL:")); |
@@ -641,6 +646,7 @@ int main(int argc, char *argv[]) { | |||
641 | else if (li_alarm) | 646 | else if (li_alarm) |
642 | xasprintf(&result_line, "%s %s,", result_line, _("Server has the LI_ALARM bit set")); | 647 | xasprintf(&result_line, "%s %s,", result_line, _("Server has the LI_ALARM bit set")); |
643 | 648 | ||
649 | char *perfdata_line; | ||
644 | if (offset_result == STATE_UNKNOWN) { | 650 | if (offset_result == STATE_UNKNOWN) { |
645 | xasprintf(&result_line, "%s %s", result_line, _("Offset unknown")); | 651 | xasprintf(&result_line, "%s %s", result_line, _("Offset unknown")); |
646 | xasprintf(&perfdata_line, ""); | 652 | xasprintf(&perfdata_line, ""); |