X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fethstat.c;h=a213b60a684a0b5652328912957ab797dc2e8737;hb=43954e07f30d05b2da8319749400c9bf4d01c23c;hp=dec14f3d148962df3b1aaf41eb1f50f0a7a5f92c;hpb=d1483d2e2a9116c233aeaceba99fa58a7e6bdc87;p=collectd.git diff --git a/src/ethstat.c b/src/ethstat.c index dec14f3d..a213b60a 100644 --- a/src/ethstat.c +++ b/src/ethstat.c @@ -23,6 +23,7 @@ **/ #include "collectd.h" + #include "common.h" #include "plugin.h" #include "configfile.h" @@ -104,14 +105,13 @@ static int ethstat_add_map (const oconfig_item_t *ci) /* {{{ */ return (ENOMEM); } - map = malloc (sizeof (*map)); + map = calloc (1, sizeof (*map)); if (map == NULL) { sfree (key); - ERROR ("ethstat plugin: malloc(3) failed."); + ERROR ("ethstat plugin: calloc failed."); return (ENOMEM); } - memset (map, 0, sizeof (*map)); sstrncpy (map->type, ci->values[1].value.string, sizeof (map->type)); if (ci->values_num == 3) @@ -120,7 +120,7 @@ static int ethstat_add_map (const oconfig_item_t *ci) /* {{{ */ if (value_map == NULL) { - value_map = c_avl_create ((void *) strcmp); + value_map = c_avl_create ((int (*) (const void *, const void *)) strcmp); if (value_map == NULL) { sfree (map); @@ -150,9 +150,7 @@ static int ethstat_add_map (const oconfig_item_t *ci) /* {{{ */ static int ethstat_config (oconfig_item_t *ci) /* {{{ */ { - int i; - - for (i = 0; i < ci->children_num; i++) + for (int i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; @@ -217,19 +215,13 @@ static void ethstat_submit_value (const char *device, 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) { @@ -239,9 +231,16 @@ static int ethstat_read_interface (char *device) return 1; } - memset (&drvinfo, 0, sizeof (drvinfo)); - drvinfo.cmd = ETHTOOL_GDRVINFO; - req.ifr_data = (void *) &drvinfo; + struct ethtool_drvinfo drvinfo = { + .cmd = ETHTOOL_GDRVINFO + }; + + struct ifreq req = { + .ifr_data = (void *) &drvinfo + }; + + sstrncpy(req.ifr_name, device, sizeof (req.ifr_name)); + status = ioctl (fd, SIOCETHTOOL, &req); if (status < 0) { @@ -273,7 +272,7 @@ static int ethstat_read_interface (char *device) close (fd); sfree (strings); sfree (stats); - ERROR("ethstat plugin: malloc(3) failed."); + ERROR("ethstat plugin: malloc failed."); return (-1); } @@ -310,11 +309,15 @@ static int ethstat_read_interface (char *device) return (-1); } - for (i = 0; i < n_stats; i++) + for (size_t i = 0; i < n_stats; i++) { - const char *stat_name; + char *stat_name; stat_name = (void *) &strings->data[i * ETH_GSTRING_LEN]; + /* Remove leading spaces in key name */ + while (isspace ((int) *stat_name)) + stat_name++; + DEBUG("ethstat plugin: device = \"%s\": %s = %"PRIu64, device, stat_name, (uint64_t) stats->data[i]); ethstat_submit_value (device, @@ -330,9 +333,7 @@ static int ethstat_read_interface (char *device) static int ethstat_read(void) { - size_t i; - - for (i = 0; i < interfaces_num; i++) + for (size_t i = 0; i < interfaces_num; i++) ethstat_read_interface (interfaces[i]); return 0;