snmp plugin: Fix error message printed by the "Shift" option.
[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_LINUX_IP_VS_H
49 # include <linux/ip_vs.h>
50 #elif HAVE_NET_IP_VS_H
51 # include <net/ip_vs.h>
52 #elif HAVE_IP_VS_H
53 # include <ip_vs.h>
54 #endif /* HAVE_IP_VS_H */
55
56 #define log_err(...) ERROR ("ipvs: " __VA_ARGS__)
57 #define log_info(...) INFO ("ipvs: " __VA_ARGS__)
58
59 /*
60  * private variables
61  */
62 static int sockfd = -1;
63
64 /*
65  * libipvs API
66  */
67 static struct ip_vs_get_services *ipvs_get_services (void)
68 {
69         struct ip_vs_getinfo       ipvs_info;
70         struct ip_vs_get_services *ret;
71
72         socklen_t len;
73
74         len = sizeof (ipvs_info);
75
76         if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_INFO,
77                                 (void *)&ipvs_info, &len)) {
78                 char errbuf[1024];
79                 log_err ("ip_vs_get_services: getsockopt() failed: %s",
80                                 sstrerror (errno, errbuf, sizeof (errbuf)));
81                 return NULL;
82         }
83
84         len = sizeof (*ret) +
85                 sizeof (struct ip_vs_service_entry) * ipvs_info.num_services;
86
87         if (NULL == (ret = malloc (len))) {
88                 log_err ("ipvs_get_services: Out of memory.");
89                 exit (3);
90         }
91
92         ret->num_services = ipvs_info.num_services;
93
94         if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_SERVICES,
95                                 (void *)ret, &len)) {
96                 char errbuf[1024];
97                 log_err ("ipvs_get_services: getsockopt failed: %s",
98                                 sstrerror (errno, errbuf, sizeof (errbuf)));
99
100                 free(ret);
101                 return NULL;
102         }
103         return ret;
104 } /* ipvs_get_services */
105
106 static struct ip_vs_get_dests *ipvs_get_dests (struct ip_vs_service_entry *se)
107 {
108         struct ip_vs_get_dests *ret;
109         socklen_t len;
110
111         len = sizeof (*ret) + sizeof (struct ip_vs_dest_entry) * se->num_dests;
112
113         if (NULL == (ret = malloc (len))) {
114                 log_err ("ipvs_get_dests: Out of memory.");
115                 exit (3);
116         }
117
118         ret->fwmark    = se->fwmark;
119         ret->protocol  = se->protocol;
120         ret->addr      = se->addr;
121         ret->port      = se->port;
122         ret->num_dests = se->num_dests;
123
124         if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_DESTS,
125                                 (void *)ret, &len)) {
126                 char errbuf[1024];
127                 log_err ("ipvs_get_dests: getsockopt() failed: %s",
128                                 sstrerror (errno, errbuf, sizeof (errbuf)));
129                 free (ret);
130                 return NULL;
131         }
132         return ret;
133 } /* ip_vs_get_dests */
134
135 /*
136  * collectd plugin API and helper functions
137  */
138 static int cipvs_init (void)
139 {
140         struct ip_vs_getinfo ipvs_info;
141
142         socklen_t len;
143
144         if (-1 == (sockfd = socket (AF_INET, SOCK_RAW, IPPROTO_RAW))) {
145                 char errbuf[1024];
146                 log_err ("cipvs_init: socket() failed: %s",
147                                 sstrerror (errno, errbuf, sizeof (errbuf)));
148                 return -1;
149         }
150
151         len = sizeof (ipvs_info);
152
153         if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_INFO,
154                                 (void *)&ipvs_info, &len)) {
155                 char errbuf[1024];
156                 log_err ("cipvs_init: getsockopt() failed: %s",
157                                 sstrerror (errno, errbuf, sizeof (errbuf)));
158                 close (sockfd);
159                 sockfd = -1;
160                 return -1;
161         }
162
163         /* we need IPVS >= 1.1.4 */
164         if (ipvs_info.version < ((1 << 16) + (1 << 8) + 4)) {
165                 log_err ("cipvs_init: IPVS version too old (%d.%d.%d < %d.%d.%d)",
166                                 NVERSION (ipvs_info.version), 1, 1, 4);
167                 close (sockfd);
168                 sockfd = -1;
169                 return -1;
170         }
171         else {
172                 log_info ("Successfully connected to IPVS %d.%d.%d",
173                                 NVERSION (ipvs_info.version));
174         }
175         return 0;
176 } /* cipvs_init */
177
178 /*
179  * ipvs-<virtual IP>_{UDP,TCP}<port>/<type>-total
180  * ipvs-<virtual IP>_{UDP,TCP}<port>/<type>-<real IP>_<port>
181  */
182
183 /* plugin instance */
184 static int get_pi (struct ip_vs_service_entry *se, char *pi, size_t size)
185 {
186         struct in_addr addr;
187         int len = 0;
188
189         if ((NULL == se) || (NULL == pi))
190                 return 0;
191
192         addr.s_addr = se->addr;
193
194         /* inet_ntoa() returns a pointer to a statically allocated buffer
195          * I hope non-glibc systems behave the same */
196         len = ssnprintf (pi, size, "%s_%s%u", inet_ntoa (addr),
197                         (se->protocol == IPPROTO_TCP) ? "TCP" : "UDP",
198                         ntohs (se->port));
199
200         if ((0 > len) || (size <= len)) {
201                 log_err ("plugin instance truncated: %s", pi);
202                 return -1;
203         }
204         return 0;
205 } /* get_pi */
206
207 /* type instance */
208 static int get_ti (struct ip_vs_dest_entry *de, char *ti, size_t size)
209 {
210         struct in_addr addr;
211         int len = 0;
212
213         if ((NULL == de) || (NULL == ti))
214                 return 0;
215
216         addr.s_addr = de->addr;
217
218         /* inet_ntoa() returns a pointer to a statically allocated buffer
219          * I hope non-glibc systems behave the same */
220         len = ssnprintf (ti, size, "%s_%u", inet_ntoa (addr),
221                         ntohs (de->port));
222
223         if ((0 > len) || (size <= len)) {
224                 log_err ("type instance truncated: %s", ti);
225                 return -1;
226         }
227         return 0;
228 } /* get_ti */
229
230 static void cipvs_submit_connections (char *pi, char *ti, counter_t value)
231 {
232         value_t values[1];
233         value_list_t vl = VALUE_LIST_INIT;
234
235         values[0].counter = value;
236
237         vl.values     = values;
238         vl.values_len = 1;
239
240         vl.interval = interval_g;
241
242         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
243         sstrncpy (vl.plugin, "ipvs", sizeof (vl.plugin));
244         sstrncpy (vl.plugin_instance, pi, sizeof (vl.plugin_instance));
245         sstrncpy (vl.type, "connections", sizeof (vl.type));
246         sstrncpy (vl.type_instance, (NULL != ti) ? ti : "total",
247                 sizeof (vl.type_instance));
248
249         plugin_dispatch_values (&vl);
250         return;
251 } /* cipvs_submit_connections */
252
253 static void cipvs_submit_if (char *pi, char *t, char *ti,
254                 counter_t rx, counter_t tx)
255 {
256         value_t values[2];
257         value_list_t vl = VALUE_LIST_INIT;
258
259         values[0].counter = rx;
260         values[1].counter = tx;
261
262         vl.values     = values;
263         vl.values_len = 2;
264
265         vl.interval = interval_g;
266
267         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
268         sstrncpy (vl.plugin, "ipvs", sizeof (vl.plugin));
269         sstrncpy (vl.plugin_instance, pi, sizeof (vl.plugin_instance));
270         sstrncpy (vl.type, t, sizeof (vl.type));
271         sstrncpy (vl.type_instance, (NULL != ti) ? ti : "total",
272                 sizeof (vl.type_instance));
273
274         plugin_dispatch_values (&vl);
275         return;
276 } /* cipvs_submit_if */
277
278 static void cipvs_submit_dest (char *pi, struct ip_vs_dest_entry *de) {
279         struct ip_vs_stats_user stats = de->stats;
280
281         char ti[DATA_MAX_NAME_LEN];
282
283         if (0 != get_ti (de, ti, sizeof (ti)))
284                 return;
285
286         cipvs_submit_connections (pi, ti, stats.conns);
287         cipvs_submit_if (pi, "if_packets", ti, stats.inpkts, stats.outpkts);
288         cipvs_submit_if (pi, "if_octets", ti, stats.inbytes, stats.outbytes);
289         return;
290 } /* cipvs_submit_dest */
291
292 static void cipvs_submit_service (struct ip_vs_service_entry *se)
293 {
294         struct ip_vs_stats_user  stats = se->stats;
295         struct ip_vs_get_dests  *dests = ipvs_get_dests (se);
296
297         char pi[DATA_MAX_NAME_LEN];
298
299         int i = 0;
300
301         if (0 != get_pi (se, pi, sizeof (pi)))
302                 return;
303
304         cipvs_submit_connections (pi, NULL, stats.conns);
305         cipvs_submit_if (pi, "if_packets", NULL, stats.inpkts, stats.outpkts);
306         cipvs_submit_if (pi, "if_octets", NULL, stats.inbytes, stats.outbytes);
307
308         for (i = 0; i < dests->num_dests; ++i)
309                 cipvs_submit_dest (pi, &dests->entrytable[i]);
310
311         free (dests);
312         return;
313 } /* cipvs_submit_service */
314
315 static int cipvs_read (void)
316 {
317         struct ip_vs_get_services *services = NULL;
318         int i = 0;
319
320         if (sockfd < 0)
321                 return (-1);
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         if (sockfd >= 0)
336                 close (sockfd);
337         sockfd = -1;
338
339         return 0;
340 } /* cipvs_shutdown */
341
342 void module_register (void)
343 {
344         plugin_register_init ("ipvs", cipvs_init);
345         plugin_register_read ("ipvs", cipvs_read);
346         plugin_register_shutdown ("ipvs", cipvs_shutdown);
347         return;
348 } /* module_register */
349
350 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */