fix: eliminate MIB dependency for ifname processor (#10214)

This commit is contained in:
Thomas Casteleyn 2021-12-07 23:38:09 +01:00 committed by GitHub
parent 2d420fbd35
commit 3ddc3c6b51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 14 deletions

View File

@ -1,6 +1,7 @@
package ifname
import (
"errors"
"fmt"
"strconv"
"sync"
@ -192,13 +193,14 @@ func (d *IfName) invalidate(agent string) {
func (d *IfName) Start(acc telegraf.Accumulator) error {
var err error
d.ifTable, err = d.makeTable("IF-MIB::ifDescr")
d.ifTable, err = d.makeTable("1.3.6.1.2.1.2.2.1.2")
if err != nil {
return fmt.Errorf("looking up ifDescr in local MIB: %w", err)
return fmt.Errorf("preparing ifTable: %v", err)
}
d.ifXTable, err = d.makeTable("IF-MIB::ifName")
d.ifXTable, err = d.makeTable("1.3.6.1.2.1.31.1.1.1.1")
if err != nil {
return fmt.Errorf("looking up ifName in local MIB: %w", err)
return fmt.Errorf("preparing ifXTable: %v", err)
}
fn := func(m telegraf.Metric) []telegraf.Metric {
@ -307,11 +309,11 @@ func (d *IfName) getMapRemoteNoMock(agent string) (nameMap, error) {
//try ifXtable and ifName first. if that fails, fall back to
//ifTable and ifDescr
var m nameMap
if m, err = buildMap(gs, d.ifXTable, "ifName"); err == nil {
if m, err = buildMap(gs, d.ifXTable); err == nil {
return m, nil
}
if m, err = buildMap(gs, d.ifTable, "ifDescr"); err == nil {
if m, err = buildMap(gs, d.ifTable); err == nil {
return m, nil
}
@ -338,13 +340,13 @@ func init() {
})
}
func makeTableNoMock(fieldName string) (*si.Table, error) {
func makeTableNoMock(oid string) (*si.Table, error) {
var err error
tab := si.Table{
Name: "ifTable",
IndexAsTag: true,
Fields: []si.Field{
{Oid: fieldName},
{Oid: oid, Name: "ifName"},
},
}
@ -357,7 +359,7 @@ func makeTableNoMock(fieldName string) (*si.Table, error) {
return &tab, nil
}
func buildMap(gs snmp.GosnmpWrapper, tab *si.Table, column string) (nameMap, error) {
func buildMap(gs snmp.GosnmpWrapper, tab *si.Table) (nameMap, error) {
var err error
rtab, err := tab.Build(gs, true)
@ -382,13 +384,13 @@ func buildMap(gs snmp.GosnmpWrapper, tab *si.Table, column string) (nameMap, err
if err != nil {
return nil, fmt.Errorf("index tag isn't a uint")
}
nameIf, ok := v.Fields[column]
nameIf, ok := v.Fields["ifName"]
if !ok {
return nil, fmt.Errorf("field %s is missing", column)
return nil, errors.New("ifName field is missing")
}
name, ok := nameIf.(string)
if !ok {
return nil, fmt.Errorf("field %s isn't a string", column)
return nil, errors.New("ifName field isn't a string")
}
t[i] = name

View File

@ -20,7 +20,7 @@ func TestTable(t *testing.T) {
d := IfName{}
err := d.Init()
require.NoError(t, err)
tab, err := d.makeTable("IF-MIB::ifTable")
tab, err := d.makeTable("1.3.6.1.2.1.2.2.1.2")
require.NoError(t, err)
clientConfig := snmp.ClientConfig{
@ -36,7 +36,7 @@ func TestTable(t *testing.T) {
require.NoError(t, err)
// Could use ifIndex but oid index is always the same
m, err := buildMap(gs, tab, "ifDescr")
m, err := buildMap(gs, tab)
require.NoError(t, err)
require.NotEmpty(t, m)
}