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