Merge branch 'collectd-5.5'
[collectd.git] / src / ipvs.c
1 /**
2  * collectd - src/ipvs.c (based on ipvsadm and libipvs)
3  * Copyright (C) 1997  Steven Clarke <steven@monmouth.demon.co.uk>
4  * Copyright (C) 1998-2004  Wensong Zhang <wensong@linuxvirtualserver.org>
5  * Copyright (C) 2003-2004  Peter Kese <peter.kese@ijs.si>
6  * Copyright (C) 2007  Sebastian Harl
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; only version 2 of the License is applicable.
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 St, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  * Authors:
22  *   Sebastian Harl <sh at tokkee.org>
23  **/
24
25 /*
26  * This plugin collects statistics about IPVS connections. It requires Linux
27  * kernels >= 2.6.
28  *
29  * See http://www.linuxvirtualserver.org/software/index.html for more
30  * information about IPVS.
31  */
32
33 #include "collectd.h"
34 #include "plugin.h"
35 #include "common.h"
36
37 #if HAVE_ARPA_INET_H
38 # include <arpa/inet.h>
39 #endif /* HAVE_ARPA_INET_H */
40 #if HAVE_NETINET_IN_H
41 # include <netinet/in.h>
42 #endif /* HAVE_NETINET_IN_H */
43
44 /* this can probably only be found in the kernel sources */
45 #if HAVE_LINUX_IP_VS_H
46 # include <linux/ip_vs.h>
47 #elif HAVE_NET_IP_VS_H
48 # include <net/ip_vs.h>
49 #elif HAVE_IP_VS_H
50 # include <ip_vs.h>
51 #endif /* HAVE_IP_VS_H */
52
53 #define log_err(...) ERROR ("ipvs: " __VA_ARGS__)
54 #define log_info(...) INFO ("ipvs: " __VA_ARGS__)
55
56 /*
57  * private variables
58  */
59 static int sockfd = -1;
60
61 /*
62  * libipvs API
63  */
64 static struct ip_vs_get_services *ipvs_get_services (void)
65 {
66         struct ip_vs_getinfo       ipvs_info;
67         struct ip_vs_get_services *ret;
68
69         socklen_t len;
70
71         len = sizeof (ipvs_info);
72
73         if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_INFO,
74                                 (void *)&ipvs_info, &len)) {
75                 char errbuf[1024];
76                 log_err ("ip_vs_get_services: getsockopt() failed: %s",
77                                 sstrerror (errno, errbuf, sizeof (errbuf)));
78                 return NULL;
79         }
80
81         len = sizeof (*ret) +
82                 sizeof (struct ip_vs_service_entry) * ipvs_info.num_services;
83
84         if (NULL == (ret = malloc (len))) {
85                 log_err ("ipvs_get_services: Out of memory.");
86                 exit (3);
87         }
88
89         ret->num_services = ipvs_info.num_services;
90
91         if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_SERVICES,
92                                 (void *)ret, &len)) {
93                 char errbuf[1024];
94                 log_err ("ipvs_get_services: getsockopt failed: %s",
95                                 sstrerror (errno, errbuf, sizeof (errbuf)));
96
97                 free(ret);
98                 return NULL;
99         }
100         return ret;
101 } /* ipvs_get_services */
102
103 static struct ip_vs_get_dests *ipvs_get_dests (struct ip_vs_service_entry *se)
104 {
105         struct ip_vs_get_dests *ret;
106         socklen_t len;
107
108         len = sizeof (*ret) + sizeof (struct ip_vs_dest_entry) * se->num_dests;
109
110         if (NULL == (ret = malloc (len))) {
111                 log_err ("ipvs_get_dests: Out of memory.");
112                 exit (3);
113         }
114
115         ret->fwmark    = se->fwmark;
116         ret->protocol  = se->protocol;
117         ret->addr      = se->addr;
118         ret->port      = se->port;
119         ret->num_dests = se->num_dests;
120
121         if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_DESTS,
122                                 (void *)ret, &len)) {
123                 char errbuf[1024];
124                 log_err ("ipvs_get_dests: getsockopt() failed: %s",
125                                 sstrerror (errno, errbuf, sizeof (errbuf)));
126                 free (ret);
127                 return NULL;
128         }
129         return ret;
130 } /* ip_vs_get_dests */
131
132 /*
133  * collectd plugin API and helper functions
134  */
135 static int cipvs_init (void)
136 {
137         struct ip_vs_getinfo ipvs_info;
138
139         socklen_t len;
140
141         if (-1 == (sockfd = socket (AF_INET, SOCK_RAW, IPPROTO_RAW))) {
142                 char errbuf[1024];
143                 log_err ("cipvs_init: socket() failed: %s",
144                                 sstrerror (errno, errbuf, sizeof (errbuf)));
145                 return -1;
146         }
147
148         len = sizeof (ipvs_info);
149
150         if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_INFO,
151                                 (void *)&ipvs_info, &len)) {
152                 char errbuf[1024];
153                 log_err ("cipvs_init: getsockopt() failed: %s",
154                                 sstrerror (errno, errbuf, sizeof (errbuf)));
155                 close (sockfd);
156                 sockfd = -1;
157                 return -1;
158         }
159
160         /* we need IPVS >= 1.1.4 */
161         if (ipvs_info.version < ((1 << 16) + (1 << 8) + 4)) {
162                 log_err ("cipvs_init: IPVS version too old (%d.%d.%d < %d.%d.%d)",
163                                 NVERSION (ipvs_info.version), 1, 1, 4);
164                 close (sockfd);
165                 sockfd = -1;
166                 return -1;
167         }
168         else {
169                 log_info ("Successfully connected to IPVS %d.%d.%d",
170                                 NVERSION (ipvs_info.version));
171         }
172         return 0;
173 } /* cipvs_init */
174
175 /*
176  * ipvs-<virtual IP>_{UDP,TCP}<port>/<type>-total
177  * ipvs-<virtual IP>_{UDP,TCP}<port>/<type>-<real IP>_<port>
178  */
179
180 /* plugin instance */
181 static int get_pi (struct ip_vs_service_entry *se, char *pi, size_t size)
182 {
183         struct in_addr addr;
184         int len = 0;
185
186         if ((NULL == se) || (NULL == pi))
187                 return 0;
188
189         addr.s_addr = se->addr;
190
191         /* inet_ntoa() returns a pointer to a statically allocated buffer
192          * I hope non-glibc systems behave the same */
193         len = ssnprintf (pi, size, "%s_%s%u", inet_ntoa (addr),
194                         (se->protocol == IPPROTO_TCP) ? "TCP" : "UDP",
195                         ntohs (se->port));
196
197         if ((0 > len) || (size <= ((size_t) len))) {
198                 log_err ("plugin instance truncated: %s", pi);
199                 return -1;
200         }
201         return 0;
202 } /* get_pi */
203
204 /* type instance */
205 static int get_ti (struct ip_vs_dest_entry *de, char *ti, size_t size)
206 {
207         struct in_addr addr;
208         int len = 0;
209
210         if ((NULL == de) || (NULL == ti))
211                 return 0;
212
213         addr.s_addr = de->addr;
214
215         /* inet_ntoa() returns a pointer to a statically allocated buffer
216          * I hope non-glibc systems behave the same */
217         len = ssnprintf (ti, size, "%s_%u", inet_ntoa (addr),
218                         ntohs (de->port));
219
220         if ((0 > len) || (size <= ((size_t) len))) {
221                 log_err ("type instance truncated: %s", ti);
222                 return -1;
223         }
224         return 0;
225 } /* get_ti */
226
227 static void cipvs_submit_connections (const char *pi, const char *ti,
228                 derive_t value)
229 {
230         value_t values[1];
231         value_list_t vl = VALUE_LIST_INIT;
232
233         values[0].derive = value;
234
235         vl.values     = values;
236         vl.values_len = 1;
237
238         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
239         sstrncpy (vl.plugin, "ipvs", sizeof (vl.plugin));
240         sstrncpy (vl.plugin_instance, pi, sizeof (vl.plugin_instance));
241         sstrncpy (vl.type, "connections", sizeof (vl.type));
242         sstrncpy (vl.type_instance, (NULL != ti) ? ti : "total",
243                 sizeof (vl.type_instance));
244
245         plugin_dispatch_values (&vl);
246         return;
247 } /* cipvs_submit_connections */
248
249 static void cipvs_submit_if (const char *pi, const char *t, const char *ti,
250                 derive_t rx, derive_t tx)
251 {
252         value_t values[2];
253         value_list_t vl = VALUE_LIST_INIT;
254
255         values[0].derive = rx;
256         values[1].derive = tx;
257
258         vl.values     = values;
259         vl.values_len = 2;
260
261         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
262         sstrncpy (vl.plugin, "ipvs", sizeof (vl.plugin));
263         sstrncpy (vl.plugin_instance, pi, sizeof (vl.plugin_instance));
264         sstrncpy (vl.type, t, sizeof (vl.type));
265         sstrncpy (vl.type_instance, (NULL != ti) ? ti : "total",
266                 sizeof (vl.type_instance));
267
268         plugin_dispatch_values (&vl);
269         return;
270 } /* cipvs_submit_if */
271
272 static void cipvs_submit_dest (const char *pi, struct ip_vs_dest_entry *de)
273 {
274         struct ip_vs_stats_user stats = de->stats;
275
276         char ti[DATA_MAX_NAME_LEN];
277
278         if (0 != get_ti (de, ti, sizeof (ti)))
279                 return;
280
281         cipvs_submit_connections (pi, ti, stats.conns);
282         cipvs_submit_if (pi, "if_packets", ti, stats.inpkts, stats.outpkts);
283         cipvs_submit_if (pi, "if_octets", ti, stats.inbytes, stats.outbytes);
284         return;
285 } /* cipvs_submit_dest */
286
287 static void cipvs_submit_service (struct ip_vs_service_entry *se)
288 {
289         struct ip_vs_stats_user  stats = se->stats;
290         struct ip_vs_get_dests  *dests = ipvs_get_dests (se);
291
292         char pi[DATA_MAX_NAME_LEN];
293
294         size_t i;
295
296         if (0 != get_pi (se, pi, sizeof (pi)))
297         {
298                 free (dests);
299                 return;
300         }
301
302         cipvs_submit_connections (pi, NULL, stats.conns);
303         cipvs_submit_if (pi, "if_packets", NULL, stats.inpkts, stats.outpkts);
304         cipvs_submit_if (pi, "if_octets", NULL, stats.inbytes, stats.outbytes);
305
306         for (i = 0; i < dests->num_dests; ++i)
307                 cipvs_submit_dest (pi, &dests->entrytable[i]);
308
309         free (dests);
310         return;
311 } /* cipvs_submit_service */
312
313 static int cipvs_read (void)
314 {
315         struct ip_vs_get_services *services = NULL;
316         size_t i;
317
318         if (sockfd < 0)
319                 return (-1);
320
321         if (NULL == (services = ipvs_get_services ()))
322                 return -1;
323
324         for (i = 0; i < services->num_services; ++i)
325                 cipvs_submit_service (&services->entrytable[i]);
326
327         free (services);
328         return 0;
329 } /* cipvs_read */
330
331 static int cipvs_shutdown (void)
332 {
333         if (sockfd >= 0)
334                 close (sockfd);
335         sockfd = -1;
336
337         return 0;
338 } /* cipvs_shutdown */
339
340 void module_register (void)
341 {
342         plugin_register_init ("ipvs", cipvs_init);
343         plugin_register_read ("ipvs", cipvs_read);
344         plugin_register_shutdown ("ipvs", cipvs_shutdown);
345         return;
346 } /* module_register */
347
348 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */