c670b09da1da52c976a2f440aae1b4ccff61569f
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 **/
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 */
33 #include "collectd.h"
34 #include "plugin.h"
35 #include "common.h"
37 #if HAVE_ARPA_INET_H
38 # include <arpa/inet.h>
39 #endif /* HAVE_ARPA_INET_H */
40 #if HAVE_SYS_SOCKET_H
41 # include <sys/socket.h>
42 #endif /* HAVE_SYS_SOCKET_H */
43 #if HAVE_NETINET_IN_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_NET_IP_VS_H
49 # include <net/ip_vs.h>
50 #elif HAVE_IP_VS_H
51 # include <ip_vs.h>
52 #endif /* HAVE_IP_VS_H */
54 #define log_err(...) ERROR ("ipvs: " __VA_ARGS__)
57 /*
58 * private variables
59 */
61 static int sockfd = -1;
62 static void *ipvs_func = NULL;
64 static struct ip_vs_getinfo ipvs_info;
67 /*
68 * libipvs API
69 */
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)
75 {
76 char errbuf[1024];
77 unsigned int i;
79 struct {
80 void *func;
81 int err;
82 const char *message;
83 } table [] = {
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" },
90 };
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;
96 }
97 return sstrerror (err, errbuf, sizeof (errbuf));
98 } /* ipvs_strerror */
100 static struct ip_vs_get_services *ipvs_get_services (void)
101 {
102 struct ip_vs_get_services *ret;
103 socklen_t len;
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.");
110 exit (3);
111 }
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));
122 free(ret);
123 return NULL;
124 }
125 return ret;
126 } /* ipvs_get_services */
128 static struct ip_vs_get_dests *ipvs_get_dests (struct ip_vs_service_entry *se)
129 {
130 struct ip_vs_get_dests *ret;
131 socklen_t len;
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.");
137 exit (3);
138 }
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));
152 free (ret);
153 return NULL;
154 }
155 return ret;
156 } /* ip_vs_get_dests */
159 /*
160 * collectd plugin API and helper functions
161 */
163 static int cipvs_init (void)
164 {
165 socklen_t len;
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));
171 return -1;
172 }
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));
177 return -1;
178 }
179 return 0;
180 } /* cipvs_init */
182 /*
183 * ipvs-<virtual IP>_{UDP,TCP}<port>/<type>-total
184 * ipvs-<virtual IP>_{UDP,TCP}<port>/<type>-<real IP>_<port>
185 */
187 /* plugin instance */
188 static int get_pi (struct ip_vs_service_entry *se, char *pi, size_t size)
189 {
190 struct in_addr addr;
191 int len = 0;
193 if ((NULL == se) || (NULL == pi))
194 return 0;
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",
202 ntohs (se->port));
204 if ((0 > len) || (size <= len)) {
205 log_err ("plugin instance truncated: %s", pi);
206 return -1;
207 }
208 return 0;
209 } /* get_pi */
211 /* type instance */
212 static int get_ti (struct ip_vs_dest_entry *de, char *ti, size_t size)
213 {
214 struct in_addr addr;
215 int len = 0;
217 if ((NULL == de) || (NULL == ti))
218 return 0;
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),
225 ntohs (de->port));
227 if ((0 > len) || (size <= len)) {
228 log_err ("type instance truncated: %s", ti);
229 return -1;
230 }
231 return 0;
232 } /* get_ti */
234 static void cipvs_submit_connections (char *pi, char *ti, counter_t value)
235 {
236 value_t values[1];
237 value_list_t vl = VALUE_LIST_INIT;
239 values[0].counter = value;
241 vl.values = values;
242 vl.values_len = 1;
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);
253 return;
254 } /* cipvs_submit_connections */
256 static void cipvs_submit_if (char *pi, char *t, char *ti,
257 counter_t rx, counter_t tx)
258 {
259 value_t values[2];
260 value_list_t vl = VALUE_LIST_INIT;
262 values[0].counter = rx;
263 values[1].counter = tx;
265 vl.values = values;
266 vl.values_len = 2;
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);
277 return;
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))
286 return;
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);
291 return;
292 } /* cipvs_submit_dest */
294 static void cipvs_submit_service (struct ip_vs_service_entry *se)
295 {
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];
301 int i = 0;
303 if (0 != get_pi (se, pi, DATA_MAX_NAME_LEN))
304 return;
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 free (dests);
314 return;
315 } /* cipvs_submit_service */
317 static int cipvs_read (void)
318 {
319 struct ip_vs_get_services *services = NULL;
321 int i = 0;
323 if (NULL == (services = ipvs_get_services ()))
324 return -1;
326 for (i = 0; i < services->num_services; ++i)
327 cipvs_submit_service (&services->entrytable[i]);
329 free (services);
330 return 0;
331 } /* cipvs_read */
333 static int cipvs_shutdown (void)
334 {
335 close (sockfd);
336 return 0;
337 } /* cipvs_shutdown */
339 void module_register (void)
340 {
341 plugin_register_init ("ipvs", cipvs_init);
342 plugin_register_read ("ipvs", cipvs_read);
343 plugin_register_shutdown ("ipvs", cipvs_shutdown);
344 return;
345 } /* module_register */
347 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */