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
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.
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.
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
22 * Sebastian Harl <sh at tokkee.org>
26 * This plugin collects statistics about IPVS connections. It requires Linux
29 * See http://www.linuxvirtualserver.org/software/index.html for more
30 * information about IPVS.
38 # include <arpa/inet.h>
39 #endif /* HAVE_ARPA_INET_H */
41 # include <sys/socket.h>
42 #endif /* HAVE_SYS_SOCKET_H */
44 # include <netinet/in.h>
45 #endif /* HAVE_NETINET_IN_H */
47 /* this can probably only be found in the kernel sources */
48 #if HAVE_LINUX_IP_VS_H
49 # include <linux/ip_vs.h>
50 #elif HAVE_NET_IP_VS_H
51 # include <net/ip_vs.h>
54 #endif /* HAVE_IP_VS_H */
56 #define log_err(...) ERROR ("ipvs: " __VA_ARGS__)
57 #define log_info(...) INFO ("ipvs: " __VA_ARGS__)
62 static int sockfd = -1;
67 static struct ip_vs_get_services *ipvs_get_services (void)
69 struct ip_vs_getinfo ipvs_info;
70 struct ip_vs_get_services *ret;
74 len = sizeof (ipvs_info);
76 if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_INFO,
77 (void *)&ipvs_info, &len)) {
79 log_err ("ip_vs_get_services: getsockopt() failed: %s",
80 sstrerror (errno, errbuf, sizeof (errbuf)));
85 sizeof (struct ip_vs_service_entry) * ipvs_info.num_services;
87 if (NULL == (ret = malloc (len))) {
88 log_err ("ipvs_get_services: Out of memory.");
92 ret->num_services = ipvs_info.num_services;
94 if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_SERVICES,
97 log_err ("ipvs_get_services: getsockopt failed: %s",
98 sstrerror (errno, errbuf, sizeof (errbuf)));
104 } /* ipvs_get_services */
106 static struct ip_vs_get_dests *ipvs_get_dests (struct ip_vs_service_entry *se)
108 struct ip_vs_get_dests *ret;
111 len = sizeof (*ret) + sizeof (struct ip_vs_dest_entry) * se->num_dests;
113 if (NULL == (ret = malloc (len))) {
114 log_err ("ipvs_get_dests: Out of memory.");
118 ret->fwmark = se->fwmark;
119 ret->protocol = se->protocol;
120 ret->addr = se->addr;
121 ret->port = se->port;
122 ret->num_dests = se->num_dests;
124 if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_DESTS,
125 (void *)ret, &len)) {
127 log_err ("ipvs_get_dests: getsockopt() failed: %s",
128 sstrerror (errno, errbuf, sizeof (errbuf)));
133 } /* ip_vs_get_dests */
136 * collectd plugin API and helper functions
138 static int cipvs_init (void)
140 struct ip_vs_getinfo ipvs_info;
144 if (-1 == (sockfd = socket (AF_INET, SOCK_RAW, IPPROTO_RAW))) {
146 log_err ("cipvs_init: socket() failed: %s",
147 sstrerror (errno, errbuf, sizeof (errbuf)));
151 len = sizeof (ipvs_info);
153 if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_INFO,
154 (void *)&ipvs_info, &len)) {
156 log_err ("cipvs_init: getsockopt() failed: %s",
157 sstrerror (errno, errbuf, sizeof (errbuf)));
163 /* we need IPVS >= 1.1.4 */
164 if (ipvs_info.version < ((1 << 16) + (1 << 8) + 4)) {
165 log_err ("cipvs_init: IPVS version too old (%d.%d.%d < %d.%d.%d)",
166 NVERSION (ipvs_info.version), 1, 1, 4);
172 log_info ("Successfully connected to IPVS %d.%d.%d",
173 NVERSION (ipvs_info.version));
179 * ipvs-<virtual IP>_{UDP,TCP}<port>/<type>-total
180 * ipvs-<virtual IP>_{UDP,TCP}<port>/<type>-<real IP>_<port>
183 /* plugin instance */
184 static int get_pi (struct ip_vs_service_entry *se, char *pi, size_t size)
189 if ((NULL == se) || (NULL == pi))
192 addr.s_addr = se->addr;
194 /* inet_ntoa() returns a pointer to a statically allocated buffer
195 * I hope non-glibc systems behave the same */
196 len = ssnprintf (pi, size, "%s_%s%u", inet_ntoa (addr),
197 (se->protocol == IPPROTO_TCP) ? "TCP" : "UDP",
200 if ((0 > len) || (size <= len)) {
201 log_err ("plugin instance truncated: %s", pi);
208 static int get_ti (struct ip_vs_dest_entry *de, char *ti, size_t size)
213 if ((NULL == de) || (NULL == ti))
216 addr.s_addr = de->addr;
218 /* inet_ntoa() returns a pointer to a statically allocated buffer
219 * I hope non-glibc systems behave the same */
220 len = ssnprintf (ti, size, "%s_%u", inet_ntoa (addr),
223 if ((0 > len) || (size <= len)) {
224 log_err ("type instance truncated: %s", ti);
230 static void cipvs_submit_connections (char *pi, char *ti, derive_t value)
233 value_list_t vl = VALUE_LIST_INIT;
235 values[0].derive = value;
240 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
241 sstrncpy (vl.plugin, "ipvs", sizeof (vl.plugin));
242 sstrncpy (vl.plugin_instance, pi, sizeof (vl.plugin_instance));
243 sstrncpy (vl.type, "connections", sizeof (vl.type));
244 sstrncpy (vl.type_instance, (NULL != ti) ? ti : "total",
245 sizeof (vl.type_instance));
247 plugin_dispatch_values (&vl);
249 } /* cipvs_submit_connections */
251 static void cipvs_submit_if (char *pi, char *t, char *ti,
252 derive_t rx, derive_t tx)
255 value_list_t vl = VALUE_LIST_INIT;
257 values[0].derive = rx;
258 values[1].derive = tx;
263 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
264 sstrncpy (vl.plugin, "ipvs", sizeof (vl.plugin));
265 sstrncpy (vl.plugin_instance, pi, sizeof (vl.plugin_instance));
266 sstrncpy (vl.type, t, sizeof (vl.type));
267 sstrncpy (vl.type_instance, (NULL != ti) ? ti : "total",
268 sizeof (vl.type_instance));
270 plugin_dispatch_values (&vl);
272 } /* cipvs_submit_if */
274 static void cipvs_submit_dest (char *pi, struct ip_vs_dest_entry *de) {
275 struct ip_vs_stats_user stats = de->stats;
277 char ti[DATA_MAX_NAME_LEN];
279 if (0 != get_ti (de, ti, sizeof (ti)))
282 cipvs_submit_connections (pi, ti, stats.conns);
283 cipvs_submit_if (pi, "if_packets", ti, stats.inpkts, stats.outpkts);
284 cipvs_submit_if (pi, "if_octets", ti, stats.inbytes, stats.outbytes);
286 } /* cipvs_submit_dest */
288 static void cipvs_submit_service (struct ip_vs_service_entry *se)
290 struct ip_vs_stats_user stats = se->stats;
291 struct ip_vs_get_dests *dests = ipvs_get_dests (se);
293 char pi[DATA_MAX_NAME_LEN];
297 if (0 != get_pi (se, pi, sizeof (pi)))
300 cipvs_submit_connections (pi, NULL, stats.conns);
301 cipvs_submit_if (pi, "if_packets", NULL, stats.inpkts, stats.outpkts);
302 cipvs_submit_if (pi, "if_octets", NULL, stats.inbytes, stats.outbytes);
304 for (i = 0; i < dests->num_dests; ++i)
305 cipvs_submit_dest (pi, &dests->entrytable[i]);
309 } /* cipvs_submit_service */
311 static int cipvs_read (void)
313 struct ip_vs_get_services *services = NULL;
319 if (NULL == (services = ipvs_get_services ()))
322 for (i = 0; i < services->num_services; ++i)
323 cipvs_submit_service (&services->entrytable[i]);
329 static int cipvs_shutdown (void)
336 } /* cipvs_shutdown */
338 void module_register (void)
340 plugin_register_init ("ipvs", cipvs_init);
341 plugin_register_read ("ipvs", cipvs_read);
342 plugin_register_shutdown ("ipvs", cipvs_shutdown);
344 } /* module_register */
346 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */