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 */
49 # include <net/ip_vs.h>
52 #endif /* HAVE_IP_VS_H */
54 #define log_err(...) ERROR ("ipvs: " __VA_ARGS__)
61 static int sockfd = -1;
68 static struct ip_vs_get_services *ipvs_get_services (void)
70 struct ip_vs_getinfo ipvs_info;
71 struct ip_vs_get_services *ret;
75 len = sizeof (ipvs_info);
77 if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_INFO,
78 (void *)&ipvs_info, &len)) {
80 log_err ("ip_vs_get_services: getsockopt() failed: %s",
81 sstrerror (errno, errbuf, sizeof (errbuf)));
86 sizeof (struct ip_vs_service_entry) * ipvs_info.num_services;
88 if (NULL == (ret = malloc (len))) {
89 log_err ("ipvs_get_services: Out of memory.");
93 ret->num_services = ipvs_info.num_services;
95 if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_SERVICES,
98 log_err ("ipvs_get_services: getsockopt failed: %s",
99 sstrerror (errno, errbuf, sizeof (errbuf)));
105 } /* ipvs_get_services */
107 static struct ip_vs_get_dests *ipvs_get_dests (struct ip_vs_service_entry *se)
109 struct ip_vs_get_dests *ret;
112 len = sizeof (*ret) + sizeof (struct ip_vs_dest_entry) * se->num_dests;
114 if (NULL == (ret = malloc (len))) {
115 log_err ("ipvs_get_dests: Out of memory.");
119 ret->fwmark = se->fwmark;
120 ret->protocol = se->protocol;
121 ret->addr = se->addr;
122 ret->port = se->port;
123 ret->num_dests = se->num_dests;
125 if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_DESTS,
126 (void *)ret, &len)) {
128 log_err ("ipvs_get_dests: getsockopt() failed: %s",
129 sstrerror (errno, errbuf, sizeof (errbuf)));
134 } /* ip_vs_get_dests */
138 * collectd plugin API and helper functions
141 static int cipvs_init (void)
143 if (-1 == (sockfd = socket (AF_INET, SOCK_RAW, IPPROTO_RAW))) {
145 log_err ("cipvs_init: socket() failed: %s",
146 sstrerror (errno, errbuf, sizeof (errbuf)));
153 * ipvs-<virtual IP>_{UDP,TCP}<port>/<type>-total
154 * ipvs-<virtual IP>_{UDP,TCP}<port>/<type>-<real IP>_<port>
157 /* plugin instance */
158 static int get_pi (struct ip_vs_service_entry *se, char *pi, size_t size)
163 if ((NULL == se) || (NULL == pi))
166 addr.s_addr = se->addr;
168 /* inet_ntoa() returns a pointer to a statically allocated buffer
169 * I hope non-glibc systems behave the same */
170 len = snprintf (pi, size, "%s_%s%u", inet_ntoa (addr),
171 (se->protocol == IPPROTO_TCP) ? "TCP" : "UDP",
174 if ((0 > len) || (size <= len)) {
175 log_err ("plugin instance truncated: %s", pi);
182 static int get_ti (struct ip_vs_dest_entry *de, char *ti, size_t size)
187 if ((NULL == de) || (NULL == ti))
190 addr.s_addr = de->addr;
192 /* inet_ntoa() returns a pointer to a statically allocated buffer
193 * I hope non-glibc systems behave the same */
194 len = snprintf (ti, size, "%s_%u", inet_ntoa (addr),
197 if ((0 > len) || (size <= len)) {
198 log_err ("type instance truncated: %s", ti);
204 static void cipvs_submit_connections (char *pi, char *ti, counter_t value)
207 value_list_t vl = VALUE_LIST_INIT;
209 values[0].counter = value;
214 vl.time = time (NULL);
215 vl.interval = interval_g;
217 strcpy (vl.host, hostname_g);
218 strcpy (vl.plugin, "ipvs");
219 strcpy (vl.plugin_instance, pi);
220 strcpy (vl.type_instance, (NULL != ti) ? ti : "total");
222 plugin_dispatch_values ("connections", &vl);
224 } /* cipvs_submit_connections */
226 static void cipvs_submit_if (char *pi, char *t, char *ti,
227 counter_t rx, counter_t tx)
230 value_list_t vl = VALUE_LIST_INIT;
232 values[0].counter = rx;
233 values[1].counter = tx;
238 vl.time = time (NULL);
239 vl.interval = interval_g;
241 strcpy (vl.host, hostname_g);
242 strcpy (vl.plugin, "ipvs");
243 strcpy (vl.plugin_instance, pi);
244 strcpy (vl.type_instance, (NULL != ti) ? ti : "total");
246 plugin_dispatch_values (t, &vl);
248 } /* cipvs_submit_if */
250 static void cipvs_submit_dest (char *pi, struct ip_vs_dest_entry *de) {
251 struct ip_vs_stats_user stats = de->stats;
253 char ti[DATA_MAX_NAME_LEN];
255 if (0 != get_ti (de, ti, DATA_MAX_NAME_LEN))
258 cipvs_submit_connections (pi, ti, stats.conns);
259 cipvs_submit_if (pi, "if_packets", ti, stats.inpkts, stats.outpkts);
260 cipvs_submit_if (pi, "if_octets", ti, stats.inbytes, stats.outbytes);
262 } /* cipvs_submit_dest */
264 static void cipvs_submit_service (struct ip_vs_service_entry *se)
266 struct ip_vs_stats_user stats = se->stats;
267 struct ip_vs_get_dests *dests = ipvs_get_dests (se);
269 char pi[DATA_MAX_NAME_LEN];
273 if (0 != get_pi (se, pi, DATA_MAX_NAME_LEN))
276 cipvs_submit_connections (pi, NULL, stats.conns);
277 cipvs_submit_if (pi, "if_packets", NULL, stats.inpkts, stats.outpkts);
278 cipvs_submit_if (pi, "if_octets", NULL, stats.inbytes, stats.outbytes);
280 for (i = 0; i < dests->num_dests; ++i)
281 cipvs_submit_dest (pi, &dests->entrytable[i]);
285 } /* cipvs_submit_service */
287 static int cipvs_read (void)
289 struct ip_vs_get_services *services = NULL;
293 if (NULL == (services = ipvs_get_services ()))
296 for (i = 0; i < services->num_services; ++i)
297 cipvs_submit_service (&services->entrytable[i]);
303 static int cipvs_shutdown (void)
307 } /* cipvs_shutdown */
309 void module_register (void)
311 plugin_register_init ("ipvs", cipvs_init);
312 plugin_register_read ("ipvs", cipvs_read);
313 plugin_register_shutdown ("ipvs", cipvs_shutdown);
315 } /* module_register */
317 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */