X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fethstat.c;h=08381a821087e275fb03922a32bc54c35ca7c3cd;hb=77311aed583ac4566d76f1ce3a1dc9deae2e59dc;hp=490a9bf69ac54610c30a55feb532ed7b8170702f;hpb=28f252c4c5c879991c4c59ac5a4bb80c5d209eb5;p=collectd.git diff --git a/src/ethstat.c b/src/ethstat.c index 490a9bf6..08381a82 100644 --- a/src/ethstat.c +++ b/src/ethstat.c @@ -1,6 +1,7 @@ /** * collectd - src/ethstat.c * Copyright (C) 2011 Cyril Feraudet + * Copyright (C) 2012 Florian "octo" Forster * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the @@ -18,6 +19,7 @@ * * Authors: * Cyril Feraudet + * Florian "octo" Forster **/ #include "collectd.h" @@ -25,6 +27,7 @@ #include "plugin.h" #include "configfile.h" #include "utils_avltree.h" +#include "utils_complain.h" #if HAVE_SYS_IOCTL_H # include @@ -51,6 +54,8 @@ static size_t interfaces_num = 0; static c_avl_tree_t *value_map = NULL; +static _Bool collect_mapped_only = 0; + static int ethstat_add_interface (const oconfig_item_t *ci) /* {{{ */ { char **tmp; @@ -61,6 +66,7 @@ static int ethstat_add_interface (const oconfig_item_t *ci) /* {{{ */ if (tmp == NULL) return (-1); interfaces = tmp; + interfaces[interfaces_num] = NULL; status = cf_util_get_string (ci, interfaces + interfaces_num); if (status != 0) @@ -99,7 +105,7 @@ static int ethstat_add_map (const oconfig_item_t *ci) /* {{{ */ memset (map, 0, sizeof (*map)); sstrncpy (map->type, ci->values[1].value.string, sizeof (map->type)); - if (ci->values_num == 2) + if (ci->values_num == 3) sstrncpy (map->type_instance, ci->values[2].value.string, sizeof (map->type_instance)); @@ -144,6 +150,8 @@ static int ethstat_config (oconfig_item_t *ci) /* {{{ */ ethstat_add_interface (child); else if (strcasecmp ("Map", child->key) == 0) ethstat_add_map (child); + else if (strcasecmp ("MappedOnly", child->key) == 0) + (void) cf_util_get_boolean (child, &collect_mapped_only); else WARNING ("ethstat plugin: The config option \"%s\" is unknown.", child->key); @@ -153,166 +161,177 @@ static int ethstat_config (oconfig_item_t *ci) /* {{{ */ } /* }}} */ static void ethstat_submit_value (const char *device, - const char *type_instance, derive_t value) + const char *type_instance, derive_t value) { - value_t values[1]; - value_list_t vl = VALUE_LIST_INIT; - value_map_t *map = NULL; - - if (value_map != NULL) - c_avl_get (value_map, type_instance, (void *) &map); - - values[0].derive = value; - vl.values = values; - vl.values_len = 1; - - sstrncpy (vl.host, hostname_g, sizeof (vl.host)); - sstrncpy (vl.plugin, "ethstat", sizeof (vl.plugin)); - sstrncpy (vl.plugin_instance, device, sizeof (vl.plugin_instance)); - if (map != NULL) - { - sstrncpy (vl.type, map->type, sizeof (vl.type)); - sstrncpy (vl.type_instance, map->type_instance, - sizeof (vl.type_instance)); - } - else - { - sstrncpy (vl.type, "derive", sizeof (vl.type)); - sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance)); - } - - plugin_dispatch_values (&vl); + static c_complain_t complain_no_map = C_COMPLAIN_INIT_STATIC; + + value_t values[1]; + value_list_t vl = VALUE_LIST_INIT; + value_map_t *map = NULL; + + if (value_map != NULL) + c_avl_get (value_map, type_instance, (void *) &map); + + /* If the "MappedOnly" option is specified, ignore unmapped values. */ + if (collect_mapped_only && (map == NULL)) + { + if (value_map == NULL) + c_complain (LOG_WARNING, &complain_no_map, + "ethstat plugin: The \"MappedOnly\" option has been set to true, " + "but no mapping has been configured. All values will be ignored!"); + return; + } + + values[0].derive = value; + vl.values = values; + vl.values_len = 1; + + sstrncpy (vl.host, hostname_g, sizeof (vl.host)); + sstrncpy (vl.plugin, "ethstat", sizeof (vl.plugin)); + sstrncpy (vl.plugin_instance, device, sizeof (vl.plugin_instance)); + if (map != NULL) + { + sstrncpy (vl.type, map->type, sizeof (vl.type)); + sstrncpy (vl.type_instance, map->type_instance, + sizeof (vl.type_instance)); + } + else + { + sstrncpy (vl.type, "derive", sizeof (vl.type)); + sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance)); + } + + plugin_dispatch_values (&vl); } static int ethstat_read_interface (char *device) { - int fd; - struct ifreq req; - struct ethtool_drvinfo drvinfo; - struct ethtool_gstrings *strings; - struct ethtool_stats *stats; - size_t n_stats; - size_t strings_size; - size_t stats_size; - size_t i; - int status; - - memset (&req, 0, sizeof (req)); - sstrncpy(req.ifr_name, device, sizeof (req.ifr_name)); - - fd = socket(AF_INET, SOCK_DGRAM, /* protocol = */ 0); - if (fd < 0) - { - char errbuf[1024]; - ERROR("ethstat plugin: Failed to open control socket: %s", - sstrerror (errno, errbuf, sizeof (errbuf))); - return 1; - } - - memset (&drvinfo, 0, sizeof (drvinfo)); - drvinfo.cmd = ETHTOOL_GDRVINFO; - req.ifr_data = (void *) &drvinfo; - status = ioctl (fd, SIOCETHTOOL, &req); - if (status < 0) - { - char errbuf[1024]; - close (fd); - ERROR ("ethstat plugin: Failed to get driver information " - "from %s: %s", device, - sstrerror (errno, errbuf, sizeof (errbuf))); - return (-1); - } - - n_stats = (size_t) drvinfo.n_stats; - if (n_stats < 1) - { - close (fd); - ERROR("ethstat plugin: No stats available for %s", device); - return (-1); - } - - strings_size = sizeof (struct ethtool_gstrings) - + (n_stats * ETH_GSTRING_LEN); - stats_size = sizeof (struct ethtool_stats) - + (n_stats * sizeof (uint64_t)); - - strings = malloc (strings_size); - stats = malloc (stats_size); - if ((strings == NULL) || (stats == NULL)) - { - close (fd); - sfree (strings); - sfree (stats); - ERROR("ethstat plugin: malloc(3) failed."); - return (-1); - } - - strings->cmd = ETHTOOL_GSTRINGS; - strings->string_set = ETH_SS_STATS; - strings->len = n_stats; - req.ifr_data = (void *) strings; - status = ioctl (fd, SIOCETHTOOL, &req); - if (status < 0) - { - char errbuf[1024]; - close (fd); - free (strings); - free (stats); - ERROR ("ethstat plugin: Cannot get strings from %s: %s", - device, - sstrerror (errno, errbuf, sizeof (errbuf))); - return (-1); - } - - stats->cmd = ETHTOOL_GSTATS; - stats->n_stats = n_stats; - req.ifr_data = (void *) stats; - status = ioctl (fd, SIOCETHTOOL, &req); - if (status < 0) - { - char errbuf[1024]; - close (fd); - free(strings); - free(stats); - ERROR("ethstat plugin: Reading statistics from %s failed: %s", - device, - sstrerror (errno, errbuf, sizeof (errbuf))); - return (-1); - } - - for (i = 0; i < n_stats; i++) - { - const char *stat_name; - - stat_name = (void *) &strings->data[i * ETH_GSTRING_LEN], - DEBUG("ethstat plugin: device = \"%s\": %s = %"PRIu64, - device, stat_name, - (uint64_t) stats->data[i]); - ethstat_submit_value (device, - stat_name, (derive_t) stats->data[i]); - } - - close (fd); - sfree (strings); - sfree (stats); - - return (0); + int fd; + struct ifreq req; + struct ethtool_drvinfo drvinfo; + struct ethtool_gstrings *strings; + struct ethtool_stats *stats; + size_t n_stats; + size_t strings_size; + size_t stats_size; + size_t i; + int status; + + memset (&req, 0, sizeof (req)); + sstrncpy(req.ifr_name, device, sizeof (req.ifr_name)); + + fd = socket(AF_INET, SOCK_DGRAM, /* protocol = */ 0); + if (fd < 0) + { + char errbuf[1024]; + ERROR("ethstat plugin: Failed to open control socket: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + return 1; + } + + memset (&drvinfo, 0, sizeof (drvinfo)); + drvinfo.cmd = ETHTOOL_GDRVINFO; + req.ifr_data = (void *) &drvinfo; + status = ioctl (fd, SIOCETHTOOL, &req); + if (status < 0) + { + char errbuf[1024]; + close (fd); + ERROR ("ethstat plugin: Failed to get driver information " + "from %s: %s", device, + sstrerror (errno, errbuf, sizeof (errbuf))); + return (-1); + } + + n_stats = (size_t) drvinfo.n_stats; + if (n_stats < 1) + { + close (fd); + ERROR("ethstat plugin: No stats available for %s", device); + return (-1); + } + + strings_size = sizeof (struct ethtool_gstrings) + + (n_stats * ETH_GSTRING_LEN); + stats_size = sizeof (struct ethtool_stats) + + (n_stats * sizeof (uint64_t)); + + strings = malloc (strings_size); + stats = malloc (stats_size); + if ((strings == NULL) || (stats == NULL)) + { + close (fd); + sfree (strings); + sfree (stats); + ERROR("ethstat plugin: malloc(3) failed."); + return (-1); + } + + strings->cmd = ETHTOOL_GSTRINGS; + strings->string_set = ETH_SS_STATS; + strings->len = n_stats; + req.ifr_data = (void *) strings; + status = ioctl (fd, SIOCETHTOOL, &req); + if (status < 0) + { + char errbuf[1024]; + close (fd); + free (strings); + free (stats); + ERROR ("ethstat plugin: Cannot get strings from %s: %s", + device, + sstrerror (errno, errbuf, sizeof (errbuf))); + return (-1); + } + + stats->cmd = ETHTOOL_GSTATS; + stats->n_stats = n_stats; + req.ifr_data = (void *) stats; + status = ioctl (fd, SIOCETHTOOL, &req); + if (status < 0) + { + char errbuf[1024]; + close (fd); + free(strings); + free(stats); + ERROR("ethstat plugin: Reading statistics from %s failed: %s", + device, + sstrerror (errno, errbuf, sizeof (errbuf))); + return (-1); + } + + for (i = 0; i < n_stats; i++) + { + const char *stat_name; + + stat_name = (void *) &strings->data[i * ETH_GSTRING_LEN]; + DEBUG("ethstat plugin: device = \"%s\": %s = %"PRIu64, + device, stat_name, (uint64_t) stats->data[i]); + ethstat_submit_value (device, + stat_name, (derive_t) stats->data[i]); + } + + close (fd); + sfree (strings); + sfree (stats); + + return (0); } /* }}} ethstat_read_interface */ static int ethstat_read(void) { - size_t i; + size_t i; - for (i = 0; i < interfaces_num; i++) - ethstat_read_interface (interfaces[i]); + for (i = 0; i < interfaces_num; i++) + ethstat_read_interface (interfaces[i]); - return 0; + return 0; } void module_register (void) { - plugin_register_complex_config ("ethstat", ethstat_config); - plugin_register_read ("ethstat", ethstat_read); + plugin_register_complex_config ("ethstat", ethstat_config); + plugin_register_read ("ethstat", ethstat_read); } /* vim: set sw=2 sts=2 et fdm=marker : */