Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: eliminate MIB dependency for ifname processor #10214

Merged
merged 2 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions plugins/processors/ifname/ifname.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ifname

import (
"errors"
"fmt"
"strconv"
"sync"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand All @@ -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"},
},
}

Expand All @@ -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)
Expand All @@ -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")
Hipska marked this conversation as resolved.
Show resolved Hide resolved
}
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
Expand Down
4 changes: 2 additions & 2 deletions plugins/processors/ifname/ifname_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
}
Expand Down