diff options
author | M. Sean Finney <seanius@users.sourceforge.net> | 2006-03-18 19:00:43 (GMT) |
---|---|---|
committer | M. Sean Finney <seanius@users.sourceforge.net> | 2006-03-18 19:00:43 (GMT) |
commit | d4ddb4893211c4524c9701208ff5144d62c2425b (patch) | |
tree | 8ecd6a9ef377c2d054a21076d330989cb7ed7947 /plugins/check_ntp.c | |
parent | cd8b45f633bfda6a414b4120672efa96123119f6 (diff) | |
download | monitoring-plugins-d4ddb4893211c4524c9701208ff5144d62c2425b.tar.gz |
initial version of the pure-c check_ntp implementation. jitter not yet
implemented, and a couple other misc things to do, so i haven't
yet patched Makefile.am
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1330 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'plugins/check_ntp.c')
-rw-r--r-- | plugins/check_ntp.c | 355 |
1 files changed, 355 insertions, 0 deletions
diff --git a/plugins/check_ntp.c b/plugins/check_ntp.c new file mode 100644 index 0000000..c2fa98d --- /dev/null +++ b/plugins/check_ntp.c | |||
@@ -0,0 +1,355 @@ | |||
1 | /****************************************************************************** | ||
2 | check_ntp.c: utility to check ntp servers independant of any commandline | ||
3 | programs or external libraries. | ||
4 | original author: sean finney <seanius@seanius.net> | ||
5 | ****************************************************************************** | ||
6 | This program is free software; you can redistribute it and/or modify | ||
7 | it under the terms of the GNU General Public License as published by | ||
8 | the Free Software Foundation; either version 2 of the License, or | ||
9 | (at your option) any later version. | ||
10 | |||
11 | This program is distributed in the hope that it will be useful, | ||
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | GNU General Public License for more details. | ||
15 | |||
16 | You should have received a copy of the GNU General Public License | ||
17 | along with this program; if not, write to the Free Software | ||
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
19 | |||
20 | $Id$ | ||
21 | |||
22 | *****************************************************************************/ | ||
23 | |||
24 | const char *progname = "check_ntp"; | ||
25 | const char *revision = "$Revision$"; | ||
26 | const char *copyright = "2006"; | ||
27 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; | ||
28 | |||
29 | #include "common.h" | ||
30 | #include "netutils.h" | ||
31 | #include "utils.h" | ||
32 | |||
33 | static char *server_address=NULL; | ||
34 | static int verbose=0; | ||
35 | static int zero_offset_bad=0; | ||
36 | static double owarn=0; | ||
37 | static double ocrit=0; | ||
38 | static short do_jitter=0; | ||
39 | static double jwarn=0; | ||
40 | static double jcrit=0; | ||
41 | |||
42 | int process_arguments (int, char **); | ||
43 | void print_help (void); | ||
44 | void print_usage (void); | ||
45 | |||
46 | /* this structure holds everything in an ntp request/response as per rfc1305 */ | ||
47 | typedef struct { | ||
48 | uint8_t flags; /* byte with leapindicator,vers,mode. see macros */ | ||
49 | uint8_t stratum; /* clock stratum */ | ||
50 | int8_t poll; /* polling interval */ | ||
51 | int8_t precision; /* precision of the local clock */ | ||
52 | int32_t rtdelay; /* total rt delay, as a fixed point num. see macros */ | ||
53 | uint32_t rtdisp; /* like above, but for max err to primary src */ | ||
54 | uint32_t refid; /* ref clock identifier */ | ||
55 | uint64_t refts; /* reference timestamp. local time local clock */ | ||
56 | uint64_t origts; /* time at which request departed client */ | ||
57 | uint64_t rxts; /* time at which request arrived at server */ | ||
58 | uint64_t txts; /* time at which request departed server */ | ||
59 | } ntp_message; | ||
60 | |||
61 | /* bits 1,2 are the leap indicator */ | ||
62 | #define LI_MASK 0xc0 | ||
63 | #define LI(x) ((x&LI_MASK)>>6) | ||
64 | #define LI_SET(x,y) do{ x |= ((y<<6)&LI_MASK); }while(0) | ||
65 | /* and these are the values of the leap indicator */ | ||
66 | #define LI_NOWARNING 0x00 | ||
67 | #define LI_EXTRASEC 0x01 | ||
68 | #define LI_MISSINGSEC 0x02 | ||
69 | #define LI_ALARM 0x03 | ||
70 | /* bits 3,4,5 are the ntp version */ | ||
71 | #define VN_MASK 0x38 | ||
72 | #define VN(x) ((x&VN_MASK)>>3) | ||
73 | #define VN_SET(x,y) do{ x |= ((y<<3)&VN_MASK); }while(0) | ||
74 | /* bits 6,7,8 are the ntp mode */ | ||
75 | #define MODE_MASK 0x07 | ||
76 | #define MODE(x) (x&MODE_MASK) | ||
77 | #define MODE_SET(x,y) do{ x |= (y&MODE_MASK); }while(0) | ||
78 | /* here are some values */ | ||
79 | #define MODE_CLIENT 0x03 | ||
80 | |||
81 | /** | ||
82 | ** a note about the 32-bit "fixed point" numbers: | ||
83 | ** | ||
84 | they are divided into halves, each being a 16-bit int in network byte order: | ||
85 | - the first 16 bits are an int on the left side of a decimal point. | ||
86 | - the second 16 bits represent a fraction n/(2^16) | ||
87 | likewise for the 64-bit "fixed point" numbers with everything doubled :) | ||
88 | **/ | ||
89 | |||
90 | /* macros to access the left/right 16 bits of a 32-bit ntp "fixed point" | ||
91 | number. note that these can be used as lvalues too */ | ||
92 | #define L16(x) (((uint16_t*)&x)[0]) | ||
93 | #define R16(x) (((uint16_t*)&x)[1]) | ||
94 | /* macros to access the left/right 32 bits of a 64-bit ntp "fixed point" | ||
95 | number. these too can be used as lvalues */ | ||
96 | #define L32(x) (((uint32_t*)&x)[0]) | ||
97 | #define R32(x) (((uint32_t*)&x)[1]) | ||
98 | |||
99 | /* ntp wants seconds since 1/1/00, epoch is 1/1/70. this is the difference */ | ||
100 | #define EPOCHDIFF 0x83aa7e80UL | ||
101 | |||
102 | /* extract a 32-bit ntp fixed point number into a double */ | ||
103 | #define NTP32asDOUBLE(x) (ntohs(L16(x)) + (double)ntohs(R16(x))/65536.0) | ||
104 | |||
105 | /* likewise for a 64-bit ntp fp number */ | ||
106 | #define NTP64asDOUBLE(n) (double)(((uint64_t)n)?\ | ||
107 | (ntohl(L32(n))-EPOCHDIFF) + \ | ||
108 | (.00000001*(0.5+(double)(ntohl(R32(n))/42.94967296))):\ | ||
109 | 0) | ||
110 | |||
111 | /* convert a struct timeval to a double */ | ||
112 | #define TVasDOUBLE(x) (double)(x.tv_sec+(0.000001*x.tv_usec)) | ||
113 | |||
114 | /* convert an ntp 64-bit fp number to a struct timeval */ | ||
115 | #define NTP64toTV(n,t) \ | ||
116 | do{ if(!n) t.tv_sec = t.tv_usec = 0; \ | ||
117 | else { \ | ||
118 | t.tv_sec=ntohl(L32(n))-EPOCHDIFF; \ | ||
119 | t.tv_usec=(int)(0.5+(double)(ntohl(R32(n))/4294.967296)); \ | ||
120 | } \ | ||
121 | }while(0) | ||
122 | |||
123 | /* convert a struct timeval to an ntp 64-bit fp number */ | ||
124 | #define TVtoNTP64(t,n) \ | ||
125 | do{ if(!t.tv_usec && !t.tv_sec) n=0x0UL; \ | ||
126 | else { \ | ||
127 | L32(n)=htonl(t.tv_sec + EPOCHDIFF); \ | ||
128 | R32(n)=htonl((4294.967296*t.tv_usec)+.5); \ | ||
129 | } \ | ||
130 | } while(0) | ||
131 | |||
132 | |||
133 | /* calculate the offset of the local clock */ | ||
134 | static inline double calc_offset(const ntp_message *m, const struct timeval *t){ | ||
135 | double client_tx, peer_rx, peer_tx, client_rx, rtdelay; | ||
136 | client_tx = NTP64asDOUBLE(m->origts); | ||
137 | peer_rx = NTP64asDOUBLE(m->rxts); | ||
138 | peer_tx = NTP64asDOUBLE(m->txts); | ||
139 | client_rx=TVasDOUBLE((*t)); | ||
140 | rtdelay=NTP32asDOUBLE(m->rtdelay); | ||
141 | return (.5*((peer_tx-client_rx)+(peer_rx-client_tx)))-rtdelay; | ||
142 | } | ||
143 | |||
144 | /* print out a ntp packet in human readable/debuggable format */ | ||
145 | void print_packet(const ntp_message *p){ | ||
146 | struct timeval ref, orig, rx, tx; | ||
147 | |||
148 | NTP64toTV(p->refts,ref); | ||
149 | NTP64toTV(p->origts,orig); | ||
150 | NTP64toTV(p->rxts,rx); | ||
151 | NTP64toTV(p->txts,tx); | ||
152 | |||
153 | printf("packet contents:\n"); | ||
154 | printf("\tflags: 0x%.2x\n", p->flags); | ||
155 | printf("\t li=%d (0x%.2x)\n", LI(p->flags), p->flags&LI_MASK); | ||
156 | printf("\t vn=%d (0x%.2x)\n", VN(p->flags), p->flags&VN_MASK); | ||
157 | printf("\t mode=%d (0x%.2x)\n", MODE(p->flags), p->flags&MODE_MASK); | ||
158 | printf("\tstratum = %d\n", p->stratum); | ||
159 | printf("\tpoll = %g\n", pow(2, p->poll)); | ||
160 | printf("\tprecision = %g\n", pow(2, p->precision)); | ||
161 | printf("\trtdelay = %-.16g\n", NTP32asDOUBLE(p->rtdelay)); | ||
162 | printf("\trtdisp = %-.16g\n", NTP32asDOUBLE(p->rtdisp)); | ||
163 | printf("\trefid = %x\n", p->refid); | ||
164 | printf("\trefts = %-.16g\n", NTP64asDOUBLE(p->refts)); | ||
165 | printf("\torigts = %-.16g\n", NTP64asDOUBLE(p->origts)); | ||
166 | printf("\trxts = %-.16g\n", NTP64asDOUBLE(p->rxts)); | ||
167 | printf("\ttxts = %-.16g\n", NTP64asDOUBLE(p->txts)); | ||
168 | } | ||
169 | |||
170 | void setup_request(ntp_message *p){ | ||
171 | struct timeval t; | ||
172 | |||
173 | memset(p, 0, sizeof(ntp_message)); | ||
174 | LI_SET(p->flags, LI_ALARM); | ||
175 | VN_SET(p->flags, 4); | ||
176 | MODE_SET(p->flags, MODE_CLIENT); | ||
177 | p->poll=4; | ||
178 | p->precision=0xfa; | ||
179 | L16(p->rtdelay)=htons(1); | ||
180 | L16(p->rtdisp)=htons(1); | ||
181 | |||
182 | gettimeofday(&t, NULL); | ||
183 | TVtoNTP64(t,p->txts); | ||
184 | } | ||
185 | |||
186 | int process_arguments(int argc, char **argv){ | ||
187 | int c; | ||
188 | int option=0; | ||
189 | static struct option longopts[] = { | ||
190 | {"version", no_argument, 0, 'V'}, | ||
191 | {"help", no_argument, 0, 'h'}, | ||
192 | {"verbose", no_argument, 0, 'v'}, | ||
193 | {"use-ipv4", no_argument, 0, '4'}, | ||
194 | /* {"use-ipv6", no_argument, 0, '6'}, */ | ||
195 | {"warning", required_argument, 0, 'w'}, | ||
196 | {"critical", required_argument, 0, 'c'}, | ||
197 | {"zero-offset", no_argument, 0, 'O'}, | ||
198 | {"jwarn", required_argument, 0, 'j'}, | ||
199 | {"jcrit", required_argument, 0, 'k'}, | ||
200 | {"timeout", required_argument, 0, 't'}, | ||
201 | {"hostname", required_argument, 0, 'H'}, | ||
202 | {0, 0, 0, 0} | ||
203 | }; | ||
204 | |||
205 | |||
206 | if (argc < 2) | ||
207 | usage ("\n"); | ||
208 | |||
209 | while (1) { | ||
210 | c = getopt_long (argc, argv, "Vhv4w:c:Oj:k:t:H:", longopts, &option); | ||
211 | if (c == -1 || c == EOF || c == 1) | ||
212 | break; | ||
213 | |||
214 | switch (c) { | ||
215 | case 'h': | ||
216 | print_help(); | ||
217 | exit(STATE_OK); | ||
218 | break; | ||
219 | case 'V': | ||
220 | print_revision(progname, revision); | ||
221 | exit(STATE_OK); | ||
222 | break; | ||
223 | case 'v': | ||
224 | verbose = 1; | ||
225 | break; | ||
226 | case 'w': | ||
227 | owarn = atof(optarg); | ||
228 | break; | ||
229 | case 'c': | ||
230 | ocrit = atof(optarg); | ||
231 | break; | ||
232 | case 'j': | ||
233 | do_jitter=1; | ||
234 | jwarn = atof(optarg); | ||
235 | break; | ||
236 | case 'k': | ||
237 | do_jitter=1; | ||
238 | jcrit = atof(optarg); | ||
239 | break; | ||
240 | case 'H': | ||
241 | if(is_host(optarg) == FALSE) | ||
242 | usage2(_("Invalid hostname/address"), optarg); | ||
243 | server_address = strdup(optarg); | ||
244 | break; | ||
245 | case 't': | ||
246 | socket_timeout=atoi(optarg); | ||
247 | break; | ||
248 | case 'O': | ||
249 | zero_offset_bad=1; | ||
250 | break; | ||
251 | case '?': | ||
252 | /* print short usage statement if args not parsable */ | ||
253 | usage2 (_("Unknown argument"), optarg); | ||
254 | break; | ||
255 | } | ||
256 | } | ||
257 | |||
258 | if (ocrit < owarn){ | ||
259 | usage4(_("Critical offset should be larger than warning offset")); | ||
260 | } | ||
261 | |||
262 | if (ocrit < owarn){ | ||
263 | usage4(_("Critical jitter should be larger than warning jitter")); | ||
264 | } | ||
265 | |||
266 | if(server_address == NULL){ | ||
267 | usage4(_("Hostname was not supplied")); | ||
268 | } | ||
269 | |||
270 | return 0; | ||
271 | } | ||
272 | |||
273 | int main(int argc, char *argv[]){ | ||
274 | int result = STATE_UNKNOWN; | ||
275 | int conn; | ||
276 | ntp_message m; | ||
277 | struct timeval recv_time; | ||
278 | double offset=0, jitter=0; | ||
279 | |||
280 | if (process_arguments (argc, argv) == ERROR) | ||
281 | usage4 (_("Could not parse arguments")); | ||
282 | |||
283 | /* initialize alarm signal handling */ | ||
284 | signal (SIGALRM, socket_timeout_alarm_handler); | ||
285 | |||
286 | /* set socket timeout */ | ||
287 | alarm (socket_timeout); | ||
288 | |||
289 | setup_request(&m); | ||
290 | if(verbose) print_packet(&m); | ||
291 | my_udp_connect(server_address, 123, &conn); | ||
292 | write(conn, &m, sizeof(ntp_message)); | ||
293 | read(conn, &m, sizeof(ntp_message)); | ||
294 | gettimeofday(&recv_time, NULL); | ||
295 | if(verbose) print_packet(&m); | ||
296 | close(conn); | ||
297 | |||
298 | offset=calc_offset(&m, &recv_time); | ||
299 | printf("total offset: %g\n", offset); | ||
300 | |||
301 | if(offset > ocrit){ | ||
302 | printf("NTP CRITICAL: "); | ||
303 | result = STATE_CRITICAL; | ||
304 | } else if(offset > owarn) { | ||
305 | printf("NTP WARNING: "); | ||
306 | result = STATE_WARNING; | ||
307 | } else { | ||
308 | printf("NTP OK: "); | ||
309 | result = STATE_OK; | ||
310 | } | ||
311 | |||
312 | /* not implemented yet: | ||
313 | jitter=calc_jitter(&m, &recv_time); | ||
314 | |||
315 | if(do_jitter){ | ||
316 | if(offset > ocrit){ | ||
317 | printf("NTP CRITICAL: "); | ||
318 | result = STATE_CRITICAL; | ||
319 | } else if(offset > owarn) { | ||
320 | printf("NTP WARNING: "); | ||
321 | result = STATE_WARNING; | ||
322 | } else { | ||
323 | printf("NTP OK: "); | ||
324 | result = STATE_OK; | ||
325 | } | ||
326 | } | ||
327 | */ | ||
328 | |||
329 | printf("Offset %g secs|offset=%g\n", offset, offset); | ||
330 | |||
331 | if(server_address!=NULL) free(server_address); | ||
332 | return result; | ||
333 | } | ||
334 | |||
335 | |||
336 | void print_usage(void){ | ||
337 | printf("\ | ||
338 | Usage: %s -H <host> [-O] [-w <warn>] [-c <crit>] [-j <warn>] [-k <crit>] [-v verbose]\ | ||
339 | \n", progname); | ||
340 | } | ||
341 | |||
342 | void print_help(void){ | ||
343 | print_revision(progname, revision); | ||
344 | |||
345 | printf ("Copyright (c) 1999 Ethan Galstad\n"); | ||
346 | printf (COPYRIGHT, copyright, email); | ||
347 | |||
348 | print_usage(); | ||
349 | printf (_(UT_HELP_VRSN)); | ||
350 | printf (_(UT_HOST_PORT), 'p', "123"); | ||
351 | printf (_(UT_WARN_CRIT)); | ||
352 | printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT); | ||
353 | printf (_(UT_VERBOSE)); | ||
354 | printf(_(UT_SUPPORT)); | ||
355 | } | ||