Speed up interface stats in net input (#5757)

This commit is contained in:
Łukasz Oleś 2019-04-23 20:13:14 +02:00 committed by Daniel Nelson
parent 17a772d7ae
commit 01eecee8cf
1 changed files with 11 additions and 2 deletions

View File

@ -54,6 +54,15 @@ func (s *NetIOStats) Gather(acc telegraf.Accumulator) error {
}
}
interfaces, err := net.Interfaces()
if err != nil {
return fmt.Errorf("error getting list of interfaces: %s", err)
}
interfacesByName := map[string]net.Interface{}
for _, iface := range interfaces {
interfacesByName[iface.Name] = iface
}
for _, io := range netio {
if len(s.Interfaces) != 0 {
var found bool
@ -66,8 +75,8 @@ func (s *NetIOStats) Gather(acc telegraf.Accumulator) error {
continue
}
} else if !s.skipChecks {
iface, err := net.InterfaceByName(io.Name)
if err != nil {
iface, ok := interfacesByName[io.Name]
if !ok {
continue
}