Fixed indentation to match collectd's coding style
[collectd.git] / src / chrony.c
1 /* chrony plugin for collectd (monitoring of chrony time server daemon)
2  **********************************************************************
3  * Copyright (C) Claudius M Zingerli, ZSeng, 2015-2016 
4  *
5  * Internas roughly based on the ntpd plugin
6  * Some functions copied from chronyd/web (as marked)
7  * 
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.
11  * 
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.
16  * 
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.
20  * 
21  **********************************************************************
22 */
23 /* Formated using 'GNU indent -bli0 -ce -ppi 2' and some manual alignment
24  * TODO:
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
29  */
30
31 #include "config.h"
32
33 #if HAVE_SYS_TYPES_H
34 #  include <sys/types.h>        /* getaddrinfo */
35 #endif
36 #if HAVE_SYS_SOCKET_H
37 #  include <sys/socket.h>
38 #endif
39 #if HAVE_NETDB_H
40 #  include <netdb.h>
41 #endif
42 #if HAVE_ARPA_INET_H
43 #  include <arpa/inet.h>        /* ntohs/ntohl */
44 #endif
45
46 #include "collectd.h"
47 #include "common.h"             /* auxiliary functions */
48 #include "plugin.h"             /* plugin_register_*, plugin_dispatch_values */
49
50 #define CONFIG_KEY_HOST    "Host"
51 #define CONFIG_KEY_PORT    "Port"
52 #define CONFIG_KEY_TIMEOUT "Timeout"
53
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) */
56
57 static const char *g_config_keys[] = {
58   CONFIG_KEY_HOST,
59   CONFIG_KEY_PORT,
60   CONFIG_KEY_TIMEOUT
61 };
62
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;
72
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
79
80 /* Return codes (collectd expects non-zero on errors) */
81 #define CHRONY_RC_OK    0
82 #define CHRONY_RC_FAIL  1
83
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)
90
91 typedef enum
92 {
93   PKT_TYPE_CMD_REQUEST = 1,
94   PKT_TYPE_CMD_REPLY   = 2
95 } ePacketType;
96
97 typedef enum
98 {
99   REQ_N_SOURCES    = 14,
100   REQ_SOURCE_DATA  = 15,
101   REQ_TRACKING     = 33,
102   REQ_SOURCE_STATS = 34
103 } eDaemonRequests;
104
105
106 typedef enum
107 {
108   RPY_NULL             = 1,
109   RPY_N_SOURCES        = 2,
110   RPY_SOURCE_DATA      = 3,
111   RPY_MANUAL_TIMESTAMP = 4,
112   RPY_TRACKING         = 5,
113   RPY_SOURCE_STATS     = 6,
114   RPY_RTC              = 7
115 } eDaemonReplies;
116
117 #define ATTRIB_PACKED __attribute__((packed))
118 typedef struct ATTRIB_PACKED
119 {
120   int32_t value;
121 } tFloat;
122
123 typedef struct ATTRIB_PACKED
124 {
125   uint32_t tv_sec_high;
126   uint32_t tv_sec_low;
127   uint32_t tv_nsec;
128 } tTimeval;
129
130 typedef enum
131 {
132   STT_SUCCESS        =  0,
133   STT_FAILED         =  1,
134   STT_UNAUTH         =  2,
135   STT_INVALID        =  3,
136   STT_NOSUCHSOURCE   =  4,
137   STT_INVALIDTS      =  5,
138   STT_NOTENABLED     =  6,
139   STT_BADSUBNET      =  7,
140   STT_ACCESSALLOWED  =  8,
141   STT_ACCESSDENIED   =  9,
142   STT_NOHOSTACCESS   = 10,
143   STT_SOURCEALREADYKNOWN = 11,
144   STT_TOOMANYSOURCES = 12,
145   STT_NORTC          = 13,
146   STT_BADRTCFILE     = 14,
147   STT_INACTIVE       = 15,
148   STT_BADSAMPLE      = 16,
149   STT_INVALIDAF      = 17,
150   STT_BADPKTVERSION  = 18,
151   STT_BADPKTLENGTH   = 19
152 } eChrony_Status;
153
154 /* Chrony client request packets */
155 typedef struct ATTRIB_PACKED
156 {
157   uint8_t f_dummy0[80];         /* Chrony expects 80bytes dummy data (Avoiding UDP Amplification) */
158 } tChrony_Req_Tracking;
159
160 typedef struct ATTRIB_PACKED
161 {
162   uint32_t f_n_sources;
163 } tChrony_Req_N_Sources;
164
165 typedef struct ATTRIB_PACKED
166 {
167   int32_t f_index;
168   uint8_t f_dummy0[44];
169 } tChrony_Req_Source_data;
170
171 typedef struct ATTRIB_PACKED
172 {
173   int32_t f_index;
174   uint8_t f_dummy0[56];
175 } tChrony_Req_Source_stats;
176
177 typedef struct ATTRIB_PACKED
178 {
179   struct
180   {
181     uint8_t f_version;
182     uint8_t f_type;
183     uint8_t f_dummy0;
184     uint8_t f_dummy1;
185     uint16_t f_cmd;
186     uint16_t f_cmd_try;
187     uint32_t f_seq;
188
189     uint32_t f_dummy2;
190     uint32_t f_dummy3;
191   } header;                     /* Packed: 20Bytes */
192   union
193   {
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;
198   } body;
199   uint8_t padding[4 + 16];      /* Padding to match minimal response size */
200 } tChrony_Request;
201
202 /* Chrony daemon response packets */
203 typedef struct ATTRIB_PACKED
204 {
205   uint32_t f_n_sources;
206 } tChrony_Resp_N_Sources;
207
208 typedef struct ATTRIB_PACKED
209 {
210   union
211   {
212     uint32_t ip4;
213     uint8_t ip6[16];
214   } addr;
215   uint16_t f_family;
216 } tChrony_IPAddr;
217
218 typedef struct ATTRIB_PACKED
219 {
220   tChrony_IPAddr addr;
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 */
229
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;
235
236 typedef struct ATTRIB_PACKED
237 {
238   uint32_t f_ref_id;
239   tChrony_IPAddr addr;
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;
250
251 typedef struct ATTRIB_PACKED
252 {
253   uint32_t f_ref_id;
254   tChrony_IPAddr addr;
255   uint16_t dummy;               /* FIXME: Strange dummy space. Needed on gcc 4.8.3/clang 3.4.1 on x86_64 */
256   uint16_t f_stratum;
257   uint16_t f_leap_status;
258   tTimeval f_ref_time;
259   tFloat f_current_correction;
260   tFloat f_last_offset;
261   tFloat f_rms_offset;
262   tFloat f_freq_ppm;
263   tFloat f_resid_freq_ppm;
264   tFloat f_skew_ppm;
265   tFloat f_root_delay;
266   tFloat f_root_dispersion;
267   tFloat f_last_update_interval;
268 } tChrony_Resp_Tracking;
269
270 typedef struct ATTRIB_PACKED
271 {
272   struct
273   {
274     uint8_t f_version;
275     uint8_t f_type;
276     uint8_t f_dummy0;
277     uint8_t f_dummy1;
278     uint16_t f_cmd;
279     uint16_t f_reply;
280     uint16_t f_status;
281     uint16_t f_dummy2;
282     uint16_t f_dummy3;
283     uint16_t f_dummy4;
284     uint32_t f_seq;
285     uint32_t f_dummy5;
286     uint32_t f_dummy6;
287   } header;                     /* Packed: 28 Bytes */
288
289   union
290   {
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;
295   } body;
296
297   uint8_t padding[1024];
298 } tChrony_Response;
299
300
301 /*****************************************************************************/
302 /* Internal functions */
303 /*****************************************************************************/
304
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 */
307 static int
308 connect_client(const char *p_hostname,
309                const char *p_service, int p_family, int p_socktype)
310 {
311   struct addrinfo hints, *res = NULL, *ressave = NULL;
312   int n, sockfd;
313
314   memset(&hints, 0, sizeof(struct addrinfo));
315
316   hints.ai_family = p_family;
317   hints.ai_socktype = p_socktype;
318
319   n = getaddrinfo(p_hostname, p_service, &hints, &res);
320
321   if (n < 0)
322   {
323     ERROR(PLUGIN_NAME ": getaddrinfo error:: [%s]", gai_strerror(n));
324     return -1;
325   }
326
327   ressave = res;
328
329   sockfd = -1;
330   while (res)
331   {
332     sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
333
334     if (!(sockfd < 0))
335     {
336       if (connect(sockfd, res->ai_addr, res->ai_addrlen) == 0)
337       {
338         /* Success */
339         break;
340       }
341
342       close(sockfd);
343       sockfd = -1;
344     }
345     res = res->ai_next;
346   }
347
348   freeaddrinfo(ressave);
349   return sockfd;
350 }
351
352
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)*/
356 static char *
357 niptoha(const tChrony_IPAddr * addr, char *p_buf, size_t p_buf_size)
358 {
359   int rc = 1;
360   unsigned long a, b, c, d, ip;
361   const uint8_t *ip6;
362
363   switch (ntohs(addr->f_family))
364   {
365   case IPADDR_UNSPEC:
366     rc = snprintf(p_buf, p_buf_size, "[UNSPEC]");
367     break;
368   case IPADDR_INET4:
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);
375     break;
376   case IPADDR_INET6:
377     ip6 = addr->addr.ip6;
378
379 #ifdef FEAT_IPV6
380     rc = inet_ntop(AF_INET6, ip6, p_buf, p_bug_size);
381 #else
382 #  if defined(BYTE_ORDER) && (BYTE_ORDER == BIG_ENDIAN)
383     rc =
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],
388                ip6[1], ip6[0]);
389 #  else
390     rc =
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],
395                ip6[14], ip6[15]);
396 #  endif
397 #endif
398     break;
399   default:
400     rc = snprintf(p_buf, p_buf_size, "[UNKNOWN]");
401   }
402   assert(rc > 0);
403   return p_buf;
404 }
405
406
407 static int
408 chrony_set_timeout()
409 {
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) */
412
413   struct timeval tv;
414   tv.tv_sec = g_chrony_timeout;
415   tv.tv_usec = 0;
416
417   assert(g_chrony_socket >= 0);
418   if (setsockopt
419       (g_chrony_socket, SOL_SOCKET, SO_RCVTIMEO, (char *) &tv,
420        sizeof(struct timeval)) < 0)
421   {
422     return CHRONY_RC_FAIL;
423   }
424   return CHRONY_RC_OK;
425 }
426
427
428 static int
429 chrony_connect()
430 {
431   /*Connects to the chrony daemon */
432   /*Returns 0 on success, !0 on error (check errno) */
433   int socket;
434
435   if (g_chrony_host == NULL)
436   {
437     g_chrony_host = strdup(CHRONY_DEFAULT_HOST);
438     assert(g_chrony_host);
439   }
440   if (g_chrony_port == NULL)
441   {
442     g_chrony_port = strdup(CHRONY_DEFAULT_PORT);
443     assert(g_chrony_port);
444   }
445   if (g_chrony_timeout < 0)
446   {
447     g_chrony_timeout = CHRONY_DEFAULT_TIMEOUT;
448     assert(g_chrony_timeout >= 0);
449   }
450
451   DEBUG(PLUGIN_NAME ": Connecting to %s:%s", g_chrony_host, g_chrony_port);
452   socket =
453     connect_client(g_chrony_host, g_chrony_port, AF_UNSPEC, SOCK_DGRAM);
454   if (socket < 0)
455   {
456     ERROR(PLUGIN_NAME ": Error connecting to daemon. Errno = %d", errno);
457     return CHRONY_RC_FAIL;
458   }
459   DEBUG(PLUGIN_NAME ": Connected");
460   g_chrony_socket = socket;
461
462   if (chrony_set_timeout())
463   {
464     ERROR(PLUGIN_NAME ": Error setting timeout to %lds. Errno = %d",
465           g_chrony_timeout, errno);
466     return CHRONY_RC_FAIL;
467   }
468   return CHRONY_RC_OK;
469 }
470
471
472 static int
473 chrony_send_request(const tChrony_Request * p_req, size_t p_req_size)
474 {
475   if (send(g_chrony_socket, p_req, p_req_size, 0) < 0)
476   {
477     ERROR(PLUGIN_NAME ": Error sending packet. Errno = %d", errno);
478     return CHRONY_RC_FAIL;
479   }
480   return CHRONY_RC_OK;
481 }
482
483
484 static int
485 chrony_recv_response(tChrony_Response * p_resp, size_t p_resp_max_size,
486                      size_t * p_resp_size)
487 {
488   ssize_t rc = recv(g_chrony_socket, p_resp, p_resp_max_size, 0);
489   if (rc <= 0)
490   {
491     ERROR(PLUGIN_NAME ": Error receiving packet: %s (%d)", strerror(errno),
492           errno);
493     return CHRONY_RC_FAIL;
494   } else
495   {
496     *p_resp_size = rc;
497     return CHRONY_RC_OK;
498   }
499 }
500
501
502 static int
503 chrony_query(const int p_command, tChrony_Request * p_req,
504              tChrony_Response * p_resp, size_t * p_resp_size)
505 {
506   /* Check connection. We simply perform one try as collectd already handles retries */
507   assert(p_req);
508   assert(p_resp);
509   assert(p_resp_size);
510
511   if (g_is_connected == 0)
512   {
513     if (chrony_connect() == 0)
514     {
515       g_is_connected = 1;
516     } else
517     {
518       ERROR(PLUGIN_NAME ": Unable to connect. Errno = %d", errno);
519       return CHRONY_RC_FAIL;
520     }
521   }
522
523   do
524   {
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;
529     switch (p_command)
530     {
531     case REQ_TRACKING:
532       req_size += sizeof(p_req->body.tracking);
533       resp_size += sizeof(p_resp->body.tracking);
534       resp_code = RPY_TRACKING;
535       valid_command = 1;
536       break;
537     case REQ_N_SOURCES:
538       req_size += sizeof(p_req->body.n_sources);
539       resp_size += sizeof(p_resp->body.n_sources);
540       resp_code = RPY_N_SOURCES;
541       valid_command = 1;
542       break;
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;
547       valid_command = 1;
548       break;
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;
553       valid_command = 1;
554       break;
555     default:
556       ERROR(PLUGIN_NAME ": Unknown request command (Was: %d)", p_command);
557       break;
558     }
559
560     if (valid_command == 0)
561     {
562       break;
563     }
564
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;
569
570     DEBUG(PLUGIN_NAME ": Sending request (.cmd = %d, .seq = %d)", p_command,
571           seq_nr);
572     if (chrony_send_request(p_req, req_size) != 0)
573     {
574       break;
575     }
576
577     DEBUG(PLUGIN_NAME ": Waiting for response");
578     if (chrony_recv_response(p_resp, resp_size, p_resp_size) != 0)
579     {
580       break;
581     }
582     DEBUG(PLUGIN_NAME
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);
587
588     if (p_resp->header.f_version != p_req->header.f_version)
589     {
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;
593     }
594     if (p_resp->header.f_type != PKT_TYPE_CMD_REPLY)
595     {
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;
599     }
600     if (p_resp->header.f_seq != seq_nr)
601     {
602       /* FIXME: Implement sequence number handling */
603       ERROR(PLUGIN_NAME
604             ": Unexpected sequence number (Was: %d, expected: %d)",
605             p_resp->header.f_seq, p_req->header.f_seq);
606       return CHRONY_RC_FAIL;
607     }
608     if (p_resp->header.f_cmd != p_req->header.f_cmd)
609     {
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;
613     }
614
615     if (ntohs(p_resp->header.f_reply) != resp_code)
616     {
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;
620     }
621
622     switch (p_resp->header.f_status)
623     {
624     case STT_SUCCESS:
625       DEBUG(PLUGIN_NAME ": Reply packet status STT_SUCCESS");
626       break;
627     default:
628       ERROR(PLUGIN_NAME
629             ": Reply packet contains error status: %d (expected: %d)",
630             p_resp->header.f_status, STT_SUCCESS);
631       return CHRONY_RC_FAIL;
632     }
633
634     /* Good result */
635     return CHRONY_RC_OK;
636   }
637   while (0);
638
639   /* Some error occured */
640   return CHRONY_RC_FAIL;
641 }
642
643
644 static void
645 chrony_init_req(tChrony_Request * p_req)
646 {
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;
654 }
655
656
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) */
660 static double
661 ntohf(tFloat p_float)
662 {
663   /* Convert tFloat in Network-bit-order to double in host-bit-order */
664
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)
671
672   int32_t exp, coef;
673   uint32_t uval;
674
675   uval = ntohl(p_float.value);
676   exp = (uval >> FLOAT_COEF_BITS) - FLOAT_COEF_BITS;
677   if (exp >= 1 << (FLOAT_EXP_BITS - 1))
678   {
679     exp -= 1 << FLOAT_EXP_BITS;
680   }
681
682   /* coef = (x << FLOAT_EXP_BITS) >> FLOAT_EXP_BITS; */
683   coef = uval % (1U << FLOAT_COEF_BITS);
684   if (coef >= 1 << (FLOAT_COEF_BITS - 1))
685   {
686     coef -= 1 << FLOAT_COEF_BITS;
687   }
688   return coef * pow(2.0, exp);
689 }
690
691
692 static void
693 chrony_push_data(char *p_type, char *p_type_inst, double p_value)
694 {
695   value_t values[1];
696   value_list_t vl = VALUE_LIST_INIT;
697
698   values[0].gauge = p_value;    /* TODO: Check type??? (counter, gauge, derive, absolute) */
699
700   vl.values = values;
701   vl.values_len = 1;
702
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)
709   {
710     sstrncpy(vl.plugin_instance, g_plugin_instance,
711              sizeof(vl.plugin_instance));
712   }
713   if (p_type != NULL)
714   {
715     sstrncpy(vl.type, p_type, sizeof(vl.type));
716   }
717   if (p_type_inst != NULL)
718   {
719     sstrncpy(vl.type_instance, p_type_inst, sizeof(vl.type_instance));
720   }
721
722   plugin_dispatch_values(&vl);
723 }
724
725
726 static void
727 chrony_push_data_valid(char *p_type, char *p_type_inst, const int p_is_valid,
728                        double p_value)
729 {
730   /* Push real value if p_is_valid is true, push NAN if p_is_valid is not true (idea from ntp plugin) */
731   if (p_is_valid == 0)
732   {
733     p_value = NAN;
734   }
735   chrony_push_data(p_type, p_type_inst, p_value);
736 }
737
738
739 static int
740 chrony_init_seq()
741 {
742   /* Initialize the sequence number generator from /dev/urandom */
743   /* Fallbacks: /dev/random and time(NULL) */
744
745   /* Try urandom */
746   int fh = open(URAND_DEVICE_PATH, O_RDONLY);
747   if (fh >= 0)
748   {
749     ssize_t rc = read(fh, &g_chrony_rand, sizeof(g_chrony_rand));
750     if (rc != sizeof(g_chrony_rand))
751     {
752       ERROR(PLUGIN_NAME ": Reading from random source \'%s\'failed: %s (%d)",
753             URAND_DEVICE_PATH, strerror(errno), errno);
754       close(fh);
755       return CHRONY_RC_FAIL;
756     }
757     close(fh);
758     DEBUG(PLUGIN_NAME ": Seeding RNG from " URAND_DEVICE_PATH);
759   } else
760   {
761     if (errno == ENOENT)
762     {
763       /* URAND_DEVICE_PATH device not found. Try RAND_DEVICE_PATH as fall-back */
764       int fh = open(RAND_DEVICE_PATH, O_RDONLY);
765       if (fh >= 0)
766       {
767         ssize_t rc = read(fh, &g_chrony_rand, sizeof(g_chrony_rand));
768         if (rc != sizeof(g_chrony_rand))
769         {
770           ERROR(PLUGIN_NAME
771                 ": Reading from random source \'%s\'failed: %s (%d)",
772                 RAND_DEVICE_PATH, strerror(errno), errno);
773           close(fh);
774           return CHRONY_RC_FAIL;
775         }
776         close(fh);
777         DEBUG(PLUGIN_NAME ": Seeding RNG from " RAND_DEVICE_PATH);
778       } else
779       {
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();
783       }
784     } else
785     {
786       ERROR(PLUGIN_NAME ": Opening random source \'%s\' failed: %s (%d)",
787             URAND_DEVICE_PATH, strerror(errno), errno);
788       return CHRONY_RC_FAIL;
789     }
790   }
791
792   return CHRONY_RC_OK;
793 }
794
795
796 /*****************************************************************************/
797 /* Exported functions */
798 /*****************************************************************************/
799 static int
800 chrony_config(const char *p_key, const char *p_value)
801 {
802   assert(p_key);
803   assert(p_value);
804   /* Parse config variables */
805   if (strcasecmp(p_key, CONFIG_KEY_HOST) == 0)
806   {
807     if (g_chrony_host != NULL)
808     {
809       free(g_chrony_host);
810     }
811     if ((g_chrony_host = strdup(p_value)) == NULL)
812     {
813       ERROR(PLUGIN_NAME ": Error duplicating host name");
814       return CHRONY_RC_FAIL;
815     }
816   } else if (strcasecmp(p_key, CONFIG_KEY_PORT) == 0)
817   {
818     if (g_chrony_port != NULL)
819     {
820       free(g_chrony_port);
821     }
822     if ((g_chrony_port = strdup(p_value)) == NULL)
823     {
824       ERROR(PLUGIN_NAME ": Error duplicating port name");
825       return CHRONY_RC_FAIL;
826     }
827   } else if (strcasecmp(p_key, CONFIG_KEY_TIMEOUT) == 0)
828   {
829     time_t tosec = strtol(p_value, NULL, 0);
830     g_chrony_timeout = tosec;
831   } else
832   {
833     WARNING(PLUGIN_NAME ": Unknown configuration variable: %s %s", p_key,
834             p_value);
835     return CHRONY_RC_FAIL;
836   }
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 */
838
839   return CHRONY_RC_OK;
840 }
841
842
843 static int
844 chrony_request_daemon_stats()
845 {
846   /* Perform Tracking request */
847   int rc;
848   size_t chrony_resp_size;
849   tChrony_Request chrony_req;
850   tChrony_Response chrony_resp;
851
852   chrony_init_req(&chrony_req);
853   rc =
854     chrony_query(REQ_TRACKING, &chrony_req, &chrony_resp, &chrony_resp_size);
855   if (rc != 0)
856   {
857     ERROR(PLUGIN_NAME ": chrony_query (REQ_TRACKING) failed with status %i",
858           rc);
859     return rc;
860   }
861 #if COLLECT_DEBUG
862   {
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));
880   }
881 #endif
882
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)
887   {
888     double secs_high =
889       ntohl(chrony_resp.body.tracking.f_ref_time.tv_sec_high);
890     secs_high *= 4294967296.0;
891     time_ref += secs_high;
892   }
893
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));
907
908   return CHRONY_RC_OK;
909 }
910
911
912 static int
913 chrony_request_sources_count(unsigned int *p_count)
914 {
915   /* Requests the number of time sources from the chrony daemon */
916   int rc;
917   size_t chrony_resp_size;
918   tChrony_Request chrony_req;
919   tChrony_Response chrony_resp;
920
921   DEBUG(PLUGIN_NAME ": Requesting data");
922   chrony_init_req(&chrony_req);
923   rc =
924     chrony_query(REQ_N_SOURCES, &chrony_req, &chrony_resp, &chrony_resp_size);
925   if (rc != 0)
926   {
927     ERROR(PLUGIN_NAME ": chrony_query (REQ_N_SOURCES) failed with status %i",
928           rc);
929     return rc;
930   }
931
932   *p_count = ntohl(chrony_resp.body.n_sources.f_n_sources);
933   DEBUG(PLUGIN_NAME ": Getting data of %d clock sources", *p_count);
934
935   return CHRONY_RC_OK;
936 }
937
938
939 static int
940 chrony_request_source_data(int p_src_idx, int *p_is_reachable)
941 {
942   /* Perform Source data request for source #p_src_idx */
943   int rc;
944   size_t chrony_resp_size;
945   tChrony_Request chrony_req;
946   tChrony_Response chrony_resp;
947
948   char src_addr[IPV6_STR_MAX_SIZE];
949   memset(src_addr, 0, sizeof(src_addr));
950
951   chrony_init_req(&chrony_req);
952   chrony_req.body.source_data.f_index = htonl(p_src_idx);
953   rc =
954     chrony_query(REQ_SOURCE_DATA, &chrony_req, &chrony_resp,
955                  &chrony_resp_size);
956   if (rc != 0)
957   {
958     ERROR(PLUGIN_NAME
959           ": chrony_query (REQ_SOURCE_DATA) failed with status %i", rc);
960     return rc;
961   }
962
963   niptoha(&chrony_resp.body.source_data.addr, src_addr, sizeof(src_addr));
964   DEBUG(PLUGIN_NAME
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));
976
977   /* Push NaN if source is currently not reachable */
978   int is_reachable =
979     ntohs(chrony_resp.body.source_data.f_reachability) & 0x01;
980   *p_is_reachable = is_reachable;
981
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));
993
994   return CHRONY_RC_OK;
995 }
996
997
998 static int
999 chrony_request_source_stats(int p_src_idx, const int *p_is_reachable)
1000 {
1001   /* Perform Source stats request for source #p_src_idx */
1002   int rc;
1003   size_t chrony_resp_size;
1004   tChrony_Request chrony_req;
1005   tChrony_Response chrony_resp;
1006   double skew_ppm, frequency_error, time_offset;
1007
1008   char src_addr[IPV6_STR_MAX_SIZE];
1009   memset(src_addr, 0, sizeof(src_addr));
1010
1011   if (*p_is_reachable == 0)
1012   {
1013     skew_ppm = 0;
1014     frequency_error = 0;
1015     time_offset = 0;
1016   } else
1017   {
1018     chrony_init_req(&chrony_req);
1019     chrony_req.body.source_stats.f_index = htonl(p_src_idx);
1020     rc =
1021       chrony_query(REQ_SOURCE_STATS, &chrony_req, &chrony_resp,
1022                    &chrony_resp_size);
1023     if (rc != 0)
1024     {
1025       ERROR(PLUGIN_NAME
1026             ": chrony_query (REQ_SOURCE_STATS) failed with status %i", rc);
1027       return rc;
1028     }
1029
1030     skew_ppm = ntohf(chrony_resp.body.source_stats.f_skew_ppm);
1031     frequency_error =
1032       ntohf(chrony_resp.body.source_stats.f_rtc_gain_rate_ppm);
1033     time_offset = ntohf(chrony_resp.body.source_stats.f_est_offset);
1034
1035     niptoha(&chrony_resp.body.source_stats.addr, src_addr, sizeof(src_addr));
1036     DEBUG(PLUGIN_NAME
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));
1048
1049   }                             /* if (*is_reachable) */
1050
1051   /* Forward results to collectd-daemon */
1052   chrony_push_data_valid("clock_skew_ppm", src_addr, *p_is_reachable,
1053                          skew_ppm);
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 */
1056
1057   return CHRONY_RC_OK;
1058 }
1059
1060
1061 static int
1062 chrony_read()
1063 {
1064   /* collectd read callback: Perform data acquisition */
1065   int rc;
1066   unsigned int now_src, n_sources;
1067
1068   if (g_chrony_seq_is_initialized == 0)
1069   {
1070     /* Seed RNG for sequence number generation */
1071     rc = chrony_init_seq();
1072     if (rc != CHRONY_RC_OK)
1073     {
1074       return rc;
1075     }
1076     g_chrony_seq_is_initialized = 1;
1077   }
1078
1079   /* Get daemon stats */
1080   rc = chrony_request_daemon_stats();
1081   if (rc != CHRONY_RC_OK)
1082   {
1083     return rc;
1084   }
1085
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)
1089   {
1090     return rc;
1091   }
1092
1093   for (now_src = 0; now_src < n_sources; ++now_src)
1094   {
1095     int is_reachable;
1096     rc = chrony_request_source_data(now_src, &is_reachable);
1097     if (rc != CHRONY_RC_OK)
1098     {
1099       return rc;
1100     }
1101
1102     rc = chrony_request_source_stats(now_src, &is_reachable);
1103     if (rc != CHRONY_RC_OK)
1104     {
1105       return rc;
1106     }
1107   }
1108   return CHRONY_RC_OK;
1109 }
1110
1111
1112 static int
1113 chrony_shutdown()
1114 {
1115   /* Collectd shutdown callback: Free mem */
1116   if (g_is_connected != 0)
1117   {
1118     close(g_chrony_socket);
1119     g_is_connected = 0;
1120   }
1121   if (g_chrony_host != NULL)
1122   {
1123     free(g_chrony_host);
1124     g_chrony_host = NULL;
1125   }
1126   if (g_chrony_port != NULL)
1127   {
1128     free(g_chrony_port);
1129     g_chrony_port = NULL;
1130   }
1131   if (g_plugin_instance != NULL)
1132   {
1133     free(g_plugin_instance);
1134     g_plugin_instance = NULL;
1135   }
1136   return CHRONY_RC_OK;
1137 }
1138
1139
1140 void
1141 module_register(void)
1142 {
1143   plugin_register_config(PLUGIN_NAME_SHORT, chrony_config, g_config_keys,
1144                          g_config_keys_num);
1145   plugin_register_read(PLUGIN_NAME_SHORT, chrony_read);
1146   plugin_register_shutdown(PLUGIN_NAME_SHORT, chrony_shutdown);
1147 }