Code

- Fix check_ntp_peer returning misleading information when an ICMP message came back
[nagiosplug.git] / plugins / check_ntp_peer.c
1 /******************************************************************************
2 *
3 * Nagios check_ntp_peer plugin
4 *
5 * License: GPL
6 * Copyright (c) 2006 sean finney <seanius@seanius.net>
7 * Copyright (c) 2007 nagios-plugins team
8 *
9 * Last Modified: $Date$
10 *
11 * Description:
12 *
13 * This file contains the check_ntp_peer plugin
14 *
15 *  This plugin checks an NTP server independent of any commandline
16 *  programs or external libraries.
17 *
18 *  Use this plugin to check the health of an NTP server. It supports
19 *  checking the offset with the sync peer, the jitter and stratum. This
20 *  plugin will not check the clock offset between the local host and NTP
21 *  server; please use check_ntp_time for that purpose.
22 *
23 *
24 * License Information:
25 *
26 * This program is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation; either version 2 of the License, or
29 * (at your option) any later version.
30 *
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34 * GNU General Public License for more details.
35 *
36 * You should have received a copy of the GNU General Public License
37 * along with this program; if not, write to the Free Software
38 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
40  $Id$
41  
42 *****************************************************************************/
44 const char *progname = "check_ntp_peer";
45 const char *revision = "$Revision$";
46 const char *copyright = "2007";
47 const char *email = "nagiosplug-devel@lists.sourceforge.net";
49 #include "common.h"
50 #include "netutils.h"
51 #include "utils.h"
53 static char *server_address=NULL;
54 static int verbose=0;
55 static int quiet=0;
56 static short do_offset=0;
57 static char *owarn="60";
58 static char *ocrit="120";
59 static short do_stratum=0;
60 static char *swarn="-1:16";
61 static char *scrit="-1:16";
62 static short do_jitter=0;
63 static char *jwarn="-1:5000";
64 static char *jcrit="-1:10000";
65 static int syncsource_found=0;
67 int process_arguments (int, char **);
68 thresholds *offset_thresholds = NULL;
69 thresholds *jitter_thresholds = NULL;
70 thresholds *stratum_thresholds = NULL;
71 void print_help (void);
72 void print_usage (void);
74 /* max size of control message data */
75 #define MAX_CM_SIZE 468
77 /* this structure holds everything in an ntp control message as per rfc1305 */
78 typedef struct {
79         uint8_t flags;       /* byte with leapindicator,vers,mode. see macros */
80         uint8_t op;          /* R,E,M bits and Opcode */
81         uint16_t seq;        /* Packet sequence */
82         uint16_t status;     /* Clock status */
83         uint16_t assoc;      /* Association */
84         uint16_t offset;     /* Similar to TCP sequence # */
85         uint16_t count;      /* # bytes of data */
86         char data[MAX_CM_SIZE]; /* ASCII data of the request */
87                                 /* NB: not necessarily NULL terminated! */
88 } ntp_control_message;
90 /* this is an association/status-word pair found in control packet reponses */
91 typedef struct {
92         uint16_t assoc;
93         uint16_t status;
94 } ntp_assoc_status_pair;
96 /* bits 1,2 are the leap indicator */
97 #define LI_MASK 0xc0
98 #define LI(x) ((x&LI_MASK)>>6)
99 #define LI_SET(x,y) do{ x |= ((y<<6)&LI_MASK); }while(0)
100 /* and these are the values of the leap indicator */
101 #define LI_NOWARNING 0x00
102 #define LI_EXTRASEC 0x01
103 #define LI_MISSINGSEC 0x02
104 #define LI_ALARM 0x03
105 /* bits 3,4,5 are the ntp version */
106 #define VN_MASK 0x38
107 #define VN(x)   ((x&VN_MASK)>>3)
108 #define VN_SET(x,y)     do{ x |= ((y<<3)&VN_MASK); }while(0)
109 #define VN_RESERVED 0x02
110 /* bits 6,7,8 are the ntp mode */
111 #define MODE_MASK 0x07
112 #define MODE(x) (x&MODE_MASK)
113 #define MODE_SET(x,y)   do{ x |= (y&MODE_MASK); }while(0)
114 /* here are some values */
115 #define MODE_CLIENT 0x03
116 #define MODE_CONTROLMSG 0x06
117 /* In control message, bits 8-10 are R,E,M bits */
118 #define REM_MASK 0xe0
119 #define REM_RESP 0x80
120 #define REM_ERROR 0x40
121 #define REM_MORE 0x20
122 /* In control message, bits 11 - 15 are opcode */
123 #define OP_MASK 0x1f
124 #define OP_SET(x,y)   do{ x |= (y&OP_MASK); }while(0)
125 #define OP_READSTAT 0x01
126 #define OP_READVAR  0x02
127 /* In peer status bytes, bits 6,7,8 determine clock selection status */
128 #define PEER_SEL(x) ((ntohs(x)>>8)&0x07)
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.
134  */
135 #define SIZEOF_NTPCM(m) (12+ntohs(m.count)+((m.count)?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_INCLUDED){
169                                 if(PEER_SEL(peer[i].status) >= PEER_SYNCSOURCE){
170                                         printf(" <-- current sync source");
171                                 } else {
172                                         printf(" <-- current sync candidate");
173                                 }
174                         }
175                         printf("\n");
176                 }
177         }
180 char *extract_value(const char *varlist, const char *name){
181         char *tmpvarlist=NULL, *tmpkey=NULL, *value=NULL;
182         int last=0;
184         /* The following code require a non-empty varlist */
185         if(strlen(varlist) == 0)
186                 return NULL;
188         tmpvarlist = strdup(varlist);
189         tmpkey = strtok(tmpvarlist, "=");
191         do {
192                 if(strstr(tmpkey, name) != NULL) {
193                 value = strtok(NULL, ",");
194                         last = 1;
195                 }
196         } while (last == 0 && (tmpkey = strtok(NULL, "=")));
198         return value;
201 void
202 setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq){
203         memset(p, 0, sizeof(ntp_control_message));
204         LI_SET(p->flags, LI_NOWARNING);
205         VN_SET(p->flags, VN_RESERVED);
206         MODE_SET(p->flags, MODE_CONTROLMSG);
207         OP_SET(p->op, opcode);
208         p->seq = htons(seq);
209         /* Remaining fields are zero for requests */
212 /* This function does all the actual work; roughly here's what it does
213  * beside setting the offest, jitter and stratum passed as argument:
214  *  - offset can be negative, so if it cannot get the offset, offset_result
215  *    is set to UNKNOWN, otherwise OK.
216  *  - jitter and stratum are set to -1 if they cannot be retrieved so any
217  *    positive value means a success retrieving the value.
218  *  - status is set to WARNING if there's no sync.peer (otherwise OK) and is
219  *    the return value of the function.
220  *  status is pretty much useless as syncsource_found is a global variable
221  *  used later in main to check is the server was synchronized. It works
222  *  so I left it alone */
223 int ntp_request(const char *host, double *offset, int *offset_result, double *jitter, int *stratum){
224         int conn=-1, i, npeers=0, num_candidates=0;
225         double tmp_offset = 0;
226         int min_peer_sel=PEER_INCLUDED;
227         int peers_size=0, peer_offset=0;
228         int status;
229         ntp_assoc_status_pair *peers=NULL;
230         ntp_control_message req;
231         const char *getvar = "stratum,offset,jitter";
232         char *data, *value, *nptr;
233         void *tmp;
235         status = STATE_OK;
236         *offset_result = STATE_UNKNOWN;
237         *jitter = *stratum = -1;
239         /* Long-winded explanation:
240          * Getting the sync peer offset, jitter and stratum requires a number of
241          * steps:
242          * 1) Send a READSTAT request.
243          * 2) Interpret the READSTAT reply
244          *  a) The data section contains a list of peer identifiers (16 bits)
245          *     and associated status words (16 bits)
246          *  b) We want the value of 0x06 in the SEL (peer selection) value,
247          *     which means "current synchronizatin source".  If that's missing,
248          *     we take anything better than 0x04 (see the rfc for details) but
249          *     set a minimum of warning.
250          * 3) Send a READVAR request for information on each peer identified
251          *    in 2b greater than the minimum selection value.
252          * 4) Extract the offset, jitter and stratum value from the data[]
253          *    (it's ASCII)
254          */
255         my_udp_connect(server_address, 123, &conn);
257         /* keep sending requests until the server stops setting the
258          * REM_MORE bit, though usually this is only 1 packet. */
259         do{
260                 setup_control_request(&req, OP_READSTAT, 1);
261                 DBG(printf("sending READSTAT request"));
262                 write(conn, &req, SIZEOF_NTPCM(req));
263                 DBG(print_ntp_control_message(&req));
264                 /* Attempt to read the largest size packet possible */
265                 req.count=htons(MAX_CM_SIZE);
266                 DBG(printf("recieving READSTAT response"))
267                 if(read(conn, &req, SIZEOF_NTPCM(req)) == -1)
268                         die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n");
269                 DBG(print_ntp_control_message(&req));
270                 /* Each peer identifier is 4 bytes in the data section, which
271                  * we represent as a ntp_assoc_status_pair datatype.
272                  */
273                 peers_size+=ntohs(req.count);
274                 if((tmp=realloc(peers, peers_size)) == NULL)
275                         free(peers), die(STATE_UNKNOWN, "can not (re)allocate 'peers' buffer\n");
276                 peers=tmp;
277                 memcpy((void*)((ptrdiff_t)peers+peer_offset), (void*)req.data, ntohs(req.count));
278                 npeers=peers_size/sizeof(ntp_assoc_status_pair);
279                 peer_offset+=ntohs(req.count);
280         } while(req.op&REM_MORE);
282         /* first, let's find out if we have a sync source, or if there are
283          * at least some candidates. In the latter case we'll issue
284          * a warning but go ahead with the check on them. */
285         for (i = 0; i < npeers; i++){
286                 if (PEER_SEL(peers[i].status) >= PEER_INCLUDED){
287                         num_candidates++;
288                         if(PEER_SEL(peers[i].status) >= PEER_SYNCSOURCE){
289                                 syncsource_found=1;
290                                 min_peer_sel=PEER_SYNCSOURCE;
291                         }
292                 }
293         }
294         if(verbose) printf("%d candiate peers available\n", num_candidates);
295         if(verbose && syncsource_found) printf("synchronization source found\n");
296         if(! syncsource_found){
297                 status = STATE_WARNING;
298                 if(verbose) printf("warning: no synchronization source found\n");
299         }
302         for (i = 0; i < npeers; i++){
303                 /* Only query this server if it is the current sync source */
304                 /* If there's no sync.peer, query all candidates and use the best one */
305                 if (PEER_SEL(peers[i].status) >= min_peer_sel){
306                         if(verbose) printf("Getting offset, jitter and stratum for peer %.2x\n", ntohs(peers[i].assoc));
307                         asprintf(&data, "");
308                         do{
309                                 setup_control_request(&req, OP_READVAR, 2);
310                                 req.assoc = peers[i].assoc;
311                                 /* Putting the wanted variable names in the request
312                                  * cause the server to provide _only_ the requested values.
313                                  * thus reducing net traffic, guaranteeing us only a single
314                                  * datagram in reply, and making intepretation much simpler
315                                  */
316                                 /* Older servers doesn't know what jitter is, so if we get an
317                                  * error on the first pass we redo it with "dispersion" */
318                                 strncpy(req.data, getvar, MAX_CM_SIZE-1);
319                                 req.count = htons(strlen(getvar));
320                                 DBG(printf("sending READVAR request...\n"));
321                                 write(conn, &req, SIZEOF_NTPCM(req));
322                                 DBG(print_ntp_control_message(&req));
324                                 req.count = htons(MAX_CM_SIZE);
325                                 DBG(printf("receiving READVAR response...\n"));
326                                 read(conn, &req, SIZEOF_NTPCM(req));
327                                 DBG(print_ntp_control_message(&req));
329                                 if(!(req.op&REM_ERROR))
330                                         asprintf(&data, "%s%s", data, req.data);
331                         } while(req.op&REM_MORE);
333                         if(req.op&REM_ERROR) {
334                                 if(strstr(getvar, "jitter")) {
335                                         if(verbose) printf("The command failed. This is usually caused by servers refusing the 'jitter'\nvariable. Restarting with 'dispersion'...\n");
336                                         getvar = "stratum,offset,dispersion";
337                                         i--;
338                                         continue;
339                                 } else if(strlen(getvar)) {
340                                         if(verbose) printf("Server didn't like dispersion either; will retrieve everything\n");
341                                         getvar = "";
342                                         i--;
343                                         continue;
344                                 }
345                         }
347                         if(verbose > 1)
348                                 printf("Server responded: >>>%s<<<\n", data);
350                         /* get the offset */
351                         if(verbose)
352                                 printf("parsing offset from peer %.2x: ", ntohs(peers[i].assoc));
354                         value = extract_value(data, "offset");
355                         nptr=NULL;
356                         /* Convert the value if we have one */
357                         if(value != NULL)
358                                 tmp_offset = strtod(value, &nptr) / 1000;
359                         /* If value is null or no conversion was performed */
360                         if(value == NULL || value==nptr) {
361                                 if(verbose) printf("error: unable to read server offset response.\n");
362                         } else {
363                                 if(verbose) printf("%.10g\n", tmp_offset);
364                                 if(*offset_result == STATE_UNKNOWN || fabs(tmp_offset) < fabs(*offset)) {
365                                         *offset = tmp_offset;
366                                         *offset_result = STATE_OK;
367                                 } else {
368                                         /* Skip this one; move to the next */
369                                         continue;
370                                 }
371                         }
373                         if(do_jitter) {
374                                 /* get the jitter */
375                                 if(verbose) {
376                                         printf("parsing %s from peer %.2x: ", strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter", ntohs(peers[i].assoc));
377                                 }
378                                 value = extract_value(data, strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter");
379                                 nptr=NULL;
380                                 /* Convert the value if we have one */
381                                 if(value != NULL)
382                                         *jitter = strtod(value, &nptr);
383                                 /* If value is null or no conversion was performed */
384                                 if(value == NULL || value==nptr) {
385                                         if(verbose) printf("error: unable to read server jitter/dispersion response.\n");
386                                         *jitter = -1;
387                                 } else if(verbose) {
388                                         printf("%.10g\n", *jitter);
389                                 }
390                         }
392                         if(do_stratum) {
393                                 /* get the stratum */
394                                 if(verbose) {
395                                         printf("parsing stratum from peer %.2x: ", ntohs(peers[i].assoc));
396                                 }
397                                 value = extract_value(data, "stratum");
398                                 nptr=NULL;
399                                 /* Convert the value if we have one */
400                                 if(value != NULL)
401                                         *stratum = strtol(value, &nptr, 10);
402                                 if(value == NULL || value==nptr) {
403                                         if(verbose) printf("error: unable to read server stratum response.\n");
404                                         *stratum = -1;
405                                 } else {
406                                         if(verbose) printf("%i\n", *stratum);
407                                 }
408                         }
409                 } /* if (PEER_SEL(peers[i].status) >= min_peer_sel) */
410         } /* for (i = 0; i < npeers; i++) */
412         close(conn);
413         if(peers!=NULL) free(peers);
415         return status;
418 int process_arguments(int argc, char **argv){
419         int c;
420         int option=0;
421         static struct option longopts[] = {
422                 {"version", no_argument, 0, 'V'},
423                 {"help", no_argument, 0, 'h'},
424                 {"verbose", no_argument, 0, 'v'},
425                 {"use-ipv4", no_argument, 0, '4'},
426                 {"use-ipv6", no_argument, 0, '6'},
427                 {"quiet", no_argument, 0, 'q'},
428                 {"warning", required_argument, 0, 'w'},
429                 {"critical", required_argument, 0, 'c'},
430                 {"swarn", required_argument, 0, 'W'},
431                 {"scrit", required_argument, 0, 'C'},
432                 {"jwarn", required_argument, 0, 'j'},
433                 {"jcrit", required_argument, 0, 'k'},
434                 {"timeout", required_argument, 0, 't'},
435                 {"hostname", required_argument, 0, 'H'},
436                 {0, 0, 0, 0}
437         };
439         
440         if (argc < 2)
441                 usage ("\n");
443         while (1) {
444                 c = getopt_long (argc, argv, "Vhv46qw:c:W:C:j:k:t:H:", 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, revision);
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 'H':
488                         if(is_host(optarg) == FALSE)
489                                 usage2(_("Invalid hostname/address"), optarg);
490                         server_address = strdup(optarg);
491                         break;
492                 case 't':
493                         socket_timeout=atoi(optarg);
494                         break;
495                 case '4':
496                         address_family = AF_INET;
497                         break;
498                 case '6':
499 #ifdef USE_IPV6
500                         address_family = AF_INET6;
501 #else
502                         usage4 (_("IPv6 support not available"));
503 #endif
504                         break;
505                 case '?':
506                         /* print short usage statement if args not parsable */
507                         usage5 ();
508                         break;
509                 }
510         }
512         if(server_address == NULL){
513                 usage4(_("Hostname was not supplied"));
514         }
516         return 0;
519 char *perfd_offset (double offset)
521         return fperfdata ("offset", offset, "s",
522                 TRUE, offset_thresholds->warning->end,
523                 TRUE, offset_thresholds->critical->end,
524                 FALSE, 0, FALSE, 0);
527 char *perfd_jitter (double jitter)
529         return fperfdata ("jitter", jitter, "",
530                 do_jitter, jitter_thresholds->warning->end,
531                 do_jitter, jitter_thresholds->critical->end,
532                 TRUE, 0, FALSE, 0);
535 char *perfd_stratum (int stratum)
537         return perfdata ("stratum", stratum, "",
538                 do_stratum, (int)stratum_thresholds->warning->end,
539                 do_stratum, (int)stratum_thresholds->critical->end,
540                 TRUE, 0, TRUE, 16);
543 int main(int argc, char *argv[]){
544         int result, offset_result, stratum;
545         double offset=0, jitter=0;
546         char *result_line, *perfdata_line;
548         setlocale (LC_ALL, "");
549         bindtextdomain (PACKAGE, LOCALEDIR);
550         textdomain (PACKAGE);
552         if (process_arguments (argc, argv) == ERROR)
553                 usage4 (_("Could not parse arguments"));
555         set_thresholds(&offset_thresholds, owarn, ocrit);
556         set_thresholds(&jitter_thresholds, jwarn, jcrit);
557         set_thresholds(&stratum_thresholds, swarn, scrit);
559         /* initialize alarm signal handling */
560         signal (SIGALRM, socket_timeout_alarm_handler);
562         /* set socket timeout */
563         alarm (socket_timeout);
565         /* This returns either OK or WARNING (See comment preceeding ntp_request) */
566         result = ntp_request(server_address, &offset, &offset_result, &jitter, &stratum);
568         if(offset_result == STATE_UNKNOWN) {
569                 /* if there's no sync peer (this overrides ntp_request output): */
570                 result = (quiet == 1 ? STATE_UNKNOWN : STATE_CRITICAL);
571         } else {
572                 /* Be quiet if there's no candidates either */
573                 if (quiet == 1 && result == STATE_WARNING)
574                         result = STATE_UNKNOWN;
575                 result = max_state_alt(result, get_status(fabs(offset), offset_thresholds));
576         }
578         if(do_stratum)
579                 result = max_state_alt(result, get_status(stratum, stratum_thresholds));
581         if(do_jitter)
582                 result = max_state_alt(result, get_status(jitter, jitter_thresholds));
584         switch (result) {
585                 case STATE_CRITICAL :
586                         asprintf(&result_line, _("NTP CRITICAL:"));
587                         break;
588                 case STATE_WARNING :
589                         asprintf(&result_line, _("NTP WARNING:"));
590                         break;
591                 case STATE_OK :
592                         asprintf(&result_line, _("NTP OK:"));
593                         break;
594                 default :
595                         asprintf(&result_line, _("NTP UNKNOWN:"));
596                         break;
597         }
598         if(!syncsource_found)
599                 asprintf(&result_line, "%s %s,", result_line, _("Server not synchronized"));
601         if(offset_result == STATE_UNKNOWN){
602                 asprintf(&result_line, "%s %s", result_line, _("Offset unknown"));
603                 asprintf(&perfdata_line, "");
604         } else {
605                 asprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset);
606                 asprintf(&perfdata_line, "%s", perfd_offset(offset));
607         }
608         if (do_jitter) {
609                 asprintf(&result_line, "%s, jitter=%f", result_line, jitter);
610                 asprintf(&perfdata_line, "%s %s", perfdata_line, perfd_jitter(jitter));
611         }
612         if (do_stratum) {
613                 asprintf(&result_line, "%s, stratum=%i", result_line, stratum);
614                 asprintf(&perfdata_line, "%s %s", perfdata_line, perfd_stratum(stratum));
615         }
616         printf("%s|%s\n", result_line, perfdata_line);
618         if(server_address!=NULL) free(server_address);
619         return result;
624 void print_help(void){
625         print_revision(progname, revision);
627         printf ("Copyright (c) 2006 Sean Finney\n");
628         printf (COPYRIGHT, copyright, email);
630         printf ("%s\n", _("This plugin checks the selected ntp server"));
632         printf ("\n\n");
634         print_usage();
635         printf (_(UT_HELP_VRSN));
636         printf (_(UT_HOST_PORT), 'p', "123");
637         printf (" %s\n", "-q, --quiet");
638         printf ("    %s\n", _("Returns UNKNOWN instead of CRITICAL or WARNING if server isn't synchronized"));
639         printf (" %s\n", "-w, --warning=THRESHOLD");
640         printf ("    %s\n", _("Offset to result in warning status (seconds)"));
641         printf (" %s\n", "-c, --critical=THRESHOLD");
642         printf ("    %s\n", _("Offset to result in critical status (seconds)"));
643         printf (" %s\n", "-W, --warning=THRESHOLD");
644         printf ("    %s\n", _("Warning threshold for stratum"));
645         printf (" %s\n", "-W, --critical=THRESHOLD");
646         printf ("    %s\n", _("Critical threshold for stratum"));
647         printf (" %s\n", "-j, --warning=THRESHOLD");
648         printf ("    %s\n", _("Warning threshold for jitter"));
649         printf (" %s\n", "-k, --critical=THRESHOLD");
650         printf ("    %s\n", _("Critical threshold for jitter"));
651         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
652         printf (_(UT_VERBOSE));
654         printf("\n");
655         printf("%s\n", _("Notes:"));
656         printf(" %s\n", _("This plugin checks an NTP server independent of any commandline"));
657         printf(" %s\n\n", _("programs or external libraries."));
658         printf(" %s\n", _("Use this plugin to check the health of an NTP server. It supports"));
659         printf(" %s\n", _("checking the offset with the sync peer, the jitter and stratum. This"));
660         printf(" %s\n", _("plugin will not check the clock offset between the local host and NTP"));
661         printf(" %s\n\n", _("server; please use check_ntp_time for that purpose."));
663         printf(" %s\n", _("See:"));
664         printf(" %s\n", ("http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT"));
665         printf(" %s\n", _("for THRESHOLD format and examples."));
667         printf("\n");
668         printf("%s\n", _("Examples:"));
669         printf(" %s\n", _("Simple NTP server check:"));
670         printf("  %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1"));
671         printf(" %s\n", _("Check jitter too, avoiding critical notifications if jitter isn't available"));
672         printf(" %s\n", _("(See Notes above for more details on thresholds formats):"));
673         printf("  %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1 -j -1:100 -k -1:200"));
674         printf(" %s\n", _("Check only stratum:"));
675         printf("  %s\n", ("./check_ntp_peer -H ntpserv -W 4 -C 6"));
677         printf (_(UT_SUPPORT));
680 void
681 print_usage(void)
683         printf (_("Usage:"));
684         printf(" %s -H <host> [-w <warn>] [-c <crit>] [-W <warn>] [-C <crit>]\n", progname);
685         printf("       [-j <warn>] [-k <crit>] [-v verbose]\n");