From dd8157ec6885020630306e3e421ec26b00676d4f Mon Sep 17 00:00:00 2001 From: Bob Shannon Date: Wed, 29 Nov 2017 15:16:34 -0500 Subject: [PATCH] Add support for glob patterns in net input plugin (#3140) --- plugins/inputs/system/NET_README.md | 6 ++++-- plugins/inputs/system/net.go | 17 +++++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/plugins/inputs/system/NET_README.md b/plugins/inputs/system/NET_README.md index de9e7d7a7..771cabb48 100644 --- a/plugins/inputs/system/NET_README.md +++ b/plugins/inputs/system/NET_README.md @@ -9,9 +9,11 @@ This plugin gathers metrics about network interface and protocol usage (Linux on [[inputs.net]] ## By default, telegraf gathers stats from any up interface (excluding loopback) ## Setting interfaces will tell it to gather these explicit interfaces, - ## regardless of status. + ## regardless of status. When specifying an interface, glob-style + ## patterns are also supported. + ## + # interfaces = ["eth*", "enp0s[0-1]", "lo"] ## - # interfaces = ["eth0"] ``` ### Measurements & Fields: diff --git a/plugins/inputs/system/net.go b/plugins/inputs/system/net.go index f47a2cc6c..cfb712dfb 100644 --- a/plugins/inputs/system/net.go +++ b/plugins/inputs/system/net.go @@ -6,11 +6,13 @@ import ( "strings" "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/filter" "github.com/influxdata/telegraf/plugins/inputs" ) type NetIOStats struct { - ps PS + filter filter.Filter + ps PS skipChecks bool Interfaces []string @@ -38,15 +40,18 @@ func (s *NetIOStats) Gather(acc telegraf.Accumulator) error { return fmt.Errorf("error getting net io info: %s", err) } + if s.filter == nil { + if s.filter, err = filter.Compile(s.Interfaces); err != nil { + return fmt.Errorf("error compiling filter: %s", err) + } + } + for _, io := range netio { if len(s.Interfaces) != 0 { var found bool - for _, name := range s.Interfaces { - if name == io.Name { - found = true - break - } + if s.filter.Match(io.Name) { + found = true } if !found {