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:
10 *
11 * This file contains the check_ntp_peer plugin
12 *
13 * This plugin checks an NTP server independent of any commandline
14 * programs or external libraries.
15 *
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.
20 *
21 *
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.
26 *
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.
31 *
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/>.
34 *
35 *
36 *****************************************************************************/
37
38 const char *progname = "check_ntp_peer";
39 const char *copyright = "2006-2008";
40 const char *email = "devel@monitoring-plugins.org";
41
42 #include "common.h"
43 #include "netutils.h"
44 #include "utils.h"
45
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;
64
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);
72
73 /* max size of control message data */
74 #define MAX_CM_SIZE 468
75
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;
88
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;
94
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
131
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.
134 */
135 #define SIZEOF_NTPCM(m) (12+ntohs(m.count)+((ntohs(m.count)%4)?4-(ntohs(m.count)%4):0))
136
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);
143
print_ntp_control_message(const ntp_control_message * p)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;
147
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");
174 }
175 printf("\n");
176 }
177 }
178 }
179
180 void
setup_control_request(ntp_control_message * p,uint8_t opcode,uint16_t seq)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 */
189 }
190
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 */
ntp_request(const char * host,double * offset,int * offset_result,double * jitter,int * stratum,int * num_truechimers)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;
213
214 status = STATE_OK;
215 *offset_result = STATE_UNKNOWN;
216 *jitter = *stratum = -1;
217 *num_truechimers = 0;
218
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)
234 */
235 my_udp_connect(server_address, port, &conn);
236
237 /* keep sending requests until the server stops setting the
238 * REM_MORE bit, though usually this is only 1 packet. */
239 do{
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));
244
245 do {
246 /* Attempt to read the largest size packet possible */
247 req.count=htons(MAX_CM_SIZE);
248 DBG(printf("receiving 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));
256
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.
260 */
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);
269
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;
281 }
282 }
283 }
284 }
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");
290 }
291 if(li_alarm){
292 status = STATE_WARNING;
293 if(verbose) printf("warning: LI_ALARM bit is set\n");
294 }
295
296
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, "");
303 do{
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
310 */
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));
318
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));
325
326 if(!(req.op&REM_ERROR))
327 xasprintf(&data, "%s%s", data, req.data);
328 } while(req.op&REM_MORE);
329
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;
341 }
342 }
343
344 if(verbose > 1)
345 printf("Server responded: >>>%s<<<\n", data);
346
347 /* get the offset */
348 if(verbose)
349 printf("parsing offset from peer %.2x: ", ntohs(peers[i].assoc));
350
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;
367 }
368 }
369
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));
374 }
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);
386 }
387 }
388
389 if(do_stratum) {
390 /* get the stratum */
391 if(verbose) {
392 printf("parsing stratum from peer %.2x: ", ntohs(peers[i].assoc));
393 }
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);
404 }
405 }
406 } /* if (PEER_SEL(peers[i].status) >= min_peer_sel) */
407 } /* for (i = 0; i < npeers; i++) */
408
409 close(conn);
410 if(peers!=NULL) free(peers);
411
412 return status;
413 }
414
process_arguments(int argc,char ** argv)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}
437 };
438
439
440 if (argc < 2)
441 usage ("\n");
442
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;
447
448 switch (c) {
449 case 'h':
450 print_help();
451 exit(STATE_UNKNOWN);
452 break;
453 case 'V':
454 print_revision(progname, NP_VERSION);
455 exit(STATE_UNKNOWN);
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;
520 }
521 }
522
523 if(server_address == NULL){
524 usage4(_("Hostname was not supplied"));
525 }
526
527 return 0;
528 }
529
perfd_offset(double offset)530 char *perfd_offset (double offset)
531 {
532 return fperfdata ("offset", offset, "s",
533 TRUE, offset_thresholds->warning->end,
534 TRUE, offset_thresholds->critical->end,
535 FALSE, 0, FALSE, 0);
536 }
537
perfd_jitter(double jitter)538 char *perfd_jitter (double jitter)
539 {
540 return fperfdata ("jitter", jitter, "",
541 do_jitter, jitter_thresholds->warning->end,
542 do_jitter, jitter_thresholds->critical->end,
543 TRUE, 0, FALSE, 0);
544 }
545
perfd_stratum(int stratum)546 char *perfd_stratum (int stratum)
547 {
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);
552 }
553
perfd_truechimers(int num_truechimers)554 char *perfd_truechimers (int num_truechimers)
555 {
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);
560 }
561
main(int argc,char * argv[])562 int main(int argc, char *argv[]){
563 int result, offset_result, stratum, num_truechimers, oresult, jresult, sresult, tresult;
564 double offset=0, jitter=0;
565 char *result_line, *perfdata_line;
566
567 setlocale (LC_ALL, ""); setlocale(LC_NUMERIC, "C");
568 bindtextdomain (PACKAGE, LOCALEDIR);
569 textdomain (PACKAGE);
570
571 /* Parse extra opts if any */
572 argv=np_extra_opts (&argc, argv, progname);
573
574 if (process_arguments (argc, argv) == ERROR)
575 usage4 (_("Could not parse arguments"));
576
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);
581
582 /* initialize alarm signal handling */
583 signal (SIGALRM, socket_timeout_alarm_handler);
584
585 /* set socket timeout */
586 alarm (socket_timeout);
587
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);
590
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));
599 }
600 oresult = result;
601
602 if(do_truechimers) {
603 tresult = get_status(num_truechimers, truechimer_thresholds);
604 result = max_state_alt(result, tresult);
605 }
606
607 if(do_stratum) {
608 sresult = get_status(stratum, stratum_thresholds);
609 result = max_state_alt(result, sresult);
610 }
611
612 if(do_jitter) {
613 jresult = get_status(jitter, jitter_thresholds);
614 result = max_state_alt(result, jresult);
615 }
616
617 switch (result) {
618 case STATE_CRITICAL :
619 xasprintf(&result_line, _("NTP CRITICAL:"));
620 break;
621 case STATE_WARNING :
622 xasprintf(&result_line, _("NTP WARNING:"));
623 break;
624 case STATE_OK :
625 xasprintf(&result_line, _("NTP OK:"));
626 break;
627 default :
628 xasprintf(&result_line, _("NTP UNKNOWN:"));
629 break;
630 }
631 if(!syncsource_found)
632 xasprintf(&result_line, "%s %s,", result_line, _("Server not synchronized"));
633 else if(li_alarm)
634 xasprintf(&result_line, "%s %s,", result_line, _("Server has the LI_ALARM bit set"));
635
636 if(offset_result == STATE_UNKNOWN){
637 xasprintf(&result_line, "%s %s", result_line, _("Offset unknown"));
638 xasprintf(&perfdata_line, "");
639 } else if (oresult == STATE_WARNING) {
640 xasprintf(&result_line, "%s %s %.10g secs (WARNING)", result_line, _("Offset"), offset);
641 } else if (oresult == STATE_CRITICAL) {
642 xasprintf(&result_line, "%s %s %.10g secs (CRITICAL)", result_line, _("Offset"), offset);
643 } else {
644 xasprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset);
645 }
646 xasprintf(&perfdata_line, "%s", perfd_offset(offset));
647
648 if (do_jitter) {
649 if (jresult == STATE_WARNING) {
650 xasprintf(&result_line, "%s, jitter=%f (WARNING)", result_line, jitter);
651 } else if (jresult == STATE_CRITICAL) {
652 xasprintf(&result_line, "%s, jitter=%f (CRITICAL)", result_line, jitter);
653 } else {
654 xasprintf(&result_line, "%s, jitter=%f", result_line, jitter);
655 }
656 xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_jitter(jitter));
657 }
658 if (do_stratum) {
659 if (sresult == STATE_WARNING) {
660 xasprintf(&result_line, "%s, stratum=%i (WARNING)", result_line, stratum);
661 } else if (sresult == STATE_CRITICAL) {
662 xasprintf(&result_line, "%s, stratum=%i (CRITICAL)", result_line, stratum);
663 } else {
664 xasprintf(&result_line, "%s, stratum=%i", result_line, stratum);
665 }
666 xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_stratum(stratum));
667 }
668 if (do_truechimers) {
669 if (tresult == STATE_WARNING) {
670 xasprintf(&result_line, "%s, truechimers=%i (WARNING)", result_line, num_truechimers);
671 } else if (tresult == STATE_CRITICAL) {
672 xasprintf(&result_line, "%s, truechimers=%i (CRITICAL)", result_line, num_truechimers);
673 } else {
674 xasprintf(&result_line, "%s, truechimers=%i", result_line, num_truechimers);
675 }
676 xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_truechimers(num_truechimers));
677 }
678 printf("%s|%s\n", result_line, perfdata_line);
679
680 if(server_address!=NULL) free(server_address);
681 return result;
682 }
683
print_help(void)684 void print_help(void){
685 print_revision(progname, NP_VERSION);
686
687 printf ("Copyright (c) 2006 Sean Finney\n");
688 printf (COPYRIGHT, copyright, email);
689
690 printf ("%s\n", _("This plugin checks the selected ntp server"));
691
692 printf ("\n\n");
693
694 print_usage();
695 printf (UT_HELP_VRSN);
696 printf (UT_EXTRA_OPTS);
697 printf (UT_IPv46);
698 printf (UT_HOST_PORT, 'p', "123");
699 printf (" %s\n", "-q, --quiet");
700 printf (" %s\n", _("Returns UNKNOWN instead of CRITICAL or WARNING if server isn't synchronized"));
701 printf (" %s\n", "-w, --warning=THRESHOLD");
702 printf (" %s\n", _("Offset to result in warning status (seconds)"));
703 printf (" %s\n", "-c, --critical=THRESHOLD");
704 printf (" %s\n", _("Offset to result in critical status (seconds)"));
705 printf (" %s\n", "-W, --swarn=THRESHOLD");
706 printf (" %s\n", _("Warning threshold for stratum of server's synchronization peer"));
707 printf (" %s\n", "-C, --scrit=THRESHOLD");
708 printf (" %s\n", _("Critical threshold for stratum of server's synchronization peer"));
709 printf (" %s\n", "-j, --jwarn=THRESHOLD");
710 printf (" %s\n", _("Warning threshold for jitter"));
711 printf (" %s\n", "-k, --jcrit=THRESHOLD");
712 printf (" %s\n", _("Critical threshold for jitter"));
713 printf (" %s\n", "-m, --twarn=THRESHOLD");
714 printf (" %s\n", _("Warning threshold for number of usable time sources (\"truechimers\")"));
715 printf (" %s\n", "-n, --tcrit=THRESHOLD");
716 printf (" %s\n", _("Critical threshold for number of usable time sources (\"truechimers\")"));
717 printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
718 printf (UT_VERBOSE);
719
720 printf("\n");
721 printf("%s\n", _("This plugin checks an NTP server independent of any commandline"));
722 printf("%s\n\n", _("programs or external libraries."));
723
724 printf("%s\n", _("Notes:"));
725 printf(" %s\n", _("Use this plugin to check the health of an NTP server. It supports"));
726 printf(" %s\n", _("checking the offset with the sync peer, the jitter and stratum. This"));
727 printf(" %s\n", _("plugin will not check the clock offset between the local host and NTP"));
728 printf(" %s\n", _("server; please use check_ntp_time for that purpose."));
729 printf("\n");
730 printf(UT_THRESHOLDS_NOTES);
731
732 printf("\n");
733 printf("%s\n", _("Examples:"));
734 printf(" %s\n", _("Simple NTP server check:"));
735 printf(" %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1"));
736 printf("\n");
737 printf(" %s\n", _("Check jitter too, avoiding critical notifications if jitter isn't available"));
738 printf(" %s\n", _("(See Notes above for more details on thresholds formats):"));
739 printf(" %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1 -j -1:100 -k -1:200"));
740 printf("\n");
741 printf(" %s\n", _("Only check the number of usable time sources (\"truechimers\"):"));
742 printf(" %s\n", ("./check_ntp_peer -H ntpserv -m @5 -n @3"));
743 printf("\n");
744 printf(" %s\n", _("Check only stratum:"));
745 printf(" %s\n", ("./check_ntp_peer -H ntpserv -W 4 -C 6"));
746
747 printf (UT_SUPPORT);
748 }
749
750 void
print_usage(void)751 print_usage(void)
752 {
753 printf ("%s\n", _("Usage:"));
754 printf(" %s -H <host> [-4|-6] [-w <warn>] [-c <crit>] [-W <warn>] [-C <crit>]\n", progname);
755 printf(" [-j <warn>] [-k <crit>] [-v verbose]\n");
756 }
757