X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Finterface.c;h=849de955ed1be50916a880c694f6eb6a5a21dbe1;hb=50ad45a6a6c68119823d56c1ccec74135ba744f9;hp=a91c13bec9b46f25b94afb10c8ca623155529103;hpb=fc8f6e1bfd3b121c72ac30d1aa855d90f724b00b;p=collectd.git diff --git a/src/interface.c b/src/interface.c index a91c13be..849de955 100644 --- a/src/interface.c +++ b/src/interface.c @@ -23,9 +23,9 @@ **/ #include "collectd.h" + #include "common.h" #include "plugin.h" -#include "configfile.h" #include "utils_ignorelist.h" #if HAVE_SYS_TYPES_H @@ -84,14 +84,13 @@ static const char *config_keys[] = { "Interface", "IgnoreSelected", - "ActiveInterfaceOnly", - NULL + "ReportInactive", }; -static int config_keys_num = 3; +static int config_keys_num = STATIC_ARRAY_SIZE (config_keys); static ignorelist_t *ignorelist = NULL; -static _Bool active_interface_only = 0; +static _Bool report_inactive = 1; #ifdef HAVE_LIBKSTAT #define MAX_NUMIF 256 @@ -117,13 +116,8 @@ static int interface_config (const char *key, const char *value) invert = 0; ignorelist_set_invert (ignorelist, invert); } - else if (strcasecmp (key, "ActiveInterfaceOnly") == 0) - { - if (IS_TRUE (value)) - active_interface_only = 1; - else - active_interface_only = 0; - } + else if (strcasecmp (key, "ReportInactive") == 0) + report_inactive = IS_TRUE (value); else if (strcasecmp (key, "UniqueName") == 0) { #ifdef HAVE_LIBKSTAT @@ -145,7 +139,6 @@ static int interface_config (const char *key, const char *value) static int interface_init (void) { kstat_t *ksp_chain; - derive_t val; numif = 0; @@ -162,7 +155,7 @@ static int interface_init (void) continue; if (kstat_read (kc, ksp_chain, NULL) == -1) continue; - if ((val = get_kstat_value (ksp_chain, "obytes")) == -1LL) + if (get_kstat_value (ksp_chain, "obytes") == -1LL) continue; ksp[numif++] = ksp_chain; } @@ -175,17 +168,17 @@ static void if_submit (const char *dev, const char *type, derive_t rx, derive_t tx) { - value_t values[2]; value_list_t vl = VALUE_LIST_INIT; + value_t values[] = { + { .derive = rx }, + { .derive = tx }, + }; if (ignorelist_match (ignorelist, dev) != 0) return; - values[0].derive = rx; - values[1].derive = tx; - vl.values = values; - vl.values_len = 2; + vl.values_len = STATIC_ARRAY_SIZE (values); sstrncpy (vl.host, hostname_g, sizeof (vl.host)); sstrncpy (vl.plugin, "interface", sizeof (vl.plugin)); sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance)); @@ -198,7 +191,6 @@ static int interface_read (void) { #if HAVE_GETIFADDRS struct ifaddrs *if_list; - struct ifaddrs *if_ptr; /* Darwin/Mac OS X and possible other *BSDs */ #if HAVE_STRUCT_IF_DATA @@ -228,12 +220,12 @@ static int interface_read (void) if (getifaddrs (&if_list) != 0) return (-1); - for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next) + for (struct ifaddrs *if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next) { if (if_ptr->ifa_addr != NULL && if_ptr->ifa_addr->sa_family == AF_LINK) { if_data = (struct IFA_DATA *) if_ptr->ifa_data; - if ( active_interface_only && if_data->IFA_RX_PACKT == 0 && if_data->IFA_TX_PACKT == 0 ) + if (!report_inactive && if_data->IFA_RX_PACKT == 0 && if_data->IFA_TX_PACKT == 0) continue; if_submit (if_ptr->ifa_name, "if_octets", @@ -290,7 +282,7 @@ static int interface_read (void) incoming = atoll (fields[1]); outgoing = atoll (fields[9]); - if ( active_interface_only && incoming == 0 && outgoing == 0 ) + if (!report_inactive && incoming == 0 && outgoing == 0) continue; if_submit (device, "if_packets", incoming, outgoing); @@ -312,7 +304,6 @@ static int interface_read (void) /* #endif KERNEL_LINUX */ #elif HAVE_LIBKSTAT - int i; derive_t rx; derive_t tx; char iname[DATA_MAX_NAME_LEN]; @@ -320,7 +311,7 @@ static int interface_read (void) if (kc == NULL) return (-1); - for (i = 0; i < numif; i++) + for (int i = 0; i < numif; i++) { if (kstat_read (kc, ksp[i], NULL) == -1) continue; @@ -338,7 +329,7 @@ static int interface_read (void) rx = get_kstat_value (ksp[i], "ipackets"); if (tx == -1LL) tx = get_kstat_value (ksp[i], "opackets"); - if ( active_interface_only && rx == 0 && tx == 0 ) + if (!report_inactive && rx == 0 && tx == 0) continue; if ((rx != -1LL) || (tx != -1LL)) if_submit (iname, "if_packets", rx, tx); @@ -364,12 +355,12 @@ static int interface_read (void) #elif defined(HAVE_LIBSTATGRAB) sg_network_io_stats *ios; - int i, num; + int num; ios = sg_get_network_io_stats (&num); - for (i = 0; i < num; i++) { - if ( active_interface_only && ios[i].rx == 0 && ios[i].tx == 0 ) + for (int i = 0; i < num; i++) { + if (!report_inactive && ios[i].rx == 0 && ios[i].tx == 0) continue; if_submit (ios[i].interface_name, "if_octets", ios[i].rx, ios[i].tx); } @@ -377,7 +368,7 @@ static int interface_read (void) #elif defined(HAVE_PERFSTAT) perfstat_id_t id; - int i, ifs; + int ifs; if ((nif = perfstat_netinterface(NULL, NULL, sizeof(perfstat_netinterface_t), 0)) < 0) { @@ -403,9 +394,9 @@ static int interface_read (void) return (-1); } - for (i = 0; i < ifs; i++) + for (int i = 0; i < ifs; i++) { - if ( active_interface_only && ifstat[i].ipackets == 0 && ifstat[i].opackets == 0 ) + if (!report_inactive && ifstat[i].ipackets == 0 && ifstat[i].opackets == 0) continue; if_submit (ifstat[i].name, "if_octets", ifstat[i].ibytes, ifstat[i].obytes); @@ -427,12 +418,3 @@ void module_register (void) plugin_register_read ("interface", interface_read); } /* void module_register */ -/* - * Local variables: - * c-file-style: "linux" - * indent-tabs-mode: t - * c-indent-level: 4 - * c-basic-offset: 4 - * tab-width: 4 - * End: - */