summaryrefslogtreecommitdiffstats
path: root/plugins/check_ntp_peer.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_ntp_peer.c')
-rw-r--r--plugins/check_ntp_peer.c487
1 files changed, 274 insertions, 213 deletions
diff --git a/plugins/check_ntp_peer.c b/plugins/check_ntp_peer.c
index f99e5032..24d1c9b5 100644
--- a/plugins/check_ntp_peer.c
+++ b/plugins/check_ntp_peer.c
@@ -35,6 +35,7 @@
35 * 35 *
36 *****************************************************************************/ 36 *****************************************************************************/
37 37
38#include "thresholds.h"
38const char *progname = "check_ntp_peer"; 39const char *progname = "check_ntp_peer";
39const char *copyright = "2006-2024"; 40const char *copyright = "2006-2024";
40const char *email = "devel@monitoring-plugins.org"; 41const char *email = "devel@monitoring-plugins.org";
@@ -42,30 +43,18 @@ const char *email = "devel@monitoring-plugins.org";
42#include "common.h" 43#include "common.h"
43#include "netutils.h" 44#include "netutils.h"
44#include "utils.h" 45#include "utils.h"
46#include "../lib/states.h"
47#include "check_ntp_peer.d/config.h"
45 48
46static char *server_address = NULL;
47static int port = 123;
48static int verbose = 0; 49static int verbose = 0;
49static bool quiet = false;
50static char *owarn = "60";
51static char *ocrit = "120";
52static bool do_stratum = false;
53static char *swarn = "-1:16";
54static char *scrit = "-1:16";
55static bool do_jitter = false;
56static char *jwarn = "-1:5000";
57static char *jcrit = "-1:10000";
58static bool do_truechimers = false;
59static char *twarn = "0:";
60static char *tcrit = "0:";
61static bool syncsource_found = false; 50static bool syncsource_found = false;
62static bool li_alarm = false; 51static bool li_alarm = false;
63 52
64static int process_arguments(int /*argc*/, char ** /*argv*/); 53typedef struct {
65static thresholds *offset_thresholds = NULL; 54 int errorcode;
66static thresholds *jitter_thresholds = NULL; 55 check_ntp_peer_config config;
67static thresholds *stratum_thresholds = NULL; 56} check_ntp_peer_config_wrapper;
68static thresholds *truechimer_thresholds = NULL; 57static check_ntp_peer_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
69static void print_help(void); 58static void print_help(void);
70void print_usage(void); 59void print_usage(void);
71 60
@@ -94,9 +83,9 @@ typedef struct {
94/* bits 1,2 are the leap indicator */ 83/* bits 1,2 are the leap indicator */
95#define LI_MASK 0xc0 84#define LI_MASK 0xc0
96#define LI(x) ((x & LI_MASK) >> 6) 85#define LI(x) ((x & LI_MASK) >> 6)
97#define LI_SET(x, y) \ 86#define LI_SET(x, y) \
98 do { \ 87 do { \
99 x |= ((y << 6) & LI_MASK); \ 88 x |= ((y << 6) & LI_MASK); \
100 } while (0) 89 } while (0)
101/* and these are the values of the leap indicator */ 90/* and these are the values of the leap indicator */
102#define LI_NOWARNING 0x00 91#define LI_NOWARNING 0x00
@@ -106,17 +95,17 @@ typedef struct {
106/* bits 3,4,5 are the ntp version */ 95/* bits 3,4,5 are the ntp version */
107#define VN_MASK 0x38 96#define VN_MASK 0x38
108#define VN(x) ((x & VN_MASK) >> 3) 97#define VN(x) ((x & VN_MASK) >> 3)
109#define VN_SET(x, y) \ 98#define VN_SET(x, y) \
110 do { \ 99 do { \
111 x |= ((y << 3) & VN_MASK); \ 100 x |= ((y << 3) & VN_MASK); \
112 } while (0) 101 } while (0)
113#define VN_RESERVED 0x02 102#define VN_RESERVED 0x02
114/* bits 6,7,8 are the ntp mode */ 103/* bits 6,7,8 are the ntp mode */
115#define MODE_MASK 0x07 104#define MODE_MASK 0x07
116#define MODE(x) (x & MODE_MASK) 105#define MODE(x) (x & MODE_MASK)
117#define MODE_SET(x, y) \ 106#define MODE_SET(x, y) \
118 do { \ 107 do { \
119 x |= (y & MODE_MASK); \ 108 x |= (y & MODE_MASK); \
120 } while (0) 109 } while (0)
121/* here are some values */ 110/* here are some values */
122#define MODE_CLIENT 0x03 111#define MODE_CLIENT 0x03
@@ -128,9 +117,9 @@ typedef struct {
128#define REM_MORE 0x20 117#define REM_MORE 0x20
129/* In control message, bits 11 - 15 are opcode */ 118/* In control message, bits 11 - 15 are opcode */
130#define OP_MASK 0x1f 119#define OP_MASK 0x1f
131#define OP_SET(x, y) \ 120#define OP_SET(x, y) \
132 do { \ 121 do { \
133 x |= (y & OP_MASK); \ 122 x |= (y & OP_MASK); \
134 } while (0) 123 } while (0)
135#define OP_READSTAT 0x01 124#define OP_READSTAT 0x01
136#define OP_READVAR 0x02 125#define OP_READVAR 0x02
@@ -143,39 +132,40 @@ typedef struct {
143/* NTP control message header is 12 bytes, plus any data in the data 132/* NTP control message header is 12 bytes, plus any data in the data
144 * field, plus null padding to the nearest 32-bit boundary per rfc. 133 * field, plus null padding to the nearest 32-bit boundary per rfc.
145 */ 134 */
146#define SIZEOF_NTPCM(m) (12 + ntohs(m.count) + ((ntohs(m.count) % 4) ? 4 - (ntohs(m.count) % 4) : 0)) 135#define SIZEOF_NTPCM(m) \
136 (12 + ntohs(m.count) + ((ntohs(m.count) % 4) ? 4 - (ntohs(m.count) % 4) : 0))
147 137
148/* finally, a little helper or two for debugging: */ 138/* finally, a little helper or two for debugging: */
149#define DBG(x) \ 139#define DBG(x) \
150 do { \ 140 do { \
151 if (verbose > 1) { \ 141 if (verbose > 1) { \
152 x; \ 142 x; \
153 } \ 143 } \
154 } while (0); 144 } while (0);
155#define PRINTSOCKADDR(x) \ 145#define PRINTSOCKADDR(x) \
156 do { \ 146 do { \
157 printf("%u.%u.%u.%u", (x >> 24) & 0xff, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff); \ 147 printf("%u.%u.%u.%u", (x >> 24) & 0xff, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff); \
158 } while (0); 148 } while (0);
159 149
160void print_ntp_control_message(const ntp_control_message *p) { 150void print_ntp_control_message(const ntp_control_message *message) {
161 printf("control packet contents:\n"); 151 printf("control packet contents:\n");
162 printf("\tflags: 0x%.2x , 0x%.2x\n", p->flags, p->op); 152 printf("\tflags: 0x%.2x , 0x%.2x\n", message->flags, message->op);
163 printf("\t li=%d (0x%.2x)\n", LI(p->flags), p->flags & LI_MASK); 153 printf("\t li=%d (0x%.2x)\n", LI(message->flags), message->flags & LI_MASK);
164 printf("\t vn=%d (0x%.2x)\n", VN(p->flags), p->flags & VN_MASK); 154 printf("\t vn=%d (0x%.2x)\n", VN(message->flags), message->flags & VN_MASK);
165 printf("\t mode=%d (0x%.2x)\n", MODE(p->flags), p->flags & MODE_MASK); 155 printf("\t mode=%d (0x%.2x)\n", MODE(message->flags), message->flags & MODE_MASK);
166 printf("\t response=%d (0x%.2x)\n", (p->op & REM_RESP) > 0, p->op & REM_RESP); 156 printf("\t response=%d (0x%.2x)\n", (message->op & REM_RESP) > 0, message->op & REM_RESP);
167 printf("\t more=%d (0x%.2x)\n", (p->op & REM_MORE) > 0, p->op & REM_MORE); 157 printf("\t more=%d (0x%.2x)\n", (message->op & REM_MORE) > 0, message->op & REM_MORE);
168 printf("\t error=%d (0x%.2x)\n", (p->op & REM_ERROR) > 0, p->op & REM_ERROR); 158 printf("\t error=%d (0x%.2x)\n", (message->op & REM_ERROR) > 0, message->op & REM_ERROR);
169 printf("\t op=%d (0x%.2x)\n", p->op & OP_MASK, p->op & OP_MASK); 159 printf("\t op=%d (0x%.2x)\n", message->op & OP_MASK, message->op & OP_MASK);
170 printf("\tsequence: %d (0x%.2x)\n", ntohs(p->seq), ntohs(p->seq)); 160 printf("\tsequence: %d (0x%.2x)\n", ntohs(message->seq), ntohs(message->seq));
171 printf("\tstatus: %d (0x%.2x)\n", ntohs(p->status), ntohs(p->status)); 161 printf("\tstatus: %d (0x%.2x)\n", ntohs(message->status), ntohs(message->status));
172 printf("\tassoc: %d (0x%.2x)\n", ntohs(p->assoc), ntohs(p->assoc)); 162 printf("\tassoc: %d (0x%.2x)\n", ntohs(message->assoc), ntohs(message->assoc));
173 printf("\toffset: %d (0x%.2x)\n", ntohs(p->offset), ntohs(p->offset)); 163 printf("\toffset: %d (0x%.2x)\n", ntohs(message->offset), ntohs(message->offset));
174 printf("\tcount: %d (0x%.2x)\n", ntohs(p->count), ntohs(p->count)); 164 printf("\tcount: %d (0x%.2x)\n", ntohs(message->count), ntohs(message->count));
175 165
176 int numpeers = ntohs(p->count) / (sizeof(ntp_assoc_status_pair)); 166 int numpeers = ntohs(message->count) / (sizeof(ntp_assoc_status_pair));
177 if (p->op & REM_RESP && p->op & OP_READSTAT) { 167 if (message->op & REM_RESP && message->op & OP_READSTAT) {
178 const ntp_assoc_status_pair *peer = (ntp_assoc_status_pair *)p->data; 168 const ntp_assoc_status_pair *peer = (ntp_assoc_status_pair *)message->data;
179 for (int i = 0; i < numpeers; i++) { 169 for (int i = 0; i < numpeers; i++) {
180 printf("\tpeer id %.2x status %.2x", ntohs(peer[i].assoc), ntohs(peer[i].status)); 170 printf("\tpeer id %.2x status %.2x", ntohs(peer[i].assoc), ntohs(peer[i].status));
181 if (PEER_SEL(peer[i].status) >= PEER_SYNCSOURCE) { 171 if (PEER_SEL(peer[i].status) >= PEER_SYNCSOURCE) {
@@ -190,13 +180,13 @@ void print_ntp_control_message(const ntp_control_message *p) {
190 } 180 }
191} 181}
192 182
193void setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq) { 183void setup_control_request(ntp_control_message *message, uint8_t opcode, uint16_t seq) {
194 memset(p, 0, sizeof(ntp_control_message)); 184 memset(message, 0, sizeof(ntp_control_message));
195 LI_SET(p->flags, LI_NOWARNING); 185 LI_SET(message->flags, LI_NOWARNING);
196 VN_SET(p->flags, VN_RESERVED); 186 VN_SET(message->flags, VN_RESERVED);
197 MODE_SET(p->flags, MODE_CONTROLMSG); 187 MODE_SET(message->flags, MODE_CONTROLMSG);
198 OP_SET(p->op, opcode); 188 OP_SET(message->op, opcode);
199 p->seq = htons(seq); 189 message->seq = htons(seq);
200 /* Remaining fields are zero for requests */ 190 /* Remaining fields are zero for requests */
201} 191}
202 192
@@ -211,10 +201,23 @@ void setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq)
211 * status is pretty much useless as syncsource_found is a global variable 201 * status is pretty much useless as syncsource_found is a global variable
212 * used later in main to check is the server was synchronized. It works 202 * used later in main to check is the server was synchronized. It works
213 * so I left it alone */ 203 * so I left it alone */
214int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum, int *num_truechimers) { 204typedef struct {
215 *offset_result = STATE_UNKNOWN; 205 mp_state_enum state;
216 *jitter = *stratum = -1; 206 mp_state_enum offset_result;
217 *num_truechimers = 0; 207 double offset;
208 double jitter;
209 long stratum;
210 int num_truechimers;
211} ntp_request_result;
212ntp_request_result ntp_request(const check_ntp_peer_config config) {
213
214 ntp_request_result result = {
215 .state = STATE_OK,
216 .offset_result = STATE_UNKNOWN,
217 .jitter = -1,
218 .stratum = -1,
219 .num_truechimers = 0,
220 };
218 221
219 /* Long-winded explanation: 222 /* Long-winded explanation:
220 * Getting the sync peer offset, jitter and stratum requires a number of 223 * Getting the sync peer offset, jitter and stratum requires a number of
@@ -237,10 +240,10 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum
237 void *tmp; 240 void *tmp;
238 ntp_assoc_status_pair *peers = NULL; 241 ntp_assoc_status_pair *peers = NULL;
239 int peer_offset = 0; 242 int peer_offset = 0;
240 int peers_size = 0; 243 size_t peers_size = 0;
241 int npeers = 0; 244 size_t npeers = 0;
242 int conn = -1; 245 int conn = -1;
243 my_udp_connect(server_address, port, &conn); 246 my_udp_connect(config.server_address, config.port, &conn);
244 247
245 /* keep sending requests until the server stops setting the 248 /* keep sending requests until the server stops setting the
246 * REM_MORE bit, though usually this is only 1 packet. */ 249 * REM_MORE bit, though usually this is only 1 packet. */
@@ -255,24 +258,28 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum
255 /* Attempt to read the largest size packet possible */ 258 /* Attempt to read the largest size packet possible */
256 req.count = htons(MAX_CM_SIZE); 259 req.count = htons(MAX_CM_SIZE);
257 DBG(printf("receiving READSTAT response")) 260 DBG(printf("receiving READSTAT response"))
258 if (read(conn, &req, SIZEOF_NTPCM(req)) == -1) 261 if (read(conn, &req, SIZEOF_NTPCM(req)) == -1) {
259 die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n"); 262 die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n");
263 }
260 DBG(print_ntp_control_message(&req)); 264 DBG(print_ntp_control_message(&req));
261 /* discard obviously invalid packets */ 265 /* discard obviously invalid packets */
262 if (ntohs(req.count) > MAX_CM_SIZE) 266 if (ntohs(req.count) > MAX_CM_SIZE) {
263 die(STATE_CRITICAL, "NTP CRITICAL: Invalid packet received from NTP server\n"); 267 die(STATE_CRITICAL, "NTP CRITICAL: Invalid packet received from NTP server\n");
268 }
264 } while (!(req.op & OP_READSTAT && ntohs(req.seq) == 1)); 269 } while (!(req.op & OP_READSTAT && ntohs(req.seq) == 1));
265 270
266 if (LI(req.flags) == LI_ALARM) 271 if (LI(req.flags) == LI_ALARM) {
267 li_alarm = true; 272 li_alarm = true;
273 }
268 /* Each peer identifier is 4 bytes in the data section, which 274 /* Each peer identifier is 4 bytes in the data section, which
269 * we represent as a ntp_assoc_status_pair datatype. 275 * we represent as a ntp_assoc_status_pair datatype.
270 */ 276 */
271 peers_size += ntohs(req.count); 277 peers_size += ntohs(req.count);
272 if ((tmp = realloc(peers, peers_size)) == NULL) 278 if ((tmp = realloc(peers, peers_size)) == NULL) {
273 free(peers), die(STATE_UNKNOWN, "can not (re)allocate 'peers' buffer\n"); 279 free(peers), die(STATE_UNKNOWN, "can not (re)allocate 'peers' buffer\n");
280 }
274 peers = tmp; 281 peers = tmp;
275 memcpy((void *)((ptrdiff_t)peers + peer_offset), (void *)req.data, ntohs(req.count)); 282 memcpy((peers + peer_offset), (void *)req.data, ntohs(req.count));
276 npeers = peers_size / sizeof(ntp_assoc_status_pair); 283 npeers = peers_size / sizeof(ntp_assoc_status_pair);
277 peer_offset += ntohs(req.count); 284 peer_offset += ntohs(req.count);
278 } while (req.op & REM_MORE); 285 } while (req.op & REM_MORE);
@@ -280,9 +287,9 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum
280 /* first, let's find out if we have a sync source, or if there are 287 /* first, let's find out if we have a sync source, or if there are
281 * at least some candidates. In the latter case we'll issue 288 * at least some candidates. In the latter case we'll issue
282 * a warning but go ahead with the check on them. */ 289 * a warning but go ahead with the check on them. */
283 for (int i = 0; i < npeers; i++) { 290 for (size_t i = 0; i < npeers; i++) {
284 if (PEER_SEL(peers[i].status) >= PEER_TRUECHIMER) { 291 if (PEER_SEL(peers[i].status) >= PEER_TRUECHIMER) {
285 (*num_truechimers)++; 292 result.num_truechimers++;
286 if (PEER_SEL(peers[i].status) >= PEER_INCLUDED) { 293 if (PEER_SEL(peers[i].status) >= PEER_INCLUDED) {
287 num_candidates++; 294 num_candidates++;
288 if (PEER_SEL(peers[i].status) >= PEER_SYNCSOURCE) { 295 if (PEER_SEL(peers[i].status) >= PEER_SYNCSOURCE) {
@@ -293,31 +300,35 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum
293 } 300 }
294 } 301 }
295 302
296 if (verbose) 303 if (verbose) {
297 printf("%d candidate peers available\n", num_candidates); 304 printf("%d candidate peers available\n", num_candidates);
298 if (verbose && syncsource_found) 305 }
306 if (verbose && syncsource_found) {
299 printf("synchronization source found\n"); 307 printf("synchronization source found\n");
308 }
300 309
301 int status = STATE_OK;
302 if (!syncsource_found) { 310 if (!syncsource_found) {
303 status = STATE_WARNING; 311 result.state = STATE_WARNING;
304 if (verbose) 312 if (verbose) {
305 printf("warning: no synchronization source found\n"); 313 printf("warning: no synchronization source found\n");
314 }
306 } 315 }
307 if (li_alarm) { 316 if (li_alarm) {
308 status = STATE_WARNING; 317 result.state = STATE_WARNING;
309 if (verbose) 318 if (verbose) {
310 printf("warning: LI_ALARM bit is set\n"); 319 printf("warning: LI_ALARM bit is set\n");
320 }
311 } 321 }
312 322
313 const char *getvar = "stratum,offset,jitter"; 323 const char *getvar = "stratum,offset,jitter";
314 char *data; 324 char *data;
315 for (int i = 0; i < npeers; i++) { 325 for (size_t i = 0; i < npeers; i++) {
316 /* Only query this server if it is the current sync source */ 326 /* 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 */ 327 /* If there's no sync.peer, query all candidates and use the best one */
318 if (PEER_SEL(peers[i].status) >= min_peer_sel) { 328 if (PEER_SEL(peers[i].status) >= min_peer_sel) {
319 if (verbose) 329 if (verbose) {
320 printf("Getting offset, jitter and stratum for peer %.2x\n", ntohs(peers[i].assoc)); 330 printf("Getting offset, jitter and stratum for peer %.2x\n", ntohs(peers[i].assoc));
331 }
321 xasprintf(&data, ""); 332 xasprintf(&data, "");
322 do { 333 do {
323 setup_control_request(&req, OP_READVAR, 2); 334 setup_control_request(&req, OP_READVAR, 2);
@@ -342,81 +353,95 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum
342 DBG(print_ntp_control_message(&req)); 353 DBG(print_ntp_control_message(&req));
343 } while (!(req.op & OP_READVAR && ntohs(req.seq) == 2)); 354 } while (!(req.op & OP_READVAR && ntohs(req.seq) == 2));
344 355
345 if (!(req.op & REM_ERROR)) 356 if (!(req.op & REM_ERROR)) {
346 xasprintf(&data, "%s%s", data, req.data); 357 xasprintf(&data, "%s%s", data, req.data);
358 }
347 } while (req.op & REM_MORE); 359 } while (req.op & REM_MORE);
348 360
349 if (req.op & REM_ERROR) { 361 if (req.op & REM_ERROR) {
350 if (strstr(getvar, "jitter")) { 362 if (strstr(getvar, "jitter")) {
351 if (verbose) 363 if (verbose) {
352 printf("The command failed. This is usually caused by servers refusing the 'jitter'\nvariable. Restarting with " 364 printf("The command failed. This is usually caused by servers refusing the "
365 "'jitter'\nvariable. Restarting with "
353 "'dispersion'...\n"); 366 "'dispersion'...\n");
367 }
354 getvar = "stratum,offset,dispersion"; 368 getvar = "stratum,offset,dispersion";
355 i--; 369 i--;
356 continue; 370 continue;
357 } 371 }
358 if (strlen(getvar)) { 372 if (strlen(getvar)) {
359 if (verbose) 373 if (verbose) {
360 printf("Server didn't like dispersion either; will retrieve everything\n"); 374 printf("Server didn't like dispersion either; will retrieve everything\n");
375 }
361 getvar = ""; 376 getvar = "";
362 i--; 377 i--;
363 continue; 378 continue;
364 } 379 }
365 } 380 }
366 381
367 if (verbose > 1) 382 if (verbose > 1) {
368 printf("Server responded: >>>%s<<<\n", data); 383 printf("Server responded: >>>%s<<<\n", data);
384 }
369 385
370 double tmp_offset = 0; 386 double tmp_offset = 0;
371 char *value; 387 char *value;
372 char *nptr; 388 char *nptr;
373 /* get the offset */ 389 /* get the offset */
374 if (verbose) 390 if (verbose) {
375 printf("parsing offset from peer %.2x: ", ntohs(peers[i].assoc)); 391 printf("parsing offset from peer %.2x: ", ntohs(peers[i].assoc));
392 }
376 393
377 value = np_extract_ntpvar(data, "offset"); 394 value = np_extract_ntpvar(data, "offset");
378 nptr = NULL; 395 nptr = NULL;
379 /* Convert the value if we have one */ 396 /* Convert the value if we have one */
380 if (value != NULL) 397 if (value != NULL) {
381 tmp_offset = strtod(value, &nptr) / 1000; 398 tmp_offset = strtod(value, &nptr) / 1000;
399 }
382 /* If value is null or no conversion was performed */ 400 /* If value is null or no conversion was performed */
383 if (value == NULL || value == nptr) { 401 if (value == NULL || value == nptr) {
384 if (verbose) 402 if (verbose) {
385 printf("error: unable to read server offset response.\n"); 403 printf("error: unable to read server offset response.\n");
404 }
386 } else { 405 } else {
387 if (verbose) 406 if (verbose) {
388 printf("%.10g\n", tmp_offset); 407 printf("%.10g\n", tmp_offset);
389 if (*offset_result == STATE_UNKNOWN || fabs(tmp_offset) < fabs(*offset)) { 408 }
390 *offset = tmp_offset; 409 if (result.offset_result == STATE_UNKNOWN ||
391 *offset_result = STATE_OK; 410 fabs(tmp_offset) < fabs(result.offset)) {
411 result.offset = tmp_offset;
412 result.offset_result = STATE_OK;
392 } else { 413 } else {
393 /* Skip this one; move to the next */ 414 /* Skip this one; move to the next */
394 continue; 415 continue;
395 } 416 }
396 } 417 }
397 418
398 if (do_jitter) { 419 if (config.do_jitter) {
399 /* get the jitter */ 420 /* get the jitter */
400 if (verbose) { 421 if (verbose) {
401 printf("parsing %s from peer %.2x: ", strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter", 422 printf("parsing %s from peer %.2x: ",
423 strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter",
402 ntohs(peers[i].assoc)); 424 ntohs(peers[i].assoc));
403 } 425 }
404 value = np_extract_ntpvar(data, strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter"); 426 value = np_extract_ntpvar(data, strstr(getvar, "dispersion") != NULL ? "dispersion"
427 : "jitter");
405 nptr = NULL; 428 nptr = NULL;
406 /* Convert the value if we have one */ 429 /* Convert the value if we have one */
407 if (value != NULL) 430 if (value != NULL) {
408 *jitter = strtod(value, &nptr); 431 result.jitter = strtod(value, &nptr);
432 }
409 /* If value is null or no conversion was performed */ 433 /* If value is null or no conversion was performed */
410 if (value == NULL || value == nptr) { 434 if (value == NULL || value == nptr) {
411 if (verbose) 435 if (verbose) {
412 printf("error: unable to read server jitter/dispersion response.\n"); 436 printf("error: unable to read server jitter/dispersion response.\n");
413 *jitter = -1; 437 }
438 result.jitter = -1;
414 } else if (verbose) { 439 } else if (verbose) {
415 printf("%.10g\n", *jitter); 440 printf("%.10g\n", result.jitter);
416 } 441 }
417 } 442 }
418 443
419 if (do_stratum) { 444 if (config.do_stratum) {
420 /* get the stratum */ 445 /* get the stratum */
421 if (verbose) { 446 if (verbose) {
422 printf("parsing stratum from peer %.2x: ", ntohs(peers[i].assoc)); 447 printf("parsing stratum from peer %.2x: ", ntohs(peers[i].assoc));
@@ -424,44 +449,59 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum
424 value = np_extract_ntpvar(data, "stratum"); 449 value = np_extract_ntpvar(data, "stratum");
425 nptr = NULL; 450 nptr = NULL;
426 /* Convert the value if we have one */ 451 /* Convert the value if we have one */
427 if (value != NULL) 452 if (value != NULL) {
428 *stratum = strtol(value, &nptr, 10); 453 result.stratum = strtol(value, &nptr, 10);
454 }
429 if (value == NULL || value == nptr) { 455 if (value == NULL || value == nptr) {
430 if (verbose) 456 if (verbose) {
431 printf("error: unable to read server stratum response.\n"); 457 printf("error: unable to read server stratum response.\n");
432 *stratum = -1; 458 }
459 result.stratum = -1;
433 } else { 460 } else {
434 if (verbose) 461 if (verbose) {
435 printf("%i\n", *stratum); 462 printf("%li\n", result.stratum);
463 }
436 } 464 }
437 } 465 }
438 } /* if (PEER_SEL(peers[i].status) >= min_peer_sel) */ 466 } /* if (PEER_SEL(peers[i].status) >= min_peer_sel) */
439 } /* for (i = 0; i < npeers; i++) */ 467 } /* for (i = 0; i < npeers; i++) */
440 468
441 close(conn); 469 close(conn);
442 if (peers != NULL) 470 if (peers != NULL) {
443 free(peers); 471 free(peers);
472 }
444 473
445 return status; 474 return result;
446} 475}
447 476
448int process_arguments(int argc, char **argv) { 477check_ntp_peer_config_wrapper process_arguments(int argc, char **argv) {
449 static struct option longopts[] = { 478 static struct option longopts[] = {
450 {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {"verbose", no_argument, 0, 'v'}, 479 {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'},
451 {"use-ipv4", no_argument, 0, '4'}, {"use-ipv6", no_argument, 0, '6'}, {"quiet", no_argument, 0, 'q'}, 480 {"verbose", no_argument, 0, 'v'}, {"use-ipv4", no_argument, 0, '4'},
452 {"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, {"swarn", required_argument, 0, 'W'}, 481 {"use-ipv6", no_argument, 0, '6'}, {"quiet", no_argument, 0, 'q'},
453 {"scrit", required_argument, 0, 'C'}, {"jwarn", required_argument, 0, 'j'}, {"jcrit", required_argument, 0, 'k'}, 482 {"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'},
454 {"twarn", required_argument, 0, 'm'}, {"tcrit", required_argument, 0, 'n'}, {"timeout", required_argument, 0, 't'}, 483 {"swarn", required_argument, 0, 'W'}, {"scrit", required_argument, 0, 'C'},
455 {"hostname", required_argument, 0, 'H'}, {"port", required_argument, 0, 'p'}, {0, 0, 0, 0}}; 484 {"jwarn", required_argument, 0, 'j'}, {"jcrit", required_argument, 0, 'k'},
456 485 {"twarn", required_argument, 0, 'm'}, {"tcrit", required_argument, 0, 'n'},
457 if (argc < 2) 486 {"timeout", required_argument, 0, 't'}, {"hostname", required_argument, 0, 'H'},
487 {"port", required_argument, 0, 'p'}, {0, 0, 0, 0}};
488
489 if (argc < 2) {
458 usage("\n"); 490 usage("\n");
491 }
492
493 check_ntp_peer_config_wrapper result = {
494 .errorcode = OK,
495 .config = check_ntp_peer_config_init(),
496 };
459 497
460 while (true) { 498 while (true) {
461 int option = 0; 499 int option = 0;
462 int option_char = getopt_long(argc, argv, "Vhv46qw:c:W:C:j:k:m:n:t:H:p:", longopts, &option); 500 int option_char =
463 if (option_char == -1 || option_char == EOF || option_char == 1) 501 getopt_long(argc, argv, "Vhv46qw:c:W:C:j:k:m:n:t:H:p:", longopts, &option);
502 if (option_char == -1 || option_char == EOF || option_char == 1) {
464 break; 503 break;
504 }
465 505
466 switch (option_char) { 506 switch (option_char) {
467 case 'h': 507 case 'h':
@@ -476,45 +516,46 @@ int process_arguments(int argc, char **argv) {
476 verbose++; 516 verbose++;
477 break; 517 break;
478 case 'q': 518 case 'q':
479 quiet = true; 519 result.config.quiet = true;
480 break; 520 break;
481 case 'w': 521 case 'w':
482 owarn = optarg; 522 result.config.owarn = optarg;
483 break; 523 break;
484 case 'c': 524 case 'c':
485 ocrit = optarg; 525 result.config.ocrit = optarg;
486 break; 526 break;
487 case 'W': 527 case 'W':
488 do_stratum = true; 528 result.config.do_stratum = true;
489 swarn = optarg; 529 result.config.swarn = optarg;
490 break; 530 break;
491 case 'C': 531 case 'C':
492 do_stratum = true; 532 result.config.do_stratum = true;
493 scrit = optarg; 533 result.config.scrit = optarg;
494 break; 534 break;
495 case 'j': 535 case 'j':
496 do_jitter = true; 536 result.config.do_jitter = true;
497 jwarn = optarg; 537 result.config.jwarn = optarg;
498 break; 538 break;
499 case 'k': 539 case 'k':
500 do_jitter = true; 540 result.config.do_jitter = true;
501 jcrit = optarg; 541 result.config.jcrit = optarg;
502 break; 542 break;
503 case 'm': 543 case 'm':
504 do_truechimers = true; 544 result.config.do_truechimers = true;
505 twarn = optarg; 545 result.config.twarn = optarg;
506 break; 546 break;
507 case 'n': 547 case 'n':
508 do_truechimers = true; 548 result.config.do_truechimers = true;
509 tcrit = optarg; 549 result.config.tcrit = optarg;
510 break; 550 break;
511 case 'H': 551 case 'H':
512 if (!is_host(optarg)) 552 if (!is_host(optarg)) {
513 usage2(_("Invalid hostname/address"), optarg); 553 usage2(_("Invalid hostname/address"), optarg);
514 server_address = strdup(optarg); 554 }
555 result.config.server_address = strdup(optarg);
515 break; 556 break;
516 case 'p': 557 case 'p':
517 port = atoi(optarg); 558 result.config.port = atoi(optarg);
518 break; 559 break;
519 case 't': 560 case 't':
520 socket_timeout = atoi(optarg); 561 socket_timeout = atoi(optarg);
@@ -536,30 +577,37 @@ int process_arguments(int argc, char **argv) {
536 } 577 }
537 } 578 }
538 579
539 if (server_address == NULL) { 580 if (result.config.server_address == NULL) {
540 usage4(_("Hostname was not supplied")); 581 usage4(_("Hostname was not supplied"));
541 } 582 }
542 583
543 return 0; 584 set_thresholds(&result.config.offset_thresholds, result.config.owarn, result.config.ocrit);
585 set_thresholds(&result.config.jitter_thresholds, result.config.jwarn, result.config.jcrit);
586 set_thresholds(&result.config.stratum_thresholds, result.config.swarn, result.config.scrit);
587 set_thresholds(&result.config.truechimer_thresholds, result.config.twarn, result.config.tcrit);
588
589 return result;
544} 590}
545 591
546char *perfd_offset(double offset) { 592char *perfd_offset(double offset, thresholds *offset_thresholds) {
547 return fperfdata("offset", offset, "s", true, offset_thresholds->warning->end, true, offset_thresholds->critical->end, false, 0, false, 593 return fperfdata("offset", offset, "s", true, offset_thresholds->warning->end, true,
548 0); 594 offset_thresholds->critical->end, false, 0, false, 0);
549} 595}
550 596
551char *perfd_jitter(double jitter) { 597char *perfd_jitter(double jitter, bool do_jitter, thresholds *jitter_thresholds) {
552 return fperfdata("jitter", jitter, "", do_jitter, jitter_thresholds->warning->end, do_jitter, jitter_thresholds->critical->end, true, 0, 598 return fperfdata("jitter", jitter, "", do_jitter, jitter_thresholds->warning->end, do_jitter,
553 false, 0); 599 jitter_thresholds->critical->end, true, 0, false, 0);
554} 600}
555 601
556char *perfd_stratum(int stratum) { 602char *perfd_stratum(int stratum, bool do_stratum, thresholds *stratum_thresholds) {
557 return perfdata("stratum", stratum, "", do_stratum, (int)stratum_thresholds->warning->end, do_stratum, 603 return perfdata("stratum", stratum, "", do_stratum, (int)stratum_thresholds->warning->end,
558 (int)stratum_thresholds->critical->end, true, 0, true, 16); 604 do_stratum, (int)stratum_thresholds->critical->end, true, 0, true, 16);
559} 605}
560 606
561char *perfd_truechimers(int num_truechimers) { 607char *perfd_truechimers(int num_truechimers, const bool do_truechimers,
562 return perfdata("truechimers", num_truechimers, "", do_truechimers, (int)truechimer_thresholds->warning->end, do_truechimers, 608 thresholds *truechimer_thresholds) {
609 return perfdata("truechimers", num_truechimers, "", do_truechimers,
610 (int)truechimer_thresholds->warning->end, do_truechimers,
563 (int)truechimer_thresholds->critical->end, true, 0, false, 0); 611 (int)truechimer_thresholds->critical->end, true, 0, false, 0);
564} 612}
565 613
@@ -571,13 +619,13 @@ int main(int argc, char *argv[]) {
571 /* Parse extra opts if any */ 619 /* Parse extra opts if any */
572 argv = np_extra_opts(&argc, argv, progname); 620 argv = np_extra_opts(&argc, argv, progname);
573 621
574 if (process_arguments(argc, argv) == ERROR) 622 check_ntp_peer_config_wrapper tmp_config = process_arguments(argc, argv);
623
624 if (tmp_config.errorcode == ERROR) {
575 usage4(_("Could not parse arguments")); 625 usage4(_("Could not parse arguments"));
626 }
576 627
577 set_thresholds(&offset_thresholds, owarn, ocrit); 628 const check_ntp_peer_config config = tmp_config.config;
578 set_thresholds(&jitter_thresholds, jwarn, jcrit);
579 set_thresholds(&stratum_thresholds, swarn, scrit);
580 set_thresholds(&truechimer_thresholds, twarn, tcrit);
581 629
582 /* initialize alarm signal handling */ 630 /* initialize alarm signal handling */
583 signal(SIGALRM, socket_timeout_alarm_handler); 631 signal(SIGALRM, socket_timeout_alarm_handler);
@@ -585,44 +633,40 @@ int main(int argc, char *argv[]) {
585 /* set socket timeout */ 633 /* set socket timeout */
586 alarm(socket_timeout); 634 alarm(socket_timeout);
587 635
588 int offset_result;
589 int stratum;
590 int num_truechimers;
591 double offset = 0;
592 double jitter = 0;
593 /* This returns either OK or WARNING (See comment preceding ntp_request) */ 636 /* This returns either OK or WARNING (See comment preceding ntp_request) */
594 int result = ntp_request(&offset, &offset_result, &jitter, &stratum, &num_truechimers); 637 ntp_request_result ntp_res = ntp_request(config);
638 mp_state_enum result = STATE_UNKNOWN;
595 639
596 if (offset_result == STATE_UNKNOWN) { 640 if (ntp_res.offset_result == STATE_UNKNOWN) {
597 /* if there's no sync peer (this overrides ntp_request output): */ 641 /* if there's no sync peer (this overrides ntp_request output): */
598 result = (quiet ? STATE_UNKNOWN : STATE_CRITICAL); 642 result = (config.quiet ? STATE_UNKNOWN : STATE_CRITICAL);
599 } else { 643 } else {
600 /* Be quiet if there's no candidates either */ 644 /* Be quiet if there's no candidates either */
601 if (quiet && result == STATE_WARNING) 645 if (config.quiet && result == STATE_WARNING) {
602 result = STATE_UNKNOWN; 646 result = STATE_UNKNOWN;
603 result = max_state_alt(result, get_status(fabs(offset), offset_thresholds)); 647 }
648 result = max_state_alt(result, get_status(fabs(ntp_res.offset), config.offset_thresholds));
604 } 649 }
605 650
606 int oresult = result; 651 mp_state_enum oresult = result;
607 652 mp_state_enum tresult = STATE_UNKNOWN;
608 int tresult = STATE_UNKNOWN;
609 653
610 if (do_truechimers) { 654 if (config.do_truechimers) {
611 tresult = get_status(num_truechimers, truechimer_thresholds); 655 tresult = get_status(ntp_res.num_truechimers, config.truechimer_thresholds);
612 result = max_state_alt(result, tresult); 656 result = max_state_alt(result, tresult);
613 } 657 }
614 658
615 int sresult = STATE_UNKNOWN; 659 mp_state_enum sresult = STATE_UNKNOWN;
616 660
617 if (do_stratum) { 661 if (config.do_stratum) {
618 sresult = get_status(stratum, stratum_thresholds); 662 sresult = get_status((double)ntp_res.stratum, config.stratum_thresholds);
619 result = max_state_alt(result, sresult); 663 result = max_state_alt(result, sresult);
620 } 664 }
621 665
622 int jresult = STATE_UNKNOWN; 666 mp_state_enum jresult = STATE_UNKNOWN;
623 667
624 if (do_jitter) { 668 if (config.do_jitter) {
625 jresult = get_status(jitter, jitter_thresholds); 669 jresult = get_status(ntp_res.jitter, config.jitter_thresholds);
626 result = max_state_alt(result, jresult); 670 result = max_state_alt(result, jresult);
627 } 671 }
628 672
@@ -641,59 +685,74 @@ int main(int argc, char *argv[]) {
641 xasprintf(&result_line, _("NTP UNKNOWN:")); 685 xasprintf(&result_line, _("NTP UNKNOWN:"));
642 break; 686 break;
643 } 687 }
644 if (!syncsource_found) 688
689 if (!syncsource_found) {
645 xasprintf(&result_line, "%s %s,", result_line, _("Server not synchronized")); 690 xasprintf(&result_line, "%s %s,", result_line, _("Server not synchronized"));
646 else if (li_alarm) 691 } else if (li_alarm) {
647 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 }
648 694
649 char *perfdata_line; 695 char *perfdata_line;
650 if (offset_result == STATE_UNKNOWN) { 696 if (ntp_res.offset_result == STATE_UNKNOWN) {
651 xasprintf(&result_line, "%s %s", result_line, _("Offset unknown")); 697 xasprintf(&result_line, "%s %s", result_line, _("Offset unknown"));
652 xasprintf(&perfdata_line, ""); 698 xasprintf(&perfdata_line, "");
653 } else if (oresult == STATE_WARNING) { 699 } else if (oresult == STATE_WARNING) {
654 xasprintf(&result_line, "%s %s %.10g secs (WARNING)", result_line, _("Offset"), offset); 700 xasprintf(&result_line, "%s %s %.10g secs (WARNING)", result_line, _("Offset"),
701 ntp_res.offset);
655 } else if (oresult == STATE_CRITICAL) { 702 } else if (oresult == STATE_CRITICAL) {
656 xasprintf(&result_line, "%s %s %.10g secs (CRITICAL)", result_line, _("Offset"), offset); 703 xasprintf(&result_line, "%s %s %.10g secs (CRITICAL)", result_line, _("Offset"),
704 ntp_res.offset);
657 } else { 705 } else {
658 xasprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset); 706 xasprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), ntp_res.offset);
659 } 707 }
660 xasprintf(&perfdata_line, "%s", perfd_offset(offset)); 708 xasprintf(&perfdata_line, "%s", perfd_offset(ntp_res.offset, config.offset_thresholds));
661 709
662 if (do_jitter) { 710 if (config.do_jitter) {
663 if (jresult == STATE_WARNING) { 711 if (jresult == STATE_WARNING) {
664 xasprintf(&result_line, "%s, jitter=%f (WARNING)", result_line, jitter); 712 xasprintf(&result_line, "%s, jitter=%f (WARNING)", result_line, ntp_res.jitter);
665 } else if (jresult == STATE_CRITICAL) { 713 } else if (jresult == STATE_CRITICAL) {
666 xasprintf(&result_line, "%s, jitter=%f (CRITICAL)", result_line, jitter); 714 xasprintf(&result_line, "%s, jitter=%f (CRITICAL)", result_line, ntp_res.jitter);
667 } else { 715 } else {
668 xasprintf(&result_line, "%s, jitter=%f", result_line, jitter); 716 xasprintf(&result_line, "%s, jitter=%f", result_line, ntp_res.jitter);
669 } 717 }
670 xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_jitter(jitter)); 718 xasprintf(&perfdata_line, "%s %s", perfdata_line,
719 perfd_jitter(ntp_res.jitter, config.do_jitter, config.jitter_thresholds));
671 } 720 }
672 if (do_stratum) { 721
722 if (config.do_stratum) {
673 if (sresult == STATE_WARNING) { 723 if (sresult == STATE_WARNING) {
674 xasprintf(&result_line, "%s, stratum=%i (WARNING)", result_line, stratum); 724 xasprintf(&result_line, "%s, stratum=%li (WARNING)", result_line, ntp_res.stratum);
675 } else if (sresult == STATE_CRITICAL) { 725 } else if (sresult == STATE_CRITICAL) {
676 xasprintf(&result_line, "%s, stratum=%i (CRITICAL)", result_line, stratum); 726 xasprintf(&result_line, "%s, stratum=%li (CRITICAL)", result_line, ntp_res.stratum);
677 } else { 727 } else {
678 xasprintf(&result_line, "%s, stratum=%i", result_line, stratum); 728 xasprintf(&result_line, "%s, stratum=%li", result_line, ntp_res.stratum);
679 } 729 }
680 xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_stratum(stratum)); 730 xasprintf(&perfdata_line, "%s %s", perfdata_line,
731 perfd_stratum(ntp_res.stratum, config.do_stratum, config.stratum_thresholds));
681 } 732 }
682 if (do_truechimers) { 733
734 if (config.do_truechimers) {
683 if (tresult == STATE_WARNING) { 735 if (tresult == STATE_WARNING) {
684 xasprintf(&result_line, "%s, truechimers=%i (WARNING)", result_line, num_truechimers); 736 xasprintf(&result_line, "%s, truechimers=%i (WARNING)", result_line,
737 ntp_res.num_truechimers);
685 } else if (tresult == STATE_CRITICAL) { 738 } else if (tresult == STATE_CRITICAL) {
686 xasprintf(&result_line, "%s, truechimers=%i (CRITICAL)", result_line, num_truechimers); 739 xasprintf(&result_line, "%s, truechimers=%i (CRITICAL)", result_line,
740 ntp_res.num_truechimers);
687 } else { 741 } else {
688 xasprintf(&result_line, "%s, truechimers=%i", result_line, num_truechimers); 742 xasprintf(&result_line, "%s, truechimers=%i", result_line, ntp_res.num_truechimers);
689 } 743 }
690 xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_truechimers(num_truechimers)); 744 xasprintf(&perfdata_line, "%s %s", perfdata_line,
745 perfd_truechimers(ntp_res.num_truechimers, config.do_truechimers,
746 config.truechimer_thresholds));
691 } 747 }
748
692 printf("%s|%s\n", result_line, perfdata_line); 749 printf("%s|%s\n", result_line, perfdata_line);
693 750
694 if (server_address != NULL) 751 if (config.server_address != NULL) {
695 free(server_address); 752 free(config.server_address);
696 return result; 753 }
754
755 exit(result);
697} 756}
698 757
699void print_help(void) { 758void print_help(void) {
@@ -712,7 +771,8 @@ void print_help(void) {
712 printf(UT_IPv46); 771 printf(UT_IPv46);
713 printf(UT_HOST_PORT, 'p', "123"); 772 printf(UT_HOST_PORT, 'p', "123");
714 printf(" %s\n", "-q, --quiet"); 773 printf(" %s\n", "-q, --quiet");
715 printf(" %s\n", _("Returns UNKNOWN instead of CRITICAL or WARNING if server isn't synchronized")); 774 printf(" %s\n",
775 _("Returns UNKNOWN instead of CRITICAL or WARNING if server isn't synchronized"));
716 printf(" %s\n", "-w, --warning=THRESHOLD"); 776 printf(" %s\n", "-w, --warning=THRESHOLD");
717 printf(" %s\n", _("Offset to result in warning status (seconds)")); 777 printf(" %s\n", _("Offset to result in warning status (seconds)"));
718 printf(" %s\n", "-c, --critical=THRESHOLD"); 778 printf(" %s\n", "-c, --critical=THRESHOLD");
@@ -749,7 +809,8 @@ void print_help(void) {
749 printf(" %s\n", _("Simple NTP server check:")); 809 printf(" %s\n", _("Simple NTP server check:"));
750 printf(" %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1")); 810 printf(" %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1"));
751 printf("\n"); 811 printf("\n");
752 printf(" %s\n", _("Check jitter too, avoiding critical notifications if jitter isn't available")); 812 printf(" %s\n",
813 _("Check jitter too, avoiding critical notifications if jitter isn't available"));
753 printf(" %s\n", _("(See Notes above for more details on thresholds formats):")); 814 printf(" %s\n", _("(See Notes above for more details on thresholds formats):"));
754 printf(" %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1 -j -1:100 -k -1:200")); 815 printf(" %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1 -j -1:100 -k -1:200"));
755 printf("\n"); 816 printf("\n");