Added chrony fw
[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    License: GPL2
5 */
6
7 /* getaddrinfo */
8 #include <sys/types.h>
9 #include <sys/socket.h>
10 #include <netdb.h>
11
12 #include "collectd.h"
13 #include "common.h" /* auxiliary functions */
14 #include "plugin.h" /* plugin_register_*, plugin_dispatch_values */
15
16 static const char *g_config_keys[] =
17 {
18         "Host",
19         "Port",
20 };
21
22 static int g_config_keys_num = STATIC_ARRAY_SIZE (g_config_keys);
23
24 # define CHRONY_DEFAULT_HOST "localhost"
25 # define CHRONY_DEFAULT_PORT "323"
26
27 /* Copied from chrony/candm.h */
28 #define PROTO_VERSION_NUMBER 6
29
30 #define REQ_N_SOURCES 14
31 #define REQ_SOURCE_DATA 15
32
33 #define PKT_TYPE_CMD_REQUEST 1
34 #define PKT_TYPE_CMD_REPLY 2
35
36
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];
43
44 typedef struct
45 {
46         uint32_t f_n_sources;
47         int32_t EOR;
48 } tChrony_Req_N_Sources;
49
50 typedef struct
51 {
52         struct
53         {
54                 uint8_t f_version;
55                 uint8_t f_type;
56                 uint8_t f_dummy0;
57                 uint8_t f_dummy1;
58                 uint16_t f_cmd;
59                 uint16_t f_cmd_try;
60                 uint32_t f_seq;
61
62                 uint32_t f_dummy2;
63                 uint32_t f_dummy3;
64         } header;
65         union
66         {
67                 tChrony_Req_N_Sources n_sources;
68         } body;
69 } tChrony_Request;
70
71 typedef struct
72 {
73         struct
74         {
75                 uint8_t f_version;
76                 uint8_t f_type;
77                 uint8_t f_dummy0;
78                 uint8_t f_dummy1;
79                 uint16_t f_cmd;
80                 uint16_t f_reply;
81                 uint16_t f_status;
82                 uint16_t f_dummy2;
83                 uint16_t f_dummy3;
84                 uint16_t f_dummy4;
85                 uint32_t f_seq;
86                 uint16_t f_dummy5;
87                 uint16_t f_dummy6;
88         } header;
89
90         union
91         {
92         } data;
93 } tChrony_Response;
94
95 /*****************************************************************************/
96 /* Internal functions */
97 /*****************************************************************************/
98 /* Code from: http://long.ccaba.upc.edu/long/045Guidelines/eva/ipv6.html#daytimeClient6 */
99 static int
100 connect_client (const char *hostname,
101                 const char *service,
102                 int         family,
103                 int         socktype)
104 {
105         struct addrinfo hints, *res, *ressave;
106         int n, sockfd;
107
108         memset(&hints, 0, sizeof(struct addrinfo));
109
110         hints.ai_family = family;
111         hints.ai_socktype = socktype;
112
113         n = getaddrinfo(hostname, service, &hints, &res);
114
115         if (n <0)
116         {
117                 ERROR ("chrony plugin: getaddrinfo error:: [%s]", gai_strerror(n));
118                 return -1;
119         }
120
121         ressave = res;
122
123         sockfd=-1;
124         while (res)
125         {
126                 sockfd = socket(res->ai_family,
127                 res->ai_socktype,
128                 res->ai_protocol);
129
130                 if (!(sockfd < 0))
131                 {
132                         if (connect(sockfd, res->ai_addr, res->ai_addrlen) == 0)
133                         {
134                                 break;
135                         }
136
137                         close(sockfd);
138                         sockfd=-1;
139                 }
140                 res=res->ai_next;
141         }
142
143         freeaddrinfo(ressave);
144         return sockfd;
145 }
146
147 static int chrony_connect()
148 {
149         int socket = connect_client(g_chrony_host, g_chrony_port,  AF_UNSPEC, SOCK_DGRAM);
150         if (socket < 0)
151         {
152                 ERROR ("chrony plugin: Error connecting to daemon. Errno = %d", errno);
153                 return (1);
154         }
155         g_chrony_socket = socket;
156         return (0);
157 }
158
159 static int chrony_send_request(const tChrony_Request *p_req, size_t p_req_size)
160 {
161         if (send(g_chrony_socket,p_req,p_req_size,0) < 0)
162         {
163                 ERROR ("chrony plugin: Error sending packet. Errno = %d", errno);
164                 return (1);
165         } else {
166                 return (0);
167         }
168 }
169
170 static int chrony_recv_response(tChrony_Response *p_resp, size_t p_resp_max_size, size_t *p_resp_size)
171 {
172         ssize_t rc = recv(g_chrony_socket,p_resp,p_resp_max_size,0);
173         if (rc <= 0)
174         {
175                 ERROR ("chrony plugin: Error receiving packet. Errno = %d", errno);
176                 return (1);
177         } else {
178                 *p_resp_size = rc;
179                 return (0);
180         }
181 }
182
183 static int chrony_query(int p_command, tChrony_Request *p_req, tChrony_Response *p_resp, size_t *p_resp_size)
184 {
185         /* Check connection. We simply perform one try as collectd already handles retries */
186         assert(p_req);
187         assert(p_resp);
188         assert(p_resp_size);
189         if (g_is_connected == 0)
190         {
191                 if (chrony_connect() == 0)
192                 {
193                         g_is_connected = 1;
194                 } else {
195                         ERROR ("chrony plugin: Unable to connect. Errno = %d", errno);
196                         return 1;
197                 }
198         }
199
200
201         do
202         {
203                 int valid_command = 0;
204                 size_t req_size = sizeof(p_req->header);
205                 size_t resp_size = sizeof(p_resp->header);
206                 switch (p_command)
207                 {
208                 case REQ_N_SOURCES:
209                         req_size += sizeof(p_req->body.n_sources);
210                         valid_command = 1;
211                         break;
212                 default:
213                         break;
214                 }
215
216                 if (valid_command == 0)
217                 {
218                         break;
219                 }
220
221                 p_req->header.f_cmd     = p_command;
222                 p_req->header.f_cmd_try = 0;
223                 p_req->header.f_seq     = g_chrony_seq++;
224                 
225                 if (chrony_send_request(p_req,req_size) != 0)
226                 {
227                         break;
228                 }
229                 if (chrony_recv_response(p_resp,resp_size,p_resp_size) != 0)
230                 {
231                         break;
232                 }
233                 return (0);
234         } while (0);
235         
236         return (1);
237 }
238
239 static void chrony_init_req(tChrony_Request *p_req)
240 {
241         p_req->header.f_version = PROTO_VERSION_NUMBER;
242         p_req->header.f_type    = PKT_TYPE_CMD_REQUEST;
243         p_req->header.f_dummy0  = 0;
244         p_req->header.f_dummy1  = 0;
245         p_req->header.f_dummy2  = 0;
246         p_req->header.f_dummy3  = 0;
247 }
248
249
250 /*****************************************************************************/
251 /* Exported functions */
252 /*****************************************************************************/
253 static int chrony_config(const char *p_key, const char *p_value)
254 {
255         //Parse config variables
256         if (strcasecmp(p_key, "Host") == 0)
257         {
258                 if (g_chrony_host != NULL)
259                 {
260                         free (g_chrony_host);
261                 }
262                 if ((g_chrony_host = strdup (p_value)) == NULL)
263                 {
264                         ERROR ("chrony plugin: Error duplicating host name");
265                         return (1);
266                 }
267         } else if (strcasecmp(p_key, "Port") == 0)
268         {
269                 if (g_chrony_port != NULL)
270                 {
271                         free (g_chrony_port);
272                 }
273                 if ((g_chrony_port = strdup (p_value)) == NULL)
274                 {
275                         ERROR ("chrony plugin: Error duplicating port name");
276                         return (1);
277                 }
278         }
279         return (0);
280 }
281
282 static int chrony_read (void)
283 {
284         //plugin_dispatch_values (&vl);
285         int status;
286         tChrony_Request  chrony_req;
287         tChrony_Response chrony_resp;
288         size_t chrony_resp_size;
289
290         chrony_init_req(&chrony_req);
291         status = chrony_query (REQ_N_SOURCES, &chrony_req, &chrony_resp, &chrony_resp_size);
292         if (status != 0)
293         {
294                 ERROR ("chrony plugin: chrony_query (REQ_N_SOURCES) failed with status %i", status);
295                 return (status);
296         }
297         return (0);
298 }
299
300 static int chrony_shutdown()
301 {
302         return (0);
303 }
304
305 void module_register (void)
306 {
307         plugin_register_config ("chrony", chrony_config, g_config_keys, g_config_keys_num);
308         plugin_register_read ("chrony", chrony_read);
309         plugin_register_shutdown ("chrony", chrony_shutdown);
310 }