1 /* chrony plugin for collectd
2 (c) 2015 by Claudius M Zingerli, ZSeng
3 Internas roughly based on the ntpd plugin
4 Some functions copied from chronyd/web (marked)
8 * - More robust udp parsing (using offsets instead of structs?)
9 * -> Currently chrony parses its data the same way as we do (using structs)
10 * - Plausibility checks on values received
11 * -> Done at higher levels
17 # include <sys/types.h> /* getaddrinfo */
20 # include <sys/socket.h>
26 # include <arpa/inet.h> /* ntohs/ntohl */
30 #include "common.h" /* auxiliary functions */
31 #include "plugin.h" /* plugin_register_*, plugin_dispatch_values */
33 #define CONFIG_KEY_HOST "Host"
34 #define CONFIG_KEY_PORT "Port"
35 #define CONFIG_KEY_TIMEOUT "Timeout"
37 #define URAND_DEVICE_PATH "/dev/urandom" /* Used to initialize seq nr generator*/
38 #define RAND_DEVICE_PATH "/dev/random" /* Used to initialize seq nr generator (fall back)*/
40 static const char *g_config_keys[] =
47 static int g_config_keys_num = STATIC_ARRAY_SIZE (g_config_keys);
48 static int g_is_connected = 0;
49 static int g_chrony_socket = -1;
50 static time_t g_chrony_timeout = -1;
51 static char *g_chrony_host = NULL;
52 static char *g_chrony_port = NULL;
53 static char *g_plugin_instance = NULL;
54 static uint32_t g_chrony_rand = 1;
55 static uint32_t g_chrony_seq_is_initialized = 0;
57 #define PLUGIN_NAME_SHORT "chrony"
58 #define PLUGIN_NAME PLUGIN_NAME_SHORT " plugin"
59 #define DAEMON_NAME PLUGIN_NAME_SHORT
60 #define CHRONY_DEFAULT_HOST "localhost"
61 #define CHRONY_DEFAULT_PORT "323"
62 #define CHRONY_DEFAULT_TIMEOUT 2
64 /* Return codes (collectd expects non-zero on errors) */
65 #define CHRONY_RC_OK 0
66 #define CHRONY_RC_FAIL 1
68 /* Variables adapted from chrony/candm.h (GPL2)*/
70 #define PROTO_VERSION_NUMBER 6
71 #define IPADDR_UNSPEC 0
72 #define IPADDR_INET4 1
73 #define IPADDR_INET6 2
74 #define IPV6_STR_MAX_SIZE (8*4+7+1)
78 PKT_TYPE_CMD_REQUEST = 1,
79 PKT_TYPE_CMD_REPLY = 2
96 RPY_MANUAL_TIMESTAMP = 4,
102 #define ATTRIB_PACKED __attribute__((packed))
103 typedef struct ATTRIB_PACKED
108 typedef struct ATTRIB_PACKED
110 uint32_t tv_sec_high;
122 STT_NOSUCHSOURCE = 4,
126 STT_ACCESSALLOWED = 8,
127 STT_ACCESSDENIED = 9,
128 STT_NOHOSTACCESS = 10,
129 STT_SOURCEALREADYKNOWN = 11,
130 STT_TOOMANYSOURCES = 12,
136 STT_BADPKTVERSION = 18,
137 STT_BADPKTLENGTH = 19
140 /* Chrony client request packets */
141 typedef struct ATTRIB_PACKED
143 uint8_t f_dummy0[80]; //Chrony expects 80bytes dummy data (Avoiding UDP Amplification)
144 } tChrony_Req_Tracking;
146 typedef struct ATTRIB_PACKED
148 uint32_t f_n_sources;
149 } tChrony_Req_N_Sources;
151 typedef struct ATTRIB_PACKED
154 uint8_t f_dummy0[44];
155 } tChrony_Req_Source_data;
157 typedef struct ATTRIB_PACKED
160 uint8_t f_dummy0[56];
161 } tChrony_Req_Source_stats;
163 typedef struct ATTRIB_PACKED
177 } header; /* Packed: 20Bytes */
180 tChrony_Req_N_Sources n_sources;
181 tChrony_Req_Source_data source_data;
182 tChrony_Req_Source_stats source_stats;
183 tChrony_Req_Tracking tracking;
185 uint8_t padding[4+16]; /* Padding to match minimal response size */
188 /* Chrony daemon response packets */
189 typedef struct ATTRIB_PACKED
191 uint32_t f_n_sources;
192 } tChrony_Resp_N_Sources;
194 typedef struct ATTRIB_PACKED
204 typedef struct ATTRIB_PACKED
207 uint16_t dummy; /* FIXME: Strange dummy space. Needed on gcc 4.8.3/clang 3.4.1 on x86_64 */
208 int16_t f_poll; /* 2^f_poll = Time between polls (s) */
209 uint16_t f_stratum; /* Remote clock stratum */
210 uint16_t f_state; /* 0 = RPY_SD_ST_SYNC, 1 = RPY_SD_ST_UNREACH, 2 = RPY_SD_ST_FALSETICKER */
211 /* 3 = RPY_SD_ST_JITTERY, 4 = RPY_SD_ST_CANDIDATE, 5 = RPY_SD_ST_OUTLIER */
212 uint16_t f_mode; /* 0 = RPY_SD_MD_CLIENT, 1 = RPY_SD_MD_PEER, 2 = RPY_SD_MD_REF */
213 uint16_t f_flags; /* unused */
214 uint16_t f_reachability; /* Bit mask of successfull tries to reach the source */
216 uint32_t f_since_sample; /* Time since last sample (s) */
217 tFloat f_origin_latest_meas; /* */
218 tFloat f_latest_meas; /* */
219 tFloat f_latest_meas_err; /* */
220 } tChrony_Resp_Source_data;
222 typedef struct ATTRIB_PACKED
226 uint16_t dummy; /* FIXME: Strange dummy space. Needed on gcc 4.8.3/clang 3.4.1 on x86_64 */
227 uint32_t f_n_samples; /* Number of measurements done */
228 uint32_t f_n_runs; /* How many measurements to come */
229 uint32_t f_span_seconds; /* For how long we're measuring */
230 tFloat f_rtc_seconds_fast; /* ??? */
231 tFloat f_rtc_gain_rate_ppm; /* Estimated relative frequency error */
232 tFloat f_skew_ppm; /* Clock skew (ppm) (worst case freq est error (skew: peak2peak)) */
233 tFloat f_est_offset; /* Estimated offset of source */
234 tFloat f_est_offset_err; /* Error of estimation */
235 } tChrony_Resp_Source_stats;
237 typedef struct ATTRIB_PACKED
241 uint16_t dummy; /* FIXME: Strange dummy space. Needed on gcc 4.8.3/clang 3.4.1 on x86_64 */
243 uint16_t f_leap_status;
245 tFloat f_current_correction;
246 tFloat f_last_offset;
249 tFloat f_resid_freq_ppm;
252 tFloat f_root_dispersion;
253 tFloat f_last_update_interval;
254 } tChrony_Resp_Tracking;
256 typedef struct ATTRIB_PACKED
273 } header; /* Packed: 28 Bytes */
277 tChrony_Resp_N_Sources n_sources;
278 tChrony_Resp_Source_data source_data;
279 tChrony_Resp_Source_stats source_stats;
280 tChrony_Resp_Tracking tracking;
283 uint8_t padding[1024];
287 /*****************************************************************************/
288 /* Internal functions */
289 /*****************************************************************************/
290 /* Code adapted from: http://long.ccaba.upc.edu/long/045Guidelines/eva/ipv6.html#daytimeClient6 */
292 static int connect_client (const char *p_hostname,
293 const char *p_service,
297 struct addrinfo hints, *res=NULL, *ressave=NULL;
300 memset(&hints, 0, sizeof(struct addrinfo));
302 hints.ai_family = p_family;
303 hints.ai_socktype = p_socktype;
305 n = getaddrinfo(p_hostname, p_service, &hints, &res);
309 ERROR (PLUGIN_NAME ": getaddrinfo error:: [%s]", gai_strerror(n));
318 sockfd = socket(res->ai_family,
324 if (connect(sockfd, res->ai_addr, res->ai_addrlen) == 0)
336 freeaddrinfo(ressave);
340 /*Code originally from: git://git.tuxfamily.org/gitroot/chrony/chrony.git:util.c */
341 /*char * UTI_IPToString(IPAddr *addr)*/
342 static char * niptoha(const tChrony_IPAddr *addr,char *p_buf, size_t p_buf_size)
345 unsigned long a, b, c, d, ip;
348 switch (ntohs(addr->f_family))
351 rc=snprintf(p_buf, p_buf_size, "[UNSPEC]");
354 ip = ntohl(addr->addr.ip4);
359 rc=snprintf(p_buf, p_buf_size, "%ld.%ld.%ld.%ld", a, b, c, d);
362 ip6 = addr->addr.ip6;
365 rc=inet_ntop(AF_INET6, ip6, p_buf, p_bug_size);
367 #if defined(BYTE_ORDER) && (BYTE_ORDER == BIG_ENDIAN)
368 rc=snprintf(p_buf, p_buf_size, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
369 ip6[15], ip6[14], ip6[13], ip6[12], ip6[11], ip6[10], ip6[9], ip6[8],
370 ip6[7], ip6[6], ip6[5], ip6[4], ip6[3], ip6[2], ip6[1], ip6[0]);
372 rc=snprintf(p_buf, p_buf_size, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
373 ip6[0], ip6[1], ip6[2], ip6[3], ip6[4], ip6[5], ip6[6], ip6[7],
374 ip6[8], ip6[9], ip6[10], ip6[11], ip6[12], ip6[13], ip6[14], ip6[15]);
379 rc=snprintf(p_buf, p_buf_size, "[UNKNOWN]");
386 static int chrony_set_timeout()
388 /*Set the socket's timeout to g_chrony_timeout; a value of 0 signals infinite timeout*/
389 /*Returns 0 on success, !0 on error (check errno)*/
392 tv.tv_sec = g_chrony_timeout;
395 assert(g_chrony_socket>=0);
396 if (setsockopt(g_chrony_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval)) < 0)
398 return CHRONY_RC_FAIL;
403 static int chrony_connect()
405 /*Connects to the chrony daemon*/
406 /*Returns 0 on success, !0 on error (check errno)*/
409 if (g_chrony_host == NULL)
411 g_chrony_host = strdup(CHRONY_DEFAULT_HOST);
412 assert(g_chrony_host);
414 if (g_chrony_port == NULL)
416 g_chrony_port = strdup(CHRONY_DEFAULT_PORT);
417 assert(g_chrony_port);
419 if (g_chrony_timeout < 0)
421 g_chrony_timeout = CHRONY_DEFAULT_TIMEOUT;
422 assert(g_chrony_timeout>=0);
426 DEBUG(PLUGIN_NAME ": Connecting to %s:%s", g_chrony_host, g_chrony_port);
427 socket = connect_client(g_chrony_host, g_chrony_port, AF_UNSPEC, SOCK_DGRAM);
430 ERROR (PLUGIN_NAME ": Error connecting to daemon. Errno = %d", errno);
431 return CHRONY_RC_FAIL;
433 DEBUG(PLUGIN_NAME ": Connected");
434 g_chrony_socket = socket;
436 if (chrony_set_timeout())
438 ERROR (PLUGIN_NAME ": Error setting timeout to %lds. Errno = %d", g_chrony_timeout, errno);
439 return CHRONY_RC_FAIL;
444 static int chrony_send_request(const tChrony_Request *p_req, size_t p_req_size)
446 if (send(g_chrony_socket,p_req,p_req_size,0) < 0)
448 ERROR (PLUGIN_NAME ": Error sending packet. Errno = %d", errno);
449 return CHRONY_RC_FAIL;
454 static int chrony_recv_response(tChrony_Response *p_resp, size_t p_resp_max_size, size_t *p_resp_size)
456 ssize_t rc = recv(g_chrony_socket,p_resp,p_resp_max_size,0);
459 ERROR (PLUGIN_NAME ": Error receiving packet: %s (%d)", strerror(errno), errno);
460 return CHRONY_RC_FAIL;
467 static int chrony_query(const int p_command, tChrony_Request *p_req, tChrony_Response *p_resp, size_t *p_resp_size)
469 /* Check connection. We simply perform one try as collectd already handles retries */
474 if (g_is_connected == 0)
476 if (chrony_connect() == 0)
480 ERROR (PLUGIN_NAME ": Unable to connect. Errno = %d", errno);
481 return CHRONY_RC_FAIL;
488 int valid_command = 0;
489 size_t req_size = sizeof(p_req->header) + sizeof(p_req->padding);
490 size_t resp_size = sizeof(p_resp->header);
491 uint16_t resp_code = RPY_NULL;
495 req_size += sizeof(p_req->body.tracking);
496 resp_size += sizeof(p_resp->body.tracking);
497 resp_code = RPY_TRACKING;
501 req_size += sizeof(p_req->body.n_sources);
502 resp_size += sizeof(p_resp->body.n_sources);
503 resp_code = RPY_N_SOURCES;
506 case REQ_SOURCE_DATA:
507 req_size += sizeof(p_req->body.source_data);
508 resp_size += sizeof(p_resp->body.source_data);
509 resp_code = RPY_SOURCE_DATA;
512 case REQ_SOURCE_STATS:
513 req_size += sizeof(p_req->body.source_stats);
514 resp_size += sizeof(p_resp->body.source_stats);
515 resp_code = RPY_SOURCE_STATS;
519 ERROR (PLUGIN_NAME ": Unknown request command (Was: %d)", p_command);
523 if (valid_command == 0)
528 uint32_t seq_nr = rand_r(&g_chrony_rand);
529 p_req->header.f_cmd = htons(p_command);
530 p_req->header.f_cmd_try = 0;
531 p_req->header.f_seq = seq_nr;
533 DEBUG(PLUGIN_NAME ": Sending request (.cmd = %d, .seq = %d)",p_command, seq_nr);
534 if (chrony_send_request(p_req,req_size) != 0)
539 DEBUG(PLUGIN_NAME ": Waiting for response");
540 if (chrony_recv_response(p_resp,resp_size,p_resp_size) != 0)
544 DEBUG(PLUGIN_NAME ": Received response: .version = %u, .type = %u, .cmd = %u, .reply = %u, .status = %u, .seq = %u",
545 p_resp->header.f_version, p_resp->header.f_type, ntohs(p_resp->header.f_cmd),
546 ntohs(p_resp->header.f_reply), ntohs(p_resp->header.f_status), p_resp->header.f_seq);
548 if (p_resp->header.f_version != p_req->header.f_version)
550 ERROR(PLUGIN_NAME ": Wrong protocol version (Was: %d, expected: %d)", p_resp->header.f_version, p_req->header.f_version);
551 return CHRONY_RC_FAIL;
553 if (p_resp->header.f_type != PKT_TYPE_CMD_REPLY)
555 ERROR(PLUGIN_NAME ": Wrong packet type (Was: %d, expected: %d)", p_resp->header.f_type, PKT_TYPE_CMD_REPLY);
556 return CHRONY_RC_FAIL;
558 if (p_resp->header.f_seq != seq_nr)
560 /* FIXME: Implement sequence number handling */
561 ERROR(PLUGIN_NAME ": Unexpected sequence number (Was: %d, expected: %d)", p_resp->header.f_seq, p_req->header.f_seq);
562 return CHRONY_RC_FAIL;
564 if (p_resp->header.f_cmd != p_req->header.f_cmd)
566 ERROR(PLUGIN_NAME ": Wrong reply command (Was: %d, expected: %d)", p_resp->header.f_cmd, p_req->header.f_cmd);
567 return CHRONY_RC_FAIL;
570 if (ntohs(p_resp->header.f_reply) != resp_code)
572 ERROR(PLUGIN_NAME ": Wrong reply code (Was: %d, expected: %d)", ntohs(p_resp->header.f_reply), resp_code);
573 return CHRONY_RC_FAIL;
576 switch (p_resp->header.f_status)
579 DEBUG(PLUGIN_NAME ": Reply packet status STT_SUCCESS");
582 ERROR(PLUGIN_NAME ": Reply packet contains error status: %d (expected: %d)", p_resp->header.f_status, STT_SUCCESS);
583 return CHRONY_RC_FAIL;
590 /* Some error occured */
591 return CHRONY_RC_FAIL;
594 static void chrony_init_req(tChrony_Request *p_req)
596 memset(p_req,0,sizeof(*p_req));
597 p_req->header.f_version = PROTO_VERSION_NUMBER;
598 p_req->header.f_type = PKT_TYPE_CMD_REQUEST;
599 p_req->header.f_dummy0 = 0;
600 p_req->header.f_dummy1 = 0;
601 p_req->header.f_dummy2 = 0;
602 p_req->header.f_dummy3 = 0;
605 /* Code from: git://git.tuxfamily.org/gitroot/chrony/chrony.git:util.c (GPLv2) */
607 #define FLOAT_EXP_BITS 7
608 #define FLOAT_EXP_MIN (-(1 << (FLOAT_EXP_BITS - 1)))
609 #define FLOAT_EXP_MAX (-FLOAT_EXP_MIN - 1)
610 #define FLOAT_COEF_BITS ((int)sizeof (int32_t) * 8 - FLOAT_EXP_BITS)
611 #define FLOAT_COEF_MIN (-(1 << (FLOAT_COEF_BITS - 1)))
612 #define FLOAT_COEF_MAX (-FLOAT_COEF_MIN - 1)
614 /* double UTI_tFloatNetworkToHost(tFloat f) */
615 static double ntohf(tFloat p_float)
617 /* Convert tFloat in Network-bit-order to double in host-bit-order */
622 uval = ntohl(p_float.value);
623 exp = (uval >> FLOAT_COEF_BITS) - FLOAT_COEF_BITS;
624 if (exp >= 1 << (FLOAT_EXP_BITS - 1))
626 exp -= 1 << FLOAT_EXP_BITS;
629 /* coef = (x << FLOAT_EXP_BITS) >> FLOAT_EXP_BITS; */
630 coef = uval % (1U << FLOAT_COEF_BITS);
631 if (coef >= 1 << (FLOAT_COEF_BITS - 1))
633 coef -= 1 << FLOAT_COEF_BITS;
635 return coef * pow(2.0, exp);
639 static void chrony_push_data(char *p_type, char *p_type_inst, double p_value)
642 value_list_t vl = VALUE_LIST_INIT;
644 values[0].gauge = p_value; /* TODO: Check type??? (counter, gauge, derive, absolute) */
649 /* XXX: Shall g_chrony_host/g_chrony_port be reflected in the plugin's output? */
650 /* hostname_g is set in daemon/collectd.c (from config, via gethostname or by resolving localhost) */
651 /* defined as: char hostname_g[DATA_MAX_NAME_LEN]; (never NULL) */
652 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
653 sstrncpy (vl.plugin, PLUGIN_NAME_SHORT, sizeof (vl.plugin));
654 if (g_plugin_instance != NULL) { sstrncpy (vl.plugin_instance, g_plugin_instance, sizeof (vl.plugin_instance)); }
655 if (p_type != NULL) { sstrncpy (vl.type, p_type, sizeof (vl.type)); }
656 if (p_type_inst != NULL) { sstrncpy (vl.type_instance, p_type_inst, sizeof (vl.type_instance)); }
658 plugin_dispatch_values (&vl);
662 static void chrony_push_data_valid(char *p_type, char *p_type_inst, const int p_is_valid, double p_value)
664 /* Push real value if p_is_valid is true, push NAN if p_is_valid is not true (idea from ntp plugin)*/
669 chrony_push_data(p_type, p_type_inst, p_value);
673 static int chrony_init_seq()
675 /* Initialize the sequence number generator from /dev/urandom */
678 int fh = open(URAND_DEVICE_PATH, O_RDONLY);
681 ssize_t rc = read(fh, &g_chrony_rand, sizeof(g_chrony_rand));
682 if (rc != sizeof(g_chrony_rand))
684 ERROR (PLUGIN_NAME ": Reading from random source \'%s\'failed: %s (%d)", URAND_DEVICE_PATH, strerror(errno), errno);
686 return CHRONY_RC_FAIL;
689 DEBUG(PLUGIN_NAME ": Seeding RNG from " URAND_DEVICE_PATH);
693 /* URAND_DEVICE_PATH device not found. Try RAND_DEVICE_PATH as fall-back */
694 int fh = open(RAND_DEVICE_PATH, O_RDONLY);
697 ssize_t rc = read(fh, &g_chrony_rand, sizeof(g_chrony_rand));
698 if (rc != sizeof(g_chrony_rand))
700 ERROR (PLUGIN_NAME ": Reading from random source \'%s\'failed: %s (%d)", RAND_DEVICE_PATH, strerror(errno), errno);
702 return CHRONY_RC_FAIL;
705 DEBUG(PLUGIN_NAME ": Seeding RNG from " RAND_DEVICE_PATH);
707 /* Error opening RAND_DEVICE_PATH. Try time(NULL) as fall-back */
708 DEBUG(PLUGIN_NAME ": Seeding RNG from time(NULL)");
709 g_chrony_rand = time(NULL) ^ getpid();
712 ERROR (PLUGIN_NAME ": Opening random source \'%s\' failed: %s (%d)", URAND_DEVICE_PATH, strerror(errno), errno);
713 return CHRONY_RC_FAIL;
721 /*****************************************************************************/
722 /* Exported functions */
723 /*****************************************************************************/
724 static int chrony_config(const char *p_key, const char *p_value)
728 /* Parse config variables */
729 if (strcasecmp(p_key, CONFIG_KEY_HOST) == 0)
731 if (g_chrony_host != NULL)
733 free (g_chrony_host);
735 if ((g_chrony_host = strdup (p_value)) == NULL)
737 ERROR (PLUGIN_NAME ": Error duplicating host name");
738 return CHRONY_RC_FAIL;
740 } else if (strcasecmp(p_key, CONFIG_KEY_PORT) == 0)
742 if (g_chrony_port != NULL)
744 free (g_chrony_port);
746 if ((g_chrony_port = strdup (p_value)) == NULL)
748 ERROR (PLUGIN_NAME ": Error duplicating port name");
749 return CHRONY_RC_FAIL;
751 } else if (strcasecmp(p_key, CONFIG_KEY_TIMEOUT) == 0)
753 time_t tosec = strtol(p_value,NULL,0);
754 g_chrony_timeout = tosec;
756 WARNING(PLUGIN_NAME ": Unknown configuration variable: %s %s",p_key,p_value);
757 return CHRONY_RC_FAIL;
759 /* 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 */
765 static int chrony_request_daemon_stats()
767 /* Perform Tracking request */
769 size_t chrony_resp_size;
770 tChrony_Request chrony_req;
771 tChrony_Response chrony_resp;
773 chrony_init_req(&chrony_req);
774 rc = chrony_query(REQ_TRACKING, &chrony_req, &chrony_resp, &chrony_resp_size);
777 ERROR (PLUGIN_NAME ": chrony_query (REQ_TRACKING) failed with status %i", rc);
783 char src_addr[IPV6_STR_MAX_SIZE];
784 memset(src_addr, 0, sizeof(src_addr));
785 niptoha(&chrony_resp.body.tracking.addr, src_addr, sizeof(src_addr));
786 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",
788 ntohs(chrony_resp.body.tracking.f_ref_id), //FIXME: 16bit
789 ntohs(chrony_resp.body.tracking.f_stratum),
790 ntohs(chrony_resp.body.tracking.f_leap_status),
791 ntohl(chrony_resp.body.tracking.f_ref_time.tv_sec_high),
792 ntohl(chrony_resp.body.tracking.f_ref_time.tv_sec_low),
793 ntohl(chrony_resp.body.tracking.f_ref_time.tv_nsec),
794 ntohf(chrony_resp.body.tracking.f_current_correction),
795 ntohf(chrony_resp.body.tracking.f_last_offset),
796 ntohf(chrony_resp.body.tracking.f_rms_offset),
797 ntohf(chrony_resp.body.tracking.f_freq_ppm),
798 ntohf(chrony_resp.body.tracking.f_skew_ppm),
799 ntohf(chrony_resp.body.tracking.f_root_delay),
800 ntohf(chrony_resp.body.tracking.f_root_dispersion),
801 ntohf(chrony_resp.body.tracking.f_last_update_interval)
806 double time_ref = ntohl(chrony_resp.body.tracking.f_ref_time.tv_nsec);
807 time_ref /= 1000000000.0;
808 time_ref += ntohl(chrony_resp.body.tracking.f_ref_time.tv_sec_low);
809 if (chrony_resp.body.tracking.f_ref_time.tv_sec_high)
811 double secs_high = ntohl(chrony_resp.body.tracking.f_ref_time.tv_sec_high);
812 secs_high *= 4294967296.0;
813 time_ref += secs_high;
816 /* Forward results to collectd-daemon */
817 /* Type_instance is always 'chrony' to tag daemon-wide data */
818 /* Type Type_instance Value */
819 chrony_push_data("clock_stratum", DAEMON_NAME, ntohs(chrony_resp.body.tracking.f_stratum));
820 chrony_push_data("time_ref", DAEMON_NAME, time_ref); /* unit: s */
821 chrony_push_data("time_offset_ntp", DAEMON_NAME, ntohf(chrony_resp.body.tracking.f_current_correction)); /* Offset between system time and NTP, unit: s */
822 chrony_push_data("time_offset", DAEMON_NAME, ntohf(chrony_resp.body.tracking.f_last_offset)); /* Estimated Offset of the NTP time, unit: s */
823 chrony_push_data("time_offset_rms", DAEMON_NAME, ntohf(chrony_resp.body.tracking.f_rms_offset)); /* averaged value of the above, unit: s */
824 chrony_push_data("frequency_error", DAEMON_NAME, ntohf(chrony_resp.body.tracking.f_freq_ppm)); /* Frequency error of the local osc, unit: ppm */
825 chrony_push_data("clock_skew_ppm", DAEMON_NAME, ntohf(chrony_resp.body.tracking.f_skew_ppm));
826 chrony_push_data("root_delay", DAEMON_NAME, ntohf(chrony_resp.body.tracking.f_root_delay)); /* Network latency between local daemon and the current source */
827 chrony_push_data("root_dispersion", DAEMON_NAME, ntohf(chrony_resp.body.tracking.f_root_dispersion));
828 chrony_push_data("clock_last_update",DAEMON_NAME, ntohf(chrony_resp.body.tracking.f_last_update_interval));
834 static int chrony_request_sources_count(unsigned int *p_count)
836 /* Requests the number of time sources from the chrony daemon */
838 size_t chrony_resp_size;
839 tChrony_Request chrony_req;
840 tChrony_Response chrony_resp;
842 DEBUG(PLUGIN_NAME ": Requesting data");
843 chrony_init_req(&chrony_req);
844 rc = chrony_query (REQ_N_SOURCES, &chrony_req, &chrony_resp, &chrony_resp_size);
847 ERROR (PLUGIN_NAME ": chrony_query (REQ_N_SOURCES) failed with status %i", rc);
851 *p_count = ntohl(chrony_resp.body.n_sources.f_n_sources);
852 DEBUG(PLUGIN_NAME ": Getting data of %d clock sources", *p_count);
858 static int chrony_request_source_data(int p_src_idx, int *p_is_reachable)
860 /* Perform Source data request for source #p_src_idx*/
862 size_t chrony_resp_size;
863 tChrony_Request chrony_req;
864 tChrony_Response chrony_resp;
866 char src_addr[IPV6_STR_MAX_SIZE];
867 memset(src_addr, 0, sizeof(src_addr));
869 chrony_init_req(&chrony_req);
870 chrony_req.body.source_data.f_index = htonl(p_src_idx);
871 rc = chrony_query(REQ_SOURCE_DATA, &chrony_req, &chrony_resp, &chrony_resp_size);
874 ERROR (PLUGIN_NAME ": chrony_query (REQ_SOURCE_DATA) failed with status %i", rc);
878 niptoha(&chrony_resp.body.source_data.addr, src_addr, sizeof(src_addr));
879 DEBUG(PLUGIN_NAME ": 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",
882 ntohs(chrony_resp.body.source_data.f_poll),
883 ntohs(chrony_resp.body.source_data.f_stratum),
884 ntohs(chrony_resp.body.source_data.f_state),
885 ntohs(chrony_resp.body.source_data.f_mode),
886 ntohs(chrony_resp.body.source_data.f_flags),
887 ntohs(chrony_resp.body.source_data.f_reachability),
888 ntohl(chrony_resp.body.source_data.f_since_sample),
889 ntohf(chrony_resp.body.source_data.f_origin_latest_meas),
890 ntohf(chrony_resp.body.source_data.f_latest_meas),
891 ntohf(chrony_resp.body.source_data.f_latest_meas_err)
894 /* Push NaN if source is currently not reachable */
895 int is_reachable = ntohs(chrony_resp.body.source_data.f_reachability) & 0x01;
896 *p_is_reachable = is_reachable;
898 /* Forward results to collectd-daemon */
899 chrony_push_data_valid("clock_stratum", src_addr, is_reachable, ntohs(chrony_resp.body.source_data.f_stratum));
900 chrony_push_data_valid("clock_state", src_addr, is_reachable, ntohs(chrony_resp.body.source_data.f_state));
901 chrony_push_data_valid("clock_mode", src_addr, is_reachable, ntohs(chrony_resp.body.source_data.f_mode));
902 chrony_push_data_valid("clock_reachability",src_addr, is_reachable, ntohs(chrony_resp.body.source_data.f_reachability));
903 chrony_push_data_valid("clock_last_meas", src_addr, is_reachable, ntohs(chrony_resp.body.source_data.f_since_sample));
909 static int chrony_request_source_stats(int p_src_idx, const int *p_is_reachable)
911 /* Perform Source stats request for source #p_src_idx */
913 size_t chrony_resp_size;
914 tChrony_Request chrony_req;
915 tChrony_Response chrony_resp;
916 double skew_ppm, frequency_error, time_offset;
918 char src_addr[IPV6_STR_MAX_SIZE];
919 memset(src_addr, 0, sizeof(src_addr));
921 if (*p_is_reachable == 0)
927 chrony_init_req(&chrony_req);
928 chrony_req.body.source_stats.f_index = htonl(p_src_idx);
929 rc = chrony_query(REQ_SOURCE_STATS, &chrony_req, &chrony_resp, &chrony_resp_size);
932 ERROR (PLUGIN_NAME ": chrony_query (REQ_SOURCE_STATS) failed with status %i", rc);
936 skew_ppm = ntohf(chrony_resp.body.source_stats.f_skew_ppm);
937 frequency_error = ntohf(chrony_resp.body.source_stats.f_rtc_gain_rate_ppm);
938 time_offset = ntohf(chrony_resp.body.source_stats.f_est_offset);
940 niptoha(&chrony_resp.body.source_stats.addr, src_addr, sizeof(src_addr));
941 DEBUG(PLUGIN_NAME ": Source[%d] stat: .addr = %s, .ref_id= %u, .n_samples = %u, " \
942 ".n_runs = %u, .span_seconds = %u, .rtc_seconds_fast = %f, " \
943 ".rtc_gain_rate_ppm = %f, .skew_ppm= %f, .est_offset = %f, .est_offset_err = %f",
946 ntohl(chrony_resp.body.source_stats.f_ref_id),
947 ntohl(chrony_resp.body.source_stats.f_n_samples),
948 ntohl(chrony_resp.body.source_stats.f_n_runs),
949 ntohl(chrony_resp.body.source_stats.f_span_seconds),
950 ntohf(chrony_resp.body.source_stats.f_rtc_seconds_fast),
954 ntohf(chrony_resp.body.source_stats.f_est_offset_err)
957 } //if (*is_reachable)
959 /* Forward results to collectd-daemon */
960 chrony_push_data_valid("clock_skew_ppm", src_addr, *p_is_reachable, skew_ppm);
961 chrony_push_data_valid("frequency_error", src_addr, *p_is_reachable, frequency_error); /* unit: ppm */
962 chrony_push_data_valid("time_offset", src_addr, *p_is_reachable, time_offset); /* unit: s */
967 static int chrony_read()
969 /* collectd read callback: Perform data acquisition */
971 unsigned int now_src, n_sources;
973 if (g_chrony_seq_is_initialized == 0)
975 /* Seed RNG for sequence number generation */
976 rc = chrony_init_seq();
977 if (rc != CHRONY_RC_OK)
981 g_chrony_seq_is_initialized = 1;
984 /* Get daemon stats */
985 rc = chrony_request_daemon_stats();
986 if (rc != CHRONY_RC_OK)
991 /* Get number of time sources, then check every source for status */
992 rc = chrony_request_sources_count(&n_sources);
993 if (rc != CHRONY_RC_OK)
998 for (now_src = 0; now_src < n_sources; ++now_src)
1001 rc = chrony_request_source_data(now_src, &is_reachable);
1002 if (rc != CHRONY_RC_OK)
1007 rc = chrony_request_source_stats(now_src, &is_reachable);
1008 if (rc != CHRONY_RC_OK)
1013 return CHRONY_RC_OK;
1017 static int chrony_shutdown()
1019 /* Collectd shutdown callback: Free mem */
1020 if (g_is_connected != 0)
1022 close(g_chrony_socket);
1025 if (g_chrony_host != NULL)
1027 free (g_chrony_host);
1028 g_chrony_host = NULL;
1030 if (g_chrony_port != NULL)
1032 free (g_chrony_port);
1033 g_chrony_port = NULL;
1035 if (g_plugin_instance != NULL)
1037 free (g_plugin_instance);
1038 g_plugin_instance = NULL;
1040 return CHRONY_RC_OK;
1044 void module_register (void)
1046 plugin_register_config( PLUGIN_NAME_SHORT, chrony_config, g_config_keys, g_config_keys_num);
1047 plugin_register_read( PLUGIN_NAME_SHORT, chrony_read);
1048 plugin_register_shutdown(PLUGIN_NAME_SHORT, chrony_shutdown);