Code

Merge remote-tracking branch 'github/pr/387'
[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  **/
25 #include "collectd.h"
26 #include "common.h"
27 #include "plugin.h"
28 #include "configfile.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 = malloc (sizeof (*map));
108   if (map == NULL)
109   {
110     sfree (key);
111     ERROR ("ethstat plugin: malloc(3) failed.");
112     return (ENOMEM);
113   }
114   memset (map, 0, sizeof (*map));
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));
121   if (value_map == NULL)
122   {
123     value_map = c_avl_create ((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   }
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);
143     sfree (map);
144     sfree (key);
145     return (-1);
146   }
148   return (0);
149 } /* }}} int ethstat_add_map */
151 static int ethstat_config (oconfig_item_t *ci) /* {{{ */
153   int i;
155   for (i = 0; i < ci->children_num; i++)
156   {
157     oconfig_item_t *child = ci->children + i;
159     if (strcasecmp ("Interface", child->key) == 0)
160       ethstat_add_interface (child);
161     else if (strcasecmp ("Map", child->key) == 0)
162       ethstat_add_map (child);
163     else if (strcasecmp ("MappedOnly", child->key) == 0)
164       (void) cf_util_get_boolean (child, &collect_mapped_only);
165     else
166       WARNING ("ethstat plugin: The config option \"%s\" is unknown.",
167           child->key);
168   }
170   return (0);
171 } /* }}} */
173 static void ethstat_submit_value (const char *device,
174     const char *type_instance, derive_t value)
176   static c_complain_t complain_no_map = C_COMPLAIN_INIT_STATIC;
178   value_t values[1];
179   value_list_t vl = VALUE_LIST_INIT;
180   value_map_t *map = NULL;
182   if (value_map != NULL)
183     c_avl_get (value_map, type_instance, (void *) &map);
185   /* If the "MappedOnly" option is specified, ignore unmapped values. */
186   if (collect_mapped_only && (map == NULL))
187   {
188     if (value_map == NULL)
189       c_complain (LOG_WARNING, &complain_no_map,
190           "ethstat plugin: The \"MappedOnly\" option has been set to true, "
191           "but no mapping has been configured. All values will be ignored!");
192     return;
193   }
195   values[0].derive = value;
196   vl.values = values;
197   vl.values_len = 1;
199   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
200   sstrncpy (vl.plugin, "ethstat", sizeof (vl.plugin));
201   sstrncpy (vl.plugin_instance, device, sizeof (vl.plugin_instance));
202   if (map != NULL)
203   {
204     sstrncpy (vl.type, map->type, sizeof (vl.type));
205     sstrncpy (vl.type_instance, map->type_instance,
206         sizeof (vl.type_instance));
207   }
208   else
209   {
210     sstrncpy (vl.type, "derive", sizeof (vl.type));
211     sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
212   }
214   plugin_dispatch_values (&vl);
217 static int ethstat_read_interface (char *device)
219   int fd;
220   struct ifreq req;
221   struct ethtool_drvinfo drvinfo;
222   struct ethtool_gstrings *strings;
223   struct ethtool_stats *stats;
224   size_t n_stats;
225   size_t strings_size;
226   size_t stats_size;
227   size_t i;
228   int status;
230   memset (&req, 0, sizeof (req));
231   sstrncpy(req.ifr_name, device, sizeof (req.ifr_name));
233   fd = socket(AF_INET, SOCK_DGRAM, /* protocol = */ 0);
234   if (fd < 0)
235   {
236     char errbuf[1024];
237     ERROR("ethstat plugin: Failed to open control socket: %s",
238         sstrerror (errno, errbuf, sizeof (errbuf)));
239     return 1;
240   }
242   memset (&drvinfo, 0, sizeof (drvinfo));
243   drvinfo.cmd = ETHTOOL_GDRVINFO;
244   req.ifr_data = (void *) &drvinfo;
245   status = ioctl (fd, SIOCETHTOOL, &req);
246   if (status < 0)
247   {
248     char errbuf[1024];
249     close (fd);
250     ERROR ("ethstat plugin: Failed to get driver information "
251         "from %s: %s", device,
252         sstrerror (errno, errbuf, sizeof (errbuf)));
253     return (-1);
254   }
256   n_stats = (size_t) drvinfo.n_stats;
257   if (n_stats < 1)
258   {
259     close (fd);
260     ERROR("ethstat plugin: No stats available for %s", device);
261     return (-1);
262   }
264   strings_size = sizeof (struct ethtool_gstrings)
265     + (n_stats * ETH_GSTRING_LEN);
266   stats_size = sizeof (struct ethtool_stats)
267     + (n_stats * sizeof (uint64_t));
269   strings = malloc (strings_size);
270   stats = malloc (stats_size);
271   if ((strings == NULL) || (stats == NULL))
272   {
273     close (fd);
274     sfree (strings);
275     sfree (stats);
276     ERROR("ethstat plugin: malloc(3) failed.");
277     return (-1);
278   }
280   strings->cmd = ETHTOOL_GSTRINGS;
281   strings->string_set = ETH_SS_STATS;
282   strings->len = n_stats;
283   req.ifr_data = (void *) strings;
284   status = ioctl (fd, SIOCETHTOOL, &req);
285   if (status < 0)
286   {
287     char errbuf[1024];
288     close (fd);
289     free (strings);
290     free (stats);
291     ERROR ("ethstat plugin: Cannot get strings from %s: %s",
292         device,
293         sstrerror (errno, errbuf, sizeof (errbuf)));
294     return (-1);
295   }
297   stats->cmd = ETHTOOL_GSTATS;
298   stats->n_stats = n_stats;
299   req.ifr_data = (void *) stats;
300   status = ioctl (fd, SIOCETHTOOL, &req);
301   if (status < 0)
302   {
303     char errbuf[1024];
304     close (fd);
305     free(strings);
306     free(stats);
307     ERROR("ethstat plugin: Reading statistics from %s failed: %s",
308         device,
309         sstrerror (errno, errbuf, sizeof (errbuf)));
310     return (-1);
311   }
313   for (i = 0; i < n_stats; i++)
314   {
315     const char *stat_name;
317     stat_name = (void *) &strings->data[i * ETH_GSTRING_LEN];
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)
333   size_t i;
335   for (i = 0; i < interfaces_num; i++)
336     ethstat_read_interface (interfaces[i]);
338   return 0;
341 static int ethstat_shutdown (void)
343   void *key = NULL;
344   void *value = NULL;
346   if (value_map == NULL)
347     return (0);
349   while (c_avl_pick (value_map, &key, &value) == 0)
350   {
351     sfree (key);
352     sfree (value);
353   }
355   c_avl_destroy (value_map);
356   value_map = NULL;
358   return (0);
361 void module_register (void)
363   plugin_register_complex_config ("ethstat", ethstat_config);
364   plugin_register_read ("ethstat", ethstat_read);
365   plugin_register_shutdown ("ethstat", ethstat_shutdown);
368 /* vim: set sw=2 sts=2 et fdm=marker : */