ipvs plugin: Fixed a memory leak in cipvs_submit_services().
[collectd.git] / src / ipvs.c
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  **/
24
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  */
32
33 #include "collectd.h"
34 #include "plugin.h"
35 #include "common.h"
36
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 */
46
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 */
53
54 #define log_err(...) ERROR ("ipvs: " __VA_ARGS__)
55
56
57 /*
58  * private variables
59  */
60
61 static int   sockfd    = -1;
62 static void *ipvs_func = NULL;
63
64 static struct ip_vs_getinfo ipvs_info;
65
66
67 /*
68  * libipvs API
69  */
70
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 *);
73
74 static const char *ipvs_strerror (int err)
75 {
76         char errbuf[1024];
77         unsigned int i;
78
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         };
91
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 */
99
100 static struct ip_vs_get_services *ipvs_get_services (void)
101 {
102         struct ip_vs_get_services *ret;
103         socklen_t len;
104
105         len = sizeof (*ret) +
106                 sizeof (struct ip_vs_service_entry) * ipvs_info.num_services;
107
108         if (NULL == (ret = malloc (len))) {
109                 log_err ("ipvs_get_services: Out of memory.");
110                 exit (3);
111         }
112
113         ipvs_func = ipvs_get_services;
114
115         ret->num_services = ipvs_info.num_services;
116
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));
121
122                 free(ret);
123                 return NULL;
124         }
125         return ret;
126 } /* ipvs_get_services */
127
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;
132
133         len = sizeof (*ret) + sizeof (struct ip_vs_dest_entry) * se->num_dests;
134
135         if (NULL == (ret = malloc (len))) {
136                 log_err ("ipvs_get_dests: Out of memory.");
137                 exit (3);
138         }
139
140         ipvs_func = ipvs_get_dests;
141
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;
147
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 */
157
158
159 /*
160  * collectd plugin API and helper functions
161  */
162
163 static int cipvs_init (void)
164 {
165         socklen_t len;
166
167         len = sizeof (ipvs_info);
168
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         }
173
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 */
181
182 /*
183  * ipvs-<virtual IP>_{UDP,TCP}<port>/<type>-total
184  * ipvs-<virtual IP>_{UDP,TCP}<port>/<type>-<real IP>_<port>
185  */
186
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;
192
193         if ((NULL == se) || (NULL == pi))
194                 return 0;
195
196         addr.s_addr = se->addr;
197
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));
203
204         if ((0 > len) || (size <= len)) {
205                 log_err ("plugin instance truncated: %s", pi);
206                 return -1;
207         }
208         return 0;
209 } /* get_pi */
210
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;
216
217         if ((NULL == de) || (NULL == ti))
218                 return 0;
219
220         addr.s_addr = de->addr;
221
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));
226
227         if ((0 > len) || (size <= len)) {
228                 log_err ("type instance truncated: %s", ti);
229                 return -1;
230         }
231         return 0;
232 } /* get_ti */
233
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;
238
239         values[0].counter = value;
240
241         vl.values     = values;
242         vl.values_len = 1;
243
244         vl.time     = time (NULL);
245         vl.interval = interval_g;
246
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");
251
252         plugin_dispatch_values ("connections", &vl);
253         return;
254 } /* cipvs_submit_connections */
255
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;
261
262         values[0].counter = rx;
263         values[1].counter = tx;
264
265         vl.values     = values;
266         vl.values_len = 2;
267
268         vl.time     = time (NULL);
269         vl.interval = interval_g;
270
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");
275
276         plugin_dispatch_values (t, &vl);
277         return;
278 } /* cipvs_submit_if */
279
280 static void cipvs_submit_dest (char *pi, struct ip_vs_dest_entry *de) {
281         struct ip_vs_stats_user stats = de->stats;
282
283         char ti[DATA_MAX_NAME_LEN];
284
285         if (0 != get_ti (de, ti, DATA_MAX_NAME_LEN))
286                 return;
287
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 */
293
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);
298
299         char pi[DATA_MAX_NAME_LEN];
300
301         int i = 0;
302
303         if (0 != get_pi (se, pi, DATA_MAX_NAME_LEN))
304                 return;
305
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);
309
310         for (i = 0; i < dests->num_dests; ++i)
311                 cipvs_submit_dest (pi, &dests->entrytable[i]);
312
313         free (dests);
314         return;
315 } /* cipvs_submit_service */
316
317 static int cipvs_read (void)
318 {
319         struct ip_vs_get_services *services = NULL;
320
321         int i = 0;
322
323         if (NULL == (services = ipvs_get_services ()))
324                 return -1;
325
326         for (i = 0; i < services->num_services; ++i)
327                 cipvs_submit_service (&services->entrytable[i]);
328
329         free (services);
330         return 0;
331 } /* cipvs_read */
332
333 static int cipvs_shutdown (void)
334 {
335         close (sockfd);
336         return 0;
337 } /* cipvs_shutdown */
338
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 */
346
347 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
348