3c6aa5c12ff1666f66aff4640f1aacb13ff53b8f
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 unsigned int i;
78 struct {
79 void *func;
80 int err;
81 const char *message;
82 } table [] = {
83 { 0, EPERM, "Permission denied (you must be root)" },
84 { 0, EINVAL, "Module is wrong version" },
85 { 0, ENOPROTOOPT, "Protocol not available" },
86 { 0, ENOMEM, "Memory allocation problem" },
87 { ipvs_get_services, ESRCH, "No such service" },
88 { ipvs_get_dests, ESRCH, "No such service" },
89 };
91 for (i = 0; i < sizeof (table) / sizeof (table[0]); i++) {
92 if (((NULL == table[i].func) || (table[i].func == ipvs_func))
93 && (table[i].err == err))
94 return table[i].message;
95 }
96 return strerror (err);
97 } /* ipvs_strerror */
99 static struct ip_vs_get_services *ipvs_get_services (void)
100 {
101 struct ip_vs_get_services *ret;
102 socklen_t len;
104 len = sizeof (*ret) +
105 sizeof (struct ip_vs_service_entry) * ipvs_info.num_services;
107 if (NULL == (ret = malloc (len))) {
108 log_err ("ipvs_get_services: Out of memory.");
109 exit (3);
110 }
112 ipvs_func = ipvs_get_services;
114 ret->num_services = ipvs_info.num_services;
116 if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_SERVICES,
117 (void *)ret, &len)) {
118 log_err ("ipvs_get_services: getsockopt failed: %s",
119 ipvs_strerror (errno));
121 free(ret);
122 return NULL;
123 }
124 return ret;
125 } /* ipvs_get_services */
127 static struct ip_vs_get_dests *ipvs_get_dests (struct ip_vs_service_entry *se)
128 {
129 struct ip_vs_get_dests *ret;
130 socklen_t len;
132 len = sizeof (*ret) + sizeof (struct ip_vs_dest_entry) * se->num_dests;
134 if (NULL == (ret = malloc (len))) {
135 log_err ("ipvs_get_dests: Out of memory.");
136 exit (3);
137 }
139 ipvs_func = ipvs_get_dests;
141 ret->fwmark = se->fwmark;
142 ret->protocol = se->protocol;
143 ret->addr = se->addr;
144 ret->port = se->port;
145 ret->num_dests = se->num_dests;
147 if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_DESTS,
148 (void *)ret, &len)) {
149 log_err ("ipvs_get_dests: getsockopt() failed: %s",
150 ipvs_strerror (errno));
151 free (ret);
152 return NULL;
153 }
154 return ret;
155 } /* ip_vs_get_dests */
158 /*
159 * collectd plugin API and helper functions
160 */
162 static int cipvs_init (void)
163 {
164 socklen_t len;
166 len = sizeof (ipvs_info);
168 if (-1 == (sockfd = socket (AF_INET, SOCK_RAW, IPPROTO_RAW))) {
169 log_err ("cipvs_init: socket() failed: %s", ipvs_strerror (errno));
170 return -1;
171 }
173 if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_INFO,
174 (void *)&ipvs_info, &len)) {
175 log_err ("cipvs_init: getsockopt() 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]);
311 return;
312 } /* cipvs_submit_service */
314 static int cipvs_read (void)
315 {
316 struct ip_vs_get_services *services = NULL;
318 int i = 0;
320 if (NULL == (services = ipvs_get_services ()))
321 return -1;
323 for (i = 0; i < services->num_services; ++i)
324 cipvs_submit_service (&services->entrytable[i]);
326 free (services);
327 return 0;
328 } /* cipvs_read */
330 static int cipvs_shutdown (void)
331 {
332 close (sockfd);
333 return 0;
334 } /* cipvs_shutdown */
336 void module_register (void)
337 {
338 plugin_register_init ("ipvs", cipvs_init);
339 plugin_register_read ("ipvs", cipvs_read);
340 plugin_register_shutdown ("ipvs", cipvs_shutdown);
341 return;
342 } /* module_register */
344 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */