Debian Codegraph @

src/ipvs.c

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