diff options
| -rw-r--r-- | .github/monitoring-plugins.spec | 13 | ||||
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | plugins/Makefile.am | 3 | ||||
| -rw-r--r-- | plugins/check_ntp.c | 947 |
4 files changed, 1 insertions, 963 deletions
diff --git a/.github/monitoring-plugins.spec b/.github/monitoring-plugins.spec index 5878a862..fa47cdc7 100644 --- a/.github/monitoring-plugins.spec +++ b/.github/monitoring-plugins.spec | |||
| @@ -652,19 +652,6 @@ Provides check_nagios of the Monitoring Plugins. | |||
| 652 | 652 | ||
| 653 | 653 | ||
| 654 | 654 | ||
| 655 | # check_ntp | ||
| 656 | %package ntp | ||
| 657 | Summary: Monitoring Plugins - check_ntp | ||
| 658 | Requires: %{name} = %{version}-%{release} | ||
| 659 | |||
| 660 | %description ntp | ||
| 661 | Provides check_ntp of the Monitoring Plugins. | ||
| 662 | |||
| 663 | %files ntp | ||
| 664 | %{plugindir}/check_ntp | ||
| 665 | |||
| 666 | |||
| 667 | |||
| 668 | # check_ntp_peer | 655 | # check_ntp_peer |
| 669 | %package ntp_peer | 656 | %package ntp_peer |
| 670 | Summary: Monitoring Plugins - check_ntp_peer | 657 | Summary: Monitoring Plugins - check_ntp_peer |
| @@ -174,7 +174,6 @@ plugins/check_disk.d/.dirstamp | |||
| 174 | /plugins/check_netsaint | 174 | /plugins/check_netsaint |
| 175 | /plugins/check_nntp | 175 | /plugins/check_nntp |
| 176 | /plugins/check_nntps | 176 | /plugins/check_nntps |
| 177 | /plugins/check_ntp | ||
| 178 | /plugins/check_ntp_peer | 177 | /plugins/check_ntp_peer |
| 179 | /plugins/check_ntp_time | 178 | /plugins/check_ntp_time |
| 180 | /plugins/check_pgsql | 179 | /plugins/check_pgsql |
diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 877c0947..d098fa8a 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am | |||
| @@ -33,7 +33,7 @@ MATHLIBS = @MATHLIBS@ | |||
| 33 | #AM_CFLAGS = -Wall | 33 | #AM_CFLAGS = -Wall |
| 34 | 34 | ||
| 35 | libexec_PROGRAMS = check_apt check_cluster check_disk check_dummy check_http check_load \ | 35 | libexec_PROGRAMS = check_apt check_cluster check_disk check_dummy check_http check_load \ |
| 36 | check_mrtg check_mrtgtraf check_ntp check_ntp_peer check_ping \ | 36 | check_mrtg check_mrtgtraf check_ntp_peer check_ping \ |
| 37 | check_real check_smtp check_ssh check_tcp check_time check_ntp_time \ | 37 | check_real check_smtp check_ssh check_tcp check_time check_ntp_time \ |
| 38 | check_ups check_users negate \ | 38 | check_ups check_users negate \ |
| 39 | urlize @EXTRAS@ \ | 39 | urlize @EXTRAS@ \ |
| @@ -156,7 +156,6 @@ check_mysql_query_CFLAGS = $(AM_CFLAGS) $(MYSQLCFLAGS) | |||
| 156 | check_mysql_query_CPPFLAGS = $(AM_CPPFLAGS) $(MYSQLINCLUDE) | 156 | check_mysql_query_CPPFLAGS = $(AM_CPPFLAGS) $(MYSQLINCLUDE) |
| 157 | check_mysql_query_LDADD = $(NETLIBS) $(MYSQLLIBS) | 157 | check_mysql_query_LDADD = $(NETLIBS) $(MYSQLLIBS) |
| 158 | check_nagios_LDADD = $(BASEOBJS) | 158 | check_nagios_LDADD = $(BASEOBJS) |
| 159 | check_ntp_LDADD = $(NETLIBS) $(MATHLIBS) | ||
| 160 | check_ntp_peer_LDADD = $(NETLIBS) $(MATHLIBS) | 159 | check_ntp_peer_LDADD = $(NETLIBS) $(MATHLIBS) |
| 161 | check_pgsql_LDADD = $(NETLIBS) $(PGLIBS) | 160 | check_pgsql_LDADD = $(NETLIBS) $(PGLIBS) |
| 162 | check_ping_LDADD = $(NETLIBS) | 161 | check_ping_LDADD = $(NETLIBS) |
diff --git a/plugins/check_ntp.c b/plugins/check_ntp.c deleted file mode 100644 index b22cc3c1..00000000 --- a/plugins/check_ntp.c +++ /dev/null | |||
| @@ -1,947 +0,0 @@ | |||
| 1 | /***************************************************************************** | ||
| 2 | * | ||
| 3 | * Monitoring check_ntp plugin | ||
| 4 | * | ||
| 5 | * License: GPL | ||
| 6 | * Copyright (c) 2006 Sean Finney <seanius@seanius.net> | ||
| 7 | * Copyright (c) 2006-2024 Monitoring Plugins Development Team | ||
| 8 | * | ||
| 9 | * Description: | ||
| 10 | * | ||
| 11 | * This file contains the check_ntp plugin | ||
| 12 | * | ||
| 13 | * This plugin to check ntp servers independent of any commandline | ||
| 14 | * programs or external libraries. | ||
| 15 | * | ||
| 16 | * | ||
| 17 | * This program is free software: you can redistribute it and/or modify | ||
| 18 | * it under the terms of the GNU General Public License as published by | ||
| 19 | * the Free Software Foundation, either version 3 of the License, or | ||
| 20 | * (at your option) any later version. | ||
| 21 | * | ||
| 22 | * This program is distributed in the hope that it will be useful, | ||
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 25 | * GNU General Public License for more details. | ||
| 26 | * | ||
| 27 | * You should have received a copy of the GNU General Public License | ||
| 28 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 29 | * | ||
| 30 | * | ||
| 31 | *****************************************************************************/ | ||
| 32 | |||
| 33 | const char *progname = "check_ntp"; | ||
| 34 | const char *copyright = "2006-2024"; | ||
| 35 | const char *email = "devel@monitoring-plugins.org"; | ||
| 36 | |||
| 37 | #include "common.h" | ||
| 38 | #include "netutils.h" | ||
| 39 | #include "utils.h" | ||
| 40 | |||
| 41 | static char *server_address = NULL; | ||
| 42 | static int verbose = 0; | ||
| 43 | static bool do_offset = false; | ||
| 44 | static char *owarn = "60"; | ||
| 45 | static char *ocrit = "120"; | ||
| 46 | static bool do_jitter = false; | ||
| 47 | static char *jwarn = "5000"; | ||
| 48 | static char *jcrit = "10000"; | ||
| 49 | |||
| 50 | static int process_arguments(int /*argc*/, char ** /*argv*/); | ||
| 51 | static thresholds *offset_thresholds = NULL; | ||
| 52 | static thresholds *jitter_thresholds = NULL; | ||
| 53 | static void print_help(void); | ||
| 54 | void print_usage(void); | ||
| 55 | |||
| 56 | /* number of times to perform each request to get a good average. */ | ||
| 57 | #ifndef AVG_NUM | ||
| 58 | # define AVG_NUM 4 | ||
| 59 | #endif | ||
| 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 received 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 | /* this structure holds everything in an ntp control message as per rfc1305 */ | ||
| 91 | typedef struct { | ||
| 92 | uint8_t flags; /* byte with leapindicator,vers,mode. see macros */ | ||
| 93 | uint8_t op; /* R,E,M bits and Opcode */ | ||
| 94 | uint16_t seq; /* Packet sequence */ | ||
| 95 | uint16_t status; /* Clock status */ | ||
| 96 | uint16_t assoc; /* Association */ | ||
| 97 | uint16_t offset; /* Similar to TCP sequence # */ | ||
| 98 | uint16_t count; /* # bytes of data */ | ||
| 99 | char data[MAX_CM_SIZE]; /* ASCII data of the request */ | ||
| 100 | /* NB: not necessarily NULL terminated! */ | ||
| 101 | } ntp_control_message; | ||
| 102 | |||
| 103 | /* this is an association/status-word pair found in control packet responses */ | ||
| 104 | typedef struct { | ||
| 105 | uint16_t assoc; | ||
| 106 | uint16_t status; | ||
| 107 | } ntp_assoc_status_pair; | ||
| 108 | |||
| 109 | /* bits 1,2 are the leap indicator */ | ||
| 110 | #define LI_MASK 0xc0 | ||
| 111 | #define LI(x) ((x & LI_MASK) >> 6) | ||
| 112 | #define LI_SET(x, y) \ | ||
| 113 | do { \ | ||
| 114 | x |= ((y << 6) & LI_MASK); \ | ||
| 115 | } while (0) | ||
| 116 | /* and these are the values of the leap indicator */ | ||
| 117 | #define LI_NOWARNING 0x00 | ||
| 118 | #define LI_EXTRASEC 0x01 | ||
| 119 | #define LI_MISSINGSEC 0x02 | ||
| 120 | #define LI_ALARM 0x03 | ||
| 121 | /* bits 3,4,5 are the ntp version */ | ||
| 122 | #define VN_MASK 0x38 | ||
| 123 | #define VN(x) ((x & VN_MASK) >> 3) | ||
| 124 | #define VN_SET(x, y) \ | ||
| 125 | do { \ | ||
| 126 | x |= ((y << 3) & VN_MASK); \ | ||
| 127 | } while (0) | ||
| 128 | #define VN_RESERVED 0x02 | ||
| 129 | /* bits 6,7,8 are the ntp mode */ | ||
| 130 | #define MODE_MASK 0x07 | ||
| 131 | #define MODE(x) (x & MODE_MASK) | ||
| 132 | #define MODE_SET(x, y) \ | ||
| 133 | do { \ | ||
| 134 | x |= (y & MODE_MASK); \ | ||
| 135 | } while (0) | ||
| 136 | /* here are some values */ | ||
| 137 | #define MODE_CLIENT 0x03 | ||
| 138 | #define MODE_CONTROLMSG 0x06 | ||
| 139 | /* In control message, bits 8-10 are R,E,M bits */ | ||
| 140 | #define REM_MASK 0xe0 | ||
| 141 | #define REM_RESP 0x80 | ||
| 142 | #define REM_ERROR 0x40 | ||
| 143 | #define REM_MORE 0x20 | ||
| 144 | /* In control message, bits 11 - 15 are opcode */ | ||
| 145 | #define OP_MASK 0x1f | ||
| 146 | #define OP_SET(x, y) \ | ||
| 147 | do { \ | ||
| 148 | x |= (y & OP_MASK); \ | ||
| 149 | } while (0) | ||
| 150 | #define OP_READSTAT 0x01 | ||
| 151 | #define OP_READVAR 0x02 | ||
| 152 | /* In peer status bytes, bits 6,7,8 determine clock selection status */ | ||
| 153 | #define PEER_SEL(x) ((ntohs(x) >> 8) & 0x07) | ||
| 154 | #define PEER_INCLUDED 0x04 | ||
| 155 | #define PEER_SYNCSOURCE 0x06 | ||
| 156 | |||
| 157 | /** | ||
| 158 | ** a note about the 32-bit "fixed point" numbers: | ||
| 159 | ** | ||
| 160 | they are divided into halves, each being a 16-bit int in network byte order: | ||
| 161 | - the first 16 bits are an int on the left side of a decimal point. | ||
| 162 | - the second 16 bits represent a fraction n/(2^16) | ||
| 163 | likewise for the 64-bit "fixed point" numbers with everything doubled :) | ||
| 164 | **/ | ||
| 165 | |||
| 166 | /* macros to access the left/right 16 bits of a 32-bit ntp "fixed point" | ||
| 167 | number. note that these can be used as lvalues too */ | ||
| 168 | #define L16(x) (((uint16_t *)&x)[0]) | ||
| 169 | #define R16(x) (((uint16_t *)&x)[1]) | ||
| 170 | /* macros to access the left/right 32 bits of a 64-bit ntp "fixed point" | ||
| 171 | number. these too can be used as lvalues */ | ||
| 172 | #define L32(x) (((uint32_t *)&x)[0]) | ||
| 173 | #define R32(x) (((uint32_t *)&x)[1]) | ||
| 174 | |||
| 175 | /* ntp wants seconds since 1/1/00, epoch is 1/1/70. this is the difference */ | ||
| 176 | #define EPOCHDIFF 0x83aa7e80UL | ||
| 177 | |||
| 178 | /* extract a 32-bit ntp fixed point number into a double */ | ||
| 179 | #define NTP32asDOUBLE(x) (ntohs(L16(x)) + (double)ntohs(R16(x)) / 65536.0) | ||
| 180 | |||
| 181 | /* likewise for a 64-bit ntp fp number */ | ||
| 182 | #define NTP64asDOUBLE(n) \ | ||
| 183 | (double)(((uint64_t)n) ? (ntohl(L32(n)) - EPOCHDIFF) + \ | ||
| 184 | (.00000001 * (0.5 + (double)(ntohl(R32(n)) / 42.94967296))) \ | ||
| 185 | : 0) | ||
| 186 | |||
| 187 | /* convert a struct timeval to a double */ | ||
| 188 | #define TVasDOUBLE(x) (double)(x.tv_sec + (0.000001 * x.tv_usec)) | ||
| 189 | |||
| 190 | /* convert an ntp 64-bit fp number to a struct timeval */ | ||
| 191 | #define NTP64toTV(n, t) \ | ||
| 192 | do { \ | ||
| 193 | if (!n) \ | ||
| 194 | t.tv_sec = t.tv_usec = 0; \ | ||
| 195 | else { \ | ||
| 196 | t.tv_sec = ntohl(L32(n)) - EPOCHDIFF; \ | ||
| 197 | t.tv_usec = (int)(0.5 + (double)(ntohl(R32(n)) / 4294.967296)); \ | ||
| 198 | } \ | ||
| 199 | } while (0) | ||
| 200 | |||
| 201 | /* convert a struct timeval to an ntp 64-bit fp number */ | ||
| 202 | #define TVtoNTP64(t, n) \ | ||
| 203 | do { \ | ||
| 204 | if (!t.tv_usec && !t.tv_sec) \ | ||
| 205 | n = 0x0UL; \ | ||
| 206 | else { \ | ||
| 207 | L32(n) = htonl(t.tv_sec + EPOCHDIFF); \ | ||
| 208 | R32(n) = htonl((uint64_t)((4294.967296 * t.tv_usec) + .5)); \ | ||
| 209 | } \ | ||
| 210 | } while (0) | ||
| 211 | |||
| 212 | /* NTP control message header is 12 bytes, plus any data in the data | ||
| 213 | * field, plus null padding to the nearest 32-bit boundary per rfc. | ||
| 214 | */ | ||
| 215 | #define SIZEOF_NTPCM(m) \ | ||
| 216 | (12 + ntohs(m.count) + ((ntohs(m.count) % 4) ? 4 - (ntohs(m.count) % 4) : 0)) | ||
| 217 | |||
| 218 | /* finally, a little helper or two for debugging: */ | ||
| 219 | #define DBG(x) \ | ||
| 220 | do { \ | ||
| 221 | if (verbose > 1) { \ | ||
| 222 | x; \ | ||
| 223 | } \ | ||
| 224 | } while (0); | ||
| 225 | #define PRINTSOCKADDR(x) \ | ||
| 226 | do { \ | ||
| 227 | printf("%u.%u.%u.%u", (x >> 24) & 0xff, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff); \ | ||
| 228 | } while (0); | ||
| 229 | |||
| 230 | /* calculate the offset of the local clock */ | ||
| 231 | static inline double calc_offset(const ntp_message *m, const struct timeval *t) { | ||
| 232 | double client_tx, peer_rx, peer_tx, client_rx; | ||
| 233 | client_tx = NTP64asDOUBLE(m->origts); | ||
| 234 | peer_rx = NTP64asDOUBLE(m->rxts); | ||
| 235 | peer_tx = NTP64asDOUBLE(m->txts); | ||
| 236 | client_rx = TVasDOUBLE((*t)); | ||
| 237 | return (.5 * ((peer_tx - client_rx) + (peer_rx - client_tx))); | ||
| 238 | } | ||
| 239 | |||
| 240 | /* print out a ntp packet in human readable/debuggable format */ | ||
| 241 | void print_ntp_message(const ntp_message *p) { | ||
| 242 | struct timeval ref, orig, rx, tx; | ||
| 243 | |||
| 244 | NTP64toTV(p->refts, ref); | ||
| 245 | NTP64toTV(p->origts, orig); | ||
| 246 | NTP64toTV(p->rxts, rx); | ||
| 247 | NTP64toTV(p->txts, tx); | ||
| 248 | |||
| 249 | printf("packet contents:\n"); | ||
| 250 | printf("\tflags: 0x%.2x\n", p->flags); | ||
| 251 | printf("\t li=%d (0x%.2x)\n", LI(p->flags), p->flags & LI_MASK); | ||
| 252 | printf("\t vn=%d (0x%.2x)\n", VN(p->flags), p->flags & VN_MASK); | ||
| 253 | printf("\t mode=%d (0x%.2x)\n", MODE(p->flags), p->flags & MODE_MASK); | ||
| 254 | printf("\tstratum = %d\n", p->stratum); | ||
| 255 | printf("\tpoll = %g\n", pow(2, p->poll)); | ||
| 256 | printf("\tprecision = %g\n", pow(2, p->precision)); | ||
| 257 | printf("\trtdelay = %-.16g\n", NTP32asDOUBLE(p->rtdelay)); | ||
| 258 | printf("\trtdisp = %-.16g\n", NTP32asDOUBLE(p->rtdisp)); | ||
| 259 | printf("\trefid = %x\n", p->refid); | ||
| 260 | printf("\trefts = %-.16g\n", NTP64asDOUBLE(p->refts)); | ||
| 261 | printf("\torigts = %-.16g\n", NTP64asDOUBLE(p->origts)); | ||
| 262 | printf("\trxts = %-.16g\n", NTP64asDOUBLE(p->rxts)); | ||
| 263 | printf("\ttxts = %-.16g\n", NTP64asDOUBLE(p->txts)); | ||
| 264 | } | ||
| 265 | |||
| 266 | void print_ntp_control_message(const ntp_control_message *p) { | ||
| 267 | int i = 0, numpeers = 0; | ||
| 268 | const ntp_assoc_status_pair *peer = NULL; | ||
| 269 | |||
| 270 | printf("control packet contents:\n"); | ||
| 271 | printf("\tflags: 0x%.2x , 0x%.2x\n", p->flags, p->op); | ||
| 272 | printf("\t li=%d (0x%.2x)\n", LI(p->flags), p->flags & LI_MASK); | ||
| 273 | printf("\t vn=%d (0x%.2x)\n", VN(p->flags), p->flags & VN_MASK); | ||
| 274 | printf("\t mode=%d (0x%.2x)\n", MODE(p->flags), p->flags & MODE_MASK); | ||
| 275 | printf("\t response=%d (0x%.2x)\n", (p->op & REM_RESP) > 0, p->op & REM_RESP); | ||
| 276 | printf("\t more=%d (0x%.2x)\n", (p->op & REM_MORE) > 0, p->op & REM_MORE); | ||
| 277 | printf("\t error=%d (0x%.2x)\n", (p->op & REM_ERROR) > 0, p->op & REM_ERROR); | ||
| 278 | printf("\t op=%d (0x%.2x)\n", p->op & OP_MASK, p->op & OP_MASK); | ||
| 279 | printf("\tsequence: %d (0x%.2x)\n", ntohs(p->seq), ntohs(p->seq)); | ||
| 280 | printf("\tstatus: %d (0x%.2x)\n", ntohs(p->status), ntohs(p->status)); | ||
| 281 | printf("\tassoc: %d (0x%.2x)\n", ntohs(p->assoc), ntohs(p->assoc)); | ||
| 282 | printf("\toffset: %d (0x%.2x)\n", ntohs(p->offset), ntohs(p->offset)); | ||
| 283 | printf("\tcount: %d (0x%.2x)\n", ntohs(p->count), ntohs(p->count)); | ||
| 284 | numpeers = ntohs(p->count) / (sizeof(ntp_assoc_status_pair)); | ||
| 285 | if (p->op & REM_RESP && p->op & OP_READSTAT) { | ||
| 286 | peer = (ntp_assoc_status_pair *)p->data; | ||
| 287 | for (i = 0; i < numpeers; i++) { | ||
| 288 | printf("\tpeer id %.2x status %.2x", ntohs(peer[i].assoc), ntohs(peer[i].status)); | ||
| 289 | if (PEER_SEL(peer[i].status) >= PEER_INCLUDED) { | ||
| 290 | if (PEER_SEL(peer[i].status) >= PEER_SYNCSOURCE) { | ||
| 291 | printf(" <-- current sync source"); | ||
| 292 | } else { | ||
| 293 | printf(" <-- current sync candidate"); | ||
| 294 | } | ||
| 295 | } | ||
| 296 | printf("\n"); | ||
| 297 | } | ||
| 298 | } | ||
| 299 | } | ||
| 300 | |||
| 301 | void setup_request(ntp_message *p) { | ||
| 302 | struct timeval t; | ||
| 303 | |||
| 304 | memset(p, 0, sizeof(ntp_message)); | ||
| 305 | LI_SET(p->flags, LI_ALARM); | ||
| 306 | VN_SET(p->flags, 4); | ||
| 307 | MODE_SET(p->flags, MODE_CLIENT); | ||
| 308 | p->poll = 4; | ||
| 309 | p->precision = (int8_t)0xfa; | ||
| 310 | L16(p->rtdelay) = htons(1); | ||
| 311 | L16(p->rtdisp) = htons(1); | ||
| 312 | |||
| 313 | gettimeofday(&t, NULL); | ||
| 314 | TVtoNTP64(t, p->txts); | ||
| 315 | } | ||
| 316 | |||
| 317 | /* select the "best" server from a list of servers, and return its index. | ||
| 318 | * this is done by filtering servers based on stratum, dispersion, and | ||
| 319 | * finally round-trip delay. */ | ||
| 320 | int best_offset_server(const ntp_server_results *slist, int nservers) { | ||
| 321 | int cserver = 0, best_server = -1; | ||
| 322 | |||
| 323 | /* for each server */ | ||
| 324 | for (cserver = 0; cserver < nservers; cserver++) { | ||
| 325 | /* We don't want any servers that fails these tests */ | ||
| 326 | /* Sort out servers that didn't respond or responede with a 0 stratum; | ||
| 327 | * stratum 0 is for reference clocks so no NTP server should ever report | ||
| 328 | * a stratum 0 */ | ||
| 329 | if (slist[cserver].stratum == 0) { | ||
| 330 | if (verbose) { | ||
| 331 | printf("discarding peer %d: stratum=%d\n", cserver, slist[cserver].stratum); | ||
| 332 | } | ||
| 333 | continue; | ||
| 334 | } | ||
| 335 | /* Sort out servers with error flags */ | ||
| 336 | if (LI(slist[cserver].flags) == LI_ALARM) { | ||
| 337 | if (verbose) { | ||
| 338 | printf("discarding peer %d: flags=%d\n", cserver, LI(slist[cserver].flags)); | ||
| 339 | } | ||
| 340 | continue; | ||
| 341 | } | ||
| 342 | |||
| 343 | /* If we don't have a server yet, use the first one */ | ||
| 344 | if (best_server == -1) { | ||
| 345 | best_server = cserver; | ||
| 346 | DBG(printf("using peer %d as our first candidate\n", best_server)); | ||
| 347 | continue; | ||
| 348 | } | ||
| 349 | |||
| 350 | /* compare the server to the best one we've seen so far */ | ||
| 351 | /* does it have an equal or better stratum? */ | ||
| 352 | DBG(printf("comparing peer %d with peer %d\n", cserver, best_server)); | ||
| 353 | if (slist[cserver].stratum <= slist[best_server].stratum) { | ||
| 354 | DBG(printf("stratum for peer %d <= peer %d\n", cserver, best_server)); | ||
| 355 | /* does it have an equal or better dispersion? */ | ||
| 356 | if (slist[cserver].rtdisp <= slist[best_server].rtdisp) { | ||
| 357 | DBG(printf("dispersion for peer %d <= peer %d\n", cserver, best_server)); | ||
| 358 | /* does it have a better rtdelay? */ | ||
| 359 | if (slist[cserver].rtdelay < slist[best_server].rtdelay) { | ||
| 360 | DBG(printf("rtdelay for peer %d < peer %d\n", cserver, best_server)); | ||
| 361 | best_server = cserver; | ||
| 362 | DBG(printf("peer %d is now our best candidate\n", best_server)); | ||
| 363 | } | ||
| 364 | } | ||
| 365 | } | ||
| 366 | } | ||
| 367 | |||
| 368 | if (best_server >= 0) { | ||
| 369 | DBG(printf("best server selected: peer %d\n", best_server)); | ||
| 370 | return best_server; | ||
| 371 | } else { | ||
| 372 | DBG(printf("no peers meeting synchronization criteria :(\n")); | ||
| 373 | return -1; | ||
| 374 | } | ||
| 375 | } | ||
| 376 | |||
| 377 | /* do everything we need to get the total average offset | ||
| 378 | * - we use a certain amount of parallelization with poll() to ensure | ||
| 379 | * we don't waste time sitting around waiting for single packets. | ||
| 380 | * - we also "manually" handle resolving host names and connecting, because | ||
| 381 | * we have to do it in a way that our lazy macros don't handle currently :( */ | ||
| 382 | double offset_request(const char *host, int *status) { | ||
| 383 | int i = 0, ga_result = 0, num_hosts = 0, *socklist = NULL, respnum = 0; | ||
| 384 | int servers_completed = 0, one_read = 0, servers_readable = 0, best_index = -1; | ||
| 385 | time_t now_time = 0, start_ts = 0; | ||
| 386 | ntp_message *req = NULL; | ||
| 387 | double avg_offset = 0.; | ||
| 388 | struct timeval recv_time; | ||
| 389 | struct addrinfo *ai = NULL, *ai_tmp = NULL, hints; | ||
| 390 | struct pollfd *ufds = NULL; | ||
| 391 | ntp_server_results *servers = NULL; | ||
| 392 | |||
| 393 | /* setup hints to only return results from getaddrinfo that we'd like */ | ||
| 394 | memset(&hints, 0, sizeof(struct addrinfo)); | ||
| 395 | hints.ai_family = address_family; | ||
| 396 | hints.ai_protocol = IPPROTO_UDP; | ||
| 397 | hints.ai_socktype = SOCK_DGRAM; | ||
| 398 | |||
| 399 | /* fill in ai with the list of hosts resolved by the host name */ | ||
| 400 | ga_result = getaddrinfo(host, "123", &hints, &ai); | ||
| 401 | if (ga_result != 0) { | ||
| 402 | die(STATE_UNKNOWN, "error getting address for %s: %s\n", host, gai_strerror(ga_result)); | ||
| 403 | } | ||
| 404 | |||
| 405 | /* count the number of returned hosts, and allocate stuff accordingly */ | ||
| 406 | for (ai_tmp = ai; ai_tmp != NULL; ai_tmp = ai_tmp->ai_next) { | ||
| 407 | num_hosts++; | ||
| 408 | } | ||
| 409 | req = (ntp_message *)malloc(sizeof(ntp_message) * num_hosts); | ||
| 410 | if (req == NULL) { | ||
| 411 | die(STATE_UNKNOWN, "can not allocate ntp message array"); | ||
| 412 | } | ||
| 413 | socklist = (int *)malloc(sizeof(int) * num_hosts); | ||
| 414 | if (socklist == NULL) { | ||
| 415 | die(STATE_UNKNOWN, "can not allocate socket array"); | ||
| 416 | } | ||
| 417 | ufds = (struct pollfd *)malloc(sizeof(struct pollfd) * num_hosts); | ||
| 418 | if (ufds == NULL) { | ||
| 419 | die(STATE_UNKNOWN, "can not allocate socket array"); | ||
| 420 | } | ||
| 421 | servers = (ntp_server_results *)malloc(sizeof(ntp_server_results) * num_hosts); | ||
| 422 | if (servers == NULL) { | ||
| 423 | die(STATE_UNKNOWN, "can not allocate server array"); | ||
| 424 | } | ||
| 425 | memset(servers, 0, sizeof(ntp_server_results) * num_hosts); | ||
| 426 | DBG(printf("Found %d peers to check\n", num_hosts)); | ||
| 427 | |||
| 428 | /* setup each socket for writing, and the corresponding struct pollfd */ | ||
| 429 | ai_tmp = ai; | ||
| 430 | for (i = 0; ai_tmp; i++) { | ||
| 431 | socklist[i] = socket(ai_tmp->ai_family, SOCK_DGRAM, IPPROTO_UDP); | ||
| 432 | if (socklist[i] == -1) { | ||
| 433 | perror(NULL); | ||
| 434 | die(STATE_UNKNOWN, "can not create new socket"); | ||
| 435 | } | ||
| 436 | if (connect(socklist[i], ai_tmp->ai_addr, ai_tmp->ai_addrlen)) { | ||
| 437 | /* don't die here, because it is enough if there is one server | ||
| 438 | answering in time. This also would break for dual ipv4/6 stacked | ||
| 439 | ntp servers when the client only supports on of them. | ||
| 440 | */ | ||
| 441 | DBG(printf("can't create socket connection on peer %i: %s\n", i, strerror(errno))); | ||
| 442 | } else { | ||
| 443 | ufds[i].fd = socklist[i]; | ||
| 444 | ufds[i].events = POLLIN; | ||
| 445 | ufds[i].revents = 0; | ||
| 446 | } | ||
| 447 | ai_tmp = ai_tmp->ai_next; | ||
| 448 | } | ||
| 449 | |||
| 450 | /* now do AVG_NUM checks to each host. we stop before timeout/2 seconds | ||
| 451 | * have passed in order to ensure post-processing and jitter time. */ | ||
| 452 | now_time = start_ts = time(NULL); | ||
| 453 | while (servers_completed < num_hosts && now_time - start_ts <= socket_timeout / 2) { | ||
| 454 | /* loop through each server and find each one which hasn't | ||
| 455 | * been touched in the past second or so and is still lacking | ||
| 456 | * some responses. for each of these servers, send a new request, | ||
| 457 | * and update the "waiting" timestamp with the current time. */ | ||
| 458 | now_time = time(NULL); | ||
| 459 | |||
| 460 | for (i = 0; i < num_hosts; i++) { | ||
| 461 | if (servers[i].waiting < now_time && servers[i].num_responses < AVG_NUM) { | ||
| 462 | if (verbose && servers[i].waiting != 0) { | ||
| 463 | printf("re-"); | ||
| 464 | } | ||
| 465 | if (verbose) { | ||
| 466 | printf("sending request to peer %d\n", i); | ||
| 467 | } | ||
| 468 | setup_request(&req[i]); | ||
| 469 | write(socklist[i], &req[i], sizeof(ntp_message)); | ||
| 470 | servers[i].waiting = now_time; | ||
| 471 | break; | ||
| 472 | } | ||
| 473 | } | ||
| 474 | |||
| 475 | /* quickly poll for any sockets with pending data */ | ||
| 476 | servers_readable = poll(ufds, num_hosts, 100); | ||
| 477 | if (servers_readable == -1) { | ||
| 478 | perror("polling ntp sockets"); | ||
| 479 | die(STATE_UNKNOWN, "communication errors"); | ||
| 480 | } | ||
| 481 | |||
| 482 | /* read from any sockets with pending data */ | ||
| 483 | for (i = 0; servers_readable && i < num_hosts; i++) { | ||
| 484 | if (ufds[i].revents & POLLIN && servers[i].num_responses < AVG_NUM) { | ||
| 485 | if (verbose) { | ||
| 486 | printf("response from peer %d: ", i); | ||
| 487 | } | ||
| 488 | |||
| 489 | read(ufds[i].fd, &req[i], sizeof(ntp_message)); | ||
| 490 | gettimeofday(&recv_time, NULL); | ||
| 491 | DBG(print_ntp_message(&req[i])); | ||
| 492 | respnum = servers[i].num_responses++; | ||
| 493 | servers[i].offset[respnum] = calc_offset(&req[i], &recv_time); | ||
| 494 | if (verbose) { | ||
| 495 | printf("offset %.10g\n", servers[i].offset[respnum]); | ||
| 496 | } | ||
| 497 | servers[i].stratum = req[i].stratum; | ||
| 498 | servers[i].rtdisp = NTP32asDOUBLE(req[i].rtdisp); | ||
| 499 | servers[i].rtdelay = NTP32asDOUBLE(req[i].rtdelay); | ||
| 500 | servers[i].waiting = 0; | ||
| 501 | servers[i].flags = req[i].flags; | ||
| 502 | servers_readable--; | ||
| 503 | one_read = 1; | ||
| 504 | if (servers[i].num_responses == AVG_NUM) { | ||
| 505 | servers_completed++; | ||
| 506 | } | ||
| 507 | } | ||
| 508 | } | ||
| 509 | /* lather, rinse, repeat. */ | ||
| 510 | } | ||
| 511 | |||
| 512 | if (one_read == 0) { | ||
| 513 | die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n"); | ||
| 514 | } | ||
| 515 | |||
| 516 | /* now, pick the best server from the list */ | ||
| 517 | best_index = best_offset_server(servers, num_hosts); | ||
| 518 | if (best_index < 0) { | ||
| 519 | *status = STATE_UNKNOWN; | ||
| 520 | } else { | ||
| 521 | /* finally, calculate the average offset */ | ||
| 522 | for (i = 0; i < servers[best_index].num_responses; i++) { | ||
| 523 | avg_offset += servers[best_index].offset[i]; | ||
| 524 | } | ||
| 525 | avg_offset /= servers[best_index].num_responses; | ||
| 526 | } | ||
| 527 | |||
| 528 | /* cleanup */ | ||
| 529 | /* FIXME: Not closing the socket to avoid reuse of the local port | ||
| 530 | * which can cause old NTP packets to be read instead of NTP control | ||
| 531 | * packets in jitter_request(). THERE MUST BE ANOTHER WAY... | ||
| 532 | * for(j=0; j<num_hosts; j++){ close(socklist[j]); } */ | ||
| 533 | free(socklist); | ||
| 534 | free(ufds); | ||
| 535 | free(servers); | ||
| 536 | free(req); | ||
| 537 | freeaddrinfo(ai); | ||
| 538 | |||
| 539 | if (verbose) { | ||
| 540 | printf("overall average offset: %.10g\n", avg_offset); | ||
| 541 | } | ||
| 542 | return avg_offset; | ||
| 543 | } | ||
| 544 | |||
| 545 | void setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq) { | ||
| 546 | memset(p, 0, sizeof(ntp_control_message)); | ||
| 547 | LI_SET(p->flags, LI_NOWARNING); | ||
| 548 | VN_SET(p->flags, VN_RESERVED); | ||
| 549 | MODE_SET(p->flags, MODE_CONTROLMSG); | ||
| 550 | OP_SET(p->op, opcode); | ||
| 551 | p->seq = htons(seq); | ||
| 552 | /* Remaining fields are zero for requests */ | ||
| 553 | } | ||
| 554 | |||
| 555 | /* XXX handle responses with the error bit set */ | ||
| 556 | double jitter_request(int *status) { | ||
| 557 | int conn = -1, i, npeers = 0, num_candidates = 0; | ||
| 558 | bool syncsource_found = false; | ||
| 559 | int run = 0, min_peer_sel = PEER_INCLUDED, num_selected = 0, num_valid = 0; | ||
| 560 | int peers_size = 0, peer_offset = 0; | ||
| 561 | ntp_assoc_status_pair *peers = NULL; | ||
| 562 | ntp_control_message req; | ||
| 563 | const char *getvar = "jitter"; | ||
| 564 | double rval = 0.0, jitter = -1.0; | ||
| 565 | char *startofvalue = NULL, *nptr = NULL; | ||
| 566 | void *tmp; | ||
| 567 | |||
| 568 | /* Long-winded explanation: | ||
| 569 | * Getting the jitter requires a number of steps: | ||
| 570 | * 1) Send a READSTAT request. | ||
| 571 | * 2) Interpret the READSTAT reply | ||
| 572 | * a) The data section contains a list of peer identifiers (16 bits) | ||
| 573 | * and associated status words (16 bits) | ||
| 574 | * b) We want the value of 0x06 in the SEL (peer selection) value, | ||
| 575 | * which means "current synchronizatin source". If that's missing, | ||
| 576 | * we take anything better than 0x04 (see the rfc for details) but | ||
| 577 | * set a minimum of warning. | ||
| 578 | * 3) Send a READVAR request for information on each peer identified | ||
| 579 | * in 2b greater than the minimum selection value. | ||
| 580 | * 4) Extract the jitter value from the data[] (it's ASCII) | ||
| 581 | */ | ||
| 582 | my_udp_connect(server_address, 123, &conn); | ||
| 583 | |||
| 584 | /* keep sending requests until the server stops setting the | ||
| 585 | * REM_MORE bit, though usually this is only 1 packet. */ | ||
| 586 | do { | ||
| 587 | setup_control_request(&req, OP_READSTAT, 1); | ||
| 588 | DBG(printf("sending READSTAT request")); | ||
| 589 | write(conn, &req, SIZEOF_NTPCM(req)); | ||
| 590 | DBG(print_ntp_control_message(&req)); | ||
| 591 | /* Attempt to read the largest size packet possible */ | ||
| 592 | req.count = htons(MAX_CM_SIZE); | ||
| 593 | DBG(printf("receiving READSTAT response")) | ||
| 594 | read(conn, &req, SIZEOF_NTPCM(req)); | ||
| 595 | DBG(print_ntp_control_message(&req)); | ||
| 596 | /* Each peer identifier is 4 bytes in the data section, which | ||
| 597 | * we represent as a ntp_assoc_status_pair datatype. | ||
| 598 | */ | ||
| 599 | peers_size += ntohs(req.count); | ||
| 600 | if ((tmp = realloc(peers, peers_size)) == NULL) { | ||
| 601 | free(peers), die(STATE_UNKNOWN, "can not (re)allocate 'peers' buffer\n"); | ||
| 602 | } | ||
| 603 | peers = tmp; | ||
| 604 | memcpy((void *)((ptrdiff_t)peers + peer_offset), (void *)req.data, ntohs(req.count)); | ||
| 605 | npeers = peers_size / sizeof(ntp_assoc_status_pair); | ||
| 606 | peer_offset += ntohs(req.count); | ||
| 607 | } while (req.op & REM_MORE); | ||
| 608 | |||
| 609 | /* first, let's find out if we have a sync source, or if there are | ||
| 610 | * at least some candidates. in the case of the latter we'll issue | ||
| 611 | * a warning but go ahead with the check on them. */ | ||
| 612 | for (i = 0; i < npeers; i++) { | ||
| 613 | if (PEER_SEL(peers[i].status) >= PEER_INCLUDED) { | ||
| 614 | num_candidates++; | ||
| 615 | if (PEER_SEL(peers[i].status) >= PEER_SYNCSOURCE) { | ||
| 616 | syncsource_found = true; | ||
| 617 | min_peer_sel = PEER_SYNCSOURCE; | ||
| 618 | } | ||
| 619 | } | ||
| 620 | } | ||
| 621 | if (verbose) { | ||
| 622 | printf("%d candidate peers available\n", num_candidates); | ||
| 623 | } | ||
| 624 | if (verbose && syncsource_found) { | ||
| 625 | printf("synchronization source found\n"); | ||
| 626 | } | ||
| 627 | if (!syncsource_found) { | ||
| 628 | *status = STATE_UNKNOWN; | ||
| 629 | if (verbose) { | ||
| 630 | printf("warning: no synchronization source found\n"); | ||
| 631 | } | ||
| 632 | } | ||
| 633 | |||
| 634 | for (run = 0; run < AVG_NUM; run++) { | ||
| 635 | if (verbose) { | ||
| 636 | printf("jitter run %d of %d\n", run + 1, AVG_NUM); | ||
| 637 | } | ||
| 638 | for (i = 0; i < npeers; i++) { | ||
| 639 | /* Only query this server if it is the current sync source */ | ||
| 640 | if (PEER_SEL(peers[i].status) >= min_peer_sel) { | ||
| 641 | char jitter_data[MAX_CM_SIZE + 1]; | ||
| 642 | size_t jitter_data_count; | ||
| 643 | |||
| 644 | num_selected++; | ||
| 645 | setup_control_request(&req, OP_READVAR, 2); | ||
| 646 | req.assoc = peers[i].assoc; | ||
| 647 | /* By spec, putting the variable name "jitter" in the request | ||
| 648 | * should cause the server to provide _only_ the jitter value. | ||
| 649 | * thus reducing net traffic, guaranteeing us only a single | ||
| 650 | * datagram in reply, and making interpretation much simpler | ||
| 651 | */ | ||
| 652 | /* Older servers doesn't know what jitter is, so if we get an | ||
| 653 | * error on the first pass we redo it with "dispersion" */ | ||
| 654 | strncpy(req.data, getvar, MAX_CM_SIZE - 1); | ||
| 655 | req.count = htons(strlen(getvar)); | ||
| 656 | DBG(printf("sending READVAR request...\n")); | ||
| 657 | write(conn, &req, SIZEOF_NTPCM(req)); | ||
| 658 | DBG(print_ntp_control_message(&req)); | ||
| 659 | |||
| 660 | req.count = htons(MAX_CM_SIZE); | ||
| 661 | DBG(printf("receiving READVAR response...\n")); | ||
| 662 | read(conn, &req, SIZEOF_NTPCM(req)); | ||
| 663 | DBG(print_ntp_control_message(&req)); | ||
| 664 | |||
| 665 | if (req.op & REM_ERROR && strstr(getvar, "jitter")) { | ||
| 666 | if (verbose) { | ||
| 667 | printf("The 'jitter' command failed (old ntp server?)\nRestarting with " | ||
| 668 | "'dispersion'...\n"); | ||
| 669 | } | ||
| 670 | getvar = "dispersion"; | ||
| 671 | num_selected--; | ||
| 672 | i--; | ||
| 673 | continue; | ||
| 674 | } | ||
| 675 | |||
| 676 | /* get to the float value */ | ||
| 677 | if (verbose) { | ||
| 678 | printf("parsing jitter from peer %.2x: ", ntohs(peers[i].assoc)); | ||
| 679 | } | ||
| 680 | if ((jitter_data_count = ntohs(req.count)) >= sizeof(jitter_data)) { | ||
| 681 | die(STATE_UNKNOWN, _("jitter response too large (%lu bytes)\n"), | ||
| 682 | (unsigned long)jitter_data_count); | ||
| 683 | } | ||
| 684 | memcpy(jitter_data, req.data, jitter_data_count); | ||
| 685 | jitter_data[jitter_data_count] = '\0'; | ||
| 686 | startofvalue = strchr(jitter_data, '='); | ||
| 687 | if (startofvalue != NULL) { | ||
| 688 | startofvalue++; | ||
| 689 | jitter = strtod(startofvalue, &nptr); | ||
| 690 | } | ||
| 691 | if (startofvalue == NULL || startofvalue == nptr) { | ||
| 692 | printf("warning: unable to read server jitter response.\n"); | ||
| 693 | *status = STATE_UNKNOWN; | ||
| 694 | } else { | ||
| 695 | if (verbose) { | ||
| 696 | printf("%g\n", jitter); | ||
| 697 | } | ||
| 698 | num_valid++; | ||
| 699 | rval += jitter; | ||
| 700 | } | ||
| 701 | } | ||
| 702 | } | ||
| 703 | if (verbose) { | ||
| 704 | printf("jitter parsed from %d/%d peers\n", num_valid, num_selected); | ||
| 705 | } | ||
| 706 | } | ||
| 707 | |||
| 708 | rval = num_valid ? rval / num_valid : -1.0; | ||
| 709 | |||
| 710 | close(conn); | ||
| 711 | if (peers != NULL) { | ||
| 712 | free(peers); | ||
| 713 | } | ||
| 714 | /* If we return -1.0, it means no synchronization source was found */ | ||
| 715 | return rval; | ||
| 716 | } | ||
| 717 | |||
| 718 | int process_arguments(int argc, char **argv) { | ||
| 719 | int c; | ||
| 720 | int option = 0; | ||
| 721 | static struct option longopts[] = { | ||
| 722 | {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, | ||
| 723 | {"verbose", no_argument, 0, 'v'}, {"use-ipv4", no_argument, 0, '4'}, | ||
| 724 | {"use-ipv6", no_argument, 0, '6'}, {"warning", required_argument, 0, 'w'}, | ||
| 725 | {"critical", required_argument, 0, 'c'}, {"jwarn", required_argument, 0, 'j'}, | ||
| 726 | {"jcrit", required_argument, 0, 'k'}, {"timeout", required_argument, 0, 't'}, | ||
| 727 | {"hostname", required_argument, 0, 'H'}, {0, 0, 0, 0}}; | ||
| 728 | |||
| 729 | if (argc < 2) { | ||
| 730 | usage("\n"); | ||
| 731 | } | ||
| 732 | |||
| 733 | while (1) { | ||
| 734 | c = getopt_long(argc, argv, "Vhv46w:c:j:k:t:H:", longopts, &option); | ||
| 735 | if (c == -1 || c == EOF || c == 1) { | ||
| 736 | break; | ||
| 737 | } | ||
| 738 | |||
| 739 | switch (c) { | ||
| 740 | case 'h': | ||
| 741 | print_help(); | ||
| 742 | exit(STATE_UNKNOWN); | ||
| 743 | break; | ||
| 744 | case 'V': | ||
| 745 | print_revision(progname, NP_VERSION); | ||
| 746 | exit(STATE_UNKNOWN); | ||
| 747 | break; | ||
| 748 | case 'v': | ||
| 749 | verbose++; | ||
| 750 | break; | ||
| 751 | case 'w': | ||
| 752 | do_offset = true; | ||
| 753 | owarn = optarg; | ||
| 754 | break; | ||
| 755 | case 'c': | ||
| 756 | do_offset = true; | ||
| 757 | ocrit = optarg; | ||
| 758 | break; | ||
| 759 | case 'j': | ||
| 760 | do_jitter = true; | ||
| 761 | jwarn = optarg; | ||
| 762 | break; | ||
| 763 | case 'k': | ||
| 764 | do_jitter = true; | ||
| 765 | jcrit = optarg; | ||
| 766 | break; | ||
| 767 | case 'H': | ||
| 768 | if (!is_host(optarg)) { | ||
| 769 | usage2(_("Invalid hostname/address"), optarg); | ||
| 770 | } | ||
| 771 | server_address = strdup(optarg); | ||
| 772 | break; | ||
| 773 | case 't': | ||
| 774 | socket_timeout = atoi(optarg); | ||
| 775 | break; | ||
| 776 | case '4': | ||
| 777 | address_family = AF_INET; | ||
| 778 | break; | ||
| 779 | case '6': | ||
| 780 | #ifdef USE_IPV6 | ||
| 781 | address_family = AF_INET6; | ||
| 782 | #else | ||
| 783 | usage4(_("IPv6 support not available")); | ||
| 784 | #endif | ||
| 785 | break; | ||
| 786 | case '?': | ||
| 787 | /* print short usage statement if args not parsable */ | ||
| 788 | usage5(); | ||
| 789 | break; | ||
| 790 | } | ||
| 791 | } | ||
| 792 | |||
| 793 | if (server_address == NULL) { | ||
| 794 | usage4(_("Hostname was not supplied")); | ||
| 795 | } | ||
| 796 | |||
| 797 | return 0; | ||
| 798 | } | ||
| 799 | |||
| 800 | char *perfd_offset(double offset) { | ||
| 801 | return fperfdata("offset", offset, "s", true, offset_thresholds->warning->end, true, | ||
| 802 | offset_thresholds->critical->end, false, 0, false, 0); | ||
| 803 | } | ||
| 804 | |||
| 805 | char *perfd_jitter(double jitter) { | ||
| 806 | return fperfdata("jitter", jitter, "s", do_jitter, jitter_thresholds->warning->end, do_jitter, | ||
| 807 | jitter_thresholds->critical->end, true, 0, false, 0); | ||
| 808 | } | ||
| 809 | |||
| 810 | int main(int argc, char *argv[]) { | ||
| 811 | int result, offset_result, jitter_result; | ||
| 812 | double offset = 0, jitter = 0; | ||
| 813 | char *result_line, *perfdata_line; | ||
| 814 | |||
| 815 | setlocale(LC_ALL, ""); | ||
| 816 | bindtextdomain(PACKAGE, LOCALEDIR); | ||
| 817 | textdomain(PACKAGE); | ||
| 818 | |||
| 819 | result = offset_result = jitter_result = STATE_OK; | ||
| 820 | |||
| 821 | /* Parse extra opts if any */ | ||
| 822 | argv = np_extra_opts(&argc, argv, progname); | ||
| 823 | |||
| 824 | if (process_arguments(argc, argv) == ERROR) { | ||
| 825 | usage4(_("Could not parse arguments")); | ||
| 826 | } | ||
| 827 | |||
| 828 | set_thresholds(&offset_thresholds, owarn, ocrit); | ||
| 829 | set_thresholds(&jitter_thresholds, jwarn, jcrit); | ||
| 830 | |||
| 831 | /* initialize alarm signal handling */ | ||
| 832 | signal(SIGALRM, socket_timeout_alarm_handler); | ||
| 833 | |||
| 834 | /* set socket timeout */ | ||
| 835 | alarm(socket_timeout); | ||
| 836 | |||
| 837 | offset = offset_request(server_address, &offset_result); | ||
| 838 | /* check_ntp used to always return CRITICAL if offset_result == STATE_UNKNOWN. | ||
| 839 | * Now we'll only do that is the offset thresholds were set */ | ||
| 840 | if (do_offset && offset_result == STATE_UNKNOWN) { | ||
| 841 | result = STATE_CRITICAL; | ||
| 842 | } else { | ||
| 843 | result = get_status(fabs(offset), offset_thresholds); | ||
| 844 | } | ||
| 845 | |||
| 846 | /* If not told to check the jitter, we don't even send packets. | ||
| 847 | * jitter is checked using NTP control packets, which not all | ||
| 848 | * servers recognize. Trying to check the jitter on OpenNTPD | ||
| 849 | * (for example) will result in an error | ||
| 850 | */ | ||
| 851 | if (do_jitter) { | ||
| 852 | jitter = jitter_request(&jitter_result); | ||
| 853 | result = max_state_alt(result, get_status(jitter, jitter_thresholds)); | ||
| 854 | /* -1 indicates that we couldn't calculate the jitter | ||
| 855 | * Only overrides STATE_OK from the offset */ | ||
| 856 | if (jitter == -1.0 && result == STATE_OK) { | ||
| 857 | result = STATE_UNKNOWN; | ||
| 858 | } | ||
| 859 | } | ||
| 860 | result = max_state_alt(result, jitter_result); | ||
| 861 | |||
| 862 | switch (result) { | ||
| 863 | case STATE_CRITICAL: | ||
| 864 | xasprintf(&result_line, _("NTP CRITICAL:")); | ||
| 865 | break; | ||
| 866 | case STATE_WARNING: | ||
| 867 | xasprintf(&result_line, _("NTP WARNING:")); | ||
| 868 | break; | ||
| 869 | case STATE_OK: | ||
| 870 | xasprintf(&result_line, _("NTP OK:")); | ||
| 871 | break; | ||
| 872 | default: | ||
| 873 | xasprintf(&result_line, _("NTP UNKNOWN:")); | ||
| 874 | break; | ||
| 875 | } | ||
| 876 | if (offset_result == STATE_UNKNOWN) { | ||
| 877 | xasprintf(&result_line, "%s %s", result_line, _("Offset unknown")); | ||
| 878 | xasprintf(&perfdata_line, ""); | ||
| 879 | } else { | ||
| 880 | xasprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset); | ||
| 881 | xasprintf(&perfdata_line, "%s", perfd_offset(offset)); | ||
| 882 | } | ||
| 883 | if (do_jitter) { | ||
| 884 | xasprintf(&result_line, "%s, jitter=%f", result_line, jitter); | ||
| 885 | xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_jitter(jitter)); | ||
| 886 | } | ||
| 887 | printf("%s|%s\n", result_line, perfdata_line); | ||
| 888 | |||
| 889 | if (server_address != NULL) { | ||
| 890 | free(server_address); | ||
| 891 | } | ||
| 892 | return result; | ||
| 893 | } | ||
| 894 | |||
| 895 | void print_help(void) { | ||
| 896 | print_revision(progname, NP_VERSION); | ||
| 897 | |||
| 898 | printf("Copyright (c) 2006 Sean Finney\n"); | ||
| 899 | printf(COPYRIGHT, copyright, email); | ||
| 900 | |||
| 901 | printf("%s\n", _("This plugin checks the selected ntp server")); | ||
| 902 | |||
| 903 | printf("\n\n"); | ||
| 904 | |||
| 905 | print_usage(); | ||
| 906 | printf(UT_HELP_VRSN); | ||
| 907 | printf(UT_EXTRA_OPTS); | ||
| 908 | printf(UT_HOST_PORT, 'p', "123"); | ||
| 909 | printf(UT_IPv46); | ||
| 910 | printf(" %s\n", "-w, --warning=THRESHOLD"); | ||
| 911 | printf(" %s\n", _("Offset to result in warning status (seconds)")); | ||
| 912 | printf(" %s\n", "-c, --critical=THRESHOLD"); | ||
| 913 | printf(" %s\n", _("Offset to result in critical status (seconds)")); | ||
| 914 | printf(" %s\n", "-j, --jwarn=THRESHOLD"); | ||
| 915 | printf(" %s\n", _("Warning threshold for jitter")); | ||
| 916 | printf(" %s\n", "-k, --jcrit=THRESHOLD"); | ||
| 917 | printf(" %s\n", _("Critical threshold for jitter")); | ||
| 918 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | ||
| 919 | printf(UT_VERBOSE); | ||
| 920 | |||
| 921 | printf("\n"); | ||
| 922 | printf("%s\n", _("Notes:")); | ||
| 923 | printf(UT_THRESHOLDS_NOTES); | ||
| 924 | |||
| 925 | printf("\n"); | ||
| 926 | printf("%s\n", _("Examples:")); | ||
| 927 | printf(" %s\n", _("Normal offset check:")); | ||
| 928 | printf(" %s\n", ("./check_ntp -H ntpserv -w 0.5 -c 1")); | ||
| 929 | printf("\n"); | ||
| 930 | printf(" %s\n", | ||
| 931 | _("Check jitter too, avoiding critical notifications if jitter isn't available")); | ||
| 932 | printf(" %s\n", _("(See Notes above for more details on thresholds formats):")); | ||
| 933 | printf(" %s\n", ("./check_ntp -H ntpserv -w 0.5 -c 1 -j -1:100 -k -1:200")); | ||
| 934 | |||
| 935 | printf(UT_SUPPORT); | ||
| 936 | |||
| 937 | printf("%s\n", _("WARNING: check_ntp is deprecated. Please use check_ntp_peer or")); | ||
| 938 | printf("%s\n\n", _("check_ntp_time instead.")); | ||
| 939 | } | ||
| 940 | |||
| 941 | void print_usage(void) { | ||
| 942 | printf("%s\n", _("WARNING: check_ntp is deprecated. Please use check_ntp_peer or")); | ||
| 943 | printf("%s\n\n", _("check_ntp_time instead.")); | ||
| 944 | printf("%s\n", _("Usage:")); | ||
| 945 | printf(" %s -H <host> [-w <warn>] [-c <crit>] [-j <warn>] [-k <crit>] [-4|-6] [-v verbose]\n", | ||
| 946 | progname); | ||
| 947 | } | ||
