From aea4ef836f1609aad5b51fb58ba1858626ef046f Mon Sep 17 00:00:00 2001 From: Andrew Bays Date: Mon, 5 Feb 2018 11:52:54 -0500 Subject: [PATCH] monitor all interfaces by default --- src/collectd.conf.pod | 9 ++++-- src/connectivity.c | 88 ++++++++++++++++++++++++++++++++++----------------- 2 files changed, 66 insertions(+), 31 deletions(-) diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod index a651a049..8ce77e28 100644 --- a/src/collectd.conf.pod +++ b/src/collectd.conf.pod @@ -1586,9 +1586,14 @@ connectivity - Documentation of collectd's C The C queries interface status using netlink (man 7 netlink) which provides information about network interfaces via the NETLINK_ROUTE family (man 7 rtnetlink). The plugin translates the value it receives to collectd's internal format and, depending on the write plugins you have loaded, it may be written to disk or submitted to another instance. -The plugin listens to interfaces configured in LoadPlugin (see configuration below). +The plugin listens to interfaces configured in LoadPlugin (see configuration below). If no interfaces are listed, then the default is for all interfaces to be monitored. -Here this example shows C monitoring 2 interfaces "eth0" and "eth1" +This example shows C monitoring all interfaces. +LoadPlugin connectivity + + + +This example shows C monitoring 2 interfaces, "eth0" and "eth1". LoadPlugin connectivity Interface eth0 diff --git a/src/connectivity.c b/src/connectivity.c index 8cacf3be..a91b57df 100644 --- a/src/connectivity.c +++ b/src/connectivity.c @@ -109,6 +109,7 @@ typedef struct interface_list_s interface_list_t; * Private variables */ static interface_list_t *interface_list_head = NULL; +static int monitor_all_interfaces = 0; static int connectivity_thread_loop = 0; static int connectivity_thread_error = 0; @@ -373,6 +374,41 @@ err: return -1; } +static interface_list_t *add_interface(const char *interface, int status, + int prev_status) { + interface_list_t *il; + char *interface2; + + il = malloc(sizeof(*il)); + if (il == NULL) { + char errbuf[1024]; + ERROR("connectivity plugin: malloc failed during add_interface: %s", + sstrerror(errno, errbuf, sizeof(errbuf))); + return NULL; + } + + interface2 = strdup(interface); + if (interface2 == NULL) { + char errbuf[1024]; + sfree(il); + ERROR("connectivity plugin: strdup failed during add_interface: %s", + sstrerror(errno, errbuf, sizeof(errbuf))); + return NULL; + } + + il->interface = interface2; + il->status = status; + il->prev_status = prev_status; + il->timestamp = (long long unsigned int)CDTIME_T_TO_US(cdtime()); + il->sent = 0; + il->next = interface_list_head; + interface_list_head = il; + + DEBUG("connectivity plugin: added interface %s", interface2); + + return il; +} + static int connectivity_link_state(struct nlmsghdr *msg) { int retval = 0; struct ifinfomsg *ifi = mnl_nlmsg_get_payload(msg); @@ -402,13 +438,26 @@ static int connectivity_link_state(struct nlmsghdr *msg) { if (strcmp(dev, il->interface) == 0) break; - if (il == NULL) { + if (il == NULL && monitor_all_interfaces == 0) { DEBUG("connectivity plugin: Ignoring link state change for unmonitored " "interface: %s", dev); } else { uint32_t prev_status; + if (il == NULL) { + // We're monitoring all interfaces and we haven't encountered this one + // yet, so add it to the linked list + il = add_interface(dev, LINK_STATE_UNKNOWN, LINK_STATE_UNKNOWN); + + if (il == NULL) { + ERROR("connectivity plugin: unable to add interface %s during " + "connectivity_link_state", + dev); + return MNL_CB_ERROR; + } + } + prev_status = il->status; il->status = ((ifi->ifi_flags & IFF_RUNNING) ? LINK_STATE_UP : LINK_STATE_DOWN); @@ -646,8 +695,9 @@ static int stop_thread(int shutdown) /* {{{ */ static int connectivity_init(void) /* {{{ */ { if (interface_list_head == NULL) { - NOTICE("connectivity plugin: No interfaces have been configured."); - return (-1); + NOTICE("connectivity plugin: No interfaces have been selected, so all will " + "be monitored"); + monitor_all_interfaces = 1; } return (start_thread()); @@ -656,34 +706,14 @@ static int connectivity_init(void) /* {{{ */ static int connectivity_config(const char *key, const char *value) /* {{{ */ { if (strcasecmp(key, "Interface") == 0) { - interface_list_t *il; - char *interface; - - il = malloc(sizeof(*il)); + interface_list_t *il = + add_interface(value, LINK_STATE_UNKNOWN, LINK_STATE_UNKNOWN); if (il == NULL) { - char errbuf[1024]; - ERROR("connectivity plugin: malloc failed during connectivity_config: %s", - sstrerror(errno, errbuf, sizeof(errbuf))); - return (1); - } - - interface = strdup(value); - if (interface == NULL) { - char errbuf[1024]; - sfree(il); - ERROR("connectivity plugin: strdup failed connectivity_config: %s", - sstrerror(errno, errbuf, sizeof(errbuf))); - return (1); + ERROR("connectivity plugin: unable to add interface %s during " + "connectivity_init", + value); + return (-1); } - - il->interface = interface; - il->status = LINK_STATE_UNKNOWN; - il->prev_status = LINK_STATE_UNKNOWN; - il->timestamp = (long long unsigned int)CDTIME_T_TO_US(cdtime()); - il->sent = 0; - il->next = interface_list_head; - interface_list_head = il; - } else { return (-1); } -- 2.11.0