Merge remote-tracking branch 'origin/pr/1346'
[collectd.git] / src / ethstat.c
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  **/
24
25 #include "collectd.h"
26
27 #include "common.h"
28 #include "plugin.h"
29 #include "configfile.h"
30 #include "utils_avltree.h"
31 #include "utils_complain.h"
32
33 #if HAVE_SYS_IOCTL_H
34 # include <sys/ioctl.h>
35 #endif
36 #if HAVE_NET_IF_H
37 # include <net/if.h>
38 #endif
39 #if HAVE_LINUX_SOCKIOS_H
40 # include <linux/sockios.h>
41 #endif
42 #if HAVE_LINUX_ETHTOOL_H
43 # include <linux/ethtool.h>
44 #endif
45
46 struct value_map_s
47 {
48   char type[DATA_MAX_NAME_LEN];
49   char type_instance[DATA_MAX_NAME_LEN];
50 };
51 typedef struct value_map_s value_map_t;
52
53 static char **interfaces = NULL;
54 static size_t interfaces_num = 0;
55
56 static c_avl_tree_t *value_map = NULL;
57
58 static _Bool collect_mapped_only = 0;
59
60 static int ethstat_add_interface (const oconfig_item_t *ci) /* {{{ */
61 {
62   char **tmp;
63   int status;
64
65   tmp = realloc (interfaces,
66       sizeof (*interfaces) * (interfaces_num + 1));
67   if (tmp == NULL)
68     return (-1);
69   interfaces = tmp;
70   interfaces[interfaces_num] = NULL;
71
72   status = cf_util_get_string (ci, interfaces + interfaces_num);
73   if (status != 0)
74     return (status);
75
76   interfaces_num++;
77   INFO("ethstat plugin: Registered interface %s",
78       interfaces[interfaces_num - 1]);
79
80   return (0);
81 } /* }}} int ethstat_add_interface */
82
83 static int ethstat_add_map (const oconfig_item_t *ci) /* {{{ */
84 {
85   value_map_t *map;
86   int status;
87   char *key;
88
89   if ((ci->values_num < 2)
90       || (ci->values_num > 3)
91       || (ci->values[0].type != OCONFIG_TYPE_STRING)
92       || (ci->values[1].type != OCONFIG_TYPE_STRING)
93       || ((ci->values_num == 3)
94         && (ci->values[2].type != OCONFIG_TYPE_STRING)))
95   {
96     ERROR ("ethstat plugin: The %s option requires "
97         "two or three string arguments.", ci->key);
98     return (-1);
99   }
100
101   key = strdup (ci->values[0].value.string);
102   if (key == NULL)
103   {
104     ERROR ("ethstat plugin: strdup(3) failed.");
105     return (ENOMEM);
106   }
107
108   map = calloc (1, sizeof (*map));
109   if (map == NULL)
110   {
111     sfree (key);
112     ERROR ("ethstat plugin: calloc failed.");
113     return (ENOMEM);
114   }
115
116   sstrncpy (map->type, ci->values[1].value.string, sizeof (map->type));
117   if (ci->values_num == 3)
118     sstrncpy (map->type_instance, ci->values[2].value.string,
119         sizeof (map->type_instance));
120
121   if (value_map == NULL)
122   {
123     value_map = c_avl_create ((int (*) (const void *, const void *)) strcmp);
124     if (value_map == NULL)
125     {
126       sfree (map);
127       sfree (key);
128       ERROR ("ethstat plugin: c_avl_create() failed.");
129       return (-1);
130     }
131   }
132
133   status = c_avl_insert (value_map,
134       /* key = */ key,
135       /* value = */ map);
136   if (status != 0)
137   {
138     if (status > 0)
139       ERROR ("ethstat plugin: Multiple mappings for \"%s\".", key);
140     else
141       ERROR ("ethstat plugin: c_avl_insert(\"%s\") failed.", key);
142
143     sfree (map);
144     sfree (key);
145     return (-1);
146   }
147
148   return (0);
149 } /* }}} int ethstat_add_map */
150
151 static int ethstat_config (oconfig_item_t *ci) /* {{{ */
152 {
153   for (int i = 0; i < ci->children_num; i++)
154   {
155     oconfig_item_t *child = ci->children + i;
156
157     if (strcasecmp ("Interface", child->key) == 0)
158       ethstat_add_interface (child);
159     else if (strcasecmp ("Map", child->key) == 0)
160       ethstat_add_map (child);
161     else if (strcasecmp ("MappedOnly", child->key) == 0)
162       (void) cf_util_get_boolean (child, &collect_mapped_only);
163     else
164       WARNING ("ethstat plugin: The config option \"%s\" is unknown.",
165           child->key);
166   }
167
168   return (0);
169 } /* }}} */
170
171 static void ethstat_submit_value (const char *device,
172     const char *type_instance, derive_t value)
173 {
174   static c_complain_t complain_no_map = C_COMPLAIN_INIT_STATIC;
175
176   value_t values[1];
177   value_list_t vl = VALUE_LIST_INIT;
178   value_map_t *map = NULL;
179
180   if (value_map != NULL)
181     c_avl_get (value_map, type_instance, (void *) &map);
182
183   /* If the "MappedOnly" option is specified, ignore unmapped values. */
184   if (collect_mapped_only && (map == NULL))
185   {
186     if (value_map == NULL)
187       c_complain (LOG_WARNING, &complain_no_map,
188           "ethstat plugin: The \"MappedOnly\" option has been set to true, "
189           "but no mapping has been configured. All values will be ignored!");
190     return;
191   }
192
193   values[0].derive = value;
194   vl.values = values;
195   vl.values_len = 1;
196
197   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
198   sstrncpy (vl.plugin, "ethstat", sizeof (vl.plugin));
199   sstrncpy (vl.plugin_instance, device, sizeof (vl.plugin_instance));
200   if (map != NULL)
201   {
202     sstrncpy (vl.type, map->type, sizeof (vl.type));
203     sstrncpy (vl.type_instance, map->type_instance,
204         sizeof (vl.type_instance));
205   }
206   else
207   {
208     sstrncpy (vl.type, "derive", sizeof (vl.type));
209     sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
210   }
211
212   plugin_dispatch_values (&vl);
213 }
214
215 static int ethstat_read_interface (char *device)
216 {
217   int fd;
218   struct ethtool_gstrings *strings;
219   struct ethtool_stats *stats;
220   size_t n_stats;
221   size_t strings_size;
222   size_t stats_size;
223   int status;
224
225   fd = socket(AF_INET, SOCK_DGRAM, /* protocol = */ 0);
226   if (fd < 0)
227   {
228     char errbuf[1024];
229     ERROR("ethstat plugin: Failed to open control socket: %s",
230         sstrerror (errno, errbuf, sizeof (errbuf)));
231     return 1;
232   }
233
234   struct ethtool_drvinfo drvinfo = {
235     .cmd = ETHTOOL_GDRVINFO
236   };
237
238   struct ifreq req = {
239     .ifr_data = (void *) &drvinfo
240   };
241
242   sstrncpy(req.ifr_name, device, sizeof (req.ifr_name));
243
244   status = ioctl (fd, SIOCETHTOOL, &req);
245   if (status < 0)
246   {
247     char errbuf[1024];
248     close (fd);
249     ERROR ("ethstat plugin: Failed to get driver information "
250         "from %s: %s", device,
251         sstrerror (errno, errbuf, sizeof (errbuf)));
252     return (-1);
253   }
254
255   n_stats = (size_t) drvinfo.n_stats;
256   if (n_stats < 1)
257   {
258     close (fd);
259     ERROR("ethstat plugin: No stats available for %s", device);
260     return (-1);
261   }
262
263   strings_size = sizeof (struct ethtool_gstrings)
264     + (n_stats * ETH_GSTRING_LEN);
265   stats_size = sizeof (struct ethtool_stats)
266     + (n_stats * sizeof (uint64_t));
267
268   strings = malloc (strings_size);
269   stats = malloc (stats_size);
270   if ((strings == NULL) || (stats == NULL))
271   {
272     close (fd);
273     sfree (strings);
274     sfree (stats);
275     ERROR("ethstat plugin: malloc failed.");
276     return (-1);
277   }
278
279   strings->cmd = ETHTOOL_GSTRINGS;
280   strings->string_set = ETH_SS_STATS;
281   strings->len = n_stats;
282   req.ifr_data = (void *) strings;
283   status = ioctl (fd, SIOCETHTOOL, &req);
284   if (status < 0)
285   {
286     char errbuf[1024];
287     close (fd);
288     free (strings);
289     free (stats);
290     ERROR ("ethstat plugin: Cannot get strings from %s: %s",
291         device,
292         sstrerror (errno, errbuf, sizeof (errbuf)));
293     return (-1);
294   }
295
296   stats->cmd = ETHTOOL_GSTATS;
297   stats->n_stats = n_stats;
298   req.ifr_data = (void *) stats;
299   status = ioctl (fd, SIOCETHTOOL, &req);
300   if (status < 0)
301   {
302     char errbuf[1024];
303     close (fd);
304     free(strings);
305     free(stats);
306     ERROR("ethstat plugin: Reading statistics from %s failed: %s",
307         device,
308         sstrerror (errno, errbuf, sizeof (errbuf)));
309     return (-1);
310   }
311
312   for (size_t i = 0; i < n_stats; i++)
313   {
314     char *stat_name;
315
316     stat_name = (void *) &strings->data[i * ETH_GSTRING_LEN];
317     /* Remove leading spaces in key name */
318     while (isspace ((int) *stat_name))
319         stat_name++;
320
321     DEBUG("ethstat plugin: device = \"%s\": %s = %"PRIu64,
322         device, stat_name, (uint64_t) stats->data[i]);
323     ethstat_submit_value (device,
324         stat_name, (derive_t) stats->data[i]);
325   }
326
327   close (fd);
328   sfree (strings);
329   sfree (stats);
330
331   return (0);
332 } /* }}} ethstat_read_interface */
333
334 static int ethstat_read(void)
335 {
336   for (size_t i = 0; i < interfaces_num; i++)
337     ethstat_read_interface (interfaces[i]);
338
339   return 0;
340 }
341
342 static int ethstat_shutdown (void)
343 {
344   void *key = NULL;
345   void *value = NULL;
346
347   if (value_map == NULL)
348     return (0);
349
350   while (c_avl_pick (value_map, &key, &value) == 0)
351   {
352     sfree (key);
353     sfree (value);
354   }
355
356   c_avl_destroy (value_map);
357   value_map = NULL;
358
359   return (0);
360 }
361
362 void module_register (void)
363 {
364   plugin_register_complex_config ("ethstat", ethstat_config);
365   plugin_register_read ("ethstat", ethstat_read);
366   plugin_register_shutdown ("ethstat", ethstat_shutdown);
367 }
368
369 /* vim: set sw=2 sts=2 et fdm=marker : */