1 /* chrony plugin for collectd
2 (c) 2015 by Claudius M Zingerli, ZSeng
3 Internas roughly based on the ntpd plugin
9 #include <sys/socket.h>
13 #include "common.h" /* auxiliary functions */
14 #include "plugin.h" /* plugin_register_*, plugin_dispatch_values */
16 static const char *g_config_keys[] =
22 static int g_config_keys_num = STATIC_ARRAY_SIZE (g_config_keys);
24 # define CHRONY_DEFAULT_HOST "localhost"
25 # define CHRONY_DEFAULT_PORT "323"
27 /* Copied from chrony/candm.h */
28 #define PROTO_VERSION_NUMBER 6
30 #define REQ_N_SOURCES 14
31 #define REQ_SOURCE_DATA 15
33 #define PKT_TYPE_CMD_REQUEST 1
34 #define PKT_TYPE_CMD_REPLY 2
37 static int g_is_connected = 0;
38 static int g_chrony_socket = -1;
39 static char *g_chrony_host = NULL;
40 static char *g_chrony_port = NULL;
41 static uint32_t g_chrony_seq = 0;
42 //static char ntpd_port[16];
48 } tChrony_Req_N_Sources;
67 tChrony_Req_N_Sources n_sources;
95 /*****************************************************************************/
96 /* Internal functions */
97 /*****************************************************************************/
98 /* Code from: http://long.ccaba.upc.edu/long/045Guidelines/eva/ipv6.html#daytimeClient6 */
100 connect_client (const char *hostname,
105 struct addrinfo hints, *res, *ressave;
108 memset(&hints, 0, sizeof(struct addrinfo));
110 hints.ai_family = family;
111 hints.ai_socktype = socktype;
113 n = getaddrinfo(hostname, service, &hints, &res);
117 ERROR ("chrony plugin: getaddrinfo error:: [%s]", gai_strerror(n));
126 sockfd = socket(res->ai_family,
132 if (connect(sockfd, res->ai_addr, res->ai_addrlen) == 0)
143 freeaddrinfo(ressave);
147 static int chrony_connect()
149 DEBUG("chrony plugin: Connecting to %s:%s", g_chrony_host, g_chrony_port);
150 int socket = connect_client(g_chrony_host, g_chrony_port, AF_UNSPEC, SOCK_DGRAM);
153 ERROR ("chrony plugin: Error connecting to daemon. Errno = %d", errno);
156 //TODO: Set timeouts!
157 DEBUG("chrony plugin: Connected");
158 g_chrony_socket = socket;
162 static int chrony_send_request(const tChrony_Request *p_req, size_t p_req_size)
164 if (send(g_chrony_socket,p_req,p_req_size,0) < 0)
166 ERROR ("chrony plugin: Error sending packet. Errno = %d", errno);
173 static int chrony_recv_response(tChrony_Response *p_resp, size_t p_resp_max_size, size_t *p_resp_size)
175 ssize_t rc = recv(g_chrony_socket,p_resp,p_resp_max_size,0);
178 ERROR ("chrony plugin: Error receiving packet. Errno = %d", errno);
186 static int chrony_query(int p_command, tChrony_Request *p_req, tChrony_Response *p_resp, size_t *p_resp_size)
188 /* Check connection. We simply perform one try as collectd already handles retries */
192 if (g_is_connected == 0)
194 if (chrony_connect() == 0)
198 ERROR ("chrony plugin: Unable to connect. Errno = %d", errno);
206 int valid_command = 0;
207 size_t req_size = sizeof(p_req->header);
208 size_t resp_size = sizeof(p_resp->header);
212 req_size += sizeof(p_req->body.n_sources);
219 if (valid_command == 0)
224 p_req->header.f_cmd = p_command;
225 p_req->header.f_cmd_try = 0;
226 p_req->header.f_seq = g_chrony_seq++;
228 DEBUG("chrony plugin: Sending request");
229 if (chrony_send_request(p_req,req_size) != 0)
234 DEBUG("chrony plugin: Waiting for response");
235 if (chrony_recv_response(p_resp,resp_size,p_resp_size) != 0)
239 DEBUG("chrony plugin: Received response");
247 static void chrony_init_req(tChrony_Request *p_req)
249 p_req->header.f_version = PROTO_VERSION_NUMBER;
250 p_req->header.f_type = PKT_TYPE_CMD_REQUEST;
251 p_req->header.f_dummy0 = 0;
252 p_req->header.f_dummy1 = 0;
253 p_req->header.f_dummy2 = 0;
254 p_req->header.f_dummy3 = 0;
258 /*****************************************************************************/
259 /* Exported functions */
260 /*****************************************************************************/
261 static int chrony_config(const char *p_key, const char *p_value)
263 //Parse config variables
264 if (strcasecmp(p_key, "Host") == 0)
266 if (g_chrony_host != NULL)
268 free (g_chrony_host);
270 if ((g_chrony_host = strdup (p_value)) == NULL)
272 ERROR ("chrony plugin: Error duplicating host name");
275 } else if (strcasecmp(p_key, "Port") == 0)
277 if (g_chrony_port != NULL)
279 free (g_chrony_port);
281 if ((g_chrony_port = strdup (p_value)) == NULL)
283 ERROR ("chrony plugin: Error duplicating port name");
290 static int chrony_read (void)
292 //plugin_dispatch_values (&vl);
294 tChrony_Request chrony_req;
295 tChrony_Response chrony_resp;
296 size_t chrony_resp_size;
298 DEBUG("chrony plugin: Requesting data");
299 chrony_init_req(&chrony_req);
300 status = chrony_query (REQ_N_SOURCES, &chrony_req, &chrony_resp, &chrony_resp_size);
303 ERROR ("chrony plugin: chrony_query (REQ_N_SOURCES) failed with status %i", status);
309 static int chrony_shutdown()
311 if (g_is_connected != 0)
313 close(g_chrony_socket);
316 if (g_chrony_host != NULL)
318 free (g_chrony_host);
319 g_chrony_host = NULL;
321 if (g_chrony_port != NULL)
323 free (g_chrony_port);
324 g_chrony_port = NULL;
329 void module_register (void)
331 plugin_register_config ("chrony", chrony_config, g_config_keys, g_config_keys_num);
332 plugin_register_read ("chrony", chrony_read);
333 plugin_register_shutdown ("chrony", chrony_shutdown);