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__)
55 #define log_info(...) INFO ("ipvs: " __VA_ARGS__)
57 /*
58 * private variables
59 */
60 static int sockfd = -1;
62 /*
63 * libipvs API
64 */
65 static struct ip_vs_get_services *ipvs_get_services (void)
66 {
67 struct ip_vs_getinfo ipvs_info;
68 struct ip_vs_get_services *ret;
70 socklen_t len;
72 len = sizeof (ipvs_info);
74 if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_INFO,
75 (void *)&ipvs_info, &len)) {
76 char errbuf[1024];
77 log_err ("ip_vs_get_services: getsockopt() failed: %s",
78 sstrerror (errno, errbuf, sizeof (errbuf)));
79 return NULL;
80 }
82 len = sizeof (*ret) +
83 sizeof (struct ip_vs_service_entry) * ipvs_info.num_services;
85 if (NULL == (ret = malloc (len))) {
86 log_err ("ipvs_get_services: Out of memory.");
87 exit (3);
88 }
90 ret->num_services = ipvs_info.num_services;
92 if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_SERVICES,
93 (void *)ret, &len)) {
94 char errbuf[1024];
95 log_err ("ipvs_get_services: getsockopt failed: %s",
96 sstrerror (errno, errbuf, sizeof (errbuf)));
98 free(ret);
99 return NULL;
100 }
101 return ret;
102 } /* ipvs_get_services */
104 static struct ip_vs_get_dests *ipvs_get_dests (struct ip_vs_service_entry *se)
105 {
106 struct ip_vs_get_dests *ret;
107 socklen_t len;
109 len = sizeof (*ret) + sizeof (struct ip_vs_dest_entry) * se->num_dests;
111 if (NULL == (ret = malloc (len))) {
112 log_err ("ipvs_get_dests: Out of memory.");
113 exit (3);
114 }
116 ret->fwmark = se->fwmark;
117 ret->protocol = se->protocol;
118 ret->addr = se->addr;
119 ret->port = se->port;
120 ret->num_dests = se->num_dests;
122 if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_DESTS,
123 (void *)ret, &len)) {
124 char errbuf[1024];
125 log_err ("ipvs_get_dests: getsockopt() failed: %s",
126 sstrerror (errno, errbuf, sizeof (errbuf)));
127 free (ret);
128 return NULL;
129 }
130 return ret;
131 } /* ip_vs_get_dests */
133 /*
134 * collectd plugin API and helper functions
135 */
136 static int cipvs_init (void)
137 {
138 struct ip_vs_getinfo ipvs_info;
140 socklen_t len;
142 if (-1 == (sockfd = socket (AF_INET, SOCK_RAW, IPPROTO_RAW))) {
143 char errbuf[1024];
144 log_err ("cipvs_init: socket() failed: %s",
145 sstrerror (errno, errbuf, sizeof (errbuf)));
146 return -1;
147 }
149 len = sizeof (ipvs_info);
151 if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_INFO,
152 (void *)&ipvs_info, &len)) {
153 char errbuf[1024];
154 log_err ("cipvs_init: getsockopt() failed: %s",
155 sstrerror (errno, errbuf, sizeof (errbuf)));
156 close (sockfd);
157 sockfd = -1;
158 return -1;
159 }
161 /* we need IPVS >= 1.1.4 */
162 if (ipvs_info.version < ((1 << 16) + (1 << 8) + 4)) {
163 log_err ("cipvs_init: IPVS version too old (%d.%d.%d < %d.%d.%d)",
164 NVERSION (ipvs_info.version), 1, 1, 4);
165 close (sockfd);
166 sockfd = -1;
167 return -1;
168 }
169 else {
170 log_info ("Successfully connected to IPVS %d.%d.%d",
171 NVERSION (ipvs_info.version));
172 }
173 return 0;
174 } /* cipvs_init */
176 /*
177 * ipvs-<virtual IP>_{UDP,TCP}<port>/<type>-total
178 * ipvs-<virtual IP>_{UDP,TCP}<port>/<type>-<real IP>_<port>
179 */
181 /* plugin instance */
182 static int get_pi (struct ip_vs_service_entry *se, char *pi, size_t size)
183 {
184 struct in_addr addr;
185 int len = 0;
187 if ((NULL == se) || (NULL == pi))
188 return 0;
190 addr.s_addr = se->addr;
192 /* inet_ntoa() returns a pointer to a statically allocated buffer
193 * I hope non-glibc systems behave the same */
194 len = ssnprintf (pi, size, "%s_%s%u", inet_ntoa (addr),
195 (se->protocol == IPPROTO_TCP) ? "TCP" : "UDP",
196 ntohs (se->port));
198 if ((0 > len) || (size <= len)) {
199 log_err ("plugin instance truncated: %s", pi);
200 return -1;
201 }
202 return 0;
203 } /* get_pi */
205 /* type instance */
206 static int get_ti (struct ip_vs_dest_entry *de, char *ti, size_t size)
207 {
208 struct in_addr addr;
209 int len = 0;
211 if ((NULL == de) || (NULL == ti))
212 return 0;
214 addr.s_addr = de->addr;
216 /* inet_ntoa() returns a pointer to a statically allocated buffer
217 * I hope non-glibc systems behave the same */
218 len = ssnprintf (ti, size, "%s_%u", inet_ntoa (addr),
219 ntohs (de->port));
221 if ((0 > len) || (size <= len)) {
222 log_err ("type instance truncated: %s", ti);
223 return -1;
224 }
225 return 0;
226 } /* get_ti */
228 static void cipvs_submit_connections (char *pi, char *ti, counter_t value)
229 {
230 value_t values[1];
231 value_list_t vl = VALUE_LIST_INIT;
233 values[0].counter = value;
235 vl.values = values;
236 vl.values_len = 1;
238 vl.interval = interval_g;
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);
248 return;
249 } /* cipvs_submit_connections */
251 static void cipvs_submit_if (char *pi, char *t, char *ti,
252 counter_t rx, counter_t tx)
253 {
254 value_t values[2];
255 value_list_t vl = VALUE_LIST_INIT;
257 values[0].counter = rx;
258 values[1].counter = tx;
260 vl.values = values;
261 vl.values_len = 2;
263 vl.interval = interval_g;
265 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
266 sstrncpy (vl.plugin, "ipvs", sizeof (vl.plugin));
267 sstrncpy (vl.plugin_instance, pi, sizeof (vl.plugin_instance));
268 sstrncpy (vl.type, t, sizeof (vl.type));
269 sstrncpy (vl.type_instance, (NULL != ti) ? ti : "total",
270 sizeof (vl.type_instance));
272 plugin_dispatch_values (&vl);
273 return;
274 } /* cipvs_submit_if */
276 static void cipvs_submit_dest (char *pi, struct ip_vs_dest_entry *de) {
277 struct ip_vs_stats_user stats = de->stats;
279 char ti[DATA_MAX_NAME_LEN];
281 if (0 != get_ti (de, ti, sizeof (ti)))
282 return;
284 cipvs_submit_connections (pi, ti, stats.conns);
285 cipvs_submit_if (pi, "if_packets", ti, stats.inpkts, stats.outpkts);
286 cipvs_submit_if (pi, "if_octets", ti, stats.inbytes, stats.outbytes);
287 return;
288 } /* cipvs_submit_dest */
290 static void cipvs_submit_service (struct ip_vs_service_entry *se)
291 {
292 struct ip_vs_stats_user stats = se->stats;
293 struct ip_vs_get_dests *dests = ipvs_get_dests (se);
295 char pi[DATA_MAX_NAME_LEN];
297 int i = 0;
299 if (0 != get_pi (se, pi, sizeof (pi)))
300 return;
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);
306 for (i = 0; i < dests->num_dests; ++i)
307 cipvs_submit_dest (pi, &dests->entrytable[i]);
309 free (dests);
310 return;
311 } /* cipvs_submit_service */
313 static int cipvs_read (void)
314 {
315 struct ip_vs_get_services *services = NULL;
316 int i = 0;
318 if (sockfd < 0)
319 return (-1);
321 if (NULL == (services = ipvs_get_services ()))
322 return -1;
324 for (i = 0; i < services->num_services; ++i)
325 cipvs_submit_service (&services->entrytable[i]);
327 free (services);
328 return 0;
329 } /* cipvs_read */
331 static int cipvs_shutdown (void)
332 {
333 if (sockfd >= 0)
334 close (sockfd);
335 sockfd = -1;
337 return 0;
338 } /* cipvs_shutdown */
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 */
348 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */