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