Merge pull request #2045 from RincewindsHat/fix/calloc_argument_order
[monitoring-plugins.git] / plugins / check_ntp_peer.c
blobf99e5032673ccc715f066dd38de731109024eade
1 /*****************************************************************************
3 * Monitoring check_ntp_peer plugin
5 * License: GPL
6 * Copyright (c) 2006 Sean Finney <seanius@seanius.net>
7 * Copyright (c) 2006-2024 Monitoring Plugins Development Team
9 * Description:
11 * This file contains the check_ntp_peer plugin
13 * This plugin checks an NTP server independent of any commandline
14 * programs or external libraries.
16 * Use this plugin to check the health of an NTP server. It supports
17 * checking the offset with the sync peer, the jitter and stratum. This
18 * plugin will not check the clock offset between the local host and NTP
19 * server; please use check_ntp_time for that purpose.
22 * This program is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 *****************************************************************************/
38 const char *progname = "check_ntp_peer";
39 const char *copyright = "2006-2024";
40 const char *email = "devel@monitoring-plugins.org";
42 #include "common.h"
43 #include "netutils.h"
44 #include "utils.h"
46 static char *server_address = NULL;
47 static int port = 123;
48 static int verbose = 0;
49 static bool quiet = false;
50 static char *owarn = "60";
51 static char *ocrit = "120";
52 static bool do_stratum = false;
53 static char *swarn = "-1:16";
54 static char *scrit = "-1:16";
55 static bool do_jitter = false;
56 static char *jwarn = "-1:5000";
57 static char *jcrit = "-1:10000";
58 static bool do_truechimers = false;
59 static char *twarn = "0:";
60 static char *tcrit = "0:";
61 static bool syncsource_found = false;
62 static bool li_alarm = false;
64 static int process_arguments(int /*argc*/, char ** /*argv*/);
65 static thresholds *offset_thresholds = NULL;
66 static thresholds *jitter_thresholds = NULL;
67 static thresholds *stratum_thresholds = NULL;
68 static thresholds *truechimer_thresholds = NULL;
69 static void print_help(void);
70 void print_usage(void);
72 /* max size of control message data */
73 #define MAX_CM_SIZE 468
75 /* this structure holds everything in an ntp control message as per rfc1305 */
76 typedef struct {
77 uint8_t flags; /* byte with leapindicator,vers,mode. see macros */
78 uint8_t op; /* R,E,M bits and Opcode */
79 uint16_t seq; /* Packet sequence */
80 uint16_t status; /* Clock status */
81 uint16_t assoc; /* Association */
82 uint16_t offset; /* Similar to TCP sequence # */
83 uint16_t count; /* # bytes of data */
84 char data[MAX_CM_SIZE]; /* ASCII data of the request */
85 /* NB: not necessarily NULL terminated! */
86 } ntp_control_message;
88 /* this is an association/status-word pair found in control packet responses */
89 typedef struct {
90 uint16_t assoc;
91 uint16_t status;
92 } ntp_assoc_status_pair;
94 /* bits 1,2 are the leap indicator */
95 #define LI_MASK 0xc0
96 #define LI(x) ((x & LI_MASK) >> 6)
97 #define LI_SET(x, y) \
98 do { \
99 x |= ((y << 6) & LI_MASK); \
100 } while (0)
101 /* and these are the values of the leap indicator */
102 #define LI_NOWARNING 0x00
103 #define LI_EXTRASEC 0x01
104 #define LI_MISSINGSEC 0x02
105 #define LI_ALARM 0x03
106 /* bits 3,4,5 are the ntp version */
107 #define VN_MASK 0x38
108 #define VN(x) ((x & VN_MASK) >> 3)
109 #define VN_SET(x, y) \
110 do { \
111 x |= ((y << 3) & VN_MASK); \
112 } while (0)
113 #define VN_RESERVED 0x02
114 /* bits 6,7,8 are the ntp mode */
115 #define MODE_MASK 0x07
116 #define MODE(x) (x & MODE_MASK)
117 #define MODE_SET(x, y) \
118 do { \
119 x |= (y & MODE_MASK); \
120 } while (0)
121 /* here are some values */
122 #define MODE_CLIENT 0x03
123 #define MODE_CONTROLMSG 0x06
124 /* In control message, bits 8-10 are R,E,M bits */
125 #define REM_MASK 0xe0
126 #define REM_RESP 0x80
127 #define REM_ERROR 0x40
128 #define REM_MORE 0x20
129 /* In control message, bits 11 - 15 are opcode */
130 #define OP_MASK 0x1f
131 #define OP_SET(x, y) \
132 do { \
133 x |= (y & OP_MASK); \
134 } while (0)
135 #define OP_READSTAT 0x01
136 #define OP_READVAR 0x02
137 /* In peer status bytes, bits 6,7,8 determine clock selection status */
138 #define PEER_SEL(x) ((ntohs(x) >> 8) & 0x07)
139 #define PEER_TRUECHIMER 0x02
140 #define PEER_INCLUDED 0x04
141 #define PEER_SYNCSOURCE 0x06
143 /* 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.
146 #define SIZEOF_NTPCM(m) (12 + ntohs(m.count) + ((ntohs(m.count) % 4) ? 4 - (ntohs(m.count) % 4) : 0))
148 /* finally, a little helper or two for debugging: */
149 #define DBG(x) \
150 do { \
151 if (verbose > 1) { \
152 x; \
154 } while (0);
155 #define PRINTSOCKADDR(x) \
156 do { \
157 printf("%u.%u.%u.%u", (x >> 24) & 0xff, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff); \
158 } while (0);
160 void print_ntp_control_message(const ntp_control_message *p) {
161 printf("control packet contents:\n");
162 printf("\tflags: 0x%.2x , 0x%.2x\n", p->flags, p->op);
163 printf("\t li=%d (0x%.2x)\n", LI(p->flags), p->flags & LI_MASK);
164 printf("\t vn=%d (0x%.2x)\n", VN(p->flags), p->flags & VN_MASK);
165 printf("\t mode=%d (0x%.2x)\n", MODE(p->flags), p->flags & MODE_MASK);
166 printf("\t response=%d (0x%.2x)\n", (p->op & REM_RESP) > 0, p->op & REM_RESP);
167 printf("\t more=%d (0x%.2x)\n", (p->op & REM_MORE) > 0, p->op & REM_MORE);
168 printf("\t error=%d (0x%.2x)\n", (p->op & REM_ERROR) > 0, p->op & REM_ERROR);
169 printf("\t op=%d (0x%.2x)\n", p->op & OP_MASK, p->op & OP_MASK);
170 printf("\tsequence: %d (0x%.2x)\n", ntohs(p->seq), ntohs(p->seq));
171 printf("\tstatus: %d (0x%.2x)\n", ntohs(p->status), ntohs(p->status));
172 printf("\tassoc: %d (0x%.2x)\n", ntohs(p->assoc), ntohs(p->assoc));
173 printf("\toffset: %d (0x%.2x)\n", ntohs(p->offset), ntohs(p->offset));
174 printf("\tcount: %d (0x%.2x)\n", ntohs(p->count), ntohs(p->count));
176 int numpeers = ntohs(p->count) / (sizeof(ntp_assoc_status_pair));
177 if (p->op & REM_RESP && p->op & OP_READSTAT) {
178 const ntp_assoc_status_pair *peer = (ntp_assoc_status_pair *)p->data;
179 for (int i = 0; i < numpeers; i++) {
180 printf("\tpeer id %.2x status %.2x", ntohs(peer[i].assoc), ntohs(peer[i].status));
181 if (PEER_SEL(peer[i].status) >= PEER_SYNCSOURCE) {
182 printf(" <-- current sync source");
183 } else if (PEER_SEL(peer[i].status) >= PEER_INCLUDED) {
184 printf(" <-- current sync candidate");
185 } else if (PEER_SEL(peer[i].status) >= PEER_TRUECHIMER) {
186 printf(" <-- outlyer, but truechimer");
188 printf("\n");
193 void setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq) {
194 memset(p, 0, sizeof(ntp_control_message));
195 LI_SET(p->flags, LI_NOWARNING);
196 VN_SET(p->flags, VN_RESERVED);
197 MODE_SET(p->flags, MODE_CONTROLMSG);
198 OP_SET(p->op, opcode);
199 p->seq = htons(seq);
200 /* Remaining fields are zero for requests */
203 /* This function does all the actual work; roughly here's what it does
204 * beside setting the offset, jitter and stratum passed as argument:
205 * - offset can be negative, so if it cannot get the offset, offset_result
206 * is set to UNKNOWN, otherwise OK.
207 * - jitter and stratum are set to -1 if they cannot be retrieved so any
208 * positive value means a success retrieving the value.
209 * - status is set to WARNING if there's no sync.peer (otherwise OK) and is
210 * the return value of the function.
211 * 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
213 * so I left it alone */
214 int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum, int *num_truechimers) {
215 *offset_result = STATE_UNKNOWN;
216 *jitter = *stratum = -1;
217 *num_truechimers = 0;
219 /* Long-winded explanation:
220 * Getting the sync peer offset, jitter and stratum requires a number of
221 * steps:
222 * 1) Send a READSTAT request.
223 * 2) Interpret the READSTAT reply
224 * a) The data section contains a list of peer identifiers (16 bits)
225 * and associated status words (16 bits)
226 * b) We want the value of 0x06 in the SEL (peer selection) value,
227 * which means "current synchronizatin source". If that's missing,
228 * we take anything better than 0x04 (see the rfc for details) but
229 * set a minimum of warning.
230 * 3) Send a READVAR request for information on each peer identified
231 * in 2b greater than the minimum selection value.
232 * 4) Extract the offset, jitter and stratum value from the data[]
233 * (it's ASCII)
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;
243 my_udp_connect(server_address, port, &conn);
245 /* keep sending requests until the server stops setting the
246 * REM_MORE bit, though usually this is only 1 packet. */
247 ntp_control_message req;
248 do {
249 setup_control_request(&req, OP_READSTAT, 1);
250 DBG(printf("sending READSTAT request"));
251 write(conn, &req, SIZEOF_NTPCM(req));
252 DBG(print_ntp_control_message(&req));
254 do {
255 /* Attempt to read the largest size packet possible */
256 req.count = htons(MAX_CM_SIZE);
257 DBG(printf("receiving READSTAT response"))
258 if (read(conn, &req, SIZEOF_NTPCM(req)) == -1)
259 die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n");
260 DBG(print_ntp_control_message(&req));
261 /* discard obviously invalid packets */
262 if (ntohs(req.count) > MAX_CM_SIZE)
263 die(STATE_CRITICAL, "NTP CRITICAL: Invalid packet received from NTP server\n");
264 } while (!(req.op & OP_READSTAT && ntohs(req.seq) == 1));
266 if (LI(req.flags) == LI_ALARM)
267 li_alarm = true;
268 /* Each peer identifier is 4 bytes in the data section, which
269 * we represent as a ntp_assoc_status_pair datatype.
271 peers_size += ntohs(req.count);
272 if ((tmp = realloc(peers, peers_size)) == NULL)
273 free(peers), die(STATE_UNKNOWN, "can not (re)allocate 'peers' buffer\n");
274 peers = tmp;
275 memcpy((void *)((ptrdiff_t)peers + peer_offset), (void *)req.data, ntohs(req.count));
276 npeers = peers_size / sizeof(ntp_assoc_status_pair);
277 peer_offset += ntohs(req.count);
278 } while (req.op & REM_MORE);
280 /* 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
282 * a warning but go ahead with the check on them. */
283 for (int i = 0; i < npeers; i++) {
284 if (PEER_SEL(peers[i].status) >= PEER_TRUECHIMER) {
285 (*num_truechimers)++;
286 if (PEER_SEL(peers[i].status) >= PEER_INCLUDED) {
287 num_candidates++;
288 if (PEER_SEL(peers[i].status) >= PEER_SYNCSOURCE) {
289 syncsource_found = true;
290 min_peer_sel = PEER_SYNCSOURCE;
296 if (verbose)
297 printf("%d candidate peers available\n", num_candidates);
298 if (verbose && syncsource_found)
299 printf("synchronization source found\n");
301 int status = STATE_OK;
302 if (!syncsource_found) {
303 status = STATE_WARNING;
304 if (verbose)
305 printf("warning: no synchronization source found\n");
307 if (li_alarm) {
308 status = STATE_WARNING;
309 if (verbose)
310 printf("warning: LI_ALARM bit is set\n");
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 */
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) {
319 if (verbose)
320 printf("Getting offset, jitter and stratum for peer %.2x\n", ntohs(peers[i].assoc));
321 xasprintf(&data, "");
322 do {
323 setup_control_request(&req, OP_READVAR, 2);
324 req.assoc = peers[i].assoc;
325 /* Putting the wanted variable names in the request
326 * cause the server to provide _only_ the requested values.
327 * thus reducing net traffic, guaranteeing us only a single
328 * datagram in reply, and making interpretation much simpler
330 /* Older servers doesn't know what jitter is, so if we get an
331 * error on the first pass we redo it with "dispersion" */
332 strncpy(req.data, getvar, MAX_CM_SIZE - 1);
333 req.count = htons(strlen(getvar));
334 DBG(printf("sending READVAR request...\n"));
335 write(conn, &req, SIZEOF_NTPCM(req));
336 DBG(print_ntp_control_message(&req));
338 do {
339 req.count = htons(MAX_CM_SIZE);
340 DBG(printf("receiving READVAR response...\n"));
341 read(conn, &req, SIZEOF_NTPCM(req));
342 DBG(print_ntp_control_message(&req));
343 } while (!(req.op & OP_READVAR && ntohs(req.seq) == 2));
345 if (!(req.op & REM_ERROR))
346 xasprintf(&data, "%s%s", data, req.data);
347 } while (req.op & REM_MORE);
349 if (req.op & REM_ERROR) {
350 if (strstr(getvar, "jitter")) {
351 if (verbose)
352 printf("The command failed. This is usually caused by servers refusing the 'jitter'\nvariable. Restarting with "
353 "'dispersion'...\n");
354 getvar = "stratum,offset,dispersion";
355 i--;
356 continue;
358 if (strlen(getvar)) {
359 if (verbose)
360 printf("Server didn't like dispersion either; will retrieve everything\n");
361 getvar = "";
362 i--;
363 continue;
367 if (verbose > 1)
368 printf("Server responded: >>>%s<<<\n", data);
370 double tmp_offset = 0;
371 char *value;
372 char *nptr;
373 /* get the offset */
374 if (verbose)
375 printf("parsing offset from peer %.2x: ", ntohs(peers[i].assoc));
377 value = np_extract_ntpvar(data, "offset");
378 nptr = NULL;
379 /* Convert the value if we have one */
380 if (value != NULL)
381 tmp_offset = strtod(value, &nptr) / 1000;
382 /* If value is null or no conversion was performed */
383 if (value == NULL || value == nptr) {
384 if (verbose)
385 printf("error: unable to read server offset response.\n");
386 } else {
387 if (verbose)
388 printf("%.10g\n", tmp_offset);
389 if (*offset_result == STATE_UNKNOWN || fabs(tmp_offset) < fabs(*offset)) {
390 *offset = tmp_offset;
391 *offset_result = STATE_OK;
392 } else {
393 /* Skip this one; move to the next */
394 continue;
398 if (do_jitter) {
399 /* get the jitter */
400 if (verbose) {
401 printf("parsing %s from peer %.2x: ", strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter",
402 ntohs(peers[i].assoc));
404 value = np_extract_ntpvar(data, strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter");
405 nptr = NULL;
406 /* Convert the value if we have one */
407 if (value != NULL)
408 *jitter = strtod(value, &nptr);
409 /* If value is null or no conversion was performed */
410 if (value == NULL || value == nptr) {
411 if (verbose)
412 printf("error: unable to read server jitter/dispersion response.\n");
413 *jitter = -1;
414 } else if (verbose) {
415 printf("%.10g\n", *jitter);
419 if (do_stratum) {
420 /* get the stratum */
421 if (verbose) {
422 printf("parsing stratum from peer %.2x: ", ntohs(peers[i].assoc));
424 value = np_extract_ntpvar(data, "stratum");
425 nptr = NULL;
426 /* Convert the value if we have one */
427 if (value != NULL)
428 *stratum = strtol(value, &nptr, 10);
429 if (value == NULL || value == nptr) {
430 if (verbose)
431 printf("error: unable to read server stratum response.\n");
432 *stratum = -1;
433 } else {
434 if (verbose)
435 printf("%i\n", *stratum);
438 } /* if (PEER_SEL(peers[i].status) >= min_peer_sel) */
439 } /* for (i = 0; i < npeers; i++) */
441 close(conn);
442 if (peers != NULL)
443 free(peers);
445 return status;
448 int process_arguments(int argc, char **argv) {
449 static struct option longopts[] = {
450 {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {"verbose", no_argument, 0, 'v'},
451 {"use-ipv4", no_argument, 0, '4'}, {"use-ipv6", no_argument, 0, '6'}, {"quiet", no_argument, 0, 'q'},
452 {"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, {"swarn", required_argument, 0, 'W'},
453 {"scrit", required_argument, 0, 'C'}, {"jwarn", required_argument, 0, 'j'}, {"jcrit", required_argument, 0, 'k'},
454 {"twarn", required_argument, 0, 'm'}, {"tcrit", required_argument, 0, 'n'}, {"timeout", required_argument, 0, 't'},
455 {"hostname", required_argument, 0, 'H'}, {"port", required_argument, 0, 'p'}, {0, 0, 0, 0}};
457 if (argc < 2)
458 usage("\n");
460 while (true) {
461 int option = 0;
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)
464 break;
466 switch (option_char) {
467 case 'h':
468 print_help();
469 exit(STATE_UNKNOWN);
470 break;
471 case 'V':
472 print_revision(progname, NP_VERSION);
473 exit(STATE_UNKNOWN);
474 break;
475 case 'v':
476 verbose++;
477 break;
478 case 'q':
479 quiet = true;
480 break;
481 case 'w':
482 owarn = optarg;
483 break;
484 case 'c':
485 ocrit = optarg;
486 break;
487 case 'W':
488 do_stratum = true;
489 swarn = optarg;
490 break;
491 case 'C':
492 do_stratum = true;
493 scrit = optarg;
494 break;
495 case 'j':
496 do_jitter = true;
497 jwarn = optarg;
498 break;
499 case 'k':
500 do_jitter = true;
501 jcrit = optarg;
502 break;
503 case 'm':
504 do_truechimers = true;
505 twarn = optarg;
506 break;
507 case 'n':
508 do_truechimers = true;
509 tcrit = optarg;
510 break;
511 case 'H':
512 if (!is_host(optarg))
513 usage2(_("Invalid hostname/address"), optarg);
514 server_address = strdup(optarg);
515 break;
516 case 'p':
517 port = atoi(optarg);
518 break;
519 case 't':
520 socket_timeout = atoi(optarg);
521 break;
522 case '4':
523 address_family = AF_INET;
524 break;
525 case '6':
526 #ifdef USE_IPV6
527 address_family = AF_INET6;
528 #else
529 usage4(_("IPv6 support not available"));
530 #endif
531 break;
532 case '?':
533 /* print short usage statement if args not parsable */
534 usage5();
535 break;
539 if (server_address == NULL) {
540 usage4(_("Hostname was not supplied"));
543 return 0;
546 char *perfd_offset(double offset) {
547 return fperfdata("offset", offset, "s", true, offset_thresholds->warning->end, true, offset_thresholds->critical->end, false, 0, false,
551 char *perfd_jitter(double jitter) {
552 return fperfdata("jitter", jitter, "", do_jitter, jitter_thresholds->warning->end, do_jitter, jitter_thresholds->critical->end, true, 0,
553 false, 0);
556 char *perfd_stratum(int stratum) {
557 return perfdata("stratum", stratum, "", do_stratum, (int)stratum_thresholds->warning->end, do_stratum,
558 (int)stratum_thresholds->critical->end, true, 0, true, 16);
561 char *perfd_truechimers(int num_truechimers) {
562 return perfdata("truechimers", num_truechimers, "", do_truechimers, (int)truechimer_thresholds->warning->end, do_truechimers,
563 (int)truechimer_thresholds->critical->end, true, 0, false, 0);
566 int main(int argc, char *argv[]) {
567 setlocale(LC_ALL, "");
568 bindtextdomain(PACKAGE, LOCALEDIR);
569 textdomain(PACKAGE);
571 /* Parse extra opts if any */
572 argv = np_extra_opts(&argc, argv, progname);
574 if (process_arguments(argc, argv) == ERROR)
575 usage4(_("Could not parse arguments"));
577 set_thresholds(&offset_thresholds, owarn, ocrit);
578 set_thresholds(&jitter_thresholds, jwarn, jcrit);
579 set_thresholds(&stratum_thresholds, swarn, scrit);
580 set_thresholds(&truechimer_thresholds, twarn, tcrit);
582 /* initialize alarm signal handling */
583 signal(SIGALRM, socket_timeout_alarm_handler);
585 /* set socket timeout */
586 alarm(socket_timeout);
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) */
594 int result = ntp_request(&offset, &offset_result, &jitter, &stratum, &num_truechimers);
596 if (offset_result == STATE_UNKNOWN) {
597 /* if there's no sync peer (this overrides ntp_request output): */
598 result = (quiet ? STATE_UNKNOWN : STATE_CRITICAL);
599 } else {
600 /* Be quiet if there's no candidates either */
601 if (quiet && result == STATE_WARNING)
602 result = STATE_UNKNOWN;
603 result = max_state_alt(result, get_status(fabs(offset), offset_thresholds));
606 int oresult = result;
608 int tresult = STATE_UNKNOWN;
610 if (do_truechimers) {
611 tresult = get_status(num_truechimers, truechimer_thresholds);
612 result = max_state_alt(result, tresult);
615 int sresult = STATE_UNKNOWN;
617 if (do_stratum) {
618 sresult = get_status(stratum, stratum_thresholds);
619 result = max_state_alt(result, sresult);
622 int jresult = STATE_UNKNOWN;
624 if (do_jitter) {
625 jresult = get_status(jitter, jitter_thresholds);
626 result = max_state_alt(result, jresult);
629 char *result_line;
630 switch (result) {
631 case STATE_CRITICAL:
632 xasprintf(&result_line, _("NTP CRITICAL:"));
633 break;
634 case STATE_WARNING:
635 xasprintf(&result_line, _("NTP WARNING:"));
636 break;
637 case STATE_OK:
638 xasprintf(&result_line, _("NTP OK:"));
639 break;
640 default:
641 xasprintf(&result_line, _("NTP UNKNOWN:"));
642 break;
644 if (!syncsource_found)
645 xasprintf(&result_line, "%s %s,", result_line, _("Server not synchronized"));
646 else if (li_alarm)
647 xasprintf(&result_line, "%s %s,", result_line, _("Server has the LI_ALARM bit set"));
649 char *perfdata_line;
650 if (offset_result == STATE_UNKNOWN) {
651 xasprintf(&result_line, "%s %s", result_line, _("Offset unknown"));
652 xasprintf(&perfdata_line, "");
653 } else if (oresult == STATE_WARNING) {
654 xasprintf(&result_line, "%s %s %.10g secs (WARNING)", result_line, _("Offset"), offset);
655 } else if (oresult == STATE_CRITICAL) {
656 xasprintf(&result_line, "%s %s %.10g secs (CRITICAL)", result_line, _("Offset"), offset);
657 } else {
658 xasprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset);
660 xasprintf(&perfdata_line, "%s", perfd_offset(offset));
662 if (do_jitter) {
663 if (jresult == STATE_WARNING) {
664 xasprintf(&result_line, "%s, jitter=%f (WARNING)", result_line, jitter);
665 } else if (jresult == STATE_CRITICAL) {
666 xasprintf(&result_line, "%s, jitter=%f (CRITICAL)", result_line, jitter);
667 } else {
668 xasprintf(&result_line, "%s, jitter=%f", result_line, jitter);
670 xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_jitter(jitter));
672 if (do_stratum) {
673 if (sresult == STATE_WARNING) {
674 xasprintf(&result_line, "%s, stratum=%i (WARNING)", result_line, stratum);
675 } else if (sresult == STATE_CRITICAL) {
676 xasprintf(&result_line, "%s, stratum=%i (CRITICAL)", result_line, stratum);
677 } else {
678 xasprintf(&result_line, "%s, stratum=%i", result_line, stratum);
680 xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_stratum(stratum));
682 if (do_truechimers) {
683 if (tresult == STATE_WARNING) {
684 xasprintf(&result_line, "%s, truechimers=%i (WARNING)", result_line, num_truechimers);
685 } else if (tresult == STATE_CRITICAL) {
686 xasprintf(&result_line, "%s, truechimers=%i (CRITICAL)", result_line, num_truechimers);
687 } else {
688 xasprintf(&result_line, "%s, truechimers=%i", result_line, num_truechimers);
690 xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_truechimers(num_truechimers));
692 printf("%s|%s\n", result_line, perfdata_line);
694 if (server_address != NULL)
695 free(server_address);
696 return result;
699 void print_help(void) {
700 print_revision(progname, NP_VERSION);
702 printf("Copyright (c) 2006 Sean Finney\n");
703 printf(COPYRIGHT, copyright, email);
705 printf("%s\n", _("This plugin checks the selected ntp server"));
707 printf("\n\n");
709 print_usage();
710 printf(UT_HELP_VRSN);
711 printf(UT_EXTRA_OPTS);
712 printf(UT_IPv46);
713 printf(UT_HOST_PORT, 'p', "123");
714 printf(" %s\n", "-q, --quiet");
715 printf(" %s\n", _("Returns UNKNOWN instead of CRITICAL or WARNING if server isn't synchronized"));
716 printf(" %s\n", "-w, --warning=THRESHOLD");
717 printf(" %s\n", _("Offset to result in warning status (seconds)"));
718 printf(" %s\n", "-c, --critical=THRESHOLD");
719 printf(" %s\n", _("Offset to result in critical status (seconds)"));
720 printf(" %s\n", "-W, --swarn=THRESHOLD");
721 printf(" %s\n", _("Warning threshold for stratum of server's synchronization peer"));
722 printf(" %s\n", "-C, --scrit=THRESHOLD");
723 printf(" %s\n", _("Critical threshold for stratum of server's synchronization peer"));
724 printf(" %s\n", "-j, --jwarn=THRESHOLD");
725 printf(" %s\n", _("Warning threshold for jitter"));
726 printf(" %s\n", "-k, --jcrit=THRESHOLD");
727 printf(" %s\n", _("Critical threshold for jitter"));
728 printf(" %s\n", "-m, --twarn=THRESHOLD");
729 printf(" %s\n", _("Warning threshold for number of usable time sources (\"truechimers\")"));
730 printf(" %s\n", "-n, --tcrit=THRESHOLD");
731 printf(" %s\n", _("Critical threshold for number of usable time sources (\"truechimers\")"));
732 printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
733 printf(UT_VERBOSE);
735 printf("\n");
736 printf("%s\n", _("This plugin checks an NTP server independent of any commandline"));
737 printf("%s\n\n", _("programs or external libraries."));
739 printf("%s\n", _("Notes:"));
740 printf(" %s\n", _("Use this plugin to check the health of an NTP server. It supports"));
741 printf(" %s\n", _("checking the offset with the sync peer, the jitter and stratum. This"));
742 printf(" %s\n", _("plugin will not check the clock offset between the local host and NTP"));
743 printf(" %s\n", _("server; please use check_ntp_time for that purpose."));
744 printf("\n");
745 printf(UT_THRESHOLDS_NOTES);
747 printf("\n");
748 printf("%s\n", _("Examples:"));
749 printf(" %s\n", _("Simple NTP server check:"));
750 printf(" %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1"));
751 printf("\n");
752 printf(" %s\n", _("Check jitter too, avoiding critical notifications if jitter isn't available"));
753 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"));
755 printf("\n");
756 printf(" %s\n", _("Only check the number of usable time sources (\"truechimers\"):"));
757 printf(" %s\n", ("./check_ntp_peer -H ntpserv -m @5 -n @3"));
758 printf("\n");
759 printf(" %s\n", _("Check only stratum:"));
760 printf(" %s\n", ("./check_ntp_peer -H ntpserv -W 4 -C 6"));
762 printf(UT_SUPPORT);
765 void print_usage(void) {
766 printf("%s\n", _("Usage:"));
767 printf(" %s -H <host> [-4|-6] [-w <warn>] [-c <crit>] [-W <warn>] [-C <crit>]\n", progname);
768 printf(" [-j <warn>] [-k <crit>] [-v verbose]\n");