f9acab5cfaa39cb70b9fe568d452b6778c36cdee
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;
65 /*
66 * libipvs API
67 */
69 static struct ip_vs_get_services *ipvs_get_services (void);
70 static struct ip_vs_get_dests *ipvs_get_dests (struct ip_vs_service_entry *);
72 static const char *ipvs_strerror (int err)
73 {
74 char errbuf[1024];
75 unsigned int i;
77 struct {
78 void *func;
79 int err;
80 const char *message;
81 } table [] = {
82 { 0, EPERM, "Permission denied (you must be root)" },
83 { 0, EINVAL, "Module is wrong version" },
84 { 0, ENOPROTOOPT, "Protocol not available" },
85 { 0, ENOMEM, "Memory allocation problem" },
86 { ipvs_get_services, ESRCH, "No such service" },
87 { ipvs_get_dests, ESRCH, "No such service" },
88 };
90 for (i = 0; i < sizeof (table) / sizeof (table[0]); i++) {
91 if (((NULL == table[i].func) || (table[i].func == ipvs_func))
92 && (table[i].err == err))
93 return table[i].message;
94 }
95 return sstrerror (err, errbuf, sizeof (errbuf));
96 } /* ipvs_strerror */
98 static struct ip_vs_get_services *ipvs_get_services (void)
99 {
100 struct ip_vs_getinfo ipvs_info;
101 struct ip_vs_get_services *ret;
103 socklen_t len;
105 len = sizeof (ipvs_info);
107 if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_INFO,
108 (void *)&ipvs_info, &len)) {
109 log_err ("ip_vs_get_services: getsockopt() failed: %s",
110 ipvs_strerror (errno));
111 return NULL;
112 }
114 len = sizeof (*ret) +
115 sizeof (struct ip_vs_service_entry) * ipvs_info.num_services;
117 if (NULL == (ret = malloc (len))) {
118 log_err ("ipvs_get_services: Out of memory.");
119 exit (3);
120 }
122 ipvs_func = ipvs_get_services;
124 ret->num_services = ipvs_info.num_services;
126 if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_SERVICES,
127 (void *)ret, &len)) {
128 log_err ("ipvs_get_services: getsockopt failed: %s",
129 ipvs_strerror (errno));
131 free(ret);
132 return NULL;
133 }
134 return ret;
135 } /* ipvs_get_services */
137 static struct ip_vs_get_dests *ipvs_get_dests (struct ip_vs_service_entry *se)
138 {
139 struct ip_vs_get_dests *ret;
140 socklen_t len;
142 len = sizeof (*ret) + sizeof (struct ip_vs_dest_entry) * se->num_dests;
144 if (NULL == (ret = malloc (len))) {
145 log_err ("ipvs_get_dests: Out of memory.");
146 exit (3);
147 }
149 ipvs_func = ipvs_get_dests;
151 ret->fwmark = se->fwmark;
152 ret->protocol = se->protocol;
153 ret->addr = se->addr;
154 ret->port = se->port;
155 ret->num_dests = se->num_dests;
157 if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_DESTS,
158 (void *)ret, &len)) {
159 log_err ("ipvs_get_dests: getsockopt() failed: %s",
160 ipvs_strerror (errno));
161 free (ret);
162 return NULL;
163 }
164 return ret;
165 } /* ip_vs_get_dests */
168 /*
169 * collectd plugin API and helper functions
170 */
172 static int cipvs_init (void)
173 {
174 if (-1 == (sockfd = socket (AF_INET, SOCK_RAW, IPPROTO_RAW))) {
175 log_err ("cipvs_init: socket() failed: %s", ipvs_strerror (errno));
176 return -1;
177 }
178 return 0;
179 } /* cipvs_init */
181 /*
182 * ipvs-<virtual IP>_{UDP,TCP}<port>/<type>-total
183 * ipvs-<virtual IP>_{UDP,TCP}<port>/<type>-<real IP>_<port>
184 */
186 /* plugin instance */
187 static int get_pi (struct ip_vs_service_entry *se, char *pi, size_t size)
188 {
189 struct in_addr addr;
190 int len = 0;
192 if ((NULL == se) || (NULL == pi))
193 return 0;
195 addr.s_addr = se->addr;
197 /* inet_ntoa() returns a pointer to a statically allocated buffer
198 * I hope non-glibc systems behave the same */
199 len = snprintf (pi, size, "%s_%s%u", inet_ntoa (addr),
200 (se->protocol == IPPROTO_TCP) ? "TCP" : "UDP",
201 ntohs (se->port));
203 if ((0 > len) || (size <= len)) {
204 log_err ("plugin instance truncated: %s", pi);
205 return -1;
206 }
207 return 0;
208 } /* get_pi */
210 /* type instance */
211 static int get_ti (struct ip_vs_dest_entry *de, char *ti, size_t size)
212 {
213 struct in_addr addr;
214 int len = 0;
216 if ((NULL == de) || (NULL == ti))
217 return 0;
219 addr.s_addr = de->addr;
221 /* inet_ntoa() returns a pointer to a statically allocated buffer
222 * I hope non-glibc systems behave the same */
223 len = snprintf (ti, size, "%s_%u", inet_ntoa (addr),
224 ntohs (de->port));
226 if ((0 > len) || (size <= len)) {
227 log_err ("type instance truncated: %s", ti);
228 return -1;
229 }
230 return 0;
231 } /* get_ti */
233 static void cipvs_submit_connections (char *pi, char *ti, counter_t value)
234 {
235 value_t values[1];
236 value_list_t vl = VALUE_LIST_INIT;
238 values[0].counter = value;
240 vl.values = values;
241 vl.values_len = 1;
243 vl.time = time (NULL);
244 vl.interval = interval_g;
246 strcpy (vl.host, hostname_g);
247 strcpy (vl.plugin, "ipvs");
248 strcpy (vl.plugin_instance, pi);
249 strcpy (vl.type_instance, (NULL != ti) ? ti : "total");
251 plugin_dispatch_values ("connections", &vl);
252 return;
253 } /* cipvs_submit_connections */
255 static void cipvs_submit_if (char *pi, char *t, char *ti,
256 counter_t rx, counter_t tx)
257 {
258 value_t values[2];
259 value_list_t vl = VALUE_LIST_INIT;
261 values[0].counter = rx;
262 values[1].counter = tx;
264 vl.values = values;
265 vl.values_len = 2;
267 vl.time = time (NULL);
268 vl.interval = interval_g;
270 strcpy (vl.host, hostname_g);
271 strcpy (vl.plugin, "ipvs");
272 strcpy (vl.plugin_instance, pi);
273 strcpy (vl.type_instance, (NULL != ti) ? ti : "total");
275 plugin_dispatch_values (t, &vl);
276 return;
277 } /* cipvs_submit_if */
279 static void cipvs_submit_dest (char *pi, struct ip_vs_dest_entry *de) {
280 struct ip_vs_stats_user stats = de->stats;
282 char ti[DATA_MAX_NAME_LEN];
284 if (0 != get_ti (de, ti, DATA_MAX_NAME_LEN))
285 return;
287 cipvs_submit_connections (pi, ti, stats.conns);
288 cipvs_submit_if (pi, "if_packets", ti, stats.inpkts, stats.outpkts);
289 cipvs_submit_if (pi, "if_octets", ti, stats.inbytes, stats.outbytes);
290 return;
291 } /* cipvs_submit_dest */
293 static void cipvs_submit_service (struct ip_vs_service_entry *se)
294 {
295 struct ip_vs_stats_user stats = se->stats;
296 struct ip_vs_get_dests *dests = ipvs_get_dests (se);
298 char pi[DATA_MAX_NAME_LEN];
300 int i = 0;
302 if (0 != get_pi (se, pi, DATA_MAX_NAME_LEN))
303 return;
305 cipvs_submit_connections (pi, NULL, stats.conns);
306 cipvs_submit_if (pi, "if_packets", NULL, stats.inpkts, stats.outpkts);
307 cipvs_submit_if (pi, "if_octets", NULL, stats.inbytes, stats.outbytes);
309 for (i = 0; i < dests->num_dests; ++i)
310 cipvs_submit_dest (pi, &dests->entrytable[i]);
312 free (dests);
313 return;
314 } /* cipvs_submit_service */
316 static int cipvs_read (void)
317 {
318 struct ip_vs_get_services *services = NULL;
320 int i = 0;
322 if (NULL == (services = ipvs_get_services ()))
323 return -1;
325 for (i = 0; i < services->num_services; ++i)
326 cipvs_submit_service (&services->entrytable[i]);
328 free (services);
329 return 0;
330 } /* cipvs_read */
332 static int cipvs_shutdown (void)
333 {
334 close (sockfd);
335 return 0;
336 } /* cipvs_shutdown */
338 void module_register (void)
339 {
340 plugin_register_init ("ipvs", cipvs_init);
341 plugin_register_read ("ipvs", cipvs_read);
342 plugin_register_shutdown ("ipvs", cipvs_shutdown);
343 return;
344 } /* module_register */
346 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */