diff options
Diffstat (limited to 'plugins/check_ntp_time.c')
| -rw-r--r-- | plugins/check_ntp_time.c | 351 |
1 files changed, 210 insertions, 141 deletions
diff --git a/plugins/check_ntp_time.c b/plugins/check_ntp_time.c index 31162883..9e0beb9c 100644 --- a/plugins/check_ntp_time.c +++ b/plugins/check_ntp_time.c | |||
| @@ -34,19 +34,23 @@ | |||
| 34 | * | 34 | * |
| 35 | *****************************************************************************/ | 35 | *****************************************************************************/ |
| 36 | 36 | ||
| 37 | const char *progname = "check_ntp_time"; | 37 | #include "output.h" |
| 38 | const char *copyright = "2006-2024"; | ||
| 39 | const char *email = "devel@monitoring-plugins.org"; | ||
| 40 | |||
| 41 | #include "common.h" | 38 | #include "common.h" |
| 42 | #include "netutils.h" | 39 | #include "netutils.h" |
| 40 | #include "perfdata.h" | ||
| 43 | #include "utils.h" | 41 | #include "utils.h" |
| 44 | #include "states.h" | 42 | #include "states.h" |
| 45 | #include "thresholds.h" | 43 | #include "thresholds.h" |
| 46 | #include "check_ntp_time.d/config.h" | 44 | #include "check_ntp_time.d/config.h" |
| 45 | #include <netinet/in.h> | ||
| 46 | #include <sys/socket.h> | ||
| 47 | 47 | ||
| 48 | static int verbose = 0; | 48 | static int verbose = 0; |
| 49 | 49 | ||
| 50 | const char *progname = "check_ntp_time"; | ||
| 51 | const char *copyright = "2006-2024"; | ||
| 52 | const char *email = "devel@monitoring-plugins.org"; | ||
| 53 | |||
| 50 | typedef struct { | 54 | typedef struct { |
| 51 | int errorcode; | 55 | int errorcode; |
| 52 | check_ntp_time_config config; | 56 | check_ntp_time_config config; |
| @@ -61,9 +65,6 @@ void print_usage(void); | |||
| 61 | # define AVG_NUM 4 | 65 | # define AVG_NUM 4 |
| 62 | #endif | 66 | #endif |
| 63 | 67 | ||
| 64 | /* max size of control message data */ | ||
| 65 | #define MAX_CM_SIZE 468 | ||
| 66 | |||
| 67 | /* this structure holds everything in an ntp request/response as per rfc1305 */ | 68 | /* this structure holds everything in an ntp request/response as per rfc1305 */ |
| 68 | typedef struct { | 69 | typedef struct { |
| 69 | uint8_t flags; /* byte with leapindicator,vers,mode. see macros */ | 70 | uint8_t flags; /* byte with leapindicator,vers,mode. see macros */ |
| @@ -93,9 +94,9 @@ typedef struct { | |||
| 93 | /* bits 1,2 are the leap indicator */ | 94 | /* bits 1,2 are the leap indicator */ |
| 94 | #define LI_MASK 0xc0 | 95 | #define LI_MASK 0xc0 |
| 95 | #define LI(x) ((x & LI_MASK) >> 6) | 96 | #define LI(x) ((x & LI_MASK) >> 6) |
| 96 | #define LI_SET(x, y) \ | 97 | #define LI_SET(x, y) \ |
| 97 | do { \ | 98 | do { \ |
| 98 | x |= ((y << 6) & LI_MASK); \ | 99 | x |= ((y << 6) & LI_MASK); \ |
| 99 | } while (0) | 100 | } while (0) |
| 100 | /* and these are the values of the leap indicator */ | 101 | /* and these are the values of the leap indicator */ |
| 101 | #define LI_NOWARNING 0x00 | 102 | #define LI_NOWARNING 0x00 |
| @@ -105,17 +106,17 @@ typedef struct { | |||
| 105 | /* bits 3,4,5 are the ntp version */ | 106 | /* bits 3,4,5 are the ntp version */ |
| 106 | #define VN_MASK 0x38 | 107 | #define VN_MASK 0x38 |
| 107 | #define VN(x) ((x & VN_MASK) >> 3) | 108 | #define VN(x) ((x & VN_MASK) >> 3) |
| 108 | #define VN_SET(x, y) \ | 109 | #define VN_SET(x, y) \ |
| 109 | do { \ | 110 | do { \ |
| 110 | x |= ((y << 3) & VN_MASK); \ | 111 | x |= ((y << 3) & VN_MASK); \ |
| 111 | } while (0) | 112 | } while (0) |
| 112 | #define VN_RESERVED 0x02 | 113 | #define VN_RESERVED 0x02 |
| 113 | /* bits 6,7,8 are the ntp mode */ | 114 | /* bits 6,7,8 are the ntp mode */ |
| 114 | #define MODE_MASK 0x07 | 115 | #define MODE_MASK 0x07 |
| 115 | #define MODE(x) (x & MODE_MASK) | 116 | #define MODE(x) (x & MODE_MASK) |
| 116 | #define MODE_SET(x, y) \ | 117 | #define MODE_SET(x, y) \ |
| 117 | do { \ | 118 | do { \ |
| 118 | x |= (y & MODE_MASK); \ | 119 | x |= (y & MODE_MASK); \ |
| 119 | } while (0) | 120 | } while (0) |
| 120 | /* here are some values */ | 121 | /* here are some values */ |
| 121 | #define MODE_CLIENT 0x03 | 122 | #define MODE_CLIENT 0x03 |
| @@ -127,9 +128,9 @@ typedef struct { | |||
| 127 | #define REM_MORE 0x20 | 128 | #define REM_MORE 0x20 |
| 128 | /* In control message, bits 11 - 15 are opcode */ | 129 | /* In control message, bits 11 - 15 are opcode */ |
| 129 | #define OP_MASK 0x1f | 130 | #define OP_MASK 0x1f |
| 130 | #define OP_SET(x, y) \ | 131 | #define OP_SET(x, y) \ |
| 131 | do { \ | 132 | do { \ |
| 132 | x |= (y & OP_MASK); \ | 133 | x |= (y & OP_MASK); \ |
| 133 | } while (0) | 134 | } while (0) |
| 134 | #define OP_READSTAT 0x01 | 135 | #define OP_READSTAT 0x01 |
| 135 | #define OP_READVAR 0x02 | 136 | #define OP_READVAR 0x02 |
| @@ -163,32 +164,36 @@ typedef struct { | |||
| 163 | #define NTP32asDOUBLE(x) (ntohs(L16(x)) + ((double)ntohs(R16(x)) / 65536.0)) | 164 | #define NTP32asDOUBLE(x) (ntohs(L16(x)) + ((double)ntohs(R16(x)) / 65536.0)) |
| 164 | 165 | ||
| 165 | /* likewise for a 64-bit ntp fp number */ | 166 | /* likewise for a 64-bit ntp fp number */ |
| 166 | #define NTP64asDOUBLE(n) \ | 167 | #define NTP64asDOUBLE(n) \ |
| 167 | (double)(((uint64_t)n) ? (ntohl(L32(n)) - EPOCHDIFF) + (.00000001 * (0.5 + (double)(ntohl(R32(n)) / 42.94967296))) : 0) | 168 | (double)(((uint64_t)n) ? (ntohl(L32(n)) - EPOCHDIFF) + \ |
| 169 | (.00000001 * (0.5 + (double)(ntohl(R32(n)) / 42.94967296))) \ | ||
| 170 | : 0) | ||
| 168 | 171 | ||
| 169 | /* convert a struct timeval to a double */ | 172 | /* convert a struct timeval to a double */ |
| 170 | #define TVasDOUBLE(x) (double)(x.tv_sec + (0.000001 * x.tv_usec)) | 173 | static double TVasDOUBLE(struct timeval time) { |
| 174 | return ((double)time.tv_sec + (0.000001 * (double)time.tv_usec)); | ||
| 175 | } | ||
| 171 | 176 | ||
| 172 | /* convert an ntp 64-bit fp number to a struct timeval */ | 177 | /* convert an ntp 64-bit fp number to a struct timeval */ |
| 173 | #define NTP64toTV(n, t) \ | 178 | #define NTP64toTV(n, t) \ |
| 174 | do { \ | 179 | do { \ |
| 175 | if (!n) \ | 180 | if (!n) \ |
| 176 | t.tv_sec = t.tv_usec = 0; \ | 181 | t.tv_sec = t.tv_usec = 0; \ |
| 177 | else { \ | 182 | else { \ |
| 178 | t.tv_sec = ntohl(L32(n)) - EPOCHDIFF; \ | 183 | t.tv_sec = ntohl(L32(n)) - EPOCHDIFF; \ |
| 179 | t.tv_usec = (int)(0.5 + (double)(ntohl(R32(n)) / 4294.967296)); \ | 184 | t.tv_usec = (int)(0.5 + (double)(ntohl(R32(n)) / 4294.967296)); \ |
| 180 | } \ | 185 | } \ |
| 181 | } while (0) | 186 | } while (0) |
| 182 | 187 | ||
| 183 | /* convert a struct timeval to an ntp 64-bit fp number */ | 188 | /* convert a struct timeval to an ntp 64-bit fp number */ |
| 184 | #define TVtoNTP64(t, n) \ | 189 | #define TVtoNTP64(t, n) \ |
| 185 | do { \ | 190 | do { \ |
| 186 | if (!t.tv_usec && !t.tv_sec) \ | 191 | if (!t.tv_usec && !t.tv_sec) \ |
| 187 | n = 0x0UL; \ | 192 | n = 0x0UL; \ |
| 188 | else { \ | 193 | else { \ |
| 189 | L32(n) = htonl(t.tv_sec + EPOCHDIFF); \ | 194 | L32(n) = htonl(t.tv_sec + EPOCHDIFF); \ |
| 190 | R32(n) = htonl((uint64_t)((4294.967296 * t.tv_usec) + .5)); \ | 195 | R32(n) = htonl((uint64_t)((4294.967296 * t.tv_usec) + .5)); \ |
| 191 | } \ | 196 | } \ |
| 192 | } while (0) | 197 | } while (0) |
| 193 | 198 | ||
| 194 | /* NTP control message header is 12 bytes, plus any data in the data | 199 | /* NTP control message header is 12 bytes, plus any data in the data |
| @@ -197,15 +202,15 @@ typedef struct { | |||
| 197 | #define SIZEOF_NTPCM(m) (12 + ntohs(m.count) + ((m.count) ? 4 - (ntohs(m.count) % 4) : 0)) | 202 | #define SIZEOF_NTPCM(m) (12 + ntohs(m.count) + ((m.count) ? 4 - (ntohs(m.count) % 4) : 0)) |
| 198 | 203 | ||
| 199 | /* finally, a little helper or two for debugging: */ | 204 | /* finally, a little helper or two for debugging: */ |
| 200 | #define DBG(x) \ | 205 | #define DBG(x) \ |
| 201 | do { \ | 206 | do { \ |
| 202 | if (verbose > 1) { \ | 207 | if (verbose > 1) { \ |
| 203 | x; \ | 208 | x; \ |
| 204 | } \ | 209 | } \ |
| 205 | } while (0); | 210 | } while (0); |
| 206 | #define PRINTSOCKADDR(x) \ | 211 | #define PRINTSOCKADDR(x) \ |
| 207 | do { \ | 212 | do { \ |
| 208 | printf("%u.%u.%u.%u", (x >> 24) & 0xff, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff); \ | 213 | printf("%u.%u.%u.%u", (x >> 24) & 0xff, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff); \ |
| 209 | } while (0); | 214 | } while (0); |
| 210 | 215 | ||
| 211 | /* calculate the offset of the local clock */ | 216 | /* calculate the offset of the local clock */ |
| @@ -260,8 +265,8 @@ void setup_request(ntp_message *message) { | |||
| 260 | /* select the "best" server from a list of servers, and return its index. | 265 | /* select the "best" server from a list of servers, and return its index. |
| 261 | * this is done by filtering servers based on stratum, dispersion, and | 266 | * this is done by filtering servers based on stratum, dispersion, and |
| 262 | * finally round-trip delay. */ | 267 | * finally round-trip delay. */ |
| 263 | int best_offset_server(const ntp_server_results *slist, int nservers) { | 268 | static int best_offset_server(const ntp_server_results *slist, int nservers) { |
| 264 | int best_server = -1; | 269 | int best_server_index = -1; |
| 265 | 270 | ||
| 266 | /* for each server */ | 271 | /* for each server */ |
| 267 | for (int cserver = 0; cserver < nservers; cserver++) { | 272 | for (int cserver = 0; cserver < nservers; cserver++) { |
| @@ -284,33 +289,33 @@ int best_offset_server(const ntp_server_results *slist, int nservers) { | |||
| 284 | } | 289 | } |
| 285 | 290 | ||
| 286 | /* If we don't have a server yet, use the first one */ | 291 | /* If we don't have a server yet, use the first one */ |
| 287 | if (best_server == -1) { | 292 | if (best_server_index == -1) { |
| 288 | best_server = cserver; | 293 | best_server_index = cserver; |
| 289 | DBG(printf("using peer %d as our first candidate\n", best_server)); | 294 | DBG(printf("using peer %d as our first candidate\n", best_server_index)); |
| 290 | continue; | 295 | continue; |
| 291 | } | 296 | } |
| 292 | 297 | ||
| 293 | /* compare the server to the best one we've seen so far */ | 298 | /* compare the server to the best one we've seen so far */ |
| 294 | /* does it have an equal or better stratum? */ | 299 | /* does it have an equal or better stratum? */ |
| 295 | DBG(printf("comparing peer %d with peer %d\n", cserver, best_server)); | 300 | DBG(printf("comparing peer %d with peer %d\n", cserver, best_server_index)); |
| 296 | if (slist[cserver].stratum <= slist[best_server].stratum) { | 301 | if (slist[cserver].stratum <= slist[best_server_index].stratum) { |
| 297 | DBG(printf("stratum for peer %d <= peer %d\n", cserver, best_server)); | 302 | DBG(printf("stratum for peer %d <= peer %d\n", cserver, best_server_index)); |
| 298 | /* does it have an equal or better dispersion? */ | 303 | /* does it have an equal or better dispersion? */ |
| 299 | if (slist[cserver].rtdisp <= slist[best_server].rtdisp) { | 304 | if (slist[cserver].rtdisp <= slist[best_server_index].rtdisp) { |
| 300 | DBG(printf("dispersion for peer %d <= peer %d\n", cserver, best_server)); | 305 | DBG(printf("dispersion for peer %d <= peer %d\n", cserver, best_server_index)); |
| 301 | /* does it have a better rtdelay? */ | 306 | /* does it have a better rtdelay? */ |
| 302 | if (slist[cserver].rtdelay < slist[best_server].rtdelay) { | 307 | if (slist[cserver].rtdelay < slist[best_server_index].rtdelay) { |
| 303 | DBG(printf("rtdelay for peer %d < peer %d\n", cserver, best_server)); | 308 | DBG(printf("rtdelay for peer %d < peer %d\n", cserver, best_server_index)); |
| 304 | best_server = cserver; | 309 | best_server_index = cserver; |
| 305 | DBG(printf("peer %d is now our best candidate\n", best_server)); | 310 | DBG(printf("peer %d is now our best candidate\n", best_server_index)); |
| 306 | } | 311 | } |
| 307 | } | 312 | } |
| 308 | } | 313 | } |
| 309 | } | 314 | } |
| 310 | 315 | ||
| 311 | if (best_server >= 0) { | 316 | if (best_server_index >= 0) { |
| 312 | DBG(printf("best server selected: peer %d\n", best_server)); | 317 | DBG(printf("best server selected: peer %d\n", best_server_index)); |
| 313 | return best_server; | 318 | return best_server_index; |
| 314 | } | 319 | } |
| 315 | DBG(printf("no peers meeting synchronization criteria :(\n")); | 320 | DBG(printf("no peers meeting synchronization criteria :(\n")); |
| 316 | return -1; | 321 | return -1; |
| @@ -321,7 +326,11 @@ int best_offset_server(const ntp_server_results *slist, int nservers) { | |||
| 321 | * we don't waste time sitting around waiting for single packets. | 326 | * we don't waste time sitting around waiting for single packets. |
| 322 | * - we also "manually" handle resolving host names and connecting, because | 327 | * - we also "manually" handle resolving host names and connecting, because |
| 323 | * we have to do it in a way that our lazy macros don't handle currently :( */ | 328 | * we have to do it in a way that our lazy macros don't handle currently :( */ |
| 324 | double offset_request(const char *host, const char *port, mp_state_enum *status, int time_offset) { | 329 | typedef struct { |
| 330 | mp_state_enum offset_result; | ||
| 331 | double offset; | ||
| 332 | } offset_request_wrapper; | ||
| 333 | static offset_request_wrapper offset_request(const char *host, const char *port, int time_offset) { | ||
| 325 | /* setup hints to only return results from getaddrinfo that we'd like */ | 334 | /* setup hints to only return results from getaddrinfo that we'd like */ |
| 326 | struct addrinfo hints; | 335 | struct addrinfo hints; |
| 327 | memset(&hints, 0, sizeof(struct addrinfo)); | 336 | memset(&hints, 0, sizeof(struct addrinfo)); |
| @@ -329,17 +338,25 @@ double offset_request(const char *host, const char *port, mp_state_enum *status, | |||
| 329 | hints.ai_protocol = IPPROTO_UDP; | 338 | hints.ai_protocol = IPPROTO_UDP; |
| 330 | hints.ai_socktype = SOCK_DGRAM; | 339 | hints.ai_socktype = SOCK_DGRAM; |
| 331 | 340 | ||
| 332 | /* fill in ai with the list of hosts resolved by the host name */ | 341 | bool is_socket; |
| 333 | struct addrinfo *addresses = NULL; | 342 | struct addrinfo *addresses = NULL; |
| 334 | int ga_result = getaddrinfo(host, port, &hints, &addresses); | ||
| 335 | if (ga_result != 0) { | ||
| 336 | die(STATE_UNKNOWN, "error getting address for %s: %s\n", host, gai_strerror(ga_result)); | ||
| 337 | } | ||
| 338 | |||
| 339 | /* count the number of returned hosts, and allocate stuff accordingly */ | ||
| 340 | size_t num_hosts = 0; | 343 | size_t num_hosts = 0; |
| 341 | for (struct addrinfo *ai_tmp = addresses; ai_tmp != NULL; ai_tmp = ai_tmp->ai_next) { | 344 | if (host[0] == '/') { |
| 342 | num_hosts++; | 345 | num_hosts = 1; |
| 346 | is_socket = true; | ||
| 347 | } else { | ||
| 348 | is_socket = false; | ||
| 349 | |||
| 350 | /* fill in ai with the list of hosts resolved by the host name */ | ||
| 351 | int ga_result = getaddrinfo(host, port, &hints, &addresses); | ||
| 352 | if (ga_result != 0) { | ||
| 353 | die(STATE_UNKNOWN, "error getting address for %s: %s\n", host, gai_strerror(ga_result)); | ||
| 354 | } | ||
| 355 | |||
| 356 | /* count the number of returned hosts, and allocate stuff accordingly */ | ||
| 357 | for (struct addrinfo *ai_tmp = addresses; ai_tmp != NULL; ai_tmp = ai_tmp->ai_next) { | ||
| 358 | num_hosts++; | ||
| 359 | } | ||
| 343 | } | 360 | } |
| 344 | 361 | ||
| 345 | ntp_message *req = (ntp_message *)malloc(sizeof(ntp_message) * num_hosts); | 362 | ntp_message *req = (ntp_message *)malloc(sizeof(ntp_message) * num_hosts); |
| @@ -358,7 +375,8 @@ double offset_request(const char *host, const char *port, mp_state_enum *status, | |||
| 358 | die(STATE_UNKNOWN, "can not allocate socket array"); | 375 | die(STATE_UNKNOWN, "can not allocate socket array"); |
| 359 | } | 376 | } |
| 360 | 377 | ||
| 361 | ntp_server_results *servers = (ntp_server_results *)malloc(sizeof(ntp_server_results) * num_hosts); | 378 | ntp_server_results *servers = |
| 379 | (ntp_server_results *)malloc(sizeof(ntp_server_results) * num_hosts); | ||
| 362 | if (servers == NULL) { | 380 | if (servers == NULL) { |
| 363 | die(STATE_UNKNOWN, "can not allocate server array"); | 381 | die(STATE_UNKNOWN, "can not allocate server array"); |
| 364 | } | 382 | } |
| @@ -366,25 +384,51 @@ double offset_request(const char *host, const char *port, mp_state_enum *status, | |||
| 366 | DBG(printf("Found %zu peers to check\n", num_hosts)); | 384 | DBG(printf("Found %zu peers to check\n", num_hosts)); |
| 367 | 385 | ||
| 368 | /* setup each socket for writing, and the corresponding struct pollfd */ | 386 | /* setup each socket for writing, and the corresponding struct pollfd */ |
| 369 | struct addrinfo *ai_tmp = addresses; | 387 | if (is_socket) { |
| 370 | for (int i = 0; ai_tmp; i++) { | 388 | socklist[0] = socket(AF_UNIX, SOCK_STREAM, 0); |
| 371 | socklist[i] = socket(ai_tmp->ai_family, SOCK_DGRAM, IPPROTO_UDP); | 389 | if (socklist[0] == -1) { |
| 372 | if (socklist[i] == -1) { | 390 | DBG(printf("can't create socket: %s\n", strerror(errno))); |
| 373 | perror(NULL); | 391 | die(STATE_UNKNOWN, "can not create new socket\n"); |
| 374 | die(STATE_UNKNOWN, "can not create new socket"); | ||
| 375 | } | 392 | } |
| 376 | if (connect(socklist[i], ai_tmp->ai_addr, ai_tmp->ai_addrlen)) { | 393 | |
| 394 | struct sockaddr_un unix_socket = { | ||
| 395 | .sun_family = AF_UNIX, | ||
| 396 | }; | ||
| 397 | |||
| 398 | strncpy(unix_socket.sun_path, host, strlen(host)); | ||
| 399 | |||
| 400 | if (connect(socklist[0], &unix_socket, sizeof(unix_socket))) { | ||
| 377 | /* don't die here, because it is enough if there is one server | 401 | /* don't die here, because it is enough if there is one server |
| 378 | answering in time. This also would break for dual ipv4/6 stacked | 402 | answering in time. This also would break for dual ipv4/6 stacked |
| 379 | ntp servers when the client only supports on of them. | 403 | ntp servers when the client only supports on of them. |
| 380 | */ | 404 | */ |
| 381 | DBG(printf("can't create socket connection on peer %i: %s\n", i, strerror(errno))); | 405 | DBG(printf("can't create socket connection on peer %i: %s\n", 0, strerror(errno))); |
| 382 | } else { | 406 | } else { |
| 383 | ufds[i].fd = socklist[i]; | 407 | ufds[0].fd = socklist[0]; |
| 384 | ufds[i].events = POLLIN; | 408 | ufds[0].events = POLLIN; |
| 385 | ufds[i].revents = 0; | 409 | ufds[0].revents = 0; |
| 410 | } | ||
| 411 | } else { | ||
| 412 | struct addrinfo *ai_tmp = addresses; | ||
| 413 | for (int i = 0; ai_tmp; i++) { | ||
| 414 | socklist[i] = socket(ai_tmp->ai_family, SOCK_DGRAM, IPPROTO_UDP); | ||
| 415 | if (socklist[i] == -1) { | ||
| 416 | perror(NULL); | ||
| 417 | die(STATE_UNKNOWN, "can not create new socket"); | ||
| 418 | } | ||
| 419 | if (connect(socklist[i], ai_tmp->ai_addr, ai_tmp->ai_addrlen)) { | ||
| 420 | /* don't die here, because it is enough if there is one server | ||
| 421 | answering in time. This also would break for dual ipv4/6 stacked | ||
| 422 | ntp servers when the client only supports on of them. | ||
| 423 | */ | ||
| 424 | DBG(printf("can't create socket connection on peer %i: %s\n", i, strerror(errno))); | ||
| 425 | } else { | ||
| 426 | ufds[i].fd = socklist[i]; | ||
| 427 | ufds[i].events = POLLIN; | ||
| 428 | ufds[i].revents = 0; | ||
| 429 | } | ||
| 430 | ai_tmp = ai_tmp->ai_next; | ||
| 386 | } | 431 | } |
| 387 | ai_tmp = ai_tmp->ai_next; | ||
| 388 | } | 432 | } |
| 389 | 433 | ||
| 390 | /* now do AVG_NUM checks to each host. We stop before timeout/2 seconds | 434 | /* now do AVG_NUM checks to each host. We stop before timeout/2 seconds |
| @@ -459,12 +503,18 @@ double offset_request(const char *host, const char *port, mp_state_enum *status, | |||
| 459 | die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n"); | 503 | die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n"); |
| 460 | } | 504 | } |
| 461 | 505 | ||
| 506 | offset_request_wrapper result = { | ||
| 507 | .offset = 0, | ||
| 508 | .offset_result = STATE_UNKNOWN, | ||
| 509 | }; | ||
| 510 | |||
| 462 | /* now, pick the best server from the list */ | 511 | /* now, pick the best server from the list */ |
| 463 | double avg_offset = 0.; | 512 | double avg_offset = 0.; |
| 464 | int best_index = best_offset_server(servers, num_hosts); | 513 | int best_index = best_offset_server(servers, num_hosts); |
| 465 | if (best_index < 0) { | 514 | if (best_index < 0) { |
| 466 | *status = STATE_UNKNOWN; | 515 | result.offset_result = STATE_UNKNOWN; |
| 467 | } else { | 516 | } else { |
| 517 | result.offset_result = STATE_OK; | ||
| 468 | /* finally, calculate the average offset */ | 518 | /* finally, calculate the average offset */ |
| 469 | for (int i = 0; i < servers[best_index].num_responses; i++) { | 519 | for (int i = 0; i < servers[best_index].num_responses; i++) { |
| 470 | avg_offset += servers[best_index].offset[i]; | 520 | avg_offset += servers[best_index].offset[i]; |
| @@ -485,22 +535,30 @@ double offset_request(const char *host, const char *port, mp_state_enum *status, | |||
| 485 | if (verbose) { | 535 | if (verbose) { |
| 486 | printf("overall average offset: %.10g\n", avg_offset); | 536 | printf("overall average offset: %.10g\n", avg_offset); |
| 487 | } | 537 | } |
| 488 | return avg_offset; | 538 | |
| 539 | result.offset = avg_offset; | ||
| 540 | return result; | ||
| 489 | } | 541 | } |
| 490 | 542 | ||
| 491 | check_ntp_time_config_wrapper process_arguments(int argc, char **argv) { | 543 | static check_ntp_time_config_wrapper process_arguments(int argc, char **argv) { |
| 544 | |||
| 545 | enum { | ||
| 546 | output_format_index = CHAR_MAX + 1, | ||
| 547 | }; | ||
| 548 | |||
| 492 | static struct option longopts[] = {{"version", no_argument, 0, 'V'}, | 549 | static struct option longopts[] = {{"version", no_argument, 0, 'V'}, |
| 493 | {"help", no_argument, 0, 'h'}, | 550 | {"help", no_argument, 0, 'h'}, |
| 494 | {"verbose", no_argument, 0, 'v'}, | 551 | {"verbose", no_argument, 0, 'v'}, |
| 495 | {"use-ipv4", no_argument, 0, '4'}, | 552 | {"use-ipv4", no_argument, 0, '4'}, |
| 496 | {"use-ipv6", no_argument, 0, '6'}, | 553 | {"use-ipv6", no_argument, 0, '6'}, |
| 497 | {"quiet", no_argument, 0, 'q'}, | 554 | {"quiet", no_argument, 0, 'q'}, |
| 498 | {"time-offset", optional_argument, 0, 'o'}, | 555 | {"time-offset", required_argument, 0, 'o'}, |
| 499 | {"warning", required_argument, 0, 'w'}, | 556 | {"warning", required_argument, 0, 'w'}, |
| 500 | {"critical", required_argument, 0, 'c'}, | 557 | {"critical", required_argument, 0, 'c'}, |
| 501 | {"timeout", required_argument, 0, 't'}, | 558 | {"timeout", required_argument, 0, 't'}, |
| 502 | {"hostname", required_argument, 0, 'H'}, | 559 | {"hostname", required_argument, 0, 'H'}, |
| 503 | {"port", required_argument, 0, 'p'}, | 560 | {"port", required_argument, 0, 'p'}, |
| 561 | {"output-format", required_argument, 0, output_format_index}, | ||
| 504 | {0, 0, 0, 0}}; | 562 | {0, 0, 0, 0}}; |
| 505 | 563 | ||
| 506 | if (argc < 2) { | 564 | if (argc < 2) { |
| @@ -512,9 +570,6 @@ check_ntp_time_config_wrapper process_arguments(int argc, char **argv) { | |||
| 512 | .config = check_ntp_time_config_init(), | 570 | .config = check_ntp_time_config_init(), |
| 513 | }; | 571 | }; |
| 514 | 572 | ||
| 515 | char *owarn = "60"; | ||
| 516 | char *ocrit = "120"; | ||
| 517 | |||
| 518 | while (true) { | 573 | while (true) { |
| 519 | int option = 0; | 574 | int option = 0; |
| 520 | int option_char = getopt_long(argc, argv, "Vhv46qw:c:t:H:p:o:", longopts, &option); | 575 | int option_char = getopt_long(argc, argv, "Vhv46qw:c:t:H:p:o:", longopts, &option); |
| @@ -523,6 +578,17 @@ check_ntp_time_config_wrapper process_arguments(int argc, char **argv) { | |||
| 523 | } | 578 | } |
| 524 | 579 | ||
| 525 | switch (option_char) { | 580 | switch (option_char) { |
| 581 | case output_format_index: { | ||
| 582 | parsed_output_format parser = mp_parse_output_format(optarg); | ||
| 583 | if (!parser.parsing_success) { | ||
| 584 | printf("Invalid output format: %s\n", optarg); | ||
| 585 | exit(STATE_UNKNOWN); | ||
| 586 | } | ||
| 587 | |||
| 588 | result.config.output_format_is_set = true; | ||
| 589 | result.config.output_format = parser.output_format; | ||
| 590 | break; | ||
| 591 | } | ||
| 526 | case 'h': | 592 | case 'h': |
| 527 | print_help(); | 593 | print_help(); |
| 528 | exit(STATE_UNKNOWN); | 594 | exit(STATE_UNKNOWN); |
| @@ -537,14 +603,26 @@ check_ntp_time_config_wrapper process_arguments(int argc, char **argv) { | |||
| 537 | case 'q': | 603 | case 'q': |
| 538 | result.config.quiet = true; | 604 | result.config.quiet = true; |
| 539 | break; | 605 | break; |
| 540 | case 'w': | 606 | case 'w': { |
| 541 | owarn = optarg; | 607 | mp_range_parsed tmp = mp_parse_range_string(optarg); |
| 542 | break; | 608 | if (tmp.error != MP_PARSING_SUCCES) { |
| 543 | case 'c': | 609 | die(STATE_UNKNOWN, "failed to parse warning threshold"); |
| 544 | ocrit = optarg; | 610 | } |
| 545 | break; | 611 | |
| 612 | result.config.offset_thresholds = | ||
| 613 | mp_thresholds_set_warn(result.config.offset_thresholds, tmp.range); | ||
| 614 | } break; | ||
| 615 | case 'c': { | ||
| 616 | mp_range_parsed tmp = mp_parse_range_string(optarg); | ||
| 617 | if (tmp.error != MP_PARSING_SUCCES) { | ||
| 618 | die(STATE_UNKNOWN, "failed to parse crit threshold"); | ||
| 619 | } | ||
| 620 | |||
| 621 | result.config.offset_thresholds = | ||
| 622 | mp_thresholds_set_crit(result.config.offset_thresholds, tmp.range); | ||
| 623 | } break; | ||
| 546 | case 'H': | 624 | case 'H': |
| 547 | if (!is_host(optarg)) { | 625 | if (!is_host(optarg) && (optarg[0] != '/')) { |
| 548 | usage2(_("Invalid hostname/address"), optarg); | 626 | usage2(_("Invalid hostname/address"), optarg); |
| 549 | } | 627 | } |
| 550 | result.config.server_address = strdup(optarg); | 628 | result.config.server_address = strdup(optarg); |
| @@ -579,16 +657,9 @@ check_ntp_time_config_wrapper process_arguments(int argc, char **argv) { | |||
| 579 | usage4(_("Hostname was not supplied")); | 657 | usage4(_("Hostname was not supplied")); |
| 580 | } | 658 | } |
| 581 | 659 | ||
| 582 | set_thresholds(&result.config.offset_thresholds, owarn, ocrit); | ||
| 583 | |||
| 584 | return result; | 660 | return result; |
| 585 | } | 661 | } |
| 586 | 662 | ||
| 587 | char *perfd_offset(double offset, thresholds *offset_thresholds) { | ||
| 588 | return fperfdata("offset", offset, "s", true, offset_thresholds->warning->end, true, offset_thresholds->critical->end, false, 0, false, | ||
| 589 | 0); | ||
| 590 | } | ||
| 591 | |||
| 592 | int main(int argc, char *argv[]) { | 663 | int main(int argc, char *argv[]) { |
| 593 | setlocale(LC_ALL, ""); | 664 | setlocale(LC_ALL, ""); |
| 594 | bindtextdomain(PACKAGE, LOCALEDIR); | 665 | bindtextdomain(PACKAGE, LOCALEDIR); |
| @@ -605,51 +676,47 @@ int main(int argc, char *argv[]) { | |||
| 605 | 676 | ||
| 606 | const check_ntp_time_config config = tmp_config.config; | 677 | const check_ntp_time_config config = tmp_config.config; |
| 607 | 678 | ||
| 679 | if (config.output_format_is_set) { | ||
| 680 | mp_set_format(config.output_format); | ||
| 681 | } | ||
| 682 | |||
| 608 | /* initialize alarm signal handling */ | 683 | /* initialize alarm signal handling */ |
| 609 | signal(SIGALRM, socket_timeout_alarm_handler); | 684 | signal(SIGALRM, socket_timeout_alarm_handler); |
| 610 | 685 | ||
| 611 | /* set socket timeout */ | 686 | /* set socket timeout */ |
| 612 | alarm(socket_timeout); | 687 | alarm(socket_timeout); |
| 613 | 688 | ||
| 614 | mp_state_enum offset_result = STATE_OK; | 689 | mp_check overall = mp_check_init(); |
| 615 | mp_state_enum result = STATE_OK; | ||
| 616 | double offset = offset_request(config.server_address, config.port, &offset_result, config.time_offset); | ||
| 617 | if (offset_result == STATE_UNKNOWN) { | ||
| 618 | result = ((!config.quiet) ? STATE_UNKNOWN : STATE_CRITICAL); | ||
| 619 | } else { | ||
| 620 | result = get_status(fabs(offset), config.offset_thresholds); | ||
| 621 | } | ||
| 622 | 690 | ||
| 623 | char *result_line; | 691 | mp_subcheck sc_offset = mp_subcheck_init(); |
| 624 | switch (result) { | 692 | offset_request_wrapper offset_result = |
| 625 | case STATE_CRITICAL: | 693 | offset_request(config.server_address, config.port, config.time_offset); |
| 626 | xasprintf(&result_line, _("NTP CRITICAL:")); | ||
| 627 | break; | ||
| 628 | case STATE_WARNING: | ||
| 629 | xasprintf(&result_line, _("NTP WARNING:")); | ||
| 630 | break; | ||
| 631 | case STATE_OK: | ||
| 632 | xasprintf(&result_line, _("NTP OK:")); | ||
| 633 | break; | ||
| 634 | default: | ||
| 635 | xasprintf(&result_line, _("NTP UNKNOWN:")); | ||
| 636 | break; | ||
| 637 | } | ||
| 638 | 694 | ||
| 639 | char *perfdata_line; | 695 | if (offset_result.offset_result == STATE_UNKNOWN) { |
| 640 | if (offset_result == STATE_UNKNOWN) { | 696 | sc_offset = |
| 641 | xasprintf(&result_line, "%s %s", result_line, _("Offset unknown")); | 697 | mp_set_subcheck_state(sc_offset, (!config.quiet) ? STATE_UNKNOWN : STATE_CRITICAL); |
| 642 | xasprintf(&perfdata_line, ""); | 698 | xasprintf(&sc_offset.output, "Offset unknown"); |
| 643 | } else { | 699 | mp_add_subcheck_to_check(&overall, sc_offset); |
| 644 | xasprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset); | 700 | mp_exit(overall); |
| 645 | xasprintf(&perfdata_line, "%s", perfd_offset(offset, config.offset_thresholds)); | ||
| 646 | } | 701 | } |
| 647 | printf("%s|%s\n", result_line, perfdata_line); | 702 | |
| 703 | xasprintf(&sc_offset.output, "Offset: %.6fs", offset_result.offset); | ||
| 704 | |||
| 705 | mp_perfdata pd_offset = perfdata_init(); | ||
| 706 | pd_offset = mp_set_pd_value(pd_offset, fabs(offset_result.offset)); | ||
| 707 | pd_offset.label = "offset"; | ||
| 708 | pd_offset.uom = "s"; | ||
| 709 | pd_offset = mp_pd_set_thresholds(pd_offset, config.offset_thresholds); | ||
| 710 | |||
| 711 | sc_offset = mp_set_subcheck_state(sc_offset, mp_get_pd_status(pd_offset)); | ||
| 712 | |||
| 713 | mp_add_perfdata_to_subcheck(&sc_offset, pd_offset); | ||
| 714 | mp_add_subcheck_to_check(&overall, sc_offset); | ||
| 648 | 715 | ||
| 649 | if (config.server_address != NULL) { | 716 | if (config.server_address != NULL) { |
| 650 | free(config.server_address); | 717 | free(config.server_address); |
| 651 | } | 718 | } |
| 652 | exit(result); | 719 | mp_exit(overall); |
| 653 | } | 720 | } |
| 654 | 721 | ||
| 655 | void print_help(void) { | 722 | void print_help(void) { |
| @@ -673,10 +740,11 @@ void print_help(void) { | |||
| 673 | printf(" %s\n", _("Offset to result in warning status (seconds)")); | 740 | printf(" %s\n", _("Offset to result in warning status (seconds)")); |
| 674 | printf(" %s\n", "-c, --critical=THRESHOLD"); | 741 | printf(" %s\n", "-c, --critical=THRESHOLD"); |
| 675 | printf(" %s\n", _("Offset to result in critical status (seconds)")); | 742 | printf(" %s\n", _("Offset to result in critical status (seconds)")); |
| 676 | printf(" %s\n", "-o, --time_offset=INTEGER"); | 743 | printf(" %s\n", "-o, --time-offset=INTEGER"); |
| 677 | printf(" %s\n", _("Expected offset of the ntp server relative to local server (seconds)")); | 744 | printf(" %s\n", _("Expected offset of the ntp server relative to local server (seconds)")); |
| 678 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | 745 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); |
| 679 | printf(UT_VERBOSE); | 746 | printf(UT_VERBOSE); |
| 747 | printf(UT_OUTPUT_FORMAT); | ||
| 680 | 748 | ||
| 681 | printf("\n"); | 749 | printf("\n"); |
| 682 | printf("%s\n", _("This plugin checks the clock offset between the local host and a")); | 750 | printf("%s\n", _("This plugin checks the clock offset between the local host and a")); |
| @@ -701,5 +769,6 @@ void print_help(void) { | |||
| 701 | 769 | ||
| 702 | void print_usage(void) { | 770 | void print_usage(void) { |
| 703 | printf("%s\n", _("Usage:")); | 771 | printf("%s\n", _("Usage:")); |
| 704 | printf(" %s -H <host> [-4|-6] [-w <warn>] [-c <crit>] [-v verbose] [-o <time offset>]\n", progname); | 772 | printf(" %s -H <host> [-4|-6] [-w <warn>] [-c <crit>] [-v verbose] [-o <time offset>]\n", |
| 773 | progname); | ||
| 705 | } | 774 | } |
