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