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;
62 static void *ipvs_func = NULL;
64 static struct ip_vs_getinfo ipvs_info;
71 static struct ip_vs_get_services *ipvs_get_services (void);
72 static struct ip_vs_get_dests *ipvs_get_dests (struct ip_vs_service_entry *);
74 static const char *ipvs_strerror (int err)
84 { 0, EPERM, "Permission denied (you must be root)" },
85 { 0, EINVAL, "Module is wrong version" },
86 { 0, ENOPROTOOPT, "Protocol not available" },
87 { 0, ENOMEM, "Memory allocation problem" },
88 { ipvs_get_services, ESRCH, "No such service" },
89 { ipvs_get_dests, ESRCH, "No such service" },
92 for (i = 0; i < sizeof (table) / sizeof (table[0]); i++) {
93 if (((NULL == table[i].func) || (table[i].func == ipvs_func))
94 && (table[i].err == err))
95 return table[i].message;
97 return sstrerror (err, errbuf, sizeof (errbuf));
100 static struct ip_vs_get_services *ipvs_get_services (void)
102 struct ip_vs_get_services *ret;
105 len = sizeof (*ret) +
106 sizeof (struct ip_vs_service_entry) * ipvs_info.num_services;
108 if (NULL == (ret = malloc (len))) {
109 log_err ("ipvs_get_services: Out of memory.");
113 ipvs_func = ipvs_get_services;
115 ret->num_services = ipvs_info.num_services;
117 if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_SERVICES,
118 (void *)ret, &len)) {
119 log_err ("ipvs_get_services: getsockopt failed: %s",
120 ipvs_strerror (errno));
126 } /* ipvs_get_services */
128 static struct ip_vs_get_dests *ipvs_get_dests (struct ip_vs_service_entry *se)
130 struct ip_vs_get_dests *ret;
133 len = sizeof (*ret) + sizeof (struct ip_vs_dest_entry) * se->num_dests;
135 if (NULL == (ret = malloc (len))) {
136 log_err ("ipvs_get_dests: Out of memory.");
140 ipvs_func = ipvs_get_dests;
142 ret->fwmark = se->fwmark;
143 ret->protocol = se->protocol;
144 ret->addr = se->addr;
145 ret->port = se->port;
146 ret->num_dests = se->num_dests;
148 if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_DESTS,
149 (void *)ret, &len)) {
150 log_err ("ipvs_get_dests: getsockopt() failed: %s",
151 ipvs_strerror (errno));
156 } /* ip_vs_get_dests */
160 * collectd plugin API and helper functions
163 static int cipvs_init (void)
167 len = sizeof (ipvs_info);
169 if (-1 == (sockfd = socket (AF_INET, SOCK_RAW, IPPROTO_RAW))) {
170 log_err ("cipvs_init: socket() failed: %s", ipvs_strerror (errno));
174 if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_INFO,
175 (void *)&ipvs_info, &len)) {
176 log_err ("cipvs_init: getsockopt() failed: %s", ipvs_strerror (errno));
183 * ipvs-<virtual IP>_{UDP,TCP}<port>/<type>-total
184 * ipvs-<virtual IP>_{UDP,TCP}<port>/<type>-<real IP>_<port>
187 /* plugin instance */
188 static int get_pi (struct ip_vs_service_entry *se, char *pi, size_t size)
193 if ((NULL == se) || (NULL == pi))
196 addr.s_addr = se->addr;
198 /* inet_ntoa() returns a pointer to a statically allocated buffer
199 * I hope non-glibc systems behave the same */
200 len = snprintf (pi, size, "%s_%s%u", inet_ntoa (addr),
201 (se->protocol == IPPROTO_TCP) ? "TCP" : "UDP",
204 if ((0 > len) || (size <= len)) {
205 log_err ("plugin instance truncated: %s", pi);
212 static int get_ti (struct ip_vs_dest_entry *de, char *ti, size_t size)
217 if ((NULL == de) || (NULL == ti))
220 addr.s_addr = de->addr;
222 /* inet_ntoa() returns a pointer to a statically allocated buffer
223 * I hope non-glibc systems behave the same */
224 len = snprintf (ti, size, "%s_%u", inet_ntoa (addr),
227 if ((0 > len) || (size <= len)) {
228 log_err ("type instance truncated: %s", ti);
234 static void cipvs_submit_connections (char *pi, char *ti, counter_t value)
237 value_list_t vl = VALUE_LIST_INIT;
239 values[0].counter = value;
244 vl.time = time (NULL);
245 vl.interval = interval_g;
247 strcpy (vl.host, hostname_g);
248 strcpy (vl.plugin, "ipvs");
249 strcpy (vl.plugin_instance, pi);
250 strcpy (vl.type_instance, (NULL != ti) ? ti : "total");
252 plugin_dispatch_values ("connections", &vl);
254 } /* cipvs_submit_connections */
256 static void cipvs_submit_if (char *pi, char *t, char *ti,
257 counter_t rx, counter_t tx)
260 value_list_t vl = VALUE_LIST_INIT;
262 values[0].counter = rx;
263 values[1].counter = tx;
268 vl.time = time (NULL);
269 vl.interval = interval_g;
271 strcpy (vl.host, hostname_g);
272 strcpy (vl.plugin, "ipvs");
273 strcpy (vl.plugin_instance, pi);
274 strcpy (vl.type_instance, (NULL != ti) ? ti : "total");
276 plugin_dispatch_values (t, &vl);
278 } /* cipvs_submit_if */
280 static void cipvs_submit_dest (char *pi, struct ip_vs_dest_entry *de) {
281 struct ip_vs_stats_user stats = de->stats;
283 char ti[DATA_MAX_NAME_LEN];
285 if (0 != get_ti (de, ti, DATA_MAX_NAME_LEN))
288 cipvs_submit_connections (pi, ti, stats.conns);
289 cipvs_submit_if (pi, "if_packets", ti, stats.inpkts, stats.outpkts);
290 cipvs_submit_if (pi, "if_octets", ti, stats.inbytes, stats.outbytes);
292 } /* cipvs_submit_dest */
294 static void cipvs_submit_service (struct ip_vs_service_entry *se)
296 struct ip_vs_stats_user stats = se->stats;
297 struct ip_vs_get_dests *dests = ipvs_get_dests (se);
299 char pi[DATA_MAX_NAME_LEN];
303 if (0 != get_pi (se, pi, DATA_MAX_NAME_LEN))
306 cipvs_submit_connections (pi, NULL, stats.conns);
307 cipvs_submit_if (pi, "if_packets", NULL, stats.inpkts, stats.outpkts);
308 cipvs_submit_if (pi, "if_octets", NULL, stats.inbytes, stats.outbytes);
310 for (i = 0; i < dests->num_dests; ++i)
311 cipvs_submit_dest (pi, &dests->entrytable[i]);
313 } /* cipvs_submit_service */
315 static int cipvs_read (void)
317 struct ip_vs_get_services *services = NULL;
321 if (NULL == (services = ipvs_get_services ()))
324 for (i = 0; i < services->num_services; ++i)
325 cipvs_submit_service (&services->entrytable[i]);
331 static int cipvs_shutdown (void)
335 } /* cipvs_shutdown */
337 void module_register (void)
339 plugin_register_init ("ipvs", cipvs_init);
340 plugin_register_read ("ipvs", cipvs_read);
341 plugin_register_shutdown ("ipvs", cipvs_shutdown);
343 } /* module_register */
345 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */