Fix for SSL Versioning when multiple options are used.
[monitoring-plugins.git] / plugins / check_ntp_peer.c
blob16cce4578d11850b1ea7410079753cbeef0d245c
1 /*****************************************************************************
2 *
3 * Nagios check_ntp_peer plugin
4 *
5 * License: GPL
6 * Copyright (c) 2006 Sean Finney <seanius@seanius.net>
7 * Copyright (c) 2006-2008 Nagios Plugins Development Team
8 *
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-2008";
40 const char *email = "devel@nagios-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 int quiet=0;
50 static short do_offset=0;
51 static char *owarn="60";
52 static char *ocrit="120";
53 static short do_stratum=0;
54 static char *swarn="-1:16";
55 static char *scrit="-1:16";
56 static short do_jitter=0;
57 static char *jwarn="-1:5000";
58 static char *jcrit="-1:10000";
59 static short do_truechimers=0;
60 static char *twarn="0:";
61 static char *tcrit="0:";
62 static int syncsource_found=0;
63 static int li_alarm=0;
65 int process_arguments (int, char **);
66 thresholds *offset_thresholds = NULL;
67 thresholds *jitter_thresholds = NULL;
68 thresholds *stratum_thresholds = NULL;
69 thresholds *truechimer_thresholds = NULL;
70 void print_help (void);
71 void print_usage (void);
73 /* max size of control message data */
74 #define MAX_CM_SIZE 468
76 /* this structure holds everything in an ntp control message as per rfc1305 */
77 typedef struct {
78 uint8_t flags; /* byte with leapindicator,vers,mode. see macros */
79 uint8_t op; /* R,E,M bits and Opcode */
80 uint16_t seq; /* Packet sequence */
81 uint16_t status; /* Clock status */
82 uint16_t assoc; /* Association */
83 uint16_t offset; /* Similar to TCP sequence # */
84 uint16_t count; /* # bytes of data */
85 char data[MAX_CM_SIZE]; /* ASCII data of the request */
86 /* NB: not necessarily NULL terminated! */
87 } ntp_control_message;
89 /* this is an association/status-word pair found in control packet reponses */
90 typedef struct {
91 uint16_t assoc;
92 uint16_t status;
93 } ntp_assoc_status_pair;
95 /* bits 1,2 are the leap indicator */
96 #define LI_MASK 0xc0
97 #define LI(x) ((x&LI_MASK)>>6)
98 #define LI_SET(x,y) do{ x |= ((y<<6)&LI_MASK); }while(0)
99 /* and these are the values of the leap indicator */
100 #define LI_NOWARNING 0x00
101 #define LI_EXTRASEC 0x01
102 #define LI_MISSINGSEC 0x02
103 #define LI_ALARM 0x03
104 /* bits 3,4,5 are the ntp version */
105 #define VN_MASK 0x38
106 #define VN(x) ((x&VN_MASK)>>3)
107 #define VN_SET(x,y) do{ x |= ((y<<3)&VN_MASK); }while(0)
108 #define VN_RESERVED 0x02
109 /* bits 6,7,8 are the ntp mode */
110 #define MODE_MASK 0x07
111 #define MODE(x) (x&MODE_MASK)
112 #define MODE_SET(x,y) do{ x |= (y&MODE_MASK); }while(0)
113 /* here are some values */
114 #define MODE_CLIENT 0x03
115 #define MODE_CONTROLMSG 0x06
116 /* In control message, bits 8-10 are R,E,M bits */
117 #define REM_MASK 0xe0
118 #define REM_RESP 0x80
119 #define REM_ERROR 0x40
120 #define REM_MORE 0x20
121 /* In control message, bits 11 - 15 are opcode */
122 #define OP_MASK 0x1f
123 #define OP_SET(x,y) do{ x |= (y&OP_MASK); }while(0)
124 #define OP_READSTAT 0x01
125 #define OP_READVAR 0x02
126 /* In peer status bytes, bits 6,7,8 determine clock selection status */
127 #define PEER_SEL(x) ((ntohs(x)>>8)&0x07)
128 #define PEER_TRUECHIMER 0x02
129 #define PEER_INCLUDED 0x04
130 #define PEER_SYNCSOURCE 0x06
132 /* NTP control message header is 12 bytes, plus any data in the data
133 * field, plus null padding to the nearest 32-bit boundary per rfc.
135 #define SIZEOF_NTPCM(m) (12+ntohs(m.count)+((ntohs(m.count)%4)?4-(ntohs(m.count)%4):0))
137 /* finally, a little helper or two for debugging: */
138 #define DBG(x) do{if(verbose>1){ x; }}while(0);
139 #define PRINTSOCKADDR(x) \
140 do{ \
141 printf("%u.%u.%u.%u", (x>>24)&0xff, (x>>16)&0xff, (x>>8)&0xff, x&0xff);\
142 }while(0);
144 void print_ntp_control_message(const ntp_control_message *p){
145 int i=0, numpeers=0;
146 const ntp_assoc_status_pair *peer=NULL;
148 printf("control packet contents:\n");
149 printf("\tflags: 0x%.2x , 0x%.2x\n", p->flags, p->op);
150 printf("\t li=%d (0x%.2x)\n", LI(p->flags), p->flags&LI_MASK);
151 printf("\t vn=%d (0x%.2x)\n", VN(p->flags), p->flags&VN_MASK);
152 printf("\t mode=%d (0x%.2x)\n", MODE(p->flags), p->flags&MODE_MASK);
153 printf("\t response=%d (0x%.2x)\n", (p->op&REM_RESP)>0, p->op&REM_RESP);
154 printf("\t more=%d (0x%.2x)\n", (p->op&REM_MORE)>0, p->op&REM_MORE);
155 printf("\t error=%d (0x%.2x)\n", (p->op&REM_ERROR)>0, p->op&REM_ERROR);
156 printf("\t op=%d (0x%.2x)\n", p->op&OP_MASK, p->op&OP_MASK);
157 printf("\tsequence: %d (0x%.2x)\n", ntohs(p->seq), ntohs(p->seq));
158 printf("\tstatus: %d (0x%.2x)\n", ntohs(p->status), ntohs(p->status));
159 printf("\tassoc: %d (0x%.2x)\n", ntohs(p->assoc), ntohs(p->assoc));
160 printf("\toffset: %d (0x%.2x)\n", ntohs(p->offset), ntohs(p->offset));
161 printf("\tcount: %d (0x%.2x)\n", ntohs(p->count), ntohs(p->count));
162 numpeers=ntohs(p->count)/(sizeof(ntp_assoc_status_pair));
163 if(p->op&REM_RESP && p->op&OP_READSTAT){
164 peer=(ntp_assoc_status_pair*)p->data;
165 for(i=0;i<numpeers;i++){
166 printf("\tpeer id %.2x status %.2x",
167 ntohs(peer[i].assoc), ntohs(peer[i].status));
168 if(PEER_SEL(peer[i].status) >= PEER_SYNCSOURCE){
169 printf(" <-- current sync source");
170 } else if(PEER_SEL(peer[i].status) >= PEER_INCLUDED){
171 printf(" <-- current sync candidate");
172 } else if(PEER_SEL(peer[i].status) >= PEER_TRUECHIMER){
173 printf(" <-- outlyer, but truechimer");
175 printf("\n");
180 void
181 setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq){
182 memset(p, 0, sizeof(ntp_control_message));
183 LI_SET(p->flags, LI_NOWARNING);
184 VN_SET(p->flags, VN_RESERVED);
185 MODE_SET(p->flags, MODE_CONTROLMSG);
186 OP_SET(p->op, opcode);
187 p->seq = htons(seq);
188 /* Remaining fields are zero for requests */
191 /* This function does all the actual work; roughly here's what it does
192 * beside setting the offest, jitter and stratum passed as argument:
193 * - offset can be negative, so if it cannot get the offset, offset_result
194 * is set to UNKNOWN, otherwise OK.
195 * - jitter and stratum are set to -1 if they cannot be retrieved so any
196 * positive value means a success retrieving the value.
197 * - status is set to WARNING if there's no sync.peer (otherwise OK) and is
198 * the return value of the function.
199 * status is pretty much useless as syncsource_found is a global variable
200 * used later in main to check is the server was synchronized. It works
201 * so I left it alone */
202 int ntp_request(const char *host, double *offset, int *offset_result, double *jitter, int *stratum, int *num_truechimers){
203 int conn=-1, i, npeers=0, num_candidates=0;
204 double tmp_offset = 0;
205 int min_peer_sel=PEER_INCLUDED;
206 int peers_size=0, peer_offset=0;
207 int status;
208 ntp_assoc_status_pair *peers=NULL;
209 ntp_control_message req;
210 const char *getvar = "stratum,offset,jitter";
211 char *data, *value, *nptr;
212 void *tmp;
214 status = STATE_OK;
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 my_udp_connect(server_address, port, &conn);
237 /* keep sending requests until the server stops setting the
238 * REM_MORE bit, though usually this is only 1 packet. */
240 setup_control_request(&req, OP_READSTAT, 1);
241 DBG(printf("sending READSTAT request"));
242 write(conn, &req, SIZEOF_NTPCM(req));
243 DBG(print_ntp_control_message(&req));
245 do {
246 /* Attempt to read the largest size packet possible */
247 req.count=htons(MAX_CM_SIZE);
248 DBG(printf("recieving READSTAT response"))
249 if(read(conn, &req, SIZEOF_NTPCM(req)) == -1)
250 die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n");
251 DBG(print_ntp_control_message(&req));
252 /* discard obviously invalid packets */
253 if (ntohs(req.count) > MAX_CM_SIZE)
254 die(STATE_CRITICAL, "NTP CRITICAL: Invalid packet received from NTP server\n");
255 } while (!(req.op&OP_READSTAT && ntohs(req.seq) == 1));
257 if (LI(req.flags) == LI_ALARM) li_alarm = 1;
258 /* Each peer identifier is 4 bytes in the data section, which
259 * we represent as a ntp_assoc_status_pair datatype.
261 peers_size+=ntohs(req.count);
262 if((tmp=realloc(peers, peers_size)) == NULL)
263 free(peers), die(STATE_UNKNOWN, "can not (re)allocate 'peers' buffer\n");
264 peers=tmp;
265 memcpy((void*)((ptrdiff_t)peers+peer_offset), (void*)req.data, ntohs(req.count));
266 npeers=peers_size/sizeof(ntp_assoc_status_pair);
267 peer_offset+=ntohs(req.count);
268 } while(req.op&REM_MORE);
270 /* first, let's find out if we have a sync source, or if there are
271 * at least some candidates. In the latter case we'll issue
272 * a warning but go ahead with the check on them. */
273 for (i = 0; i < npeers; i++){
274 if(PEER_SEL(peers[i].status) >= PEER_TRUECHIMER){
275 (*num_truechimers)++;
276 if(PEER_SEL(peers[i].status) >= PEER_INCLUDED){
277 num_candidates++;
278 if(PEER_SEL(peers[i].status) >= PEER_SYNCSOURCE){
279 syncsource_found=1;
280 min_peer_sel=PEER_SYNCSOURCE;
285 if(verbose) printf("%d candidate peers available\n", num_candidates);
286 if(verbose && syncsource_found) printf("synchronization source found\n");
287 if(! syncsource_found){
288 status = STATE_WARNING;
289 if(verbose) printf("warning: no synchronization source found\n");
291 if(li_alarm){
292 status = STATE_WARNING;
293 if(verbose) printf("warning: LI_ALARM bit is set\n");
297 for (i = 0; i < npeers; i++){
298 /* Only query this server if it is the current sync source */
299 /* If there's no sync.peer, query all candidates and use the best one */
300 if (PEER_SEL(peers[i].status) >= min_peer_sel){
301 if(verbose) printf("Getting offset, jitter and stratum for peer %.2x\n", ntohs(peers[i].assoc));
302 xasprintf(&data, "");
304 setup_control_request(&req, OP_READVAR, 2);
305 req.assoc = peers[i].assoc;
306 /* Putting the wanted variable names in the request
307 * cause the server to provide _only_ the requested values.
308 * thus reducing net traffic, guaranteeing us only a single
309 * datagram in reply, and making intepretation much simpler
311 /* Older servers doesn't know what jitter is, so if we get an
312 * error on the first pass we redo it with "dispersion" */
313 strncpy(req.data, getvar, MAX_CM_SIZE-1);
314 req.count = htons(strlen(getvar));
315 DBG(printf("sending READVAR request...\n"));
316 write(conn, &req, SIZEOF_NTPCM(req));
317 DBG(print_ntp_control_message(&req));
319 do {
320 req.count = htons(MAX_CM_SIZE);
321 DBG(printf("receiving READVAR response...\n"));
322 read(conn, &req, SIZEOF_NTPCM(req));
323 DBG(print_ntp_control_message(&req));
324 } while (!(req.op&OP_READVAR && ntohs(req.seq) == 2));
326 if(!(req.op&REM_ERROR))
327 xasprintf(&data, "%s%s", data, req.data);
328 } while(req.op&REM_MORE);
330 if(req.op&REM_ERROR) {
331 if(strstr(getvar, "jitter")) {
332 if(verbose) printf("The command failed. This is usually caused by servers refusing the 'jitter'\nvariable. Restarting with 'dispersion'...\n");
333 getvar = "stratum,offset,dispersion";
334 i--;
335 continue;
336 } else if(strlen(getvar)) {
337 if(verbose) printf("Server didn't like dispersion either; will retrieve everything\n");
338 getvar = "";
339 i--;
340 continue;
344 if(verbose > 1)
345 printf("Server responded: >>>%s<<<\n", data);
347 /* get the offset */
348 if(verbose)
349 printf("parsing offset from peer %.2x: ", ntohs(peers[i].assoc));
351 value = np_extract_ntpvar(data, "offset");
352 nptr=NULL;
353 /* Convert the value if we have one */
354 if(value != NULL)
355 tmp_offset = strtod(value, &nptr) / 1000;
356 /* If value is null or no conversion was performed */
357 if(value == NULL || value==nptr) {
358 if(verbose) printf("error: unable to read server offset response.\n");
359 } else {
360 if(verbose) printf("%.10g\n", tmp_offset);
361 if(*offset_result == STATE_UNKNOWN || fabs(tmp_offset) < fabs(*offset)) {
362 *offset = tmp_offset;
363 *offset_result = STATE_OK;
364 } else {
365 /* Skip this one; move to the next */
366 continue;
370 if(do_jitter) {
371 /* get the jitter */
372 if(verbose) {
373 printf("parsing %s from peer %.2x: ", strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter", ntohs(peers[i].assoc));
375 value = np_extract_ntpvar(data, strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter");
376 nptr=NULL;
377 /* Convert the value if we have one */
378 if(value != NULL)
379 *jitter = strtod(value, &nptr);
380 /* If value is null or no conversion was performed */
381 if(value == NULL || value==nptr) {
382 if(verbose) printf("error: unable to read server jitter/dispersion response.\n");
383 *jitter = -1;
384 } else if(verbose) {
385 printf("%.10g\n", *jitter);
389 if(do_stratum) {
390 /* get the stratum */
391 if(verbose) {
392 printf("parsing stratum from peer %.2x: ", ntohs(peers[i].assoc));
394 value = np_extract_ntpvar(data, "stratum");
395 nptr=NULL;
396 /* Convert the value if we have one */
397 if(value != NULL)
398 *stratum = strtol(value, &nptr, 10);
399 if(value == NULL || value==nptr) {
400 if(verbose) printf("error: unable to read server stratum response.\n");
401 *stratum = -1;
402 } else {
403 if(verbose) printf("%i\n", *stratum);
406 } /* if (PEER_SEL(peers[i].status) >= min_peer_sel) */
407 } /* for (i = 0; i < npeers; i++) */
409 close(conn);
410 if(peers!=NULL) free(peers);
412 return status;
415 int process_arguments(int argc, char **argv){
416 int c;
417 int option=0;
418 static struct option longopts[] = {
419 {"version", no_argument, 0, 'V'},
420 {"help", no_argument, 0, 'h'},
421 {"verbose", no_argument, 0, 'v'},
422 {"use-ipv4", no_argument, 0, '4'},
423 {"use-ipv6", no_argument, 0, '6'},
424 {"quiet", no_argument, 0, 'q'},
425 {"warning", required_argument, 0, 'w'},
426 {"critical", required_argument, 0, 'c'},
427 {"swarn", required_argument, 0, 'W'},
428 {"scrit", required_argument, 0, 'C'},
429 {"jwarn", required_argument, 0, 'j'},
430 {"jcrit", required_argument, 0, 'k'},
431 {"twarn", required_argument, 0, 'm'},
432 {"tcrit", required_argument, 0, 'n'},
433 {"timeout", required_argument, 0, 't'},
434 {"hostname", required_argument, 0, 'H'},
435 {"port", required_argument, 0, 'p'},
436 {0, 0, 0, 0}
440 if (argc < 2)
441 usage ("\n");
443 while (1) {
444 c = getopt_long (argc, argv, "Vhv46qw:c:W:C:j:k:m:n:t:H:p:", longopts, &option);
445 if (c == -1 || c == EOF || c == 1)
446 break;
448 switch (c) {
449 case 'h':
450 print_help();
451 exit(STATE_OK);
452 break;
453 case 'V':
454 print_revision(progname, NP_VERSION);
455 exit(STATE_OK);
456 break;
457 case 'v':
458 verbose++;
459 break;
460 case 'q':
461 quiet = 1;
462 break;
463 case 'w':
464 do_offset=1;
465 owarn = optarg;
466 break;
467 case 'c':
468 do_offset=1;
469 ocrit = optarg;
470 break;
471 case 'W':
472 do_stratum=1;
473 swarn = optarg;
474 break;
475 case 'C':
476 do_stratum=1;
477 scrit = optarg;
478 break;
479 case 'j':
480 do_jitter=1;
481 jwarn = optarg;
482 break;
483 case 'k':
484 do_jitter=1;
485 jcrit = optarg;
486 break;
487 case 'm':
488 do_truechimers=1;
489 twarn = optarg;
490 break;
491 case 'n':
492 do_truechimers=1;
493 tcrit = optarg;
494 break;
495 case 'H':
496 if(is_host(optarg) == FALSE)
497 usage2(_("Invalid hostname/address"), optarg);
498 server_address = strdup(optarg);
499 break;
500 case 'p':
501 port=atoi(optarg);
502 break;
503 case 't':
504 socket_timeout=atoi(optarg);
505 break;
506 case '4':
507 address_family = AF_INET;
508 break;
509 case '6':
510 #ifdef USE_IPV6
511 address_family = AF_INET6;
512 #else
513 usage4 (_("IPv6 support not available"));
514 #endif
515 break;
516 case '?':
517 /* print short usage statement if args not parsable */
518 usage5 ();
519 break;
523 if(server_address == NULL){
524 usage4(_("Hostname was not supplied"));
527 return 0;
530 char *perfd_offset (double offset)
532 return fperfdata ("offset", offset, "s",
533 TRUE, offset_thresholds->warning->end,
534 TRUE, offset_thresholds->critical->end,
535 FALSE, 0, FALSE, 0);
538 char *perfd_jitter (double jitter)
540 return fperfdata ("jitter", jitter, "",
541 do_jitter, jitter_thresholds->warning->end,
542 do_jitter, jitter_thresholds->critical->end,
543 TRUE, 0, FALSE, 0);
546 char *perfd_stratum (int stratum)
548 return perfdata ("stratum", stratum, "",
549 do_stratum, (int)stratum_thresholds->warning->end,
550 do_stratum, (int)stratum_thresholds->critical->end,
551 TRUE, 0, TRUE, 16);
554 char *perfd_truechimers (int num_truechimers)
556 return perfdata ("truechimers", num_truechimers, "",
557 do_truechimers, (int)truechimer_thresholds->warning->end,
558 do_truechimers, (int)truechimer_thresholds->critical->end,
559 TRUE, 0, FALSE, 0);
562 int main(int argc, char *argv[]){
563 int result, offset_result, stratum, num_truechimers;
564 double offset=0, jitter=0;
565 char *result_line, *perfdata_line;
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 /* This returns either OK or WARNING (See comment preceeding ntp_request) */
589 result = ntp_request(server_address, &offset, &offset_result, &jitter, &stratum, &num_truechimers);
591 if(offset_result == STATE_UNKNOWN) {
592 /* if there's no sync peer (this overrides ntp_request output): */
593 result = (quiet == 1 ? STATE_UNKNOWN : STATE_CRITICAL);
594 } else {
595 /* Be quiet if there's no candidates either */
596 if (quiet == 1 && result == STATE_WARNING)
597 result = STATE_UNKNOWN;
598 result = max_state_alt(result, get_status(fabs(offset), offset_thresholds));
601 if(do_truechimers)
602 result = max_state_alt(result, get_status(num_truechimers, truechimer_thresholds));
604 if(do_stratum)
605 result = max_state_alt(result, get_status(stratum, stratum_thresholds));
607 if(do_jitter)
608 result = max_state_alt(result, get_status(jitter, jitter_thresholds));
610 switch (result) {
611 case STATE_CRITICAL :
612 xasprintf(&result_line, _("NTP CRITICAL:"));
613 break;
614 case STATE_WARNING :
615 xasprintf(&result_line, _("NTP WARNING:"));
616 break;
617 case STATE_OK :
618 xasprintf(&result_line, _("NTP OK:"));
619 break;
620 default :
621 xasprintf(&result_line, _("NTP UNKNOWN:"));
622 break;
624 if(!syncsource_found)
625 xasprintf(&result_line, "%s %s,", result_line, _("Server not synchronized"));
626 else if(li_alarm)
627 xasprintf(&result_line, "%s %s,", result_line, _("Server has the LI_ALARM bit set"));
629 if(offset_result == STATE_UNKNOWN){
630 xasprintf(&result_line, "%s %s", result_line, _("Offset unknown"));
631 xasprintf(&perfdata_line, "");
632 } else {
633 xasprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset);
634 xasprintf(&perfdata_line, "%s", perfd_offset(offset));
636 if (do_jitter) {
637 xasprintf(&result_line, "%s, jitter=%f", result_line, jitter);
638 xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_jitter(jitter));
640 if (do_stratum) {
641 xasprintf(&result_line, "%s, stratum=%i", result_line, stratum);
642 xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_stratum(stratum));
644 if (do_truechimers) {
645 xasprintf(&result_line, "%s, truechimers=%i", result_line, num_truechimers);
646 xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_truechimers(num_truechimers));
648 printf("%s|%s\n", result_line, perfdata_line);
650 if(server_address!=NULL) free(server_address);
651 return result;
656 void print_help(void){
657 print_revision(progname, NP_VERSION);
659 printf ("Copyright (c) 2006 Sean Finney\n");
660 printf (COPYRIGHT, copyright, email);
662 printf ("%s\n", _("This plugin checks the selected ntp server"));
664 printf ("\n\n");
666 print_usage();
667 printf (UT_HELP_VRSN);
668 printf (UT_EXTRA_OPTS);
669 printf (UT_IPv46);
670 printf (UT_HOST_PORT, 'p', "123");
671 printf (" %s\n", "-q, --quiet");
672 printf (" %s\n", _("Returns UNKNOWN instead of CRITICAL or WARNING if server isn't synchronized"));
673 printf (" %s\n", "-w, --warning=THRESHOLD");
674 printf (" %s\n", _("Offset to result in warning status (seconds)"));
675 printf (" %s\n", "-c, --critical=THRESHOLD");
676 printf (" %s\n", _("Offset to result in critical status (seconds)"));
677 printf (" %s\n", "-W, --swarn=THRESHOLD");
678 printf (" %s\n", _("Warning threshold for stratum of server's synchronization peer"));
679 printf (" %s\n", "-C, --scrit=THRESHOLD");
680 printf (" %s\n", _("Critical threshold for stratum of server's synchronization peer"));
681 printf (" %s\n", "-j, --jwarn=THRESHOLD");
682 printf (" %s\n", _("Warning threshold for jitter"));
683 printf (" %s\n", "-k, --jcrit=THRESHOLD");
684 printf (" %s\n", _("Critical threshold for jitter"));
685 printf (" %s\n", "-m, --twarn=THRESHOLD");
686 printf (" %s\n", _("Warning threshold for number of usable time sources (\"truechimers\")"));
687 printf (" %s\n", "-n, --tcrit=THRESHOLD");
688 printf (" %s\n", _("Critical threshold for number of usable time sources (\"truechimers\")"));
689 printf (UT_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
690 printf (UT_VERBOSE);
692 printf("\n");
693 printf("%s\n", _("This plugin checks an NTP server independent of any commandline"));
694 printf("%s\n\n", _("programs or external libraries."));
696 printf("%s\n", _("Notes:"));
697 printf(" %s\n", _("Use this plugin to check the health of an NTP server. It supports"));
698 printf(" %s\n", _("checking the offset with the sync peer, the jitter and stratum. This"));
699 printf(" %s\n", _("plugin will not check the clock offset between the local host and NTP"));
700 printf(" %s\n", _("server; please use check_ntp_time for that purpose."));
701 printf("\n");
702 printf(UT_THRESHOLDS_NOTES);
704 printf("\n");
705 printf("%s\n", _("Examples:"));
706 printf(" %s\n", _("Simple NTP server check:"));
707 printf(" %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1"));
708 printf("\n");
709 printf(" %s\n", _("Check jitter too, avoiding critical notifications if jitter isn't available"));
710 printf(" %s\n", _("(See Notes above for more details on thresholds formats):"));
711 printf(" %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1 -j -1:100 -k -1:200"));
712 printf("\n");
713 printf(" %s\n", _("Only check the number of usable time sources (\"truechimers\"):"));
714 printf(" %s\n", ("./check_ntp_peer -H ntpserv -m @5 -n @3"));
715 printf("\n");
716 printf(" %s\n", _("Check only stratum:"));
717 printf(" %s\n", ("./check_ntp_peer -H ntpserv -W 4 -C 6"));
719 printf (UT_SUPPORT);
722 void
723 print_usage(void)
725 printf ("%s\n", _("Usage:"));
726 printf(" %s -H <host> [-4|-6] [-w <warn>] [-c <crit>] [-W <warn>] [-C <crit>]\n", progname);
727 printf(" [-j <warn>] [-k <crit>] [-v verbose]\n");