f0b8d9f01f6d87c779d56e3d95cf0e5b4384b7cb
1 /**
2 * collectd - src/ethstat.c
3 * Copyright (C) 2011 Cyril Feraudet
4 * Copyright (C) 2012 Florian "octo" Forster
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Authors:
21 * Cyril Feraudet <cyril at feraudet.com>
22 * Florian "octo" Forster <octo@collectd.org>
23 **/
25 #include "collectd.h"
27 #include "common.h"
28 #include "plugin.h"
29 #include "utils_avltree.h"
30 #include "utils_complain.h"
32 #if HAVE_SYS_IOCTL_H
33 # include <sys/ioctl.h>
34 #endif
35 #if HAVE_NET_IF_H
36 # include <net/if.h>
37 #endif
38 #if HAVE_LINUX_SOCKIOS_H
39 # include <linux/sockios.h>
40 #endif
41 #if HAVE_LINUX_ETHTOOL_H
42 # include <linux/ethtool.h>
43 #endif
45 struct value_map_s
46 {
47 char type[DATA_MAX_NAME_LEN];
48 char type_instance[DATA_MAX_NAME_LEN];
49 };
50 typedef struct value_map_s value_map_t;
52 static char **interfaces = NULL;
53 static size_t interfaces_num = 0;
55 static c_avl_tree_t *value_map = NULL;
57 static _Bool collect_mapped_only = 0;
59 static int ethstat_add_interface (const oconfig_item_t *ci) /* {{{ */
60 {
61 char **tmp;
62 int status;
64 tmp = realloc (interfaces,
65 sizeof (*interfaces) * (interfaces_num + 1));
66 if (tmp == NULL)
67 return (-1);
68 interfaces = tmp;
69 interfaces[interfaces_num] = NULL;
71 status = cf_util_get_string (ci, interfaces + interfaces_num);
72 if (status != 0)
73 return (status);
75 interfaces_num++;
76 INFO("ethstat plugin: Registered interface %s",
77 interfaces[interfaces_num - 1]);
79 return (0);
80 } /* }}} int ethstat_add_interface */
82 static int ethstat_add_map (const oconfig_item_t *ci) /* {{{ */
83 {
84 value_map_t *map;
85 int status;
86 char *key;
88 if ((ci->values_num < 2)
89 || (ci->values_num > 3)
90 || (ci->values[0].type != OCONFIG_TYPE_STRING)
91 || (ci->values[1].type != OCONFIG_TYPE_STRING)
92 || ((ci->values_num == 3)
93 && (ci->values[2].type != OCONFIG_TYPE_STRING)))
94 {
95 ERROR ("ethstat plugin: The %s option requires "
96 "two or three string arguments.", ci->key);
97 return (-1);
98 }
100 key = strdup (ci->values[0].value.string);
101 if (key == NULL)
102 {
103 ERROR ("ethstat plugin: strdup(3) failed.");
104 return (ENOMEM);
105 }
107 map = calloc (1, sizeof (*map));
108 if (map == NULL)
109 {
110 sfree (key);
111 ERROR ("ethstat plugin: calloc failed.");
112 return (ENOMEM);
113 }
115 sstrncpy (map->type, ci->values[1].value.string, sizeof (map->type));
116 if (ci->values_num == 3)
117 sstrncpy (map->type_instance, ci->values[2].value.string,
118 sizeof (map->type_instance));
120 if (value_map == NULL)
121 {
122 value_map = c_avl_create ((int (*) (const void *, const void *)) strcmp);
123 if (value_map == NULL)
124 {
125 sfree (map);
126 sfree (key);
127 ERROR ("ethstat plugin: c_avl_create() failed.");
128 return (-1);
129 }
130 }
132 status = c_avl_insert (value_map,
133 /* key = */ key,
134 /* value = */ map);
135 if (status != 0)
136 {
137 if (status > 0)
138 ERROR ("ethstat plugin: Multiple mappings for \"%s\".", key);
139 else
140 ERROR ("ethstat plugin: c_avl_insert(\"%s\") failed.", key);
142 sfree (map);
143 sfree (key);
144 return (-1);
145 }
147 return (0);
148 } /* }}} int ethstat_add_map */
150 static int ethstat_config (oconfig_item_t *ci) /* {{{ */
151 {
152 for (int i = 0; i < ci->children_num; i++)
153 {
154 oconfig_item_t *child = ci->children + i;
156 if (strcasecmp ("Interface", child->key) == 0)
157 ethstat_add_interface (child);
158 else if (strcasecmp ("Map", child->key) == 0)
159 ethstat_add_map (child);
160 else if (strcasecmp ("MappedOnly", child->key) == 0)
161 (void) cf_util_get_boolean (child, &collect_mapped_only);
162 else
163 WARNING ("ethstat plugin: The config option \"%s\" is unknown.",
164 child->key);
165 }
167 return (0);
168 } /* }}} */
170 static void ethstat_submit_value (const char *device,
171 const char *type_instance, derive_t value)
172 {
173 static c_complain_t complain_no_map = C_COMPLAIN_INIT_STATIC;
175 value_list_t vl = VALUE_LIST_INIT;
176 value_map_t *map = NULL;
178 if (value_map != NULL)
179 c_avl_get (value_map, type_instance, (void *) &map);
181 /* If the "MappedOnly" option is specified, ignore unmapped values. */
182 if (collect_mapped_only && (map == NULL))
183 {
184 if (value_map == NULL)
185 c_complain (LOG_WARNING, &complain_no_map,
186 "ethstat plugin: The \"MappedOnly\" option has been set to true, "
187 "but no mapping has been configured. All values will be ignored!");
188 return;
189 }
191 vl.values = &(value_t) { .derive = value };
192 vl.values_len = 1;
194 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
195 sstrncpy (vl.plugin, "ethstat", sizeof (vl.plugin));
196 sstrncpy (vl.plugin_instance, device, sizeof (vl.plugin_instance));
197 if (map != NULL)
198 {
199 sstrncpy (vl.type, map->type, sizeof (vl.type));
200 sstrncpy (vl.type_instance, map->type_instance,
201 sizeof (vl.type_instance));
202 }
203 else
204 {
205 sstrncpy (vl.type, "derive", sizeof (vl.type));
206 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
207 }
209 plugin_dispatch_values (&vl);
210 }
212 static int ethstat_read_interface (char *device)
213 {
214 int fd;
215 struct ethtool_gstrings *strings;
216 struct ethtool_stats *stats;
217 size_t n_stats;
218 size_t strings_size;
219 size_t stats_size;
220 int status;
222 fd = socket(AF_INET, SOCK_DGRAM, /* protocol = */ 0);
223 if (fd < 0)
224 {
225 char errbuf[1024];
226 ERROR("ethstat plugin: Failed to open control socket: %s",
227 sstrerror (errno, errbuf, sizeof (errbuf)));
228 return 1;
229 }
231 struct ethtool_drvinfo drvinfo = {
232 .cmd = ETHTOOL_GDRVINFO
233 };
235 struct ifreq req = {
236 .ifr_data = (void *) &drvinfo
237 };
239 sstrncpy(req.ifr_name, device, sizeof (req.ifr_name));
241 status = ioctl (fd, SIOCETHTOOL, &req);
242 if (status < 0)
243 {
244 char errbuf[1024];
245 close (fd);
246 ERROR ("ethstat plugin: Failed to get driver information "
247 "from %s: %s", device,
248 sstrerror (errno, errbuf, sizeof (errbuf)));
249 return (-1);
250 }
252 n_stats = (size_t) drvinfo.n_stats;
253 if (n_stats < 1)
254 {
255 close (fd);
256 ERROR("ethstat plugin: No stats available for %s", device);
257 return (-1);
258 }
260 strings_size = sizeof (struct ethtool_gstrings)
261 + (n_stats * ETH_GSTRING_LEN);
262 stats_size = sizeof (struct ethtool_stats)
263 + (n_stats * sizeof (uint64_t));
265 strings = malloc (strings_size);
266 stats = malloc (stats_size);
267 if ((strings == NULL) || (stats == NULL))
268 {
269 close (fd);
270 sfree (strings);
271 sfree (stats);
272 ERROR("ethstat plugin: malloc failed.");
273 return (-1);
274 }
276 strings->cmd = ETHTOOL_GSTRINGS;
277 strings->string_set = ETH_SS_STATS;
278 strings->len = n_stats;
279 req.ifr_data = (void *) strings;
280 status = ioctl (fd, SIOCETHTOOL, &req);
281 if (status < 0)
282 {
283 char errbuf[1024];
284 close (fd);
285 free (strings);
286 free (stats);
287 ERROR ("ethstat plugin: Cannot get strings from %s: %s",
288 device,
289 sstrerror (errno, errbuf, sizeof (errbuf)));
290 return (-1);
291 }
293 stats->cmd = ETHTOOL_GSTATS;
294 stats->n_stats = n_stats;
295 req.ifr_data = (void *) stats;
296 status = ioctl (fd, SIOCETHTOOL, &req);
297 if (status < 0)
298 {
299 char errbuf[1024];
300 close (fd);
301 free(strings);
302 free(stats);
303 ERROR("ethstat plugin: Reading statistics from %s failed: %s",
304 device,
305 sstrerror (errno, errbuf, sizeof (errbuf)));
306 return (-1);
307 }
309 for (size_t i = 0; i < n_stats; i++)
310 {
311 char *stat_name;
313 stat_name = (void *) &strings->data[i * ETH_GSTRING_LEN];
314 /* Remove leading spaces in key name */
315 while (isspace ((int) *stat_name))
316 stat_name++;
318 DEBUG("ethstat plugin: device = \"%s\": %s = %"PRIu64,
319 device, stat_name, (uint64_t) stats->data[i]);
320 ethstat_submit_value (device,
321 stat_name, (derive_t) stats->data[i]);
322 }
324 close (fd);
325 sfree (strings);
326 sfree (stats);
328 return (0);
329 } /* }}} ethstat_read_interface */
331 static int ethstat_read(void)
332 {
333 for (size_t i = 0; i < interfaces_num; i++)
334 ethstat_read_interface (interfaces[i]);
336 return 0;
337 }
339 static int ethstat_shutdown (void)
340 {
341 void *key = NULL;
342 void *value = NULL;
344 if (value_map == NULL)
345 return (0);
347 while (c_avl_pick (value_map, &key, &value) == 0)
348 {
349 sfree (key);
350 sfree (value);
351 }
353 c_avl_destroy (value_map);
354 value_map = NULL;
356 return (0);
357 }
359 void module_register (void)
360 {
361 plugin_register_complex_config ("ethstat", ethstat_config);
362 plugin_register_read ("ethstat", ethstat_read);
363 plugin_register_shutdown ("ethstat", ethstat_shutdown);
364 }
366 /* vim: set sw=2 sts=2 et fdm=marker : */