summaryrefslogtreecommitdiffstats
path: root/plugins/check_ntp_time.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_ntp_time.c')
-rw-r--r--plugins/check_ntp_time.c704
1 files changed, 378 insertions, 326 deletions
diff --git a/plugins/check_ntp_time.c b/plugins/check_ntp_time.c
index b2e16556..31162883 100644
--- a/plugins/check_ntp_time.c
+++ b/plugins/check_ntp_time.c
@@ -1,63 +1,64 @@
1/***************************************************************************** 1/*****************************************************************************
2* 2 *
3* Monitoring check_ntp_time plugin 3 * Monitoring check_ntp_time plugin
4* 4 *
5* License: GPL 5 * License: GPL
6* Copyright (c) 2006 Sean Finney <seanius@seanius.net> 6 * Copyright (c) 2006 Sean Finney <seanius@seanius.net>
7* Copyright (c) 2006-2008 Monitoring Plugins Development Team 7 * Copyright (c) 2006-2024 Monitoring Plugins Development Team
8* 8 *
9* Description: 9 * Description:
10* 10 *
11* This file contains the check_ntp_time plugin 11 * This file contains the check_ntp_time plugin
12* 12 *
13* This plugin checks the clock offset between the local host and a 13 * This plugin checks the clock offset between the local host and a
14* remote NTP server. It is independent of any commandline programs or 14 * remote NTP server. It is independent of any commandline programs or
15* external libraries. 15 * external libraries.
16* 16 *
17* If you'd rather want to monitor an NTP server, please use 17 * If you'd rather want to monitor an NTP server, please use
18* check_ntp_peer. 18 * check_ntp_peer.
19* 19 *
20* 20 *
21* This program is free software: you can redistribute it and/or modify 21 * This program is free software: you can redistribute it and/or modify
22* it under the terms of the GNU General Public License as published by 22 * it under the terms of the GNU General Public License as published by
23* the Free Software Foundation, either version 3 of the License, or 23 * the Free Software Foundation, either version 3 of the License, or
24* (at your option) any later version. 24 * (at your option) any later version.
25* 25 *
26* This program is distributed in the hope that it will be useful, 26 * This program is distributed in the hope that it will be useful,
27* but WITHOUT ANY WARRANTY; without even the implied warranty of 27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29* GNU General Public License for more details. 29 * GNU General Public License for more details.
30* 30 *
31* You should have received a copy of the GNU General Public License 31 * You should have received a copy of the GNU General Public License
32* along with this program. If not, see <http://www.gnu.org/licenses/>. 32 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33* 33 *
34* 34 *
35*****************************************************************************/ 35 *****************************************************************************/
36 36
37const char *progname = "check_ntp_time"; 37const char *progname = "check_ntp_time";
38const char *copyright = "2006-2008"; 38const char *copyright = "2006-2024";
39const char *email = "devel@monitoring-plugins.org"; 39const char *email = "devel@monitoring-plugins.org";
40 40
41#include "common.h" 41#include "common.h"
42#include "netutils.h" 42#include "netutils.h"
43#include "utils.h" 43#include "utils.h"
44#include "states.h"
45#include "thresholds.h"
46#include "check_ntp_time.d/config.h"
44 47
45static char *server_address=NULL; 48static int verbose = 0;
46static char *port="123";
47static int verbose=0;
48static bool quiet = false;
49static char *owarn="60";
50static char *ocrit="120";
51static int time_offset=0;
52 49
53int process_arguments (int, char **); 50typedef struct {
54thresholds *offset_thresholds = NULL; 51 int errorcode;
55void print_help (void); 52 check_ntp_time_config config;
56void print_usage (void); 53} check_ntp_time_config_wrapper;
54static check_ntp_time_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
55
56static void print_help(void);
57void print_usage(void);
57 58
58/* number of times to perform each request to get a good average. */ 59/* number of times to perform each request to get a good average. */
59#ifndef AVG_NUM 60#ifndef AVG_NUM
60#define AVG_NUM 4 61# define AVG_NUM 4
61#endif 62#endif
62 63
63/* max size of control message data */ 64/* max size of control message data */
@@ -65,17 +66,17 @@ void print_usage (void);
65 66
66/* this structure holds everything in an ntp request/response as per rfc1305 */ 67/* this structure holds everything in an ntp request/response as per rfc1305 */
67typedef struct { 68typedef struct {
68 uint8_t flags; /* byte with leapindicator,vers,mode. see macros */ 69 uint8_t flags; /* byte with leapindicator,vers,mode. see macros */
69 uint8_t stratum; /* clock stratum */ 70 uint8_t stratum; /* clock stratum */
70 int8_t poll; /* polling interval */ 71 int8_t poll; /* polling interval */
71 int8_t precision; /* precision of the local clock */ 72 int8_t precision; /* precision of the local clock */
72 int32_t rtdelay; /* total rt delay, as a fixed point num. see macros */ 73 int32_t rtdelay; /* total rt delay, as a fixed point num. see macros */
73 uint32_t rtdisp; /* like above, but for max err to primary src */ 74 uint32_t rtdisp; /* like above, but for max err to primary src */
74 uint32_t refid; /* ref clock identifier */ 75 uint32_t refid; /* ref clock identifier */
75 uint64_t refts; /* reference timestamp. local time local clock */ 76 uint64_t refts; /* reference timestamp. local time local clock */
76 uint64_t origts; /* time at which request departed client */ 77 uint64_t origts; /* time at which request departed client */
77 uint64_t rxts; /* time at which request arrived at server */ 78 uint64_t rxts; /* time at which request arrived at server */
78 uint64_t txts; /* time at which request departed server */ 79 uint64_t txts; /* time at which request departed server */
79} ntp_message; 80} ntp_message;
80 81
81/* this structure holds data about results from querying offset from a peer */ 82/* this structure holds data about results from querying offset from a peer */
@@ -86,43 +87,55 @@ typedef struct {
86 double rtdelay; /* converted from the ntp_message */ 87 double rtdelay; /* converted from the ntp_message */
87 double rtdisp; /* converted from the ntp_message */ 88 double rtdisp; /* converted from the ntp_message */
88 double offset[AVG_NUM]; /* offsets from each response */ 89 double offset[AVG_NUM]; /* offsets from each response */
89 uint8_t flags; /* byte with leapindicator,vers,mode. see macros */ 90 uint8_t flags; /* byte with leapindicator,vers,mode. see macros */
90} ntp_server_results; 91} ntp_server_results;
91 92
92/* bits 1,2 are the leap indicator */ 93/* bits 1,2 are the leap indicator */
93#define LI_MASK 0xc0 94#define LI_MASK 0xc0
94#define LI(x) ((x&LI_MASK)>>6) 95#define LI(x) ((x & LI_MASK) >> 6)
95#define LI_SET(x,y) do{ x |= ((y<<6)&LI_MASK); }while(0) 96#define LI_SET(x, y) \
97 do { \
98 x |= ((y << 6) & LI_MASK); \
99 } while (0)
96/* and these are the values of the leap indicator */ 100/* and these are the values of the leap indicator */
97#define LI_NOWARNING 0x00 101#define LI_NOWARNING 0x00
98#define LI_EXTRASEC 0x01 102#define LI_EXTRASEC 0x01
99#define LI_MISSINGSEC 0x02 103#define LI_MISSINGSEC 0x02
100#define LI_ALARM 0x03 104#define LI_ALARM 0x03
101/* bits 3,4,5 are the ntp version */ 105/* bits 3,4,5 are the ntp version */
102#define VN_MASK 0x38 106#define VN_MASK 0x38
103#define VN(x) ((x&VN_MASK)>>3) 107#define VN(x) ((x & VN_MASK) >> 3)
104#define VN_SET(x,y) do{ x |= ((y<<3)&VN_MASK); }while(0) 108#define VN_SET(x, y) \
109 do { \
110 x |= ((y << 3) & VN_MASK); \
111 } while (0)
105#define VN_RESERVED 0x02 112#define VN_RESERVED 0x02
106/* bits 6,7,8 are the ntp mode */ 113/* bits 6,7,8 are the ntp mode */
107#define MODE_MASK 0x07 114#define MODE_MASK 0x07
108#define MODE(x) (x&MODE_MASK) 115#define MODE(x) (x & MODE_MASK)
109#define MODE_SET(x,y) do{ x |= (y&MODE_MASK); }while(0) 116#define MODE_SET(x, y) \
117 do { \
118 x |= (y & MODE_MASK); \
119 } while (0)
110/* here are some values */ 120/* here are some values */
111#define MODE_CLIENT 0x03 121#define MODE_CLIENT 0x03
112#define MODE_CONTROLMSG 0x06 122#define MODE_CONTROLMSG 0x06
113/* In control message, bits 8-10 are R,E,M bits */ 123/* In control message, bits 8-10 are R,E,M bits */
114#define REM_MASK 0xe0 124#define REM_MASK 0xe0
115#define REM_RESP 0x80 125#define REM_RESP 0x80
116#define REM_ERROR 0x40 126#define REM_ERROR 0x40
117#define REM_MORE 0x20 127#define REM_MORE 0x20
118/* In control message, bits 11 - 15 are opcode */ 128/* In control message, bits 11 - 15 are opcode */
119#define OP_MASK 0x1f 129#define OP_MASK 0x1f
120#define OP_SET(x,y) do{ x |= (y&OP_MASK); }while(0) 130#define OP_SET(x, y) \
131 do { \
132 x |= (y & OP_MASK); \
133 } while (0)
121#define OP_READSTAT 0x01 134#define OP_READSTAT 0x01
122#define OP_READVAR 0x02 135#define OP_READVAR 0x02
123/* In peer status bytes, bits 6,7,8 determine clock selection status */ 136/* In peer status bytes, bits 6,7,8 determine clock selection status */
124#define PEER_SEL(x) ((ntohs(x)>>8)&0x07) 137#define PEER_SEL(x) ((ntohs(x) >> 8) & 0x07)
125#define PEER_INCLUDED 0x04 138#define PEER_INCLUDED 0x04
126#define PEER_SYNCSOURCE 0x06 139#define PEER_SYNCSOURCE 0x06
127 140
128/** 141/**
@@ -136,129 +149,137 @@ typedef struct {
136 149
137/* macros to access the left/right 16 bits of a 32-bit ntp "fixed point" 150/* macros to access the left/right 16 bits of a 32-bit ntp "fixed point"
138 number. note that these can be used as lvalues too */ 151 number. note that these can be used as lvalues too */
139#define L16(x) (((uint16_t*)&x)[0]) 152#define L16(x) (((uint16_t *)&x)[0])
140#define R16(x) (((uint16_t*)&x)[1]) 153#define R16(x) (((uint16_t *)&x)[1])
141/* macros to access the left/right 32 bits of a 64-bit ntp "fixed point" 154/* macros to access the left/right 32 bits of a 64-bit ntp "fixed point"
142 number. these too can be used as lvalues */ 155 number. these too can be used as lvalues */
143#define L32(x) (((uint32_t*)&x)[0]) 156#define L32(x) (((uint32_t *)&x)[0])
144#define R32(x) (((uint32_t*)&x)[1]) 157#define R32(x) (((uint32_t *)&x)[1])
145 158
146/* ntp wants seconds since 1/1/00, epoch is 1/1/70. this is the difference */ 159/* ntp wants seconds since 1/1/00, epoch is 1/1/70. this is the difference */
147#define EPOCHDIFF 0x83aa7e80UL 160#define EPOCHDIFF 0x83aa7e80UL
148 161
149/* extract a 32-bit ntp fixed point number into a double */ 162/* extract a 32-bit ntp fixed point number into a double */
150#define NTP32asDOUBLE(x) (ntohs(L16(x)) + (double)ntohs(R16(x))/65536.0) 163#define NTP32asDOUBLE(x) (ntohs(L16(x)) + ((double)ntohs(R16(x)) / 65536.0))
151 164
152/* likewise for a 64-bit ntp fp number */ 165/* likewise for a 64-bit ntp fp number */
153#define NTP64asDOUBLE(n) (double)(((uint64_t)n)?\ 166#define NTP64asDOUBLE(n) \
154 (ntohl(L32(n))-EPOCHDIFF) + \ 167 (double)(((uint64_t)n) ? (ntohl(L32(n)) - EPOCHDIFF) + (.00000001 * (0.5 + (double)(ntohl(R32(n)) / 42.94967296))) : 0)
155 (.00000001*(0.5+(double)(ntohl(R32(n))/42.94967296))):\
156 0)
157 168
158/* convert a struct timeval to a double */ 169/* convert a struct timeval to a double */
159#define TVasDOUBLE(x) (double)(x.tv_sec+(0.000001*x.tv_usec)) 170#define TVasDOUBLE(x) (double)(x.tv_sec + (0.000001 * x.tv_usec))
160 171
161/* convert an ntp 64-bit fp number to a struct timeval */ 172/* convert an ntp 64-bit fp number to a struct timeval */
162#define NTP64toTV(n,t) \ 173#define NTP64toTV(n, t) \
163 do{ if(!n) t.tv_sec = t.tv_usec = 0; \ 174 do { \
164 else { \ 175 if (!n) \
165 t.tv_sec=ntohl(L32(n))-EPOCHDIFF; \ 176 t.tv_sec = t.tv_usec = 0; \
166 t.tv_usec=(int)(0.5+(double)(ntohl(R32(n))/4294.967296)); \ 177 else { \
167 } \ 178 t.tv_sec = ntohl(L32(n)) - EPOCHDIFF; \
168 }while(0) 179 t.tv_usec = (int)(0.5 + (double)(ntohl(R32(n)) / 4294.967296)); \
180 } \
181 } while (0)
169 182
170/* convert a struct timeval to an ntp 64-bit fp number */ 183/* convert a struct timeval to an ntp 64-bit fp number */
171#define TVtoNTP64(t,n) \ 184#define TVtoNTP64(t, n) \
172 do{ if(!t.tv_usec && !t.tv_sec) n=0x0UL; \ 185 do { \
173 else { \ 186 if (!t.tv_usec && !t.tv_sec) \
174 L32(n)=htonl(t.tv_sec + EPOCHDIFF); \ 187 n = 0x0UL; \
175 R32(n)=htonl((uint64_t)((4294.967296*t.tv_usec)+.5)); \ 188 else { \
176 } \ 189 L32(n) = htonl(t.tv_sec + EPOCHDIFF); \
177 } while(0) 190 R32(n) = htonl((uint64_t)((4294.967296 * t.tv_usec) + .5)); \
191 } \
192 } while (0)
178 193
179/* NTP control message header is 12 bytes, plus any data in the data 194/* NTP control message header is 12 bytes, plus any data in the data
180 * field, plus null padding to the nearest 32-bit boundary per rfc. 195 * field, plus null padding to the nearest 32-bit boundary per rfc.
181 */ 196 */
182#define SIZEOF_NTPCM(m) (12+ntohs(m.count)+((m.count)?4-(ntohs(m.count)%4):0)) 197#define SIZEOF_NTPCM(m) (12 + ntohs(m.count) + ((m.count) ? 4 - (ntohs(m.count) % 4) : 0))
183 198
184/* finally, a little helper or two for debugging: */ 199/* finally, a little helper or two for debugging: */
185#define DBG(x) do{if(verbose>1){ x; }}while(0); 200#define DBG(x) \
186#define PRINTSOCKADDR(x) \ 201 do { \
187 do{ \ 202 if (verbose > 1) { \
188 printf("%u.%u.%u.%u", (x>>24)&0xff, (x>>16)&0xff, (x>>8)&0xff, x&0xff);\ 203 x; \
189 }while(0); 204 } \
205 } while (0);
206#define PRINTSOCKADDR(x) \
207 do { \
208 printf("%u.%u.%u.%u", (x >> 24) & 0xff, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff); \
209 } while (0);
190 210
191/* calculate the offset of the local clock */ 211/* calculate the offset of the local clock */
192static inline double calc_offset(const ntp_message *m, const struct timeval *t){ 212static inline double calc_offset(const ntp_message *message, const struct timeval *time_value) {
193 double client_tx, peer_rx, peer_tx, client_rx; 213 double client_tx = NTP64asDOUBLE(message->origts);
194 client_tx = NTP64asDOUBLE(m->origts); 214 double peer_rx = NTP64asDOUBLE(message->rxts);
195 peer_rx = NTP64asDOUBLE(m->rxts); 215 double peer_tx = NTP64asDOUBLE(message->txts);
196 peer_tx = NTP64asDOUBLE(m->txts); 216 double client_rx = TVasDOUBLE((*time_value));
197 client_rx=TVasDOUBLE((*t)); 217 return (((peer_tx - client_rx) + (peer_rx - client_tx)) / 2);
198 return (.5*((peer_tx-client_rx)+(peer_rx-client_tx)));
199} 218}
200 219
201/* print out a ntp packet in human readable/debuggable format */ 220/* print out a ntp packet in human readable/debuggable format */
202void print_ntp_message(const ntp_message *p){ 221void print_ntp_message(const ntp_message *message) {
203 struct timeval ref, orig, rx, tx; 222 struct timeval ref;
223 struct timeval orig;
204 224
205 NTP64toTV(p->refts,ref); 225 NTP64toTV(message->refts, ref);
206 NTP64toTV(p->origts,orig); 226 NTP64toTV(message->origts, orig);
207 NTP64toTV(p->rxts,rx);
208 NTP64toTV(p->txts,tx);
209 227
210 printf("packet contents:\n"); 228 printf("packet contents:\n");
211 printf("\tflags: 0x%.2x\n", p->flags); 229 printf("\tflags: 0x%.2x\n", message->flags);
212 printf("\t li=%d (0x%.2x)\n", LI(p->flags), p->flags&LI_MASK); 230 printf("\t li=%d (0x%.2x)\n", LI(message->flags), message->flags & LI_MASK);
213 printf("\t vn=%d (0x%.2x)\n", VN(p->flags), p->flags&VN_MASK); 231 printf("\t vn=%d (0x%.2x)\n", VN(message->flags), message->flags & VN_MASK);
214 printf("\t mode=%d (0x%.2x)\n", MODE(p->flags), p->flags&MODE_MASK); 232 printf("\t mode=%d (0x%.2x)\n", MODE(message->flags), message->flags & MODE_MASK);
215 printf("\tstratum = %d\n", p->stratum); 233 printf("\tstratum = %d\n", message->stratum);
216 printf("\tpoll = %g\n", pow(2, p->poll)); 234 printf("\tpoll = %g\n", pow(2, message->poll));
217 printf("\tprecision = %g\n", pow(2, p->precision)); 235 printf("\tprecision = %g\n", pow(2, message->precision));
218 printf("\trtdelay = %-.16g\n", NTP32asDOUBLE(p->rtdelay)); 236 printf("\trtdelay = %-.16g\n", NTP32asDOUBLE(message->rtdelay));
219 printf("\trtdisp = %-.16g\n", NTP32asDOUBLE(p->rtdisp)); 237 printf("\trtdisp = %-.16g\n", NTP32asDOUBLE(message->rtdisp));
220 printf("\trefid = %x\n", p->refid); 238 printf("\trefid = %x\n", message->refid);
221 printf("\trefts = %-.16g\n", NTP64asDOUBLE(p->refts)); 239 printf("\trefts = %-.16g\n", NTP64asDOUBLE(message->refts));
222 printf("\torigts = %-.16g\n", NTP64asDOUBLE(p->origts)); 240 printf("\torigts = %-.16g\n", NTP64asDOUBLE(message->origts));
223 printf("\trxts = %-.16g\n", NTP64asDOUBLE(p->rxts)); 241 printf("\trxts = %-.16g\n", NTP64asDOUBLE(message->rxts));
224 printf("\ttxts = %-.16g\n", NTP64asDOUBLE(p->txts)); 242 printf("\ttxts = %-.16g\n", NTP64asDOUBLE(message->txts));
225} 243}
226 244
227void setup_request(ntp_message *p){ 245void setup_request(ntp_message *message) {
228 struct timeval t; 246 memset(message, 0, sizeof(ntp_message));
229 247 LI_SET(message->flags, LI_ALARM);
230 memset(p, 0, sizeof(ntp_message)); 248 VN_SET(message->flags, 4);
231 LI_SET(p->flags, LI_ALARM); 249 MODE_SET(message->flags, MODE_CLIENT);
232 VN_SET(p->flags, 4); 250 message->poll = 4;
233 MODE_SET(p->flags, MODE_CLIENT); 251 message->precision = (int8_t)0xfa;
234 p->poll=4; 252 L16(message->rtdelay) = htons(1);
235 p->precision=(int8_t)0xfa; 253 L16(message->rtdisp) = htons(1);
236 L16(p->rtdelay)=htons(1);
237 L16(p->rtdisp)=htons(1);
238 254
255 struct timeval t;
239 gettimeofday(&t, NULL); 256 gettimeofday(&t, NULL);
240 TVtoNTP64(t,p->txts); 257 TVtoNTP64(t, message->txts);
241} 258}
242 259
243/* select the "best" server from a list of servers, and return its index. 260/* select the "best" server from a list of servers, and return its index.
244 * this is done by filtering servers based on stratum, dispersion, and 261 * this is done by filtering servers based on stratum, dispersion, and
245 * finally round-trip delay. */ 262 * finally round-trip delay. */
246int best_offset_server(const ntp_server_results *slist, int nservers){ 263int best_offset_server(const ntp_server_results *slist, int nservers) {
247 int cserver=0, best_server=-1; 264 int best_server = -1;
248 265
249 /* for each server */ 266 /* for each server */
250 for(cserver=0; cserver<nservers; cserver++){ 267 for (int cserver = 0; cserver < nservers; cserver++) {
251 /* We don't want any servers that fails these tests */ 268 /* We don't want any servers that fails these tests */
252 /* Sort out servers that didn't respond or responede with a 0 stratum; 269 /* Sort out servers that didn't respond or responede with a 0 stratum;
253 * stratum 0 is for reference clocks so no NTP server should ever report 270 * stratum 0 is for reference clocks so no NTP server should ever report
254 * a stratum 0 */ 271 * a stratum 0 */
255 if ( slist[cserver].stratum == 0){ 272 if (slist[cserver].stratum == 0) {
256 if (verbose) printf("discarding peer %d: stratum=%d\n", cserver, slist[cserver].stratum); 273 if (verbose) {
274 printf("discarding peer %d: stratum=%d\n", cserver, slist[cserver].stratum);
275 }
257 continue; 276 continue;
258 } 277 }
259 /* Sort out servers with error flags */ 278 /* Sort out servers with error flags */
260 if ( LI(slist[cserver].flags) == LI_ALARM ){ 279 if (LI(slist[cserver].flags) == LI_ALARM) {
261 if (verbose) printf("discarding peer %d: flags=%d\n", cserver, LI(slist[cserver].flags)); 280 if (verbose) {
281 printf("discarding peer %d: flags=%d\n", cserver, LI(slist[cserver].flags));
282 }
262 continue; 283 continue;
263 } 284 }
264 285
@@ -272,13 +293,13 @@ int best_offset_server(const ntp_server_results *slist, int nservers){
272 /* compare the server to the best one we've seen so far */ 293 /* compare the server to the best one we've seen so far */
273 /* does it have an equal or better stratum? */ 294 /* does it have an equal or better stratum? */
274 DBG(printf("comparing peer %d with peer %d\n", cserver, best_server)); 295 DBG(printf("comparing peer %d with peer %d\n", cserver, best_server));
275 if(slist[cserver].stratum <= slist[best_server].stratum){ 296 if (slist[cserver].stratum <= slist[best_server].stratum) {
276 DBG(printf("stratum for peer %d <= peer %d\n", cserver, best_server)); 297 DBG(printf("stratum for peer %d <= peer %d\n", cserver, best_server));
277 /* does it have an equal or better dispersion? */ 298 /* does it have an equal or better dispersion? */
278 if(slist[cserver].rtdisp <= slist[best_server].rtdisp){ 299 if (slist[cserver].rtdisp <= slist[best_server].rtdisp) {
279 DBG(printf("dispersion for peer %d <= peer %d\n", cserver, best_server)); 300 DBG(printf("dispersion for peer %d <= peer %d\n", cserver, best_server));
280 /* does it have a better rtdelay? */ 301 /* does it have a better rtdelay? */
281 if(slist[cserver].rtdelay < slist[best_server].rtdelay){ 302 if (slist[cserver].rtdelay < slist[best_server].rtdelay) {
282 DBG(printf("rtdelay for peer %d < peer %d\n", cserver, best_server)); 303 DBG(printf("rtdelay for peer %d < peer %d\n", cserver, best_server));
283 best_server = cserver; 304 best_server = cserver;
284 DBG(printf("peer %d is now our best candidate\n", best_server)); 305 DBG(printf("peer %d is now our best candidate\n", best_server));
@@ -287,13 +308,12 @@ int best_offset_server(const ntp_server_results *slist, int nservers){
287 } 308 }
288 } 309 }
289 310
290 if(best_server >= 0) { 311 if (best_server >= 0) {
291 DBG(printf("best server selected: peer %d\n", best_server)); 312 DBG(printf("best server selected: peer %d\n", best_server));
292 return best_server; 313 return best_server;
293 } else {
294 DBG(printf("no peers meeting synchronization criteria :(\n"));
295 return -1;
296 } 314 }
315 DBG(printf("no peers meeting synchronization criteria :(\n"));
316 return -1;
297} 317}
298 318
299/* do everything we need to get the total average offset 319/* do everything we need to get the total average offset
@@ -301,178 +321,208 @@ int best_offset_server(const ntp_server_results *slist, int nservers){
301 * we don't waste time sitting around waiting for single packets. 321 * we don't waste time sitting around waiting for single packets.
302 * - we also "manually" handle resolving host names and connecting, because 322 * - we also "manually" handle resolving host names and connecting, because
303 * we have to do it in a way that our lazy macros don't handle currently :( */ 323 * we have to do it in a way that our lazy macros don't handle currently :( */
304double offset_request(const char *host, int *status){ 324double offset_request(const char *host, const char *port, mp_state_enum *status, int time_offset) {
305 int i=0, j=0, ga_result=0, num_hosts=0, *socklist=NULL, respnum=0;
306 int servers_completed=0, one_read=0, servers_readable=0, best_index=-1;
307 time_t now_time=0, start_ts=0;
308 ntp_message *req=NULL;
309 double avg_offset=0.;
310 struct timeval recv_time;
311 struct addrinfo *ai=NULL, *ai_tmp=NULL, hints;
312 struct pollfd *ufds=NULL;
313 ntp_server_results *servers=NULL;
314
315 /* setup hints to only return results from getaddrinfo that we'd like */ 325 /* setup hints to only return results from getaddrinfo that we'd like */
326 struct addrinfo hints;
316 memset(&hints, 0, sizeof(struct addrinfo)); 327 memset(&hints, 0, sizeof(struct addrinfo));
317 hints.ai_family = address_family; 328 hints.ai_family = address_family;
318 hints.ai_protocol = IPPROTO_UDP; 329 hints.ai_protocol = IPPROTO_UDP;
319 hints.ai_socktype = SOCK_DGRAM; 330 hints.ai_socktype = SOCK_DGRAM;
320 331
321 /* fill in ai with the list of hosts resolved by the host name */ 332 /* fill in ai with the list of hosts resolved by the host name */
322 ga_result = getaddrinfo(host, port, &hints, &ai); 333 struct addrinfo *addresses = NULL;
323 if(ga_result!=0){ 334 int ga_result = getaddrinfo(host, port, &hints, &addresses);
324 die(STATE_UNKNOWN, "error getting address for %s: %s\n", 335 if (ga_result != 0) {
325 host, gai_strerror(ga_result)); 336 die(STATE_UNKNOWN, "error getting address for %s: %s\n", host, gai_strerror(ga_result));
326 } 337 }
327 338
328 /* count the number of returned hosts, and allocate stuff accordingly */ 339 /* count the number of returned hosts, and allocate stuff accordingly */
329 for(ai_tmp=ai; ai_tmp!=NULL; ai_tmp=ai_tmp->ai_next){ num_hosts++; } 340 size_t num_hosts = 0;
330 req=(ntp_message*)malloc(sizeof(ntp_message)*num_hosts); 341 for (struct addrinfo *ai_tmp = addresses; ai_tmp != NULL; ai_tmp = ai_tmp->ai_next) {
331 if(req==NULL) die(STATE_UNKNOWN, "can not allocate ntp message array"); 342 num_hosts++;
332 socklist=(int*)malloc(sizeof(int)*num_hosts); 343 }
333 if(socklist==NULL) die(STATE_UNKNOWN, "can not allocate socket array"); 344
334 ufds=(struct pollfd*)malloc(sizeof(struct pollfd)*num_hosts); 345 ntp_message *req = (ntp_message *)malloc(sizeof(ntp_message) * num_hosts);
335 if(ufds==NULL) die(STATE_UNKNOWN, "can not allocate socket array"); 346
336 servers=(ntp_server_results*)malloc(sizeof(ntp_server_results)*num_hosts); 347 if (req == NULL) {
337 if(servers==NULL) die(STATE_UNKNOWN, "can not allocate server array"); 348 die(STATE_UNKNOWN, "can not allocate ntp message array");
338 memset(servers, 0, sizeof(ntp_server_results)*num_hosts); 349 }
339 DBG(printf("Found %d peers to check\n", num_hosts)); 350 int *socklist = (int *)malloc(sizeof(int) * num_hosts);
351
352 if (socklist == NULL) {
353 die(STATE_UNKNOWN, "can not allocate socket array");
354 }
355
356 struct pollfd *ufds = (struct pollfd *)malloc(sizeof(struct pollfd) * num_hosts);
357 if (ufds == NULL) {
358 die(STATE_UNKNOWN, "can not allocate socket array");
359 }
360
361 ntp_server_results *servers = (ntp_server_results *)malloc(sizeof(ntp_server_results) * num_hosts);
362 if (servers == NULL) {
363 die(STATE_UNKNOWN, "can not allocate server array");
364 }
365 memset(servers, 0, sizeof(ntp_server_results) * num_hosts);
366 DBG(printf("Found %zu peers to check\n", num_hosts));
340 367
341 /* setup each socket for writing, and the corresponding struct pollfd */ 368 /* setup each socket for writing, and the corresponding struct pollfd */
342 ai_tmp=ai; 369 struct addrinfo *ai_tmp = addresses;
343 for(i=0;ai_tmp;i++){ 370 for (int i = 0; ai_tmp; i++) {
344 socklist[i]=socket(ai_tmp->ai_family, SOCK_DGRAM, IPPROTO_UDP); 371 socklist[i] = socket(ai_tmp->ai_family, SOCK_DGRAM, IPPROTO_UDP);
345 if(socklist[i] == -1) { 372 if (socklist[i] == -1) {
346 perror(NULL); 373 perror(NULL);
347 die(STATE_UNKNOWN, "can not create new socket"); 374 die(STATE_UNKNOWN, "can not create new socket");
348 } 375 }
349 if(connect(socklist[i], ai_tmp->ai_addr, ai_tmp->ai_addrlen)){ 376 if (connect(socklist[i], ai_tmp->ai_addr, ai_tmp->ai_addrlen)) {
350 /* don't die here, because it is enough if there is one server 377 /* don't die here, because it is enough if there is one server
351 answering in time. This also would break for dual ipv4/6 stacked 378 answering in time. This also would break for dual ipv4/6 stacked
352 ntp servers when the client only supports on of them. 379 ntp servers when the client only supports on of them.
353 */ 380 */
354 DBG(printf("can't create socket connection on peer %i: %s\n", i, strerror(errno))); 381 DBG(printf("can't create socket connection on peer %i: %s\n", i, strerror(errno)));
355 } else { 382 } else {
356 ufds[i].fd=socklist[i]; 383 ufds[i].fd = socklist[i];
357 ufds[i].events=POLLIN; 384 ufds[i].events = POLLIN;
358 ufds[i].revents=0; 385 ufds[i].revents = 0;
359 } 386 }
360 ai_tmp = ai_tmp->ai_next; 387 ai_tmp = ai_tmp->ai_next;
361 } 388 }
362 389
363 /* now do AVG_NUM checks to each host. We stop before timeout/2 seconds 390 /* now do AVG_NUM checks to each host. We stop before timeout/2 seconds
364 * have passed in order to ensure post-processing and jitter time. */ 391 * have passed in order to ensure post-processing and jitter time. */
365 now_time=start_ts=time(NULL); 392 time_t start_ts = 0;
366 while(servers_completed<num_hosts && now_time-start_ts <= socket_timeout/2){ 393 time_t now_time = 0;
394 now_time = start_ts = time(NULL);
395 size_t servers_completed = 0;
396 bool one_read = false;
397 while (servers_completed < num_hosts && now_time - start_ts <= socket_timeout / 2) {
367 /* loop through each server and find each one which hasn't 398 /* loop through each server and find each one which hasn't
368 * been touched in the past second or so and is still lacking 399 * been touched in the past second or so and is still lacking
369 * some responses. For each of these servers, send a new request, 400 * some responses. For each of these servers, send a new request,
370 * and update the "waiting" timestamp with the current time. */ 401 * and update the "waiting" timestamp with the current time. */
371 now_time=time(NULL); 402 now_time = time(NULL);
372 403
373 for(i=0; i<num_hosts; i++){ 404 for (size_t i = 0; i < num_hosts; i++) {
374 if(servers[i].waiting<now_time && servers[i].num_responses<AVG_NUM){ 405 if (servers[i].waiting < now_time && servers[i].num_responses < AVG_NUM) {
375 if(verbose && servers[i].waiting != 0) printf("re-"); 406 if (verbose && servers[i].waiting != 0) {
376 if(verbose) printf("sending request to peer %d\n", i); 407 printf("re-");
408 }
409 if (verbose) {
410 printf("sending request to peer %zu\n", i);
411 }
377 setup_request(&req[i]); 412 setup_request(&req[i]);
378 write(socklist[i], &req[i], sizeof(ntp_message)); 413 write(socklist[i], &req[i], sizeof(ntp_message));
379 servers[i].waiting=now_time; 414 servers[i].waiting = now_time;
380 break; 415 break;
381 } 416 }
382 } 417 }
383 418
384 /* quickly poll for any sockets with pending data */ 419 /* quickly poll for any sockets with pending data */
385 servers_readable=poll(ufds, num_hosts, 100); 420 int servers_readable = poll(ufds, num_hosts, 100);
386 if(servers_readable==-1){ 421 if (servers_readable == -1) {
387 perror("polling ntp sockets"); 422 perror("polling ntp sockets");
388 die(STATE_UNKNOWN, "communication errors"); 423 die(STATE_UNKNOWN, "communication errors");
389 } 424 }
390 425
391 /* read from any sockets with pending data */ 426 /* read from any sockets with pending data */
392 for(i=0; servers_readable && i<num_hosts; i++){ 427 for (size_t i = 0; servers_readable && i < num_hosts; i++) {
393 if(ufds[i].revents&POLLIN && servers[i].num_responses < AVG_NUM){ 428 if (ufds[i].revents & POLLIN && servers[i].num_responses < AVG_NUM) {
394 if(verbose) { 429 if (verbose) {
395 printf("response from peer %d: ", i); 430 printf("response from peer %zu: ", i);
396 } 431 }
397 432
398 read(ufds[i].fd, &req[i], sizeof(ntp_message)); 433 read(ufds[i].fd, &req[i], sizeof(ntp_message));
434
435 struct timeval recv_time;
399 gettimeofday(&recv_time, NULL); 436 gettimeofday(&recv_time, NULL);
400 DBG(print_ntp_message(&req[i])); 437 DBG(print_ntp_message(&req[i]));
401 respnum=servers[i].num_responses++; 438 int respnum = servers[i].num_responses++;
402 servers[i].offset[respnum]=calc_offset(&req[i], &recv_time)+time_offset; 439 servers[i].offset[respnum] = calc_offset(&req[i], &recv_time) + time_offset;
403 if(verbose) { 440 if (verbose) {
404 printf("offset %.10g\n", servers[i].offset[respnum]); 441 printf("offset %.10g\n", servers[i].offset[respnum]);
405 } 442 }
406 servers[i].stratum=req[i].stratum; 443 servers[i].stratum = req[i].stratum;
407 servers[i].rtdisp=NTP32asDOUBLE(req[i].rtdisp); 444 servers[i].rtdisp = NTP32asDOUBLE(req[i].rtdisp);
408 servers[i].rtdelay=NTP32asDOUBLE(req[i].rtdelay); 445 servers[i].rtdelay = NTP32asDOUBLE(req[i].rtdelay);
409 servers[i].waiting=0; 446 servers[i].waiting = 0;
410 servers[i].flags=req[i].flags; 447 servers[i].flags = req[i].flags;
411 servers_readable--; 448 servers_readable--;
412 one_read = 1; 449 one_read = true;
413 if(servers[i].num_responses==AVG_NUM) servers_completed++; 450 if (servers[i].num_responses == AVG_NUM) {
451 servers_completed++;
452 }
414 } 453 }
415 } 454 }
416 /* lather, rinse, repeat. */ 455 /* lather, rinse, repeat. */
417 } 456 }
418 457
419 if (one_read == 0) { 458 if (!one_read) {
420 die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n"); 459 die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n");
421 } 460 }
422 461
423 /* now, pick the best server from the list */ 462 /* now, pick the best server from the list */
424 best_index=best_offset_server(servers, num_hosts); 463 double avg_offset = 0.;
425 if(best_index < 0){ 464 int best_index = best_offset_server(servers, num_hosts);
426 *status=STATE_UNKNOWN; 465 if (best_index < 0) {
466 *status = STATE_UNKNOWN;
427 } else { 467 } else {
428 /* finally, calculate the average offset */ 468 /* finally, calculate the average offset */
429 for(i=0; i<servers[best_index].num_responses;i++){ 469 for (int i = 0; i < servers[best_index].num_responses; i++) {
430 avg_offset+=servers[best_index].offset[i]; 470 avg_offset += servers[best_index].offset[i];
431 } 471 }
432 avg_offset/=servers[best_index].num_responses; 472 avg_offset /= servers[best_index].num_responses;
433 } 473 }
434 474
435 /* cleanup */ 475 /* cleanup */
436 for(j=0; j<num_hosts; j++){ close(socklist[j]); } 476 for (size_t j = 0; j < num_hosts; j++) {
477 close(socklist[j]);
478 }
437 free(socklist); 479 free(socklist);
438 free(ufds); 480 free(ufds);
439 free(servers); 481 free(servers);
440 free(req); 482 free(req);
441 freeaddrinfo(ai); 483 freeaddrinfo(addresses);
442 484
443 if(verbose) printf("overall average offset: %.10g\n", avg_offset); 485 if (verbose) {
486 printf("overall average offset: %.10g\n", avg_offset);
487 }
444 return avg_offset; 488 return avg_offset;
445} 489}
446 490
447int process_arguments(int argc, char **argv){ 491check_ntp_time_config_wrapper process_arguments(int argc, char **argv) {
448 int c; 492 static struct option longopts[] = {{"version", no_argument, 0, 'V'},
449 int option=0; 493 {"help", no_argument, 0, 'h'},
450 static struct option longopts[] = { 494 {"verbose", no_argument, 0, 'v'},
451 {"version", no_argument, 0, 'V'}, 495 {"use-ipv4", no_argument, 0, '4'},
452 {"help", no_argument, 0, 'h'}, 496 {"use-ipv6", no_argument, 0, '6'},
453 {"verbose", no_argument, 0, 'v'}, 497 {"quiet", no_argument, 0, 'q'},
454 {"use-ipv4", no_argument, 0, '4'}, 498 {"time-offset", optional_argument, 0, 'o'},
455 {"use-ipv6", no_argument, 0, '6'}, 499 {"warning", required_argument, 0, 'w'},
456 {"quiet", no_argument, 0, 'q'}, 500 {"critical", required_argument, 0, 'c'},
457 {"time-offset", optional_argument, 0, 'o'}, 501 {"timeout", required_argument, 0, 't'},
458 {"warning", required_argument, 0, 'w'}, 502 {"hostname", required_argument, 0, 'H'},
459 {"critical", required_argument, 0, 'c'}, 503 {"port", required_argument, 0, 'p'},
460 {"timeout", required_argument, 0, 't'}, 504 {0, 0, 0, 0}};
461 {"hostname", required_argument, 0, 'H'}, 505
462 {"port", required_argument, 0, 'p'}, 506 if (argc < 2) {
463 {0, 0, 0, 0} 507 usage("\n");
464 }; 508 }
465 509
510 check_ntp_time_config_wrapper result = {
511 .errorcode = OK,
512 .config = check_ntp_time_config_init(),
513 };
466 514
467 if (argc < 2) 515 char *owarn = "60";
468 usage ("\n"); 516 char *ocrit = "120";
469 517
470 while (1) { 518 while (true) {
471 c = getopt_long (argc, argv, "Vhv46qw:c:t:H:p:o:", longopts, &option); 519 int option = 0;
472 if (c == -1 || c == EOF || c == 1) 520 int option_char = getopt_long(argc, argv, "Vhv46qw:c:t:H:p:o:", longopts, &option);
521 if (option_char == -1 || option_char == EOF || option_char == 1) {
473 break; 522 break;
523 }
474 524
475 switch (c) { 525 switch (option_char) {
476 case 'h': 526 case 'h':
477 print_help(); 527 print_help();
478 exit(STATE_UNKNOWN); 528 exit(STATE_UNKNOWN);
@@ -485,7 +535,7 @@ int process_arguments(int argc, char **argv){
485 verbose++; 535 verbose++;
486 break; 536 break;
487 case 'q': 537 case 'q':
488 quiet = true; 538 result.config.quiet = true;
489 break; 539 break;
490 case 'w': 540 case 'w':
491 owarn = optarg; 541 owarn = optarg;
@@ -494,19 +544,20 @@ int process_arguments(int argc, char **argv){
494 ocrit = optarg; 544 ocrit = optarg;
495 break; 545 break;
496 case 'H': 546 case 'H':
497 if(!is_host(optarg)) 547 if (!is_host(optarg)) {
498 usage2(_("Invalid hostname/address"), optarg); 548 usage2(_("Invalid hostname/address"), optarg);
499 server_address = strdup(optarg); 549 }
550 result.config.server_address = strdup(optarg);
500 break; 551 break;
501 case 'p': 552 case 'p':
502 port = strdup(optarg); 553 result.config.port = strdup(optarg);
503 break; 554 break;
504 case 't': 555 case 't':
505 socket_timeout=atoi(optarg); 556 socket_timeout = atoi(optarg);
506 break; 557 break;
507 case 'o': 558 case 'o':
508 time_offset=atoi(optarg); 559 result.config.time_offset = atoi(optarg);
509 break; 560 break;
510 case '4': 561 case '4':
511 address_family = AF_INET; 562 address_family = AF_INET;
512 break; 563 break;
@@ -514,114 +565,118 @@ int process_arguments(int argc, char **argv){
514#ifdef USE_IPV6 565#ifdef USE_IPV6
515 address_family = AF_INET6; 566 address_family = AF_INET6;
516#else 567#else
517 usage4 (_("IPv6 support not available")); 568 usage4(_("IPv6 support not available"));
518#endif 569#endif
519 break; 570 break;
520 case '?': 571 case '?':
521 /* print short usage statement if args not parsable */ 572 /* print short usage statement if args not parsable */
522 usage5 (); 573 usage5();
523 break; 574 break;
524 } 575 }
525 } 576 }
526 577
527 if(server_address == NULL){ 578 if (result.config.server_address == NULL) {
528 usage4(_("Hostname was not supplied")); 579 usage4(_("Hostname was not supplied"));
529 } 580 }
530 581
531 return 0; 582 set_thresholds(&result.config.offset_thresholds, owarn, ocrit);
532}
533 583
534char *perfd_offset (double offset) { 584 return result;
535 return fperfdata ("offset", offset, "s",
536 true, offset_thresholds->warning->end,
537 true, offset_thresholds->critical->end,
538 false, 0, false, 0);
539} 585}
540 586
541int main(int argc, char *argv[]){ 587char *perfd_offset(double offset, thresholds *offset_thresholds) {
542 int result, offset_result; 588 return fperfdata("offset", offset, "s", true, offset_thresholds->warning->end, true, offset_thresholds->critical->end, false, 0, false,
543 double offset=0; 589 0);
544 char *result_line, *perfdata_line; 590}
545
546 setlocale (LC_ALL, "");
547 bindtextdomain (PACKAGE, LOCALEDIR);
548 textdomain (PACKAGE);
549 591
550 result = offset_result = STATE_OK; 592int main(int argc, char *argv[]) {
593 setlocale(LC_ALL, "");
594 bindtextdomain(PACKAGE, LOCALEDIR);
595 textdomain(PACKAGE);
551 596
552 /* Parse extra opts if any */ 597 /* Parse extra opts if any */
553 argv=np_extra_opts (&argc, argv, progname); 598 argv = np_extra_opts(&argc, argv, progname);
554 599
555 if (process_arguments (argc, argv) == ERROR) 600 check_ntp_time_config_wrapper tmp_config = process_arguments(argc, argv);
556 usage4 (_("Could not parse arguments"));
557 601
558 set_thresholds(&offset_thresholds, owarn, ocrit); 602 if (tmp_config.errorcode == ERROR) {
603 usage4(_("Could not parse arguments"));
604 }
605
606 const check_ntp_time_config config = tmp_config.config;
559 607
560 /* initialize alarm signal handling */ 608 /* initialize alarm signal handling */
561 signal (SIGALRM, socket_timeout_alarm_handler); 609 signal(SIGALRM, socket_timeout_alarm_handler);
562 610
563 /* set socket timeout */ 611 /* set socket timeout */
564 alarm (socket_timeout); 612 alarm(socket_timeout);
565 613
566 offset = offset_request(server_address, &offset_result); 614 mp_state_enum offset_result = STATE_OK;
615 mp_state_enum result = STATE_OK;
616 double offset = offset_request(config.server_address, config.port, &offset_result, config.time_offset);
567 if (offset_result == STATE_UNKNOWN) { 617 if (offset_result == STATE_UNKNOWN) {
568 result = ( (!quiet) ? STATE_UNKNOWN : STATE_CRITICAL); 618 result = ((!config.quiet) ? STATE_UNKNOWN : STATE_CRITICAL);
569 } else { 619 } else {
570 result = get_status(fabs(offset), offset_thresholds); 620 result = get_status(fabs(offset), config.offset_thresholds);
571 } 621 }
572 622
623 char *result_line;
573 switch (result) { 624 switch (result) {
574 case STATE_CRITICAL : 625 case STATE_CRITICAL:
575 xasprintf(&result_line, _("NTP CRITICAL:")); 626 xasprintf(&result_line, _("NTP CRITICAL:"));
576 break; 627 break;
577 case STATE_WARNING : 628 case STATE_WARNING:
578 xasprintf(&result_line, _("NTP WARNING:")); 629 xasprintf(&result_line, _("NTP WARNING:"));
579 break; 630 break;
580 case STATE_OK : 631 case STATE_OK:
581 xasprintf(&result_line, _("NTP OK:")); 632 xasprintf(&result_line, _("NTP OK:"));
582 break; 633 break;
583 default : 634 default:
584 xasprintf(&result_line, _("NTP UNKNOWN:")); 635 xasprintf(&result_line, _("NTP UNKNOWN:"));
585 break; 636 break;
586 } 637 }
587 if(offset_result == STATE_UNKNOWN){ 638
639 char *perfdata_line;
640 if (offset_result == STATE_UNKNOWN) {
588 xasprintf(&result_line, "%s %s", result_line, _("Offset unknown")); 641 xasprintf(&result_line, "%s %s", result_line, _("Offset unknown"));
589 xasprintf(&perfdata_line, ""); 642 xasprintf(&perfdata_line, "");
590 } else { 643 } else {
591 xasprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset); 644 xasprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset);
592 xasprintf(&perfdata_line, "%s", perfd_offset(offset)); 645 xasprintf(&perfdata_line, "%s", perfd_offset(offset, config.offset_thresholds));
593 } 646 }
594 printf("%s|%s\n", result_line, perfdata_line); 647 printf("%s|%s\n", result_line, perfdata_line);
595 648
596 if(server_address!=NULL) free(server_address); 649 if (config.server_address != NULL) {
597 return result; 650 free(config.server_address);
651 }
652 exit(result);
598} 653}
599 654
600void print_help(void){ 655void print_help(void) {
601 print_revision(progname, NP_VERSION); 656 print_revision(progname, NP_VERSION);
602 657
603 printf ("Copyright (c) 2006 Sean Finney\n"); 658 printf("Copyright (c) 2006 Sean Finney\n");
604 printf (COPYRIGHT, copyright, email); 659 printf(COPYRIGHT, copyright, email);
605 660
606 printf ("%s\n", _("This plugin checks the clock offset with the ntp server")); 661 printf("%s\n", _("This plugin checks the clock offset with the ntp server"));
607 662
608 printf ("\n\n"); 663 printf("\n\n");
609 664
610 print_usage(); 665 print_usage();
611 printf (UT_HELP_VRSN); 666 printf(UT_HELP_VRSN);
612 printf (UT_EXTRA_OPTS); 667 printf(UT_EXTRA_OPTS);
613 printf (UT_IPv46); 668 printf(UT_IPv46);
614 printf (UT_HOST_PORT, 'p', "123"); 669 printf(UT_HOST_PORT, 'p', "123");
615 printf (" %s\n", "-q, --quiet"); 670 printf(" %s\n", "-q, --quiet");
616 printf (" %s\n", _("Returns UNKNOWN instead of CRITICAL if offset cannot be found")); 671 printf(" %s\n", _("Returns UNKNOWN instead of CRITICAL if offset cannot be found"));
617 printf (" %s\n", "-w, --warning=THRESHOLD"); 672 printf(" %s\n", "-w, --warning=THRESHOLD");
618 printf (" %s\n", _("Offset to result in warning status (seconds)")); 673 printf(" %s\n", _("Offset to result in warning status (seconds)"));
619 printf (" %s\n", "-c, --critical=THRESHOLD"); 674 printf(" %s\n", "-c, --critical=THRESHOLD");
620 printf (" %s\n", _("Offset to result in critical status (seconds)")); 675 printf(" %s\n", _("Offset to result in critical status (seconds)"));
621 printf (" %s\n", "-o, --time_offset=INTEGER"); 676 printf(" %s\n", "-o, --time_offset=INTEGER");
622 printf (" %s\n", _("Expected offset of the ntp server relative to local server (seconds)")); 677 printf(" %s\n", _("Expected offset of the ntp server relative to local server (seconds)"));
623 printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); 678 printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
624 printf (UT_VERBOSE); 679 printf(UT_VERBOSE);
625 680
626 printf("\n"); 681 printf("\n");
627 printf("%s\n", _("This plugin checks the clock offset between the local host and a")); 682 printf("%s\n", _("This plugin checks the clock offset between the local host and a"));
@@ -641,13 +696,10 @@ void print_help(void){
641 printf("%s\n", _("Examples:")); 696 printf("%s\n", _("Examples:"));
642 printf(" %s\n", ("./check_ntp_time -H ntpserv -w 0.5 -c 1")); 697 printf(" %s\n", ("./check_ntp_time -H ntpserv -w 0.5 -c 1"));
643 698
644 printf (UT_SUPPORT); 699 printf(UT_SUPPORT);
645} 700}
646 701
647void 702void print_usage(void) {
648print_usage(void) 703 printf("%s\n", _("Usage:"));
649{
650 printf ("%s\n", _("Usage:"));
651 printf(" %s -H <host> [-4|-6] [-w <warn>] [-c <crit>] [-v verbose] [-o <time offset>]\n", progname); 704 printf(" %s -H <host> [-4|-6] [-w <warn>] [-c <crit>] [-v verbose] [-o <time offset>]\n", progname);
652} 705}
653