1 /* chrony plugin for collectd (monitoring of chrony time server daemon)
2 **********************************************************************
3 * Copyright (C) Claudius M Zingerli, ZSeng, 2015-2016
5 * Internas roughly based on the ntpd plugin
6 * Some functions copied from chronyd/web (as marked)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 **********************************************************************
23 /* Formated using 'GNU indent -bli0 -ce -ppi 2' and some manual alignment
25 * - More robust udp parsing (using offsets instead of structs?)
26 * -> Currently chrony parses its data the same way as we do (using structs)
27 * - Plausibility checks on values received
28 * -> Done at higher levels
34 # include <sys/types.h> /* getaddrinfo */
37 # include <sys/socket.h>
43 # include <arpa/inet.h> /* ntohs/ntohl */
47 #include "common.h" /* auxiliary functions */
48 #include "plugin.h" /* plugin_register_*, plugin_dispatch_values */
50 #define CONFIG_KEY_HOST "Host"
51 #define CONFIG_KEY_PORT "Port"
52 #define CONFIG_KEY_TIMEOUT "Timeout"
54 #define URAND_DEVICE_PATH "/dev/urandom" /* Used to initialize seq nr generator */
55 #define RAND_DEVICE_PATH "/dev/random" /* Used to initialize seq nr generator (fall back) */
57 static const char *g_config_keys[] = {
63 static int g_config_keys_num = STATIC_ARRAY_SIZE(g_config_keys);
64 static int g_is_connected = 0;
65 static int g_chrony_socket = -1;
66 static time_t g_chrony_timeout = -1;
67 static char *g_chrony_host = NULL;
68 static char *g_chrony_port = NULL;
69 static char *g_plugin_instance = NULL;
70 static uint32_t g_chrony_rand = 1;
71 static uint32_t g_chrony_seq_is_initialized = 0;
73 #define PLUGIN_NAME_SHORT "chrony"
74 #define PLUGIN_NAME PLUGIN_NAME_SHORT " plugin"
75 #define DAEMON_NAME PLUGIN_NAME_SHORT
76 #define CHRONY_DEFAULT_HOST "localhost"
77 #define CHRONY_DEFAULT_PORT "323"
78 #define CHRONY_DEFAULT_TIMEOUT 2
80 /* Return codes (collectd expects non-zero on errors) */
81 #define CHRONY_RC_OK 0
82 #define CHRONY_RC_FAIL 1
84 /* Chronyd command packet variables adapted from chrony/candm.h (GPL2) */
85 #define PROTO_VERSION_NUMBER 6
86 #define IPADDR_UNSPEC 0
87 #define IPADDR_INET4 1
88 #define IPADDR_INET6 2
89 #define IPV6_STR_MAX_SIZE (8*4+7+1)
93 PKT_TYPE_CMD_REQUEST = 1,
94 PKT_TYPE_CMD_REPLY = 2
100 REQ_SOURCE_DATA = 15,
102 REQ_SOURCE_STATS = 34
111 RPY_MANUAL_TIMESTAMP = 4,
113 RPY_SOURCE_STATS = 6,
117 #define ATTRIB_PACKED __attribute__((packed))
118 typedef struct ATTRIB_PACKED
123 typedef struct ATTRIB_PACKED
125 uint32_t tv_sec_high;
136 STT_NOSUCHSOURCE = 4,
140 STT_ACCESSALLOWED = 8,
141 STT_ACCESSDENIED = 9,
142 STT_NOHOSTACCESS = 10,
143 STT_SOURCEALREADYKNOWN = 11,
144 STT_TOOMANYSOURCES = 12,
150 STT_BADPKTVERSION = 18,
151 STT_BADPKTLENGTH = 19
154 /* Chrony client request packets */
155 typedef struct ATTRIB_PACKED
157 uint8_t f_dummy0[80]; /* Chrony expects 80bytes dummy data (Avoiding UDP Amplification) */
158 } tChrony_Req_Tracking;
160 typedef struct ATTRIB_PACKED
162 uint32_t f_n_sources;
163 } tChrony_Req_N_Sources;
165 typedef struct ATTRIB_PACKED
168 uint8_t f_dummy0[44];
169 } tChrony_Req_Source_data;
171 typedef struct ATTRIB_PACKED
174 uint8_t f_dummy0[56];
175 } tChrony_Req_Source_stats;
177 typedef struct ATTRIB_PACKED
191 } header; /* Packed: 20Bytes */
194 tChrony_Req_N_Sources n_sources;
195 tChrony_Req_Source_data source_data;
196 tChrony_Req_Source_stats source_stats;
197 tChrony_Req_Tracking tracking;
199 uint8_t padding[4 + 16]; /* Padding to match minimal response size */
202 /* Chrony daemon response packets */
203 typedef struct ATTRIB_PACKED
205 uint32_t f_n_sources;
206 } tChrony_Resp_N_Sources;
208 typedef struct ATTRIB_PACKED
218 typedef struct ATTRIB_PACKED
221 uint16_t dummy; /* FIXME: Strange dummy space. Needed on gcc 4.8.3/clang 3.4.1 on x86_64 */
222 int16_t f_poll; /* 2^f_poll = Time between polls (s) */
223 uint16_t f_stratum; /* Remote clock stratum */
224 uint16_t f_state; /* 0 = RPY_SD_ST_SYNC, 1 = RPY_SD_ST_UNREACH, 2 = RPY_SD_ST_FALSETICKER */
225 /* 3 = RPY_SD_ST_JITTERY, 4 = RPY_SD_ST_CANDIDATE, 5 = RPY_SD_ST_OUTLIER */
226 uint16_t f_mode; /* 0 = RPY_SD_MD_CLIENT, 1 = RPY_SD_MD_PEER, 2 = RPY_SD_MD_REF */
227 uint16_t f_flags; /* unused */
228 uint16_t f_reachability; /* Bit mask of successfull tries to reach the source */
230 uint32_t f_since_sample; /* Time since last sample (s) */
231 tFloat f_origin_latest_meas; /* */
232 tFloat f_latest_meas; /* */
233 tFloat f_latest_meas_err; /* */
234 } tChrony_Resp_Source_data;
236 typedef struct ATTRIB_PACKED
240 uint16_t dummy; /* FIXME: Strange dummy space. Needed on gcc 4.8.3/clang 3.4.1 on x86_64 */
241 uint32_t f_n_samples; /* Number of measurements done */
242 uint32_t f_n_runs; /* How many measurements to come */
243 uint32_t f_span_seconds; /* For how long we're measuring */
244 tFloat f_rtc_seconds_fast; /* ??? */
245 tFloat f_rtc_gain_rate_ppm; /* Estimated relative frequency error */
246 tFloat f_skew_ppm; /* Clock skew (ppm) (worst case freq est error (skew: peak2peak)) */
247 tFloat f_est_offset; /* Estimated offset of source */
248 tFloat f_est_offset_err; /* Error of estimation */
249 } tChrony_Resp_Source_stats;
251 typedef struct ATTRIB_PACKED
255 uint16_t dummy; /* FIXME: Strange dummy space. Needed on gcc 4.8.3/clang 3.4.1 on x86_64 */
257 uint16_t f_leap_status;
259 tFloat f_current_correction;
260 tFloat f_last_offset;
263 tFloat f_resid_freq_ppm;
266 tFloat f_root_dispersion;
267 tFloat f_last_update_interval;
268 } tChrony_Resp_Tracking;
270 typedef struct ATTRIB_PACKED
287 } header; /* Packed: 28 Bytes */
291 tChrony_Resp_N_Sources n_sources;
292 tChrony_Resp_Source_data source_data;
293 tChrony_Resp_Source_stats source_stats;
294 tChrony_Resp_Tracking tracking;
297 uint8_t padding[1024];
301 /*****************************************************************************/
302 /* Internal functions */
303 /*****************************************************************************/
305 /* connect_client code adapted from: http://long.ccaba.upc.edu/long/045Guidelines/eva/ipv6.html#daytimeClient6 */
306 /* License granted by Eva M Castro via e-mail on 2016-02-18 under the terms of GPLv3 */
308 connect_client(const char *p_hostname,
309 const char *p_service, int p_family, int p_socktype)
311 struct addrinfo hints, *res = NULL, *ressave = NULL;
314 memset(&hints, 0, sizeof(struct addrinfo));
316 hints.ai_family = p_family;
317 hints.ai_socktype = p_socktype;
319 n = getaddrinfo(p_hostname, p_service, &hints, &res);
323 ERROR(PLUGIN_NAME ": getaddrinfo error:: [%s]", gai_strerror(n));
332 sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
336 if (connect(sockfd, res->ai_addr, res->ai_addrlen) == 0)
348 freeaddrinfo(ressave);
353 /* niptoha code originally from: git://git.tuxfamily.org/gitroot/chrony/chrony.git:util.c */
354 /* Original code licensed as GPLv2, by Richard P. Purnow, Miroslav Lichvar */
355 /* Original name: char * UTI_IPToString(IPAddr *addr)*/
357 niptoha(const tChrony_IPAddr * addr, char *p_buf, size_t p_buf_size)
360 unsigned long a, b, c, d, ip;
363 switch (ntohs(addr->f_family))
366 rc = snprintf(p_buf, p_buf_size, "[UNSPEC]");
369 ip = ntohl(addr->addr.ip4);
370 a = (ip >> 24) & 0xff;
371 b = (ip >> 16) & 0xff;
372 c = (ip >> 8) & 0xff;
373 d = (ip >> 0) & 0xff;
374 rc = snprintf(p_buf, p_buf_size, "%ld.%ld.%ld.%ld", a, b, c, d);
377 ip6 = addr->addr.ip6;
380 rc = inet_ntop(AF_INET6, ip6, p_buf, p_bug_size);
382 # if defined(BYTE_ORDER) && (BYTE_ORDER == BIG_ENDIAN)
384 snprintf(p_buf, p_buf_size,
385 "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
386 ip6[15], ip6[14], ip6[13], ip6[12], ip6[11], ip6[10], ip6[9],
387 ip6[8], ip6[7], ip6[6], ip6[5], ip6[4], ip6[3], ip6[2],
391 snprintf(p_buf, p_buf_size,
392 "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
393 ip6[0], ip6[1], ip6[2], ip6[3], ip6[4], ip6[5], ip6[6],
394 ip6[7], ip6[8], ip6[9], ip6[10], ip6[11], ip6[12], ip6[13],
400 rc = snprintf(p_buf, p_buf_size, "[UNKNOWN]");
410 /*Set the socket's timeout to g_chrony_timeout; a value of 0 signals infinite timeout */
411 /*Returns 0 on success, !0 on error (check errno) */
414 tv.tv_sec = g_chrony_timeout;
417 assert(g_chrony_socket >= 0);
419 (g_chrony_socket, SOL_SOCKET, SO_RCVTIMEO, (char *) &tv,
420 sizeof(struct timeval)) < 0)
422 return CHRONY_RC_FAIL;
431 /*Connects to the chrony daemon */
432 /*Returns 0 on success, !0 on error (check errno) */
435 if (g_chrony_host == NULL)
437 g_chrony_host = strdup(CHRONY_DEFAULT_HOST);
438 assert(g_chrony_host);
440 if (g_chrony_port == NULL)
442 g_chrony_port = strdup(CHRONY_DEFAULT_PORT);
443 assert(g_chrony_port);
445 if (g_chrony_timeout < 0)
447 g_chrony_timeout = CHRONY_DEFAULT_TIMEOUT;
448 assert(g_chrony_timeout >= 0);
451 DEBUG(PLUGIN_NAME ": Connecting to %s:%s", g_chrony_host, g_chrony_port);
453 connect_client(g_chrony_host, g_chrony_port, AF_UNSPEC, SOCK_DGRAM);
456 ERROR(PLUGIN_NAME ": Error connecting to daemon. Errno = %d", errno);
457 return CHRONY_RC_FAIL;
459 DEBUG(PLUGIN_NAME ": Connected");
460 g_chrony_socket = socket;
462 if (chrony_set_timeout())
464 ERROR(PLUGIN_NAME ": Error setting timeout to %lds. Errno = %d",
465 g_chrony_timeout, errno);
466 return CHRONY_RC_FAIL;
473 chrony_send_request(const tChrony_Request * p_req, size_t p_req_size)
475 if (send(g_chrony_socket, p_req, p_req_size, 0) < 0)
477 ERROR(PLUGIN_NAME ": Error sending packet. Errno = %d", errno);
478 return CHRONY_RC_FAIL;
485 chrony_recv_response(tChrony_Response * p_resp, size_t p_resp_max_size,
486 size_t * p_resp_size)
488 ssize_t rc = recv(g_chrony_socket, p_resp, p_resp_max_size, 0);
491 ERROR(PLUGIN_NAME ": Error receiving packet: %s (%d)", strerror(errno),
493 return CHRONY_RC_FAIL;
503 chrony_query(const int p_command, tChrony_Request * p_req,
504 tChrony_Response * p_resp, size_t * p_resp_size)
506 /* Check connection. We simply perform one try as collectd already handles retries */
511 if (g_is_connected == 0)
513 if (chrony_connect() == 0)
518 ERROR(PLUGIN_NAME ": Unable to connect. Errno = %d", errno);
519 return CHRONY_RC_FAIL;
525 int valid_command = 0;
526 size_t req_size = sizeof(p_req->header) + sizeof(p_req->padding);
527 size_t resp_size = sizeof(p_resp->header);
528 uint16_t resp_code = RPY_NULL;
532 req_size += sizeof(p_req->body.tracking);
533 resp_size += sizeof(p_resp->body.tracking);
534 resp_code = RPY_TRACKING;
538 req_size += sizeof(p_req->body.n_sources);
539 resp_size += sizeof(p_resp->body.n_sources);
540 resp_code = RPY_N_SOURCES;
543 case REQ_SOURCE_DATA:
544 req_size += sizeof(p_req->body.source_data);
545 resp_size += sizeof(p_resp->body.source_data);
546 resp_code = RPY_SOURCE_DATA;
549 case REQ_SOURCE_STATS:
550 req_size += sizeof(p_req->body.source_stats);
551 resp_size += sizeof(p_resp->body.source_stats);
552 resp_code = RPY_SOURCE_STATS;
556 ERROR(PLUGIN_NAME ": Unknown request command (Was: %d)", p_command);
560 if (valid_command == 0)
565 uint32_t seq_nr = rand_r(&g_chrony_rand);
566 p_req->header.f_cmd = htons(p_command);
567 p_req->header.f_cmd_try = 0;
568 p_req->header.f_seq = seq_nr;
570 DEBUG(PLUGIN_NAME ": Sending request (.cmd = %d, .seq = %d)", p_command,
572 if (chrony_send_request(p_req, req_size) != 0)
577 DEBUG(PLUGIN_NAME ": Waiting for response");
578 if (chrony_recv_response(p_resp, resp_size, p_resp_size) != 0)
583 ": Received response: .version = %u, .type = %u, .cmd = %u, .reply = %u, .status = %u, .seq = %u",
584 p_resp->header.f_version, p_resp->header.f_type,
585 ntohs(p_resp->header.f_cmd), ntohs(p_resp->header.f_reply),
586 ntohs(p_resp->header.f_status), p_resp->header.f_seq);
588 if (p_resp->header.f_version != p_req->header.f_version)
590 ERROR(PLUGIN_NAME ": Wrong protocol version (Was: %d, expected: %d)",
591 p_resp->header.f_version, p_req->header.f_version);
592 return CHRONY_RC_FAIL;
594 if (p_resp->header.f_type != PKT_TYPE_CMD_REPLY)
596 ERROR(PLUGIN_NAME ": Wrong packet type (Was: %d, expected: %d)",
597 p_resp->header.f_type, PKT_TYPE_CMD_REPLY);
598 return CHRONY_RC_FAIL;
600 if (p_resp->header.f_seq != seq_nr)
602 /* FIXME: Implement sequence number handling */
604 ": Unexpected sequence number (Was: %d, expected: %d)",
605 p_resp->header.f_seq, p_req->header.f_seq);
606 return CHRONY_RC_FAIL;
608 if (p_resp->header.f_cmd != p_req->header.f_cmd)
610 ERROR(PLUGIN_NAME ": Wrong reply command (Was: %d, expected: %d)",
611 p_resp->header.f_cmd, p_req->header.f_cmd);
612 return CHRONY_RC_FAIL;
615 if (ntohs(p_resp->header.f_reply) != resp_code)
617 ERROR(PLUGIN_NAME ": Wrong reply code (Was: %d, expected: %d)",
618 ntohs(p_resp->header.f_reply), resp_code);
619 return CHRONY_RC_FAIL;
622 switch (p_resp->header.f_status)
625 DEBUG(PLUGIN_NAME ": Reply packet status STT_SUCCESS");
629 ": Reply packet contains error status: %d (expected: %d)",
630 p_resp->header.f_status, STT_SUCCESS);
631 return CHRONY_RC_FAIL;
639 /* Some error occured */
640 return CHRONY_RC_FAIL;
645 chrony_init_req(tChrony_Request * p_req)
647 memset(p_req, 0, sizeof(*p_req));
648 p_req->header.f_version = PROTO_VERSION_NUMBER;
649 p_req->header.f_type = PKT_TYPE_CMD_REQUEST;
650 p_req->header.f_dummy0 = 0;
651 p_req->header.f_dummy1 = 0;
652 p_req->header.f_dummy2 = 0;
653 p_req->header.f_dummy3 = 0;
657 /* ntohf code originally from: git://git.tuxfamily.org/gitroot/chrony/chrony.git:util.c */
658 /* Original code licensed as GPLv2, by Richard P. Purnow, Miroslav Lichvar */
659 /* Original name: double UTI_tFloatNetworkToHost(tFloat f) */
661 ntohf(tFloat p_float)
663 /* Convert tFloat in Network-bit-order to double in host-bit-order */
665 #define FLOAT_EXP_BITS 7
666 #define FLOAT_EXP_MIN (-(1 << (FLOAT_EXP_BITS - 1)))
667 #define FLOAT_EXP_MAX (-FLOAT_EXP_MIN - 1)
668 #define FLOAT_COEF_BITS ((int)sizeof (int32_t) * 8 - FLOAT_EXP_BITS)
669 #define FLOAT_COEF_MIN (-(1 << (FLOAT_COEF_BITS - 1)))
670 #define FLOAT_COEF_MAX (-FLOAT_COEF_MIN - 1)
675 uval = ntohl(p_float.value);
676 exp = (uval >> FLOAT_COEF_BITS) - FLOAT_COEF_BITS;
677 if (exp >= 1 << (FLOAT_EXP_BITS - 1))
679 exp -= 1 << FLOAT_EXP_BITS;
682 /* coef = (x << FLOAT_EXP_BITS) >> FLOAT_EXP_BITS; */
683 coef = uval % (1U << FLOAT_COEF_BITS);
684 if (coef >= 1 << (FLOAT_COEF_BITS - 1))
686 coef -= 1 << FLOAT_COEF_BITS;
688 return coef * pow(2.0, exp);
693 chrony_push_data(char *p_type, char *p_type_inst, double p_value)
696 value_list_t vl = VALUE_LIST_INIT;
698 values[0].gauge = p_value; /* TODO: Check type??? (counter, gauge, derive, absolute) */
703 /* XXX: Shall g_chrony_host/g_chrony_port be reflected in the plugin's output? */
704 /* hostname_g is set in daemon/collectd.c (from config, via gethostname or by resolving localhost) */
705 /* defined as: char hostname_g[DATA_MAX_NAME_LEN]; (never NULL) */
706 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
707 sstrncpy(vl.plugin, PLUGIN_NAME_SHORT, sizeof(vl.plugin));
708 if (g_plugin_instance != NULL)
710 sstrncpy(vl.plugin_instance, g_plugin_instance,
711 sizeof(vl.plugin_instance));
715 sstrncpy(vl.type, p_type, sizeof(vl.type));
717 if (p_type_inst != NULL)
719 sstrncpy(vl.type_instance, p_type_inst, sizeof(vl.type_instance));
722 plugin_dispatch_values(&vl);
727 chrony_push_data_valid(char *p_type, char *p_type_inst, const int p_is_valid,
730 /* Push real value if p_is_valid is true, push NAN if p_is_valid is not true (idea from ntp plugin) */
735 chrony_push_data(p_type, p_type_inst, p_value);
742 /* Initialize the sequence number generator from /dev/urandom */
743 /* Fallbacks: /dev/random and time(NULL) */
746 int fh = open(URAND_DEVICE_PATH, O_RDONLY);
749 ssize_t rc = read(fh, &g_chrony_rand, sizeof(g_chrony_rand));
750 if (rc != sizeof(g_chrony_rand))
752 ERROR(PLUGIN_NAME ": Reading from random source \'%s\'failed: %s (%d)",
753 URAND_DEVICE_PATH, strerror(errno), errno);
755 return CHRONY_RC_FAIL;
758 DEBUG(PLUGIN_NAME ": Seeding RNG from " URAND_DEVICE_PATH);
763 /* URAND_DEVICE_PATH device not found. Try RAND_DEVICE_PATH as fall-back */
764 int fh = open(RAND_DEVICE_PATH, O_RDONLY);
767 ssize_t rc = read(fh, &g_chrony_rand, sizeof(g_chrony_rand));
768 if (rc != sizeof(g_chrony_rand))
771 ": Reading from random source \'%s\'failed: %s (%d)",
772 RAND_DEVICE_PATH, strerror(errno), errno);
774 return CHRONY_RC_FAIL;
777 DEBUG(PLUGIN_NAME ": Seeding RNG from " RAND_DEVICE_PATH);
780 /* Error opening RAND_DEVICE_PATH. Try time(NULL) as fall-back */
781 DEBUG(PLUGIN_NAME ": Seeding RNG from time(NULL)");
782 g_chrony_rand = time(NULL) ^ getpid();
786 ERROR(PLUGIN_NAME ": Opening random source \'%s\' failed: %s (%d)",
787 URAND_DEVICE_PATH, strerror(errno), errno);
788 return CHRONY_RC_FAIL;
796 /*****************************************************************************/
797 /* Exported functions */
798 /*****************************************************************************/
800 chrony_config(const char *p_key, const char *p_value)
804 /* Parse config variables */
805 if (strcasecmp(p_key, CONFIG_KEY_HOST) == 0)
807 if (g_chrony_host != NULL)
811 if ((g_chrony_host = strdup(p_value)) == NULL)
813 ERROR(PLUGIN_NAME ": Error duplicating host name");
814 return CHRONY_RC_FAIL;
816 } else if (strcasecmp(p_key, CONFIG_KEY_PORT) == 0)
818 if (g_chrony_port != NULL)
822 if ((g_chrony_port = strdup(p_value)) == NULL)
824 ERROR(PLUGIN_NAME ": Error duplicating port name");
825 return CHRONY_RC_FAIL;
827 } else if (strcasecmp(p_key, CONFIG_KEY_TIMEOUT) == 0)
829 time_t tosec = strtol(p_value, NULL, 0);
830 g_chrony_timeout = tosec;
833 WARNING(PLUGIN_NAME ": Unknown configuration variable: %s %s", p_key,
835 return CHRONY_RC_FAIL;
837 /* XXX: We could set g_plugin_instance here to "g_chrony_host-g_chrony_port", but as multiple instances aren't yet supported, we skip this for now */
844 chrony_request_daemon_stats()
846 /* Perform Tracking request */
848 size_t chrony_resp_size;
849 tChrony_Request chrony_req;
850 tChrony_Response chrony_resp;
852 chrony_init_req(&chrony_req);
854 chrony_query(REQ_TRACKING, &chrony_req, &chrony_resp, &chrony_resp_size);
857 ERROR(PLUGIN_NAME ": chrony_query (REQ_TRACKING) failed with status %i",
863 char src_addr[IPV6_STR_MAX_SIZE];
864 memset(src_addr, 0, sizeof(src_addr));
865 niptoha(&chrony_resp.body.tracking.addr, src_addr, sizeof(src_addr));
866 DEBUG(PLUGIN_NAME ": Daemon stat: .addr = %s, .ref_id= %u, .stratum = %u, .leap_status = %u, .ref_time = %u:%u:%u, .current_correction = %f, .last_offset = %f, .rms_offset = %f, .freq_ppm = %f, .skew_ppm = %f, .root_delay = %f, .root_dispersion = %f, .last_update_interval = %f", src_addr, ntohs(chrony_resp.body.tracking.f_ref_id),
867 ntohs(chrony_resp.body.tracking.f_stratum),
868 ntohs(chrony_resp.body.tracking.f_leap_status),
869 ntohl(chrony_resp.body.tracking.f_ref_time.tv_sec_high),
870 ntohl(chrony_resp.body.tracking.f_ref_time.tv_sec_low),
871 ntohl(chrony_resp.body.tracking.f_ref_time.tv_nsec),
872 ntohf(chrony_resp.body.tracking.f_current_correction),
873 ntohf(chrony_resp.body.tracking.f_last_offset),
874 ntohf(chrony_resp.body.tracking.f_rms_offset),
875 ntohf(chrony_resp.body.tracking.f_freq_ppm),
876 ntohf(chrony_resp.body.tracking.f_skew_ppm),
877 ntohf(chrony_resp.body.tracking.f_root_delay),
878 ntohf(chrony_resp.body.tracking.f_root_dispersion),
879 ntohf(chrony_resp.body.tracking.f_last_update_interval));
883 double time_ref = ntohl(chrony_resp.body.tracking.f_ref_time.tv_nsec);
884 time_ref /= 1000000000.0;
885 time_ref += ntohl(chrony_resp.body.tracking.f_ref_time.tv_sec_low);
886 if (chrony_resp.body.tracking.f_ref_time.tv_sec_high)
889 ntohl(chrony_resp.body.tracking.f_ref_time.tv_sec_high);
890 secs_high *= 4294967296.0;
891 time_ref += secs_high;
894 /* Forward results to collectd-daemon */
895 /* Type_instance is always 'chrony' to tag daemon-wide data */
896 /* Type Type_instan Value */
897 chrony_push_data("clock_stratum", DAEMON_NAME, ntohs(chrony_resp.body.tracking.f_stratum));
898 chrony_push_data("time_ref", DAEMON_NAME, time_ref); /* unit: s */
899 chrony_push_data("time_offset_ntp", DAEMON_NAME, ntohf(chrony_resp.body.tracking.f_current_correction)); /* Offset between system time and NTP, unit: s */
900 chrony_push_data("time_offset", DAEMON_NAME, ntohf(chrony_resp.body.tracking.f_last_offset)); /* Estimated Offset of the NTP time, unit: s */
901 chrony_push_data("time_offset_rms", DAEMON_NAME, ntohf(chrony_resp.body.tracking.f_rms_offset)); /* averaged value of the above, unit: s */
902 chrony_push_data("frequency_error", DAEMON_NAME, ntohf(chrony_resp.body.tracking.f_freq_ppm)); /* Frequency error of the local osc, unit: ppm */
903 chrony_push_data("clock_skew_ppm", DAEMON_NAME, ntohf(chrony_resp.body.tracking.f_skew_ppm));
904 chrony_push_data("root_delay", DAEMON_NAME, ntohf(chrony_resp.body.tracking.f_root_delay)); /* Network latency between local daemon and the current source */
905 chrony_push_data("root_dispersion", DAEMON_NAME, ntohf(chrony_resp.body.tracking.f_root_dispersion));
906 chrony_push_data("clock_last_update", DAEMON_NAME, ntohf(chrony_resp.body.tracking.f_last_update_interval));
913 chrony_request_sources_count(unsigned int *p_count)
915 /* Requests the number of time sources from the chrony daemon */
917 size_t chrony_resp_size;
918 tChrony_Request chrony_req;
919 tChrony_Response chrony_resp;
921 DEBUG(PLUGIN_NAME ": Requesting data");
922 chrony_init_req(&chrony_req);
924 chrony_query(REQ_N_SOURCES, &chrony_req, &chrony_resp, &chrony_resp_size);
927 ERROR(PLUGIN_NAME ": chrony_query (REQ_N_SOURCES) failed with status %i",
932 *p_count = ntohl(chrony_resp.body.n_sources.f_n_sources);
933 DEBUG(PLUGIN_NAME ": Getting data of %d clock sources", *p_count);
940 chrony_request_source_data(int p_src_idx, int *p_is_reachable)
942 /* Perform Source data request for source #p_src_idx */
944 size_t chrony_resp_size;
945 tChrony_Request chrony_req;
946 tChrony_Response chrony_resp;
948 char src_addr[IPV6_STR_MAX_SIZE];
949 memset(src_addr, 0, sizeof(src_addr));
951 chrony_init_req(&chrony_req);
952 chrony_req.body.source_data.f_index = htonl(p_src_idx);
954 chrony_query(REQ_SOURCE_DATA, &chrony_req, &chrony_resp,
959 ": chrony_query (REQ_SOURCE_DATA) failed with status %i", rc);
963 niptoha(&chrony_resp.body.source_data.addr, src_addr, sizeof(src_addr));
965 ": Source[%d] data: .addr = %s, .poll = %u, .stratum = %u, .state = %u, .mode = %u, .flags = %u, .reach = %u, .latest_meas_ago = %u, .orig_latest_meas = %f, .latest_meas = %f, .latest_meas_err = %f",
966 p_src_idx, src_addr, ntohs(chrony_resp.body.source_data.f_poll),
967 ntohs(chrony_resp.body.source_data.f_stratum),
968 ntohs(chrony_resp.body.source_data.f_state),
969 ntohs(chrony_resp.body.source_data.f_mode),
970 ntohs(chrony_resp.body.source_data.f_flags),
971 ntohs(chrony_resp.body.source_data.f_reachability),
972 ntohl(chrony_resp.body.source_data.f_since_sample),
973 ntohf(chrony_resp.body.source_data.f_origin_latest_meas),
974 ntohf(chrony_resp.body.source_data.f_latest_meas),
975 ntohf(chrony_resp.body.source_data.f_latest_meas_err));
977 /* Push NaN if source is currently not reachable */
979 ntohs(chrony_resp.body.source_data.f_reachability) & 0x01;
980 *p_is_reachable = is_reachable;
982 /* Forward results to collectd-daemon */
983 chrony_push_data_valid("clock_stratum", src_addr, is_reachable,
984 ntohs(chrony_resp.body.source_data.f_stratum));
985 chrony_push_data_valid("clock_state", src_addr, is_reachable,
986 ntohs(chrony_resp.body.source_data.f_state));
987 chrony_push_data_valid("clock_mode", src_addr, is_reachable,
988 ntohs(chrony_resp.body.source_data.f_mode));
989 chrony_push_data_valid("clock_reachability", src_addr, is_reachable,
990 ntohs(chrony_resp.body.source_data.f_reachability));
991 chrony_push_data_valid("clock_last_meas", src_addr, is_reachable,
992 ntohs(chrony_resp.body.source_data.f_since_sample));
999 chrony_request_source_stats(int p_src_idx, const int *p_is_reachable)
1001 /* Perform Source stats request for source #p_src_idx */
1003 size_t chrony_resp_size;
1004 tChrony_Request chrony_req;
1005 tChrony_Response chrony_resp;
1006 double skew_ppm, frequency_error, time_offset;
1008 char src_addr[IPV6_STR_MAX_SIZE];
1009 memset(src_addr, 0, sizeof(src_addr));
1011 if (*p_is_reachable == 0)
1014 frequency_error = 0;
1018 chrony_init_req(&chrony_req);
1019 chrony_req.body.source_stats.f_index = htonl(p_src_idx);
1021 chrony_query(REQ_SOURCE_STATS, &chrony_req, &chrony_resp,
1026 ": chrony_query (REQ_SOURCE_STATS) failed with status %i", rc);
1030 skew_ppm = ntohf(chrony_resp.body.source_stats.f_skew_ppm);
1032 ntohf(chrony_resp.body.source_stats.f_rtc_gain_rate_ppm);
1033 time_offset = ntohf(chrony_resp.body.source_stats.f_est_offset);
1035 niptoha(&chrony_resp.body.source_stats.addr, src_addr, sizeof(src_addr));
1037 ": Source[%d] stat: .addr = %s, .ref_id= %u, .n_samples = %u, "
1038 ".n_runs = %u, .span_seconds = %u, .rtc_seconds_fast = %f, "
1039 ".rtc_gain_rate_ppm = %f, .skew_ppm= %f, .est_offset = %f, .est_offset_err = %f",
1040 p_src_idx, src_addr,
1041 ntohl(chrony_resp.body.source_stats.f_ref_id),
1042 ntohl(chrony_resp.body.source_stats.f_n_samples),
1043 ntohl(chrony_resp.body.source_stats.f_n_runs),
1044 ntohl(chrony_resp.body.source_stats.f_span_seconds),
1045 ntohf(chrony_resp.body.source_stats.f_rtc_seconds_fast),
1046 frequency_error, skew_ppm, time_offset,
1047 ntohf(chrony_resp.body.source_stats.f_est_offset_err));
1049 } /* if (*is_reachable) */
1051 /* Forward results to collectd-daemon */
1052 chrony_push_data_valid("clock_skew_ppm", src_addr, *p_is_reachable,
1054 chrony_push_data_valid("frequency_error", src_addr, *p_is_reachable, frequency_error); /* unit: ppm */
1055 chrony_push_data_valid("time_offset", src_addr, *p_is_reachable, time_offset); /* unit: s */
1057 return CHRONY_RC_OK;
1064 /* collectd read callback: Perform data acquisition */
1066 unsigned int now_src, n_sources;
1068 if (g_chrony_seq_is_initialized == 0)
1070 /* Seed RNG for sequence number generation */
1071 rc = chrony_init_seq();
1072 if (rc != CHRONY_RC_OK)
1076 g_chrony_seq_is_initialized = 1;
1079 /* Get daemon stats */
1080 rc = chrony_request_daemon_stats();
1081 if (rc != CHRONY_RC_OK)
1086 /* Get number of time sources, then check every source for status */
1087 rc = chrony_request_sources_count(&n_sources);
1088 if (rc != CHRONY_RC_OK)
1093 for (now_src = 0; now_src < n_sources; ++now_src)
1096 rc = chrony_request_source_data(now_src, &is_reachable);
1097 if (rc != CHRONY_RC_OK)
1102 rc = chrony_request_source_stats(now_src, &is_reachable);
1103 if (rc != CHRONY_RC_OK)
1108 return CHRONY_RC_OK;
1115 /* Collectd shutdown callback: Free mem */
1116 if (g_is_connected != 0)
1118 close(g_chrony_socket);
1121 if (g_chrony_host != NULL)
1123 free(g_chrony_host);
1124 g_chrony_host = NULL;
1126 if (g_chrony_port != NULL)
1128 free(g_chrony_port);
1129 g_chrony_port = NULL;
1131 if (g_plugin_instance != NULL)
1133 free(g_plugin_instance);
1134 g_plugin_instance = NULL;
1136 return CHRONY_RC_OK;
1141 module_register(void)
1143 plugin_register_config(PLUGIN_NAME_SHORT, chrony_config, g_config_keys,
1145 plugin_register_read(PLUGIN_NAME_SHORT, chrony_read);
1146 plugin_register_shutdown(PLUGIN_NAME_SHORT, chrony_shutdown);