Merge pull request #2001 from RincewindsHat/check_snmp_protocoll_documentation
[monitoring-plugins.git] / plugins / check_ntp_peer.c
blob464a9e10d217b6026dfdb9cf6863e6b1f6d090b2
1 /*****************************************************************************
2 *
3 * Monitoring check_ntp_peer plugin
4 *
5 * License: GPL
6 * Copyright (c) 2006 Sean Finney <seanius@seanius.net>
7 * Copyright (c) 2006-2008 Monitoring 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@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 int process_arguments (int, char **);
65 thresholds *offset_thresholds = NULL;
66 thresholds *jitter_thresholds = NULL;
67 thresholds *stratum_thresholds = NULL;
68 thresholds *truechimer_thresholds = NULL;
69 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) do{ x |= ((y<<6)&LI_MASK); }while(0)
98 /* and these are the values of the leap indicator */
99 #define LI_NOWARNING 0x00
100 #define LI_EXTRASEC 0x01
101 #define LI_MISSINGSEC 0x02
102 #define LI_ALARM 0x03
103 /* bits 3,4,5 are the ntp version */
104 #define VN_MASK 0x38
105 #define VN(x) ((x&VN_MASK)>>3)
106 #define VN_SET(x,y) do{ x |= ((y<<3)&VN_MASK); }while(0)
107 #define VN_RESERVED 0x02
108 /* bits 6,7,8 are the ntp mode */
109 #define MODE_MASK 0x07
110 #define MODE(x) (x&MODE_MASK)
111 #define MODE_SET(x,y) do{ x |= (y&MODE_MASK); }while(0)
112 /* here are some values */
113 #define MODE_CLIENT 0x03
114 #define MODE_CONTROLMSG 0x06
115 /* In control message, bits 8-10 are R,E,M bits */
116 #define REM_MASK 0xe0
117 #define REM_RESP 0x80
118 #define REM_ERROR 0x40
119 #define REM_MORE 0x20
120 /* In control message, bits 11 - 15 are opcode */
121 #define OP_MASK 0x1f
122 #define OP_SET(x,y) do{ x |= (y&OP_MASK); }while(0)
123 #define OP_READSTAT 0x01
124 #define OP_READVAR 0x02
125 /* In peer status bytes, bits 6,7,8 determine clock selection status */
126 #define PEER_SEL(x) ((ntohs(x)>>8)&0x07)
127 #define PEER_TRUECHIMER 0x02
128 #define PEER_INCLUDED 0x04
129 #define PEER_SYNCSOURCE 0x06
131 /* NTP control message header is 12 bytes, plus any data in the data
132 * field, plus null padding to the nearest 32-bit boundary per rfc.
134 #define SIZEOF_NTPCM(m) (12+ntohs(m.count)+((ntohs(m.count)%4)?4-(ntohs(m.count)%4):0))
136 /* finally, a little helper or two for debugging: */
137 #define DBG(x) do{if(verbose>1){ x; }}while(0);
138 #define PRINTSOCKADDR(x) \
139 do{ \
140 printf("%u.%u.%u.%u", (x>>24)&0xff, (x>>16)&0xff, (x>>8)&0xff, x&0xff);\
141 }while(0);
143 void print_ntp_control_message(const ntp_control_message *p){
144 int i=0, numpeers=0;
145 const ntp_assoc_status_pair *peer=NULL;
147 printf("control packet contents:\n");
148 printf("\tflags: 0x%.2x , 0x%.2x\n", p->flags, p->op);
149 printf("\t li=%d (0x%.2x)\n", LI(p->flags), p->flags&LI_MASK);
150 printf("\t vn=%d (0x%.2x)\n", VN(p->flags), p->flags&VN_MASK);
151 printf("\t mode=%d (0x%.2x)\n", MODE(p->flags), p->flags&MODE_MASK);
152 printf("\t response=%d (0x%.2x)\n", (p->op&REM_RESP)>0, p->op&REM_RESP);
153 printf("\t more=%d (0x%.2x)\n", (p->op&REM_MORE)>0, p->op&REM_MORE);
154 printf("\t error=%d (0x%.2x)\n", (p->op&REM_ERROR)>0, p->op&REM_ERROR);
155 printf("\t op=%d (0x%.2x)\n", p->op&OP_MASK, p->op&OP_MASK);
156 printf("\tsequence: %d (0x%.2x)\n", ntohs(p->seq), ntohs(p->seq));
157 printf("\tstatus: %d (0x%.2x)\n", ntohs(p->status), ntohs(p->status));
158 printf("\tassoc: %d (0x%.2x)\n", ntohs(p->assoc), ntohs(p->assoc));
159 printf("\toffset: %d (0x%.2x)\n", ntohs(p->offset), ntohs(p->offset));
160 printf("\tcount: %d (0x%.2x)\n", ntohs(p->count), ntohs(p->count));
161 numpeers=ntohs(p->count)/(sizeof(ntp_assoc_status_pair));
162 if(p->op&REM_RESP && p->op&OP_READSTAT){
163 peer=(ntp_assoc_status_pair*)p->data;
164 for(i=0;i<numpeers;i++){
165 printf("\tpeer id %.2x status %.2x",
166 ntohs(peer[i].assoc), ntohs(peer[i].status));
167 if(PEER_SEL(peer[i].status) >= PEER_SYNCSOURCE){
168 printf(" <-- current sync source");
169 } else if(PEER_SEL(peer[i].status) >= PEER_INCLUDED){
170 printf(" <-- current sync candidate");
171 } else if(PEER_SEL(peer[i].status) >= PEER_TRUECHIMER){
172 printf(" <-- outlyer, but truechimer");
174 printf("\n");
179 void
180 setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq){
181 memset(p, 0, sizeof(ntp_control_message));
182 LI_SET(p->flags, LI_NOWARNING);
183 VN_SET(p->flags, VN_RESERVED);
184 MODE_SET(p->flags, MODE_CONTROLMSG);
185 OP_SET(p->op, opcode);
186 p->seq = htons(seq);
187 /* Remaining fields are zero for requests */
190 /* This function does all the actual work; roughly here's what it does
191 * beside setting the offset, jitter and stratum passed as argument:
192 * - offset can be negative, so if it cannot get the offset, offset_result
193 * is set to UNKNOWN, otherwise OK.
194 * - jitter and stratum are set to -1 if they cannot be retrieved so any
195 * positive value means a success retrieving the value.
196 * - status is set to WARNING if there's no sync.peer (otherwise OK) and is
197 * the return value of the function.
198 * status is pretty much useless as syncsource_found is a global variable
199 * used later in main to check is the server was synchronized. It works
200 * so I left it alone */
201 int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum, int *num_truechimers){
202 int conn=-1, i, npeers=0, num_candidates=0;
203 double tmp_offset = 0;
204 int min_peer_sel=PEER_INCLUDED;
205 int peers_size=0, peer_offset=0;
206 int status;
207 ntp_assoc_status_pair *peers=NULL;
208 ntp_control_message req;
209 const char *getvar = "stratum,offset,jitter";
210 char *data, *value, *nptr;
211 void *tmp;
213 status = STATE_OK;
214 *offset_result = STATE_UNKNOWN;
215 *jitter = *stratum = -1;
216 *num_truechimers = 0;
218 /* Long-winded explanation:
219 * Getting the sync peer offset, jitter and stratum requires a number of
220 * steps:
221 * 1) Send a READSTAT request.
222 * 2) Interpret the READSTAT reply
223 * a) The data section contains a list of peer identifiers (16 bits)
224 * and associated status words (16 bits)
225 * b) We want the value of 0x06 in the SEL (peer selection) value,
226 * which means "current synchronizatin source". If that's missing,
227 * we take anything better than 0x04 (see the rfc for details) but
228 * set a minimum of warning.
229 * 3) Send a READVAR request for information on each peer identified
230 * in 2b greater than the minimum selection value.
231 * 4) Extract the offset, jitter and stratum value from the data[]
232 * (it's ASCII)
234 my_udp_connect(server_address, port, &conn);
236 /* keep sending requests until the server stops setting the
237 * REM_MORE bit, though usually this is only 1 packet. */
239 setup_control_request(&req, OP_READSTAT, 1);
240 DBG(printf("sending READSTAT request"));
241 write(conn, &req, SIZEOF_NTPCM(req));
242 DBG(print_ntp_control_message(&req));
244 do {
245 /* Attempt to read the largest size packet possible */
246 req.count=htons(MAX_CM_SIZE);
247 DBG(printf("receiving READSTAT response"))
248 if(read(conn, &req, SIZEOF_NTPCM(req)) == -1)
249 die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n");
250 DBG(print_ntp_control_message(&req));
251 /* discard obviously invalid packets */
252 if (ntohs(req.count) > MAX_CM_SIZE)
253 die(STATE_CRITICAL, "NTP CRITICAL: Invalid packet received from NTP server\n");
254 } while (!(req.op&OP_READSTAT && ntohs(req.seq) == 1));
256 if (LI(req.flags) == LI_ALARM) li_alarm = true;
257 /* Each peer identifier is 4 bytes in the data section, which
258 * we represent as a ntp_assoc_status_pair datatype.
260 peers_size+=ntohs(req.count);
261 if((tmp=realloc(peers, peers_size)) == NULL)
262 free(peers), die(STATE_UNKNOWN, "can not (re)allocate 'peers' buffer\n");
263 peers=tmp;
264 memcpy((void*)((ptrdiff_t)peers+peer_offset), (void*)req.data, ntohs(req.count));
265 npeers=peers_size/sizeof(ntp_assoc_status_pair);
266 peer_offset+=ntohs(req.count);
267 } while(req.op&REM_MORE);
269 /* first, let's find out if we have a sync source, or if there are
270 * at least some candidates. In the latter case we'll issue
271 * a warning but go ahead with the check on them. */
272 for (i = 0; i < npeers; i++){
273 if(PEER_SEL(peers[i].status) >= PEER_TRUECHIMER){
274 (*num_truechimers)++;
275 if(PEER_SEL(peers[i].status) >= PEER_INCLUDED){
276 num_candidates++;
277 if(PEER_SEL(peers[i].status) >= PEER_SYNCSOURCE){
278 syncsource_found = true;
279 min_peer_sel=PEER_SYNCSOURCE;
284 if(verbose) printf("%d candidate peers available\n", num_candidates);
285 if(verbose && syncsource_found) printf("synchronization source found\n");
286 if(! syncsource_found){
287 status = STATE_WARNING;
288 if(verbose) printf("warning: no synchronization source found\n");
290 if(li_alarm){
291 status = STATE_WARNING;
292 if(verbose) printf("warning: LI_ALARM bit is set\n");
296 for (i = 0; i < npeers; i++){
297 /* Only query this server if it is the current sync source */
298 /* If there's no sync.peer, query all candidates and use the best one */
299 if (PEER_SEL(peers[i].status) >= min_peer_sel){
300 if(verbose) printf("Getting offset, jitter and stratum for peer %.2x\n", ntohs(peers[i].assoc));
301 xasprintf(&data, "");
303 setup_control_request(&req, OP_READVAR, 2);
304 req.assoc = peers[i].assoc;
305 /* Putting the wanted variable names in the request
306 * cause the server to provide _only_ the requested values.
307 * thus reducing net traffic, guaranteeing us only a single
308 * datagram in reply, and making interpretation much simpler
310 /* Older servers doesn't know what jitter is, so if we get an
311 * error on the first pass we redo it with "dispersion" */
312 strncpy(req.data, getvar, MAX_CM_SIZE-1);
313 req.count = htons(strlen(getvar));
314 DBG(printf("sending READVAR request...\n"));
315 write(conn, &req, SIZEOF_NTPCM(req));
316 DBG(print_ntp_control_message(&req));
318 do {
319 req.count = htons(MAX_CM_SIZE);
320 DBG(printf("receiving READVAR response...\n"));
321 read(conn, &req, SIZEOF_NTPCM(req));
322 DBG(print_ntp_control_message(&req));
323 } while (!(req.op&OP_READVAR && ntohs(req.seq) == 2));
325 if(!(req.op&REM_ERROR))
326 xasprintf(&data, "%s%s", data, req.data);
327 } while(req.op&REM_MORE);
329 if(req.op&REM_ERROR) {
330 if(strstr(getvar, "jitter")) {
331 if(verbose) printf("The command failed. This is usually caused by servers refusing the 'jitter'\nvariable. Restarting with 'dispersion'...\n");
332 getvar = "stratum,offset,dispersion";
333 i--;
334 continue;
335 } else if(strlen(getvar)) {
336 if(verbose) printf("Server didn't like dispersion either; will retrieve everything\n");
337 getvar = "";
338 i--;
339 continue;
343 if(verbose > 1)
344 printf("Server responded: >>>%s<<<\n", data);
346 /* get the offset */
347 if(verbose)
348 printf("parsing offset from peer %.2x: ", ntohs(peers[i].assoc));
350 value = np_extract_ntpvar(data, "offset");
351 nptr=NULL;
352 /* Convert the value if we have one */
353 if(value != NULL)
354 tmp_offset = strtod(value, &nptr) / 1000;
355 /* If value is null or no conversion was performed */
356 if(value == NULL || value==nptr) {
357 if(verbose) printf("error: unable to read server offset response.\n");
358 } else {
359 if(verbose) printf("%.10g\n", tmp_offset);
360 if(*offset_result == STATE_UNKNOWN || fabs(tmp_offset) < fabs(*offset)) {
361 *offset = tmp_offset;
362 *offset_result = STATE_OK;
363 } else {
364 /* Skip this one; move to the next */
365 continue;
369 if(do_jitter) {
370 /* get the jitter */
371 if(verbose) {
372 printf("parsing %s from peer %.2x: ", strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter", ntohs(peers[i].assoc));
374 value = np_extract_ntpvar(data, strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter");
375 nptr=NULL;
376 /* Convert the value if we have one */
377 if(value != NULL)
378 *jitter = strtod(value, &nptr);
379 /* If value is null or no conversion was performed */
380 if(value == NULL || value==nptr) {
381 if(verbose) printf("error: unable to read server jitter/dispersion response.\n");
382 *jitter = -1;
383 } else if(verbose) {
384 printf("%.10g\n", *jitter);
388 if(do_stratum) {
389 /* get the stratum */
390 if(verbose) {
391 printf("parsing stratum from peer %.2x: ", ntohs(peers[i].assoc));
393 value = np_extract_ntpvar(data, "stratum");
394 nptr=NULL;
395 /* Convert the value if we have one */
396 if(value != NULL)
397 *stratum = strtol(value, &nptr, 10);
398 if(value == NULL || value==nptr) {
399 if(verbose) printf("error: unable to read server stratum response.\n");
400 *stratum = -1;
401 } else {
402 if(verbose) printf("%i\n", *stratum);
405 } /* if (PEER_SEL(peers[i].status) >= min_peer_sel) */
406 } /* for (i = 0; i < npeers; i++) */
408 close(conn);
409 if(peers!=NULL) free(peers);
411 return status;
414 int process_arguments(int argc, char **argv){
415 int c;
416 int option=0;
417 static struct option longopts[] = {
418 {"version", no_argument, 0, 'V'},
419 {"help", no_argument, 0, 'h'},
420 {"verbose", no_argument, 0, 'v'},
421 {"use-ipv4", no_argument, 0, '4'},
422 {"use-ipv6", no_argument, 0, '6'},
423 {"quiet", no_argument, 0, 'q'},
424 {"warning", required_argument, 0, 'w'},
425 {"critical", required_argument, 0, 'c'},
426 {"swarn", required_argument, 0, 'W'},
427 {"scrit", required_argument, 0, 'C'},
428 {"jwarn", required_argument, 0, 'j'},
429 {"jcrit", required_argument, 0, 'k'},
430 {"twarn", required_argument, 0, 'm'},
431 {"tcrit", required_argument, 0, 'n'},
432 {"timeout", required_argument, 0, 't'},
433 {"hostname", required_argument, 0, 'H'},
434 {"port", required_argument, 0, 'p'},
435 {0, 0, 0, 0}
439 if (argc < 2)
440 usage ("\n");
442 while (true) {
443 c = getopt_long (argc, argv, "Vhv46qw:c:W:C:j:k:m:n:t:H:p:", longopts, &option);
444 if (c == -1 || c == EOF || c == 1)
445 break;
447 switch (c) {
448 case 'h':
449 print_help();
450 exit(STATE_UNKNOWN);
451 break;
452 case 'V':
453 print_revision(progname, NP_VERSION);
454 exit(STATE_UNKNOWN);
455 break;
456 case 'v':
457 verbose++;
458 break;
459 case 'q':
460 quiet = true;
461 break;
462 case 'w':
463 owarn = optarg;
464 break;
465 case 'c':
466 ocrit = optarg;
467 break;
468 case 'W':
469 do_stratum = true;
470 swarn = optarg;
471 break;
472 case 'C':
473 do_stratum = true;
474 scrit = optarg;
475 break;
476 case 'j':
477 do_jitter = true;
478 jwarn = optarg;
479 break;
480 case 'k':
481 do_jitter = true;
482 jcrit = optarg;
483 break;
484 case 'm':
485 do_truechimers = true;
486 twarn = optarg;
487 break;
488 case 'n':
489 do_truechimers = true;
490 tcrit = optarg;
491 break;
492 case 'H':
493 if(!is_host(optarg))
494 usage2(_("Invalid hostname/address"), optarg);
495 server_address = strdup(optarg);
496 break;
497 case 'p':
498 port=atoi(optarg);
499 break;
500 case 't':
501 socket_timeout=atoi(optarg);
502 break;
503 case '4':
504 address_family = AF_INET;
505 break;
506 case '6':
507 #ifdef USE_IPV6
508 address_family = AF_INET6;
509 #else
510 usage4 (_("IPv6 support not available"));
511 #endif
512 break;
513 case '?':
514 /* print short usage statement if args not parsable */
515 usage5 ();
516 break;
520 if(server_address == NULL){
521 usage4(_("Hostname was not supplied"));
524 return 0;
527 char *perfd_offset (double offset)
529 return fperfdata ("offset", offset, "s",
530 true, offset_thresholds->warning->end,
531 true, offset_thresholds->critical->end,
532 false, 0, false, 0);
535 char *perfd_jitter (double jitter)
537 return fperfdata ("jitter", jitter, "",
538 do_jitter, jitter_thresholds->warning->end,
539 do_jitter, jitter_thresholds->critical->end,
540 true, 0, false, 0);
543 char *perfd_stratum (int stratum)
545 return perfdata ("stratum", stratum, "",
546 do_stratum, (int)stratum_thresholds->warning->end,
547 do_stratum, (int)stratum_thresholds->critical->end,
548 true, 0, true, 16);
551 char *perfd_truechimers (int num_truechimers)
553 return perfdata ("truechimers", num_truechimers, "",
554 do_truechimers, (int)truechimer_thresholds->warning->end,
555 do_truechimers, (int)truechimer_thresholds->critical->end,
556 true, 0, false, 0);
559 int main(int argc, char *argv[]){
560 int result, offset_result, stratum, num_truechimers;
561 double offset=0, jitter=0;
562 char *result_line, *perfdata_line;
564 setlocale (LC_ALL, "");
565 bindtextdomain (PACKAGE, LOCALEDIR);
566 textdomain (PACKAGE);
568 /* Parse extra opts if any */
569 argv=np_extra_opts (&argc, argv, progname);
571 if (process_arguments (argc, argv) == ERROR)
572 usage4 (_("Could not parse arguments"));
574 set_thresholds(&offset_thresholds, owarn, ocrit);
575 set_thresholds(&jitter_thresholds, jwarn, jcrit);
576 set_thresholds(&stratum_thresholds, swarn, scrit);
577 set_thresholds(&truechimer_thresholds, twarn, tcrit);
579 /* initialize alarm signal handling */
580 signal (SIGALRM, socket_timeout_alarm_handler);
582 /* set socket timeout */
583 alarm (socket_timeout);
585 /* This returns either OK or WARNING (See comment preceding ntp_request) */
586 result = ntp_request(&offset, &offset_result, &jitter, &stratum, &num_truechimers);
588 if(offset_result == STATE_UNKNOWN) {
589 /* if there's no sync peer (this overrides ntp_request output): */
590 result = (quiet ? STATE_UNKNOWN : STATE_CRITICAL);
591 } else {
592 /* Be quiet if there's no candidates either */
593 if (quiet && result == STATE_WARNING)
594 result = STATE_UNKNOWN;
595 result = max_state_alt(result, get_status(fabs(offset), offset_thresholds));
598 int oresult = result;
601 int tresult = STATE_UNKNOWN;
603 if(do_truechimers) {
604 tresult = get_status(num_truechimers, truechimer_thresholds);
605 result = max_state_alt(result, tresult);
609 int sresult = STATE_UNKNOWN;
611 if(do_stratum) {
612 sresult = get_status(stratum, stratum_thresholds);
613 result = max_state_alt(result, sresult);
617 int jresult = STATE_UNKNOWN;
619 if(do_jitter) {
620 jresult = get_status(jitter, jitter_thresholds);
621 result = max_state_alt(result, jresult);
624 switch (result) {
625 case STATE_CRITICAL :
626 xasprintf(&result_line, _("NTP CRITICAL:"));
627 break;
628 case STATE_WARNING :
629 xasprintf(&result_line, _("NTP WARNING:"));
630 break;
631 case STATE_OK :
632 xasprintf(&result_line, _("NTP OK:"));
633 break;
634 default :
635 xasprintf(&result_line, _("NTP UNKNOWN:"));
636 break;
638 if(!syncsource_found)
639 xasprintf(&result_line, "%s %s,", result_line, _("Server not synchronized"));
640 else if(li_alarm)
641 xasprintf(&result_line, "%s %s,", result_line, _("Server has the LI_ALARM bit set"));
643 if(offset_result == STATE_UNKNOWN){
644 xasprintf(&result_line, "%s %s", result_line, _("Offset unknown"));
645 xasprintf(&perfdata_line, "");
646 } else if (oresult == STATE_WARNING) {
647 xasprintf(&result_line, "%s %s %.10g secs (WARNING)", result_line, _("Offset"), offset);
648 } else if (oresult == STATE_CRITICAL) {
649 xasprintf(&result_line, "%s %s %.10g secs (CRITICAL)", result_line, _("Offset"), offset);
650 } else {
651 xasprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset);
653 xasprintf(&perfdata_line, "%s", perfd_offset(offset));
655 if (do_jitter) {
656 if (jresult == STATE_WARNING) {
657 xasprintf(&result_line, "%s, jitter=%f (WARNING)", result_line, jitter);
658 } else if (jresult == STATE_CRITICAL) {
659 xasprintf(&result_line, "%s, jitter=%f (CRITICAL)", result_line, jitter);
660 } else {
661 xasprintf(&result_line, "%s, jitter=%f", result_line, jitter);
663 xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_jitter(jitter));
665 if (do_stratum) {
666 if (sresult == STATE_WARNING) {
667 xasprintf(&result_line, "%s, stratum=%i (WARNING)", result_line, stratum);
668 } else if (sresult == STATE_CRITICAL) {
669 xasprintf(&result_line, "%s, stratum=%i (CRITICAL)", result_line, stratum);
670 } else {
671 xasprintf(&result_line, "%s, stratum=%i", result_line, stratum);
673 xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_stratum(stratum));
675 if (do_truechimers) {
676 if (tresult == STATE_WARNING) {
677 xasprintf(&result_line, "%s, truechimers=%i (WARNING)", result_line, num_truechimers);
678 } else if (tresult == STATE_CRITICAL) {
679 xasprintf(&result_line, "%s, truechimers=%i (CRITICAL)", result_line, num_truechimers);
680 } else {
681 xasprintf(&result_line, "%s, truechimers=%i", result_line, num_truechimers);
683 xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_truechimers(num_truechimers));
685 printf("%s|%s\n", result_line, perfdata_line);
687 if(server_address!=NULL) free(server_address);
688 return result;
691 void print_help(void){
692 print_revision(progname, NP_VERSION);
694 printf ("Copyright (c) 2006 Sean Finney\n");
695 printf (COPYRIGHT, copyright, email);
697 printf ("%s\n", _("This plugin checks the selected ntp server"));
699 printf ("\n\n");
701 print_usage();
702 printf (UT_HELP_VRSN);
703 printf (UT_EXTRA_OPTS);
704 printf (UT_IPv46);
705 printf (UT_HOST_PORT, 'p', "123");
706 printf (" %s\n", "-q, --quiet");
707 printf (" %s\n", _("Returns UNKNOWN instead of CRITICAL or WARNING if server isn't synchronized"));
708 printf (" %s\n", "-w, --warning=THRESHOLD");
709 printf (" %s\n", _("Offset to result in warning status (seconds)"));
710 printf (" %s\n", "-c, --critical=THRESHOLD");
711 printf (" %s\n", _("Offset to result in critical status (seconds)"));
712 printf (" %s\n", "-W, --swarn=THRESHOLD");
713 printf (" %s\n", _("Warning threshold for stratum of server's synchronization peer"));
714 printf (" %s\n", "-C, --scrit=THRESHOLD");
715 printf (" %s\n", _("Critical threshold for stratum of server's synchronization peer"));
716 printf (" %s\n", "-j, --jwarn=THRESHOLD");
717 printf (" %s\n", _("Warning threshold for jitter"));
718 printf (" %s\n", "-k, --jcrit=THRESHOLD");
719 printf (" %s\n", _("Critical threshold for jitter"));
720 printf (" %s\n", "-m, --twarn=THRESHOLD");
721 printf (" %s\n", _("Warning threshold for number of usable time sources (\"truechimers\")"));
722 printf (" %s\n", "-n, --tcrit=THRESHOLD");
723 printf (" %s\n", _("Critical threshold for number of usable time sources (\"truechimers\")"));
724 printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
725 printf (UT_VERBOSE);
727 printf("\n");
728 printf("%s\n", _("This plugin checks an NTP server independent of any commandline"));
729 printf("%s\n\n", _("programs or external libraries."));
731 printf("%s\n", _("Notes:"));
732 printf(" %s\n", _("Use this plugin to check the health of an NTP server. It supports"));
733 printf(" %s\n", _("checking the offset with the sync peer, the jitter and stratum. This"));
734 printf(" %s\n", _("plugin will not check the clock offset between the local host and NTP"));
735 printf(" %s\n", _("server; please use check_ntp_time for that purpose."));
736 printf("\n");
737 printf(UT_THRESHOLDS_NOTES);
739 printf("\n");
740 printf("%s\n", _("Examples:"));
741 printf(" %s\n", _("Simple NTP server check:"));
742 printf(" %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1"));
743 printf("\n");
744 printf(" %s\n", _("Check jitter too, avoiding critical notifications if jitter isn't available"));
745 printf(" %s\n", _("(See Notes above for more details on thresholds formats):"));
746 printf(" %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1 -j -1:100 -k -1:200"));
747 printf("\n");
748 printf(" %s\n", _("Only check the number of usable time sources (\"truechimers\"):"));
749 printf(" %s\n", ("./check_ntp_peer -H ntpserv -m @5 -n @3"));
750 printf("\n");
751 printf(" %s\n", _("Check only stratum:"));
752 printf(" %s\n", ("./check_ntp_peer -H ntpserv -W 4 -C 6"));
754 printf (UT_SUPPORT);
757 void
758 print_usage(void)
760 printf ("%s\n", _("Usage:"));
761 printf(" %s -H <host> [-4|-6] [-w <warn>] [-c <crit>] [-W <warn>] [-C <crit>]\n", progname);
762 printf(" [-j <warn>] [-k <crit>] [-v verbose]\n");