Code

OOPS! Last commit should have included these files.
[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                 read(conn, &req, SIZEOF_NTPCM(req));
268                 DBG(print_ntp_control_message(&req));
269                 /* Each peer identifier is 4 bytes in the data section, which
270                  * we represent as a ntp_assoc_status_pair datatype.
271                  */
272                 peers_size+=ntohs(req.count);
273                 if((tmp=realloc(peers, peers_size)) == NULL)
274                         free(peers), die(STATE_UNKNOWN, "can not (re)allocate 'peers' buffer\n");
275                 peers=tmp;
276                 memcpy((void*)((ptrdiff_t)peers+peer_offset), (void*)req.data, ntohs(req.count));
277                 npeers=peers_size/sizeof(ntp_assoc_status_pair);
278                 peer_offset+=ntohs(req.count);
279         } while(req.op&REM_MORE);
281         /* first, let's find out if we have a sync source, or if there are
282          * at least some candidates. In the latter case we'll issue
283          * a warning but go ahead with the check on them. */
284         for (i = 0; i < npeers; i++){
285                 if (PEER_SEL(peers[i].status) >= PEER_INCLUDED){
286                         num_candidates++;
287                         if(PEER_SEL(peers[i].status) >= PEER_SYNCSOURCE){
288                                 syncsource_found=1;
289                                 min_peer_sel=PEER_SYNCSOURCE;
290                         }
291                 }
292         }
293         if(verbose) printf("%d candiate peers available\n", num_candidates);
294         if(verbose && syncsource_found) printf("synchronization source found\n");
295         if(! syncsource_found){
296                 status = STATE_WARNING;
297                 if(verbose) printf("warning: no synchronization source found\n");
298         }
301         for (i = 0; i < npeers; i++){
302                 /* Only query this server if it is the current sync source */
303                 /* If there's no sync.peer, query all candidates and use the best one */
304                 if (PEER_SEL(peers[i].status) >= min_peer_sel){
305                         if(verbose) printf("Getting offset, jitter and stratum for peer %.2x\n", ntohs(peers[i].assoc));
306                         asprintf(&data, "");
307                         do{
308                                 setup_control_request(&req, OP_READVAR, 2);
309                                 req.assoc = peers[i].assoc;
310                                 /* Putting the wanted variable names in the request
311                                  * cause the server to provide _only_ the requested values.
312                                  * thus reducing net traffic, guaranteeing us only a single
313                                  * datagram in reply, and making intepretation much simpler
314                                  */
315                                 /* Older servers doesn't know what jitter is, so if we get an
316                                  * error on the first pass we redo it with "dispersion" */
317                                 strncpy(req.data, getvar, MAX_CM_SIZE-1);
318                                 req.count = htons(strlen(getvar));
319                                 DBG(printf("sending READVAR request...\n"));
320                                 write(conn, &req, SIZEOF_NTPCM(req));
321                                 DBG(print_ntp_control_message(&req));
323                                 req.count = htons(MAX_CM_SIZE);
324                                 DBG(printf("receiving READVAR response...\n"));
325                                 read(conn, &req, SIZEOF_NTPCM(req));
326                                 DBG(print_ntp_control_message(&req));
328                                 if(!(req.op&REM_ERROR))
329                                         asprintf(&data, "%s%s", data, req.data);
330                         } while(req.op&REM_MORE);
332                         if(req.op&REM_ERROR) {
333                                 if(strstr(getvar, "jitter")) {
334                                         if(verbose) printf("The command failed. This is usually caused by servers refusing the 'jitter'\nvariable. Restarting with 'dispersion'...\n");
335                                         getvar = "stratum,offset,dispersion";
336                                         i--;
337                                         continue;
338                                 } else if(strlen(getvar)) {
339                                         if(verbose) printf("Server didn't like dispersion either; will retrieve everything\n");
340                                         getvar = "";
341                                         i--;
342                                         continue;
343                                 }
344                         }
346                         if(verbose > 1)
347                                 printf("Server responded: >>>%s<<<\n", data);
349                         /* get the offset */
350                         if(verbose)
351                                 printf("parsing offset from peer %.2x: ", ntohs(peers[i].assoc));
353                         value = extract_value(data, "offset");
354                         nptr=NULL;
355                         /* Convert the value if we have one */
356                         if(value != NULL)
357                                 tmp_offset = strtod(value, &nptr) / 1000;
358                         /* If value is null or no conversion was performed */
359                         if(value == NULL || value==nptr) {
360                                 if(verbose) printf("error: unable to read server offset response.\n");
361                         } else {
362                                 if(verbose) printf("%.10g\n", tmp_offset);
363                                 if(*offset_result == STATE_UNKNOWN || fabs(tmp_offset) < fabs(*offset)) {
364                                         *offset = tmp_offset;
365                                         *offset_result = STATE_OK;
366                                 } else {
367                                         /* Skip this one; move to the next */
368                                         continue;
369                                 }
370                         }
372                         if(do_jitter) {
373                                 /* get the jitter */
374                                 if(verbose) {
375                                         printf("parsing %s from peer %.2x: ", strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter", ntohs(peers[i].assoc));
376                                 }
377                                 value = extract_value(data, strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter");
378                                 nptr=NULL;
379                                 /* Convert the value if we have one */
380                                 if(value != NULL)
381                                         *jitter = strtod(value, &nptr);
382                                 /* If value is null or no conversion was performed */
383                                 if(value == NULL || value==nptr) {
384                                         if(verbose) printf("error: unable to read server jitter/dispersion response.\n");
385                                         *jitter = -1;
386                                 } else if(verbose) {
387                                         printf("%.10g\n", *jitter);
388                                 }
389                         }
391                         if(do_stratum) {
392                                 /* get the stratum */
393                                 if(verbose) {
394                                         printf("parsing stratum from peer %.2x: ", ntohs(peers[i].assoc));
395                                 }
396                                 value = extract_value(data, "stratum");
397                                 nptr=NULL;
398                                 /* Convert the value if we have one */
399                                 if(value != NULL)
400                                         *stratum = strtol(value, &nptr, 10);
401                                 if(value == NULL || value==nptr) {
402                                         if(verbose) printf("error: unable to read server stratum response.\n");
403                                         *stratum = -1;
404                                 } else {
405                                         if(verbose) printf("%i\n", *stratum);
406                                 }
407                         }
408                 } /* if (PEER_SEL(peers[i].status) >= min_peer_sel) */
409         } /* for (i = 0; i < npeers; i++) */
411         close(conn);
412         if(peers!=NULL) free(peers);
414         return status;
417 int process_arguments(int argc, char **argv){
418         int c;
419         int option=0;
420         static struct option longopts[] = {
421                 {"version", no_argument, 0, 'V'},
422                 {"help", no_argument, 0, 'h'},
423                 {"verbose", no_argument, 0, 'v'},
424                 {"use-ipv4", no_argument, 0, '4'},
425                 {"use-ipv6", no_argument, 0, '6'},
426                 {"quiet", no_argument, 0, 'q'},
427                 {"warning", required_argument, 0, 'w'},
428                 {"critical", required_argument, 0, 'c'},
429                 {"swarn", required_argument, 0, 'W'},
430                 {"scrit", required_argument, 0, 'C'},
431                 {"jwarn", required_argument, 0, 'j'},
432                 {"jcrit", required_argument, 0, 'k'},
433                 {"timeout", required_argument, 0, 't'},
434                 {"hostname", required_argument, 0, 'H'},
435                 {0, 0, 0, 0}
436         };
438         
439         if (argc < 2)
440                 usage ("\n");
442         while (1) {
443                 c = getopt_long (argc, argv, "Vhv46qw:c:W:C:j:k:t:H:", longopts, &option);
444                 if (c == -1 || c == EOF || c == 1)
445                         break;
447                 switch (c) {
448                 case 'h':
449                         print_help();
450                         exit(STATE_OK);
451                         break;
452                 case 'V':
453                         print_revision(progname, revision);
454                         exit(STATE_OK);
455                         break;
456                 case 'v':
457                         verbose++;
458                         break;
459                 case 'q':
460                         quiet = 1;
461                         break;
462                 case 'w':
463                         do_offset=1;
464                         owarn = optarg;
465                         break;
466                 case 'c':
467                         do_offset=1;
468                         ocrit = optarg;
469                         break;
470                 case 'W':
471                         do_stratum=1;
472                         swarn = optarg;
473                         break;
474                 case 'C':
475                         do_stratum=1;
476                         scrit = optarg;
477                         break;
478                 case 'j':
479                         do_jitter=1;
480                         jwarn = optarg;
481                         break;
482                 case 'k':
483                         do_jitter=1;
484                         jcrit = optarg;
485                         break;
486                 case 'H':
487                         if(is_host(optarg) == FALSE)
488                                 usage2(_("Invalid hostname/address"), optarg);
489                         server_address = strdup(optarg);
490                         break;
491                 case 't':
492                         socket_timeout=atoi(optarg);
493                         break;
494                 case '4':
495                         address_family = AF_INET;
496                         break;
497                 case '6':
498 #ifdef USE_IPV6
499                         address_family = AF_INET6;
500 #else
501                         usage4 (_("IPv6 support not available"));
502 #endif
503                         break;
504                 case '?':
505                         /* print short usage statement if args not parsable */
506                         usage5 ();
507                         break;
508                 }
509         }
511         if(server_address == NULL){
512                 usage4(_("Hostname was not supplied"));
513         }
515         return 0;
518 char *perfd_offset (double offset)
520         return fperfdata ("offset", offset, "s",
521                 TRUE, offset_thresholds->warning->end,
522                 TRUE, offset_thresholds->critical->end,
523                 FALSE, 0, FALSE, 0);
526 char *perfd_jitter (double jitter)
528         return fperfdata ("jitter", jitter, "",
529                 do_jitter, jitter_thresholds->warning->end,
530                 do_jitter, jitter_thresholds->critical->end,
531                 TRUE, 0, FALSE, 0);
534 char *perfd_stratum (int stratum)
536         return perfdata ("stratum", stratum, "",
537                 do_stratum, (int)stratum_thresholds->warning->end,
538                 do_stratum, (int)stratum_thresholds->critical->end,
539                 TRUE, 0, TRUE, 16);
542 int main(int argc, char *argv[]){
543         int result, offset_result, stratum;
544         double offset=0, jitter=0;
545         char *result_line, *perfdata_line;
547         if (process_arguments (argc, argv) == ERROR)
548                 usage4 (_("Could not parse arguments"));
550         set_thresholds(&offset_thresholds, owarn, ocrit);
551         set_thresholds(&jitter_thresholds, jwarn, jcrit);
552         set_thresholds(&stratum_thresholds, swarn, scrit);
554         /* initialize alarm signal handling */
555         signal (SIGALRM, socket_timeout_alarm_handler);
557         /* set socket timeout */
558         alarm (socket_timeout);
560         /* This returns either OK or WARNING (See comment preceeding ntp_request) */
561         result = ntp_request(server_address, &offset, &offset_result, &jitter, &stratum);
563         if(offset_result == STATE_UNKNOWN) {
564                 /* if there's no sync peer (this overrides ntp_request output): */
565                 result = (quiet == 1 ? STATE_UNKNOWN : STATE_CRITICAL);
566         } else {
567                 /* Be quiet if there's no candidates either */
568                 if (quiet == 1 && result == STATE_WARNING)
569                         result = STATE_UNKNOWN;
570                 result = max_state_alt(result, get_status(fabs(offset), offset_thresholds));
571         }
573         if(do_stratum)
574                 result = max_state_alt(result, get_status(stratum, stratum_thresholds));
576         if(do_jitter)
577                 result = max_state_alt(result, get_status(jitter, jitter_thresholds));
579         switch (result) {
580                 case STATE_CRITICAL :
581                         asprintf(&result_line, _("NTP CRITICAL:"));
582                         break;
583                 case STATE_WARNING :
584                         asprintf(&result_line, _("NTP WARNING:"));
585                         break;
586                 case STATE_OK :
587                         asprintf(&result_line, _("NTP OK:"));
588                         break;
589                 default :
590                         asprintf(&result_line, _("NTP UNKNOWN:"));
591                         break;
592         }
593         if(!syncsource_found)
594                 asprintf(&result_line, "%s %s,", result_line, _("Server not synchronized"));
596         if(offset_result == STATE_UNKNOWN){
597                 asprintf(&result_line, "%s %s", result_line, _("Offset unknown"));
598                 asprintf(&perfdata_line, "");
599         } else {
600                 asprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset);
601                 asprintf(&perfdata_line, "%s", perfd_offset(offset));
602         }
603         if (do_jitter) {
604                 asprintf(&result_line, "%s, jitter=%f", result_line, jitter);
605                 asprintf(&perfdata_line, "%s %s", perfdata_line, perfd_jitter(jitter));
606         }
607         if (do_stratum) {
608                 asprintf(&result_line, "%s, stratum=%i", result_line, stratum);
609                 asprintf(&perfdata_line, "%s %s", perfdata_line, perfd_stratum(stratum));
610         }
611         printf("%s|%s\n", result_line, perfdata_line);
613         if(server_address!=NULL) free(server_address);
614         return result;
619 void print_help(void){
620         print_revision(progname, revision);
622         printf ("Copyright (c) 2006 Sean Finney\n");
623         printf (COPYRIGHT, copyright, email);
625         printf ("%s\n", _("This plugin checks the selected ntp server"));
627         printf ("\n\n");
629         print_usage();
630         printf (_(UT_HELP_VRSN));
631         printf (_(UT_HOST_PORT), 'p', "123");
632         printf (" %s\n", "-q, --quiet");
633         printf ("    %s\n", _("Returns UNKNOWN instead of CRITICAL or WARNING if server isn't synchronized"));
634         printf (" %s\n", "-w, --warning=THRESHOLD");
635         printf ("    %s\n", _("Offset to result in warning status (seconds)"));
636         printf (" %s\n", "-c, --critical=THRESHOLD");
637         printf ("    %s\n", _("Offset to result in critical status (seconds)"));
638         printf (" %s\n", "-W, --warning=THRESHOLD");
639         printf ("    %s\n", _("Warning threshold for stratum"));
640         printf (" %s\n", "-W, --critical=THRESHOLD");
641         printf ("    %s\n", _("Critical threshold for stratum"));
642         printf (" %s\n", "-j, --warning=THRESHOLD");
643         printf ("    %s\n", _("Warning threshold for jitter"));
644         printf (" %s\n", "-k, --critical=THRESHOLD");
645         printf ("    %s\n", _("Critical threshold for jitter"));
646         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
647         printf (_(UT_VERBOSE));
649         printf("\n");
650         printf("%s\n", _("Notes:"));
651         printf(" %s\n", _("This plugin checks an NTP server independent of any commandline"));
652         printf(" %s\n\n", _("programs or external libraries."));
653         printf(" %s\n", _("Use this plugin to check the health of an NTP server. It supports"));
654         printf(" %s\n", _("checking the offset with the sync peer, the jitter and stratum. This"));
655         printf(" %s\n", _("plugin will not check the clock offset between the local host and NTP"));
656         printf(" %s\n\n", _("server; please use check_ntp_time for that purpose."));
658         printf(" %s\n", _("See:"));
659         printf(" %s\n", ("http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT"));
660         printf(" %s\n", _("for THRESHOLD format and examples."));
662         printf("\n");
663         printf("%s\n", _("Examples:"));
664         printf(" %s\n", _("Simple NTP server check:"));
665         printf("  %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1"));
666         printf(" %s\n", _("Check jitter too, avoiding critical notifications if jitter isn't available"));
667         printf(" %s\n", _("(See Notes above for more details on thresholds formats):"));
668         printf("  %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1 -j -1:100 -k -1:200"));
669         printf(" %s\n", _("Check only stratum:"));
670         printf("  %s\n", ("./check_ntp_peer -H ntpserv -W 4 -C 6"));
672         printf (_(UT_SUPPORT));
675 void
676 print_usage(void)
678         printf (_("Usage:"));
679         printf(" %s -H <host> [-w <warn>] [-c <crit>] [-W <warn>] [-C <crit>]\n", progname);
680         printf("       [-j <warn>] [-k <crit>] [-v verbose]\n");