summaryrefslogtreecommitdiffstats
path: root/plugins/check_ntp_time.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_ntp_time.c')
-rw-r--r--plugins/check_ntp_time.c91
1 files changed, 48 insertions, 43 deletions
diff --git a/plugins/check_ntp_time.c b/plugins/check_ntp_time.c
index 31162883..ad69b804 100644
--- a/plugins/check_ntp_time.c
+++ b/plugins/check_ntp_time.c
@@ -93,9 +93,9 @@ typedef struct {
93/* bits 1,2 are the leap indicator */ 93/* bits 1,2 are the leap indicator */
94#define LI_MASK 0xc0 94#define LI_MASK 0xc0
95#define LI(x) ((x & LI_MASK) >> 6) 95#define LI(x) ((x & LI_MASK) >> 6)
96#define LI_SET(x, y) \ 96#define LI_SET(x, y) \
97 do { \ 97 do { \
98 x |= ((y << 6) & LI_MASK); \ 98 x |= ((y << 6) & LI_MASK); \
99 } while (0) 99 } while (0)
100/* and these are the values of the leap indicator */ 100/* and these are the values of the leap indicator */
101#define LI_NOWARNING 0x00 101#define LI_NOWARNING 0x00
@@ -105,17 +105,17 @@ typedef struct {
105/* bits 3,4,5 are the ntp version */ 105/* bits 3,4,5 are the ntp version */
106#define VN_MASK 0x38 106#define VN_MASK 0x38
107#define VN(x) ((x & VN_MASK) >> 3) 107#define VN(x) ((x & VN_MASK) >> 3)
108#define VN_SET(x, y) \ 108#define VN_SET(x, y) \
109 do { \ 109 do { \
110 x |= ((y << 3) & VN_MASK); \ 110 x |= ((y << 3) & VN_MASK); \
111 } while (0) 111 } while (0)
112#define VN_RESERVED 0x02 112#define VN_RESERVED 0x02
113/* bits 6,7,8 are the ntp mode */ 113/* bits 6,7,8 are the ntp mode */
114#define MODE_MASK 0x07 114#define MODE_MASK 0x07
115#define MODE(x) (x & MODE_MASK) 115#define MODE(x) (x & MODE_MASK)
116#define MODE_SET(x, y) \ 116#define MODE_SET(x, y) \
117 do { \ 117 do { \
118 x |= (y & MODE_MASK); \ 118 x |= (y & MODE_MASK); \
119 } while (0) 119 } while (0)
120/* here are some values */ 120/* here are some values */
121#define MODE_CLIENT 0x03 121#define MODE_CLIENT 0x03
@@ -127,9 +127,9 @@ typedef struct {
127#define REM_MORE 0x20 127#define REM_MORE 0x20
128/* In control message, bits 11 - 15 are opcode */ 128/* In control message, bits 11 - 15 are opcode */
129#define OP_MASK 0x1f 129#define OP_MASK 0x1f
130#define OP_SET(x, y) \ 130#define OP_SET(x, y) \
131 do { \ 131 do { \
132 x |= (y & OP_MASK); \ 132 x |= (y & OP_MASK); \
133 } while (0) 133 } while (0)
134#define OP_READSTAT 0x01 134#define OP_READSTAT 0x01
135#define OP_READVAR 0x02 135#define OP_READVAR 0x02
@@ -163,32 +163,34 @@ typedef struct {
163#define NTP32asDOUBLE(x) (ntohs(L16(x)) + ((double)ntohs(R16(x)) / 65536.0)) 163#define NTP32asDOUBLE(x) (ntohs(L16(x)) + ((double)ntohs(R16(x)) / 65536.0))
164 164
165/* likewise for a 64-bit ntp fp number */ 165/* likewise for a 64-bit ntp fp number */
166#define NTP64asDOUBLE(n) \ 166#define NTP64asDOUBLE(n) \
167 (double)(((uint64_t)n) ? (ntohl(L32(n)) - EPOCHDIFF) + (.00000001 * (0.5 + (double)(ntohl(R32(n)) / 42.94967296))) : 0) 167 (double)(((uint64_t)n) ? (ntohl(L32(n)) - EPOCHDIFF) + \
168 (.00000001 * (0.5 + (double)(ntohl(R32(n)) / 42.94967296))) \
169 : 0)
168 170
169/* convert a struct timeval to a double */ 171/* convert a struct timeval to a double */
170#define TVasDOUBLE(x) (double)(x.tv_sec + (0.000001 * x.tv_usec)) 172#define TVasDOUBLE(x) (double)(x.tv_sec + (0.000001 * x.tv_usec))
171 173
172/* convert an ntp 64-bit fp number to a struct timeval */ 174/* convert an ntp 64-bit fp number to a struct timeval */
173#define NTP64toTV(n, t) \ 175#define NTP64toTV(n, t) \
174 do { \ 176 do { \
175 if (!n) \ 177 if (!n) \
176 t.tv_sec = t.tv_usec = 0; \ 178 t.tv_sec = t.tv_usec = 0; \
177 else { \ 179 else { \
178 t.tv_sec = ntohl(L32(n)) - EPOCHDIFF; \ 180 t.tv_sec = ntohl(L32(n)) - EPOCHDIFF; \
179 t.tv_usec = (int)(0.5 + (double)(ntohl(R32(n)) / 4294.967296)); \ 181 t.tv_usec = (int)(0.5 + (double)(ntohl(R32(n)) / 4294.967296)); \
180 } \ 182 } \
181 } while (0) 183 } while (0)
182 184
183/* convert a struct timeval to an ntp 64-bit fp number */ 185/* convert a struct timeval to an ntp 64-bit fp number */
184#define TVtoNTP64(t, n) \ 186#define TVtoNTP64(t, n) \
185 do { \ 187 do { \
186 if (!t.tv_usec && !t.tv_sec) \ 188 if (!t.tv_usec && !t.tv_sec) \
187 n = 0x0UL; \ 189 n = 0x0UL; \
188 else { \ 190 else { \
189 L32(n) = htonl(t.tv_sec + EPOCHDIFF); \ 191 L32(n) = htonl(t.tv_sec + EPOCHDIFF); \
190 R32(n) = htonl((uint64_t)((4294.967296 * t.tv_usec) + .5)); \ 192 R32(n) = htonl((uint64_t)((4294.967296 * t.tv_usec) + .5)); \
191 } \ 193 } \
192 } while (0) 194 } while (0)
193 195
194/* NTP control message header is 12 bytes, plus any data in the data 196/* NTP control message header is 12 bytes, plus any data in the data
@@ -197,15 +199,15 @@ typedef struct {
197#define SIZEOF_NTPCM(m) (12 + ntohs(m.count) + ((m.count) ? 4 - (ntohs(m.count) % 4) : 0)) 199#define SIZEOF_NTPCM(m) (12 + ntohs(m.count) + ((m.count) ? 4 - (ntohs(m.count) % 4) : 0))
198 200
199/* finally, a little helper or two for debugging: */ 201/* finally, a little helper or two for debugging: */
200#define DBG(x) \ 202#define DBG(x) \
201 do { \ 203 do { \
202 if (verbose > 1) { \ 204 if (verbose > 1) { \
203 x; \ 205 x; \
204 } \ 206 } \
205 } while (0); 207 } while (0);
206#define PRINTSOCKADDR(x) \ 208#define PRINTSOCKADDR(x) \
207 do { \ 209 do { \
208 printf("%u.%u.%u.%u", (x >> 24) & 0xff, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff); \ 210 printf("%u.%u.%u.%u", (x >> 24) & 0xff, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff); \
209 } while (0); 211 } while (0);
210 212
211/* calculate the offset of the local clock */ 213/* calculate the offset of the local clock */
@@ -358,7 +360,8 @@ double offset_request(const char *host, const char *port, mp_state_enum *status,
358 die(STATE_UNKNOWN, "can not allocate socket array"); 360 die(STATE_UNKNOWN, "can not allocate socket array");
359 } 361 }
360 362
361 ntp_server_results *servers = (ntp_server_results *)malloc(sizeof(ntp_server_results) * num_hosts); 363 ntp_server_results *servers =
364 (ntp_server_results *)malloc(sizeof(ntp_server_results) * num_hosts);
362 if (servers == NULL) { 365 if (servers == NULL) {
363 die(STATE_UNKNOWN, "can not allocate server array"); 366 die(STATE_UNKNOWN, "can not allocate server array");
364 } 367 }
@@ -585,8 +588,8 @@ check_ntp_time_config_wrapper process_arguments(int argc, char **argv) {
585} 588}
586 589
587char *perfd_offset(double offset, thresholds *offset_thresholds) { 590char *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, 591 return fperfdata("offset", offset, "s", true, offset_thresholds->warning->end, true,
589 0); 592 offset_thresholds->critical->end, false, 0, false, 0);
590} 593}
591 594
592int main(int argc, char *argv[]) { 595int main(int argc, char *argv[]) {
@@ -613,7 +616,8 @@ int main(int argc, char *argv[]) {
613 616
614 mp_state_enum offset_result = STATE_OK; 617 mp_state_enum offset_result = STATE_OK;
615 mp_state_enum result = STATE_OK; 618 mp_state_enum result = STATE_OK;
616 double offset = offset_request(config.server_address, config.port, &offset_result, config.time_offset); 619 double offset =
620 offset_request(config.server_address, config.port, &offset_result, config.time_offset);
617 if (offset_result == STATE_UNKNOWN) { 621 if (offset_result == STATE_UNKNOWN) {
618 result = ((!config.quiet) ? STATE_UNKNOWN : STATE_CRITICAL); 622 result = ((!config.quiet) ? STATE_UNKNOWN : STATE_CRITICAL);
619 } else { 623 } else {
@@ -701,5 +705,6 @@ void print_help(void) {
701 705
702void print_usage(void) { 706void print_usage(void) {
703 printf("%s\n", _("Usage:")); 707 printf("%s\n", _("Usage:"));
704 printf(" %s -H <host> [-4|-6] [-w <warn>] [-c <crit>] [-v verbose] [-o <time offset>]\n", progname); 708 printf(" %s -H <host> [-4|-6] [-w <warn>] [-c <crit>] [-v verbose] [-o <time offset>]\n",
709 progname);
705} 710}