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