Fix: Added config.h include, Fixed indentation
[collectd.git] / src / chrony.c
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)
5 License: GPL2
6 */
7 /* TODO:
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
12  */
13
14 #include "config.h"
15
16 #if HAVE_SYS_TYPES_H
17 #  include <sys/types.h> /* getaddrinfo */
18 #endif
19 #if HAVE_SYS_SOCKET_H
20 #  include <sys/socket.h>
21 #endif
22 #if HAVE_NETDB_H
23 #  include <netdb.h>
24 #endif
25 #if HAVE_ARPA_INET_H
26 #  include <arpa/inet.h> /* ntohs/ntohl */
27 #endif
28
29 #include "collectd.h"
30 #include "common.h" /* auxiliary functions */
31 #include "plugin.h" /* plugin_register_*, plugin_dispatch_values */
32
33 #define CONFIG_KEY_HOST    "Host"
34 #define CONFIG_KEY_PORT    "Port"
35 #define CONFIG_KEY_TIMEOUT "Timeout"
36
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)*/
39
40 static const char *g_config_keys[] =
41 {
42         CONFIG_KEY_HOST,
43         CONFIG_KEY_PORT,
44         CONFIG_KEY_TIMEOUT
45 };
46
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;
56
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
63
64 /* Return codes (collectd expects non-zero on errors) */
65 #define CHRONY_RC_OK    0
66 #define CHRONY_RC_FAIL  1
67
68 /* Variables adapted from chrony/candm.h (GPL2)*/
69 /*BEGIN*/
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)
75
76 typedef enum
77 {
78         PKT_TYPE_CMD_REQUEST = 1,
79         PKT_TYPE_CMD_REPLY   = 2
80 } ePacketType;
81
82 typedef enum
83 {
84         REQ_N_SOURCES    = 14,
85         REQ_SOURCE_DATA  = 15,
86         REQ_TRACKING     = 33,
87         REQ_SOURCE_STATS = 34
88 } eDaemonRequests;
89
90
91 typedef enum
92 {
93         RPY_NULL             = 1,
94         RPY_N_SOURCES        = 2,
95         RPY_SOURCE_DATA      = 3,
96         RPY_MANUAL_TIMESTAMP = 4,
97         RPY_TRACKING         = 5,
98         RPY_SOURCE_STATS     = 6,
99         RPY_RTC              = 7
100 } eDaemonReplies;
101
102 #define ATTRIB_PACKED __attribute__((packed))
103 typedef struct ATTRIB_PACKED
104 {
105         int32_t value;
106 } tFloat;
107
108 typedef struct ATTRIB_PACKED
109 {
110         uint32_t tv_sec_high;
111         uint32_t tv_sec_low;
112         uint32_t tv_nsec;
113 } tTimeval;
114 /*END*/
115
116 typedef enum
117 {
118         STT_SUCCESS        =  0,
119         STT_FAILED         =  1,
120         STT_UNAUTH         =  2,
121         STT_INVALID        =  3,
122         STT_NOSUCHSOURCE   =  4,
123         STT_INVALIDTS      =  5,
124         STT_NOTENABLED     =  6,
125         STT_BADSUBNET      =  7,
126         STT_ACCESSALLOWED  =  8,
127         STT_ACCESSDENIED   =  9,
128         STT_NOHOSTACCESS   = 10,
129         STT_SOURCEALREADYKNOWN = 11,
130         STT_TOOMANYSOURCES = 12,
131         STT_NORTC          = 13,
132         STT_BADRTCFILE     = 14,
133         STT_INACTIVE       = 15,
134         STT_BADSAMPLE      = 16,
135         STT_INVALIDAF      = 17,
136         STT_BADPKTVERSION  = 18,
137         STT_BADPKTLENGTH   = 19
138 } eChrony_Status;
139
140 /* Chrony client request packets */
141 typedef struct ATTRIB_PACKED
142 {
143         uint8_t f_dummy0[80]; //Chrony expects 80bytes dummy data (Avoiding UDP Amplification)
144 } tChrony_Req_Tracking;
145
146 typedef struct ATTRIB_PACKED
147 {
148         uint32_t f_n_sources;
149 } tChrony_Req_N_Sources;
150
151 typedef struct ATTRIB_PACKED
152 {
153         int32_t f_index;
154         uint8_t f_dummy0[44];
155 } tChrony_Req_Source_data;
156
157 typedef struct ATTRIB_PACKED
158 {
159         int32_t f_index;
160         uint8_t f_dummy0[56];
161 } tChrony_Req_Source_stats;
162
163 typedef struct ATTRIB_PACKED
164 {
165         struct
166         {
167                 uint8_t  f_version;
168                 uint8_t  f_type;
169                 uint8_t  f_dummy0;
170                 uint8_t  f_dummy1;
171                 uint16_t f_cmd;
172                 uint16_t f_cmd_try;
173                 uint32_t f_seq;
174
175                 uint32_t f_dummy2;
176                 uint32_t f_dummy3;
177         } header; /* Packed: 20Bytes */
178         union
179         {
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;
184         } body;
185         uint8_t padding[4+16]; /* Padding to match minimal response size */
186 } tChrony_Request;
187
188 /* Chrony daemon response packets */
189 typedef struct ATTRIB_PACKED
190 {
191         uint32_t f_n_sources;
192 } tChrony_Resp_N_Sources;
193
194 typedef struct ATTRIB_PACKED
195 {
196         union
197         {
198                 uint32_t ip4;
199                 uint8_t  ip6[16];
200         } addr;
201         uint16_t f_family;
202 } tChrony_IPAddr;
203
204 typedef struct ATTRIB_PACKED
205 {
206         tChrony_IPAddr addr;
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 */
215
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;
221
222 typedef struct ATTRIB_PACKED
223 {
224         uint32_t f_ref_id;
225         tChrony_IPAddr addr;
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;
236
237 typedef struct ATTRIB_PACKED
238 {
239         uint32_t f_ref_id;
240         tChrony_IPAddr addr;
241         uint16_t dummy;     /* FIXME: Strange dummy space. Needed on gcc 4.8.3/clang 3.4.1 on x86_64 */
242         uint16_t f_stratum;
243         uint16_t f_leap_status;
244         tTimeval f_ref_time;
245         tFloat   f_current_correction;
246         tFloat   f_last_offset;
247         tFloat   f_rms_offset;
248         tFloat   f_freq_ppm;
249         tFloat   f_resid_freq_ppm;
250         tFloat   f_skew_ppm;
251         tFloat   f_root_delay;
252         tFloat   f_root_dispersion;
253         tFloat   f_last_update_interval;
254 } tChrony_Resp_Tracking;
255
256 typedef struct ATTRIB_PACKED
257 {
258         struct
259         {
260                 uint8_t f_version;
261                 uint8_t f_type;
262                 uint8_t f_dummy0;
263                 uint8_t f_dummy1;
264                 uint16_t f_cmd;
265                 uint16_t f_reply;
266                 uint16_t f_status;
267                 uint16_t f_dummy2;
268                 uint16_t f_dummy3;
269                 uint16_t f_dummy4;
270                 uint32_t f_seq;
271                 uint32_t f_dummy5;
272                 uint32_t f_dummy6;
273         } header; /* Packed: 28 Bytes */
274
275         union
276         {
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;
281         } body;
282
283         uint8_t padding[1024];
284 } tChrony_Response;
285
286
287 /*****************************************************************************/
288 /* Internal functions */
289 /*****************************************************************************/
290 /* Code adapted from: http://long.ccaba.upc.edu/long/045Guidelines/eva/ipv6.html#daytimeClient6 */
291 /*BEGIN*/
292 static int connect_client (const char *p_hostname,
293                 const char *p_service,
294                 int         p_family,
295                 int         p_socktype)
296 {
297         struct addrinfo hints, *res=NULL, *ressave=NULL;
298         int n, sockfd;
299
300         memset(&hints, 0, sizeof(struct addrinfo));
301
302         hints.ai_family   = p_family;
303         hints.ai_socktype = p_socktype;
304
305         n = getaddrinfo(p_hostname, p_service, &hints, &res);
306
307         if (n <0)
308         {
309                 ERROR (PLUGIN_NAME ": getaddrinfo error:: [%s]", gai_strerror(n));
310                 return -1;
311         }
312
313         ressave = res;
314
315         sockfd=-1;
316         while (res)
317         {
318                 sockfd = socket(res->ai_family,
319                                 res->ai_socktype,
320                                 res->ai_protocol);
321
322                 if (!(sockfd < 0))
323                 {
324                         if (connect(sockfd, res->ai_addr, res->ai_addrlen) == 0)
325                         {
326                                 /* Success */
327                                 break;
328                         }
329
330                         close(sockfd);
331                         sockfd=-1;
332                 }
333                 res=res->ai_next;
334         }
335
336         freeaddrinfo(ressave);
337         return sockfd;
338 }
339
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)
343 {
344         int rc=1;
345         unsigned long a, b, c, d, ip;
346         const uint8_t *ip6;
347
348         switch (ntohs(addr->f_family))
349         {
350                 case IPADDR_UNSPEC:
351                         rc=snprintf(p_buf, p_buf_size, "[UNSPEC]");
352                         break;
353                 case IPADDR_INET4:
354                         ip = ntohl(addr->addr.ip4);
355                         a = (ip>>24) & 0xff;
356                         b = (ip>>16) & 0xff;
357                         c = (ip>> 8) & 0xff;
358                         d = (ip>> 0) & 0xff;
359                         rc=snprintf(p_buf, p_buf_size, "%ld.%ld.%ld.%ld", a, b, c, d);
360                         break;
361                 case IPADDR_INET6:
362                         ip6 = addr->addr.ip6;
363
364 #ifdef FEAT_IPV6
365                         rc=inet_ntop(AF_INET6, ip6, p_buf, p_bug_size);
366 #else
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]);
371 #else
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]);
375 #endif
376 #endif
377                         break;
378                 default:
379                         rc=snprintf(p_buf, p_buf_size, "[UNKNOWN]");
380         }
381         assert(rc>0);
382         return p_buf;
383 }
384 /*END*/
385
386 static int chrony_set_timeout()
387 {
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)*/
390
391         struct timeval tv;
392         tv.tv_sec  = g_chrony_timeout;
393         tv.tv_usec = 0;
394
395         assert(g_chrony_socket>=0);
396         if (setsockopt(g_chrony_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval)) < 0)
397         {
398                 return CHRONY_RC_FAIL;
399         }
400         return CHRONY_RC_OK;
401 }
402
403 static int chrony_connect()
404 {
405         /*Connects to the chrony daemon*/
406         /*Returns 0 on success, !0 on error (check errno)*/
407         int socket;
408
409         if (g_chrony_host == NULL)
410         {
411                 g_chrony_host = strdup(CHRONY_DEFAULT_HOST);
412                 assert(g_chrony_host);
413         }
414         if (g_chrony_port == NULL)
415         {
416                 g_chrony_port = strdup(CHRONY_DEFAULT_PORT);
417                 assert(g_chrony_port);
418         }
419         if (g_chrony_timeout < 0)
420         {
421                 g_chrony_timeout = CHRONY_DEFAULT_TIMEOUT;
422                 assert(g_chrony_timeout>=0);
423         }
424
425
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);
428         if (socket < 0)
429         {
430                 ERROR (PLUGIN_NAME ": Error connecting to daemon. Errno = %d", errno);
431                 return CHRONY_RC_FAIL;
432         }
433         DEBUG(PLUGIN_NAME ": Connected");
434         g_chrony_socket = socket;
435
436         if (chrony_set_timeout())
437         {
438                 ERROR (PLUGIN_NAME ": Error setting timeout to %lds. Errno = %d", g_chrony_timeout, errno);
439                 return CHRONY_RC_FAIL;
440         }
441         return CHRONY_RC_OK;
442 }
443
444 static int chrony_send_request(const tChrony_Request *p_req, size_t p_req_size)
445 {
446         if (send(g_chrony_socket,p_req,p_req_size,0) < 0)
447         {
448                 ERROR (PLUGIN_NAME ": Error sending packet. Errno = %d", errno);
449                 return CHRONY_RC_FAIL;
450         }
451         return CHRONY_RC_OK;
452 }
453
454 static int chrony_recv_response(tChrony_Response *p_resp, size_t p_resp_max_size, size_t *p_resp_size)
455 {
456         ssize_t rc = recv(g_chrony_socket,p_resp,p_resp_max_size,0);
457         if (rc <= 0)
458         {
459                 ERROR (PLUGIN_NAME ": Error receiving packet: %s (%d)", strerror(errno), errno);
460                 return CHRONY_RC_FAIL;
461         } else {
462                 *p_resp_size = rc;
463                 return CHRONY_RC_OK;
464         }
465 }
466
467 static int chrony_query(const int p_command, tChrony_Request *p_req, tChrony_Response *p_resp, size_t *p_resp_size)
468 {
469         /* Check connection. We simply perform one try as collectd already handles retries */
470         assert(p_req);
471         assert(p_resp);
472         assert(p_resp_size);
473
474         if (g_is_connected == 0)
475         {
476                 if (chrony_connect() == 0)
477                 {
478                         g_is_connected = 1;
479                 } else {
480                         ERROR (PLUGIN_NAME ": Unable to connect. Errno = %d", errno);
481                         return CHRONY_RC_FAIL;
482                 }
483         }
484
485
486         do
487         {
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;
492                 switch (p_command)
493                 {
494                         case REQ_TRACKING:
495                                 req_size  += sizeof(p_req->body.tracking);
496                                 resp_size += sizeof(p_resp->body.tracking); 
497                                 resp_code  = RPY_TRACKING;
498                                 valid_command = 1;
499                                 break;
500                         case REQ_N_SOURCES:
501                                 req_size  += sizeof(p_req->body.n_sources);
502                                 resp_size += sizeof(p_resp->body.n_sources); 
503                                 resp_code  = RPY_N_SOURCES;
504                                 valid_command = 1;
505                                 break;
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;
510                                 valid_command = 1;
511                                 break;
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;
516                                 valid_command = 1;
517                                 break;
518                         default:
519                                 ERROR (PLUGIN_NAME ": Unknown request command (Was: %d)", p_command);
520                                 break;
521                 }
522
523                 if (valid_command == 0)
524                 {
525                         break;
526                 }
527
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;
532
533                 DEBUG(PLUGIN_NAME ": Sending request (.cmd = %d, .seq = %d)",p_command, seq_nr);
534                 if (chrony_send_request(p_req,req_size) != 0)
535                 {
536                         break;
537                 }
538
539                 DEBUG(PLUGIN_NAME ": Waiting for response");
540                 if (chrony_recv_response(p_resp,resp_size,p_resp_size) != 0)
541                 {
542                         break;
543                 }
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);
547
548                 if (p_resp->header.f_version != p_req->header.f_version)
549                 {
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;
552                 }
553                 if (p_resp->header.f_type != PKT_TYPE_CMD_REPLY)
554                 {
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;
557                 }
558                 if (p_resp->header.f_seq != seq_nr)
559                 {
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;
563                 }
564                 if (p_resp->header.f_cmd != p_req->header.f_cmd)
565                 {
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;
568                 }
569
570                 if (ntohs(p_resp->header.f_reply) !=  resp_code)
571                 {
572                         ERROR(PLUGIN_NAME ": Wrong reply code (Was: %d, expected: %d)", ntohs(p_resp->header.f_reply), resp_code);
573                         return CHRONY_RC_FAIL;
574                 }
575
576                 switch (p_resp->header.f_status)
577                 {
578                         case STT_SUCCESS:
579                                 DEBUG(PLUGIN_NAME ": Reply packet status STT_SUCCESS");
580                                 break;
581                         default:
582                                 ERROR(PLUGIN_NAME ": Reply packet contains error status: %d (expected: %d)", p_resp->header.f_status, STT_SUCCESS);
583                                 return CHRONY_RC_FAIL;
584                 }
585
586                 /* Good result */
587                 return CHRONY_RC_OK;
588         } while (0);
589
590         /* Some error occured */
591         return CHRONY_RC_FAIL;
592 }
593
594 static void chrony_init_req(tChrony_Request *p_req)
595 {
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;
603 }
604
605 /* Code from: git://git.tuxfamily.org/gitroot/chrony/chrony.git:util.c (GPLv2) */
606 /*BEGIN*/
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)
613
614 /* double UTI_tFloatNetworkToHost(tFloat f) */
615 static double ntohf(tFloat p_float)
616 {
617         /* Convert tFloat in Network-bit-order to double in host-bit-order */
618
619         int32_t exp, coef;
620         uint32_t uval;
621
622         uval = ntohl(p_float.value);
623         exp = (uval >> FLOAT_COEF_BITS) - FLOAT_COEF_BITS;
624         if (exp >= 1 << (FLOAT_EXP_BITS - 1))
625         {
626                 exp -= 1 << FLOAT_EXP_BITS;
627         }
628
629         /* coef = (x << FLOAT_EXP_BITS) >> FLOAT_EXP_BITS; */
630         coef = uval % (1U << FLOAT_COEF_BITS);
631         if (coef >= 1 << (FLOAT_COEF_BITS - 1))
632         {
633                 coef -= 1 << FLOAT_COEF_BITS; 
634         }
635         return coef * pow(2.0, exp);
636 }
637 /*END*/
638
639 static void chrony_push_data(char *p_type, char *p_type_inst, double p_value)
640 {
641         value_t values[1];
642         value_list_t vl = VALUE_LIST_INIT;
643
644         values[0].gauge = p_value; /* TODO: Check type??? (counter, gauge, derive, absolute) */
645
646         vl.values     = values;
647         vl.values_len = 1;
648
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)); }
657
658         plugin_dispatch_values (&vl);
659 }
660
661
662 static void chrony_push_data_valid(char *p_type, char *p_type_inst, const int p_is_valid, double p_value)
663 {
664         /* Push real value if p_is_valid is true, push NAN if p_is_valid is not true (idea from ntp plugin)*/
665         if (p_is_valid == 0)
666         {
667                 p_value = NAN;
668         }
669         chrony_push_data(p_type, p_type_inst, p_value);
670 }
671
672
673 static int chrony_init_seq()
674 {
675         /* Initialize the sequence number generator from /dev/urandom */
676
677         /* Try urandom */
678         int fh = open(URAND_DEVICE_PATH, O_RDONLY);
679         if (fh >= 0)
680         {
681                 ssize_t rc = read(fh, &g_chrony_rand, sizeof(g_chrony_rand));
682                 if (rc != sizeof(g_chrony_rand))
683                 {
684                         ERROR (PLUGIN_NAME ": Reading from random source \'%s\'failed: %s (%d)", URAND_DEVICE_PATH, strerror(errno), errno);
685                         close(fh);
686                         return CHRONY_RC_FAIL;
687                 }
688                 close(fh);
689                 DEBUG(PLUGIN_NAME ": Seeding RNG from " URAND_DEVICE_PATH);
690         } else {
691                 if (errno == ENOENT)
692                 {
693                         /* URAND_DEVICE_PATH device not found. Try RAND_DEVICE_PATH as fall-back */
694                         int fh = open(RAND_DEVICE_PATH, O_RDONLY);
695                         if (fh >= 0)
696                         {
697                                 ssize_t rc = read(fh, &g_chrony_rand, sizeof(g_chrony_rand));
698                                 if (rc != sizeof(g_chrony_rand))
699                                 {
700                                         ERROR (PLUGIN_NAME ": Reading from random source \'%s\'failed: %s (%d)", RAND_DEVICE_PATH, strerror(errno), errno);
701                                         close(fh);
702                                         return CHRONY_RC_FAIL;
703                                 }
704                                 close(fh);
705                                 DEBUG(PLUGIN_NAME ": Seeding RNG from " RAND_DEVICE_PATH);
706                         } else {
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();
710                         }
711                 } else {
712                         ERROR (PLUGIN_NAME ": Opening random source \'%s\' failed: %s (%d)", URAND_DEVICE_PATH, strerror(errno), errno);
713                         return CHRONY_RC_FAIL;
714                 }
715         }
716
717
718         return CHRONY_RC_OK;
719 }
720
721 /*****************************************************************************/
722 /* Exported functions */
723 /*****************************************************************************/
724 static int chrony_config(const char *p_key, const char *p_value)
725 {
726         assert(p_key);
727         assert(p_value);
728         /* Parse config variables */
729         if (strcasecmp(p_key, CONFIG_KEY_HOST) == 0)
730         {
731                 if (g_chrony_host != NULL)
732                 {
733                         free (g_chrony_host);
734                 }
735                 if ((g_chrony_host = strdup (p_value)) == NULL)
736                 {
737                         ERROR (PLUGIN_NAME ": Error duplicating host name");
738                         return CHRONY_RC_FAIL;
739                 }
740         } else if (strcasecmp(p_key, CONFIG_KEY_PORT) == 0)
741         {
742                 if (g_chrony_port != NULL)
743                 {
744                         free (g_chrony_port);
745                 }
746                 if ((g_chrony_port = strdup (p_value)) == NULL)
747                 {
748                         ERROR (PLUGIN_NAME ": Error duplicating port name");
749                         return CHRONY_RC_FAIL;
750                 }
751         } else if (strcasecmp(p_key, CONFIG_KEY_TIMEOUT) == 0)
752         {
753                 time_t tosec = strtol(p_value,NULL,0);
754                 g_chrony_timeout = tosec;
755         } else {
756                 WARNING(PLUGIN_NAME ": Unknown configuration variable: %s %s",p_key,p_value);
757                 return CHRONY_RC_FAIL;
758         }
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 */
760
761         return CHRONY_RC_OK;
762 }
763
764
765 static int chrony_request_daemon_stats()
766 {
767         /* Perform Tracking request */
768         int rc;
769         size_t chrony_resp_size;
770         tChrony_Request  chrony_req;
771         tChrony_Response chrony_resp;
772
773         chrony_init_req(&chrony_req);
774         rc = chrony_query(REQ_TRACKING, &chrony_req, &chrony_resp, &chrony_resp_size);
775         if (rc != 0)
776         {
777                 ERROR (PLUGIN_NAME ": chrony_query (REQ_TRACKING) failed with status %i", rc);
778                 return rc;
779         }
780
781 #if COLLECT_DEBUG
782         {
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",
787                                 src_addr,
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)
802                      );
803         }
804 #endif
805
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)
810         {
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;
814         }
815
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));
829
830         return CHRONY_RC_OK;
831 }
832
833
834 static int chrony_request_sources_count(unsigned int *p_count)
835 {
836         /* Requests the number of time sources from the chrony daemon */
837         int rc;
838         size_t chrony_resp_size;
839         tChrony_Request  chrony_req;
840         tChrony_Response chrony_resp;
841
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);
845         if (rc != 0)
846         {
847                 ERROR (PLUGIN_NAME ": chrony_query (REQ_N_SOURCES) failed with status %i", rc);
848                 return rc;
849         }
850
851         *p_count = ntohl(chrony_resp.body.n_sources.f_n_sources);
852         DEBUG(PLUGIN_NAME ": Getting data of %d clock sources", *p_count);
853
854         return CHRONY_RC_OK;
855 }
856
857
858 static int chrony_request_source_data(int p_src_idx, int *p_is_reachable)
859 {
860         /* Perform Source data request for source #p_src_idx*/
861         int rc;
862         size_t chrony_resp_size;
863         tChrony_Request  chrony_req;
864         tChrony_Response chrony_resp;
865
866         char src_addr[IPV6_STR_MAX_SIZE];
867         memset(src_addr, 0, sizeof(src_addr));
868
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);
872         if (rc != 0)
873         {
874                 ERROR (PLUGIN_NAME ": chrony_query (REQ_SOURCE_DATA) failed with status %i", rc);
875                 return rc;
876         }
877
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",
880                         p_src_idx,
881                         src_addr,
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)
892              );
893
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;
897
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));
904
905         return CHRONY_RC_OK;
906 }
907
908
909 static int chrony_request_source_stats(int p_src_idx, const int *p_is_reachable)
910 {
911         /* Perform Source stats request for source #p_src_idx */
912         int rc;
913         size_t chrony_resp_size;
914         tChrony_Request  chrony_req;
915         tChrony_Response chrony_resp;
916         double skew_ppm, frequency_error, time_offset;
917
918         char src_addr[IPV6_STR_MAX_SIZE];
919         memset(src_addr, 0, sizeof(src_addr));
920
921         if (*p_is_reachable == 0)
922         {
923                 skew_ppm        = 0;
924                 frequency_error = 0;
925                 time_offset     = 0;
926         } else {
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);
930                 if (rc != 0)
931                 {
932                         ERROR (PLUGIN_NAME ": chrony_query (REQ_SOURCE_STATS) failed with status %i", rc);
933                         return rc;
934                 }
935
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);
939
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",
944                                 p_src_idx,
945                                 src_addr,
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),
951                                 frequency_error,
952                                 skew_ppm,
953                                 time_offset,
954                                 ntohf(chrony_resp.body.source_stats.f_est_offset_err)
955                      );
956
957         } //if (*is_reachable)
958
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 */
963
964         return CHRONY_RC_OK;
965 }
966
967 static int chrony_read()
968 {
969         /* collectd read callback: Perform data acquisition */
970         int  rc;
971         unsigned int now_src, n_sources;
972
973         if (g_chrony_seq_is_initialized == 0)
974         {
975                 /* Seed RNG for sequence number generation */
976                 rc = chrony_init_seq();
977                 if (rc != CHRONY_RC_OK)
978                 {
979                         return rc;
980                 }
981                 g_chrony_seq_is_initialized = 1;
982         }
983
984         /* Get daemon stats */
985         rc = chrony_request_daemon_stats();
986         if (rc != CHRONY_RC_OK)
987         {
988                 return rc;
989         }
990
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)
994         {
995                 return rc;
996         }
997
998         for (now_src = 0; now_src < n_sources; ++now_src)
999         {
1000                 int is_reachable;
1001                 rc = chrony_request_source_data(now_src, &is_reachable);
1002                 if (rc != CHRONY_RC_OK)
1003                 {
1004                         return rc;
1005                 }
1006
1007                 rc = chrony_request_source_stats(now_src, &is_reachable);
1008                 if (rc != CHRONY_RC_OK)
1009                 {
1010                         return rc;
1011                 }
1012         }
1013         return CHRONY_RC_OK;
1014 }
1015
1016
1017 static int chrony_shutdown()
1018 {
1019         /* Collectd shutdown callback: Free mem */
1020         if (g_is_connected != 0)
1021         {
1022                 close(g_chrony_socket);
1023                 g_is_connected = 0;
1024         }
1025         if (g_chrony_host != NULL)
1026         {
1027                 free (g_chrony_host);
1028                 g_chrony_host = NULL;
1029         }
1030         if (g_chrony_port != NULL)
1031         {
1032                 free (g_chrony_port);
1033                 g_chrony_port = NULL;
1034         }
1035         if (g_plugin_instance != NULL)
1036         {
1037                 free (g_plugin_instance);
1038                 g_plugin_instance = NULL;
1039         }
1040         return CHRONY_RC_OK;
1041 }
1042
1043
1044 void module_register (void)
1045 {
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);
1049 }
1050