Skip to content

Commit

Permalink
fix: added .9 in front of oids in testSnmpInit_noTranslate so they do…
Browse files Browse the repository at this point in the history
…n't match prewritten mibs
  • Loading branch information
MyaLongmire committed Oct 12, 2021
1 parent 48b0a3b commit 6108859
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 19 deletions.
8 changes: 4 additions & 4 deletions plugins/inputs/snmp/snmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ func snmpTranslateCall(oid string) (mibName string, oidNum string, oidText strin

out, err = gosmi.GetNode(node)
if err != nil {
return oid, oid, oid, "", err
return oid, oid, oid, oid, err
}

oidNum = "." + out.RenderNumeric() + end
Expand All @@ -917,7 +917,7 @@ func snmpTranslateCall(oid string) (mibName string, oidNum string, oidText strin
if strings.ContainsAny(s[i], "abcdefghijklmnopqrstuvwxyz") {
out, err = gosmi.GetNode(s[i])
if err != nil {
return oid, oid, oid, "", err
return oid, oid, oid, oid, err
}
s[i] = out.RenderNumeric()
}
Expand All @@ -929,7 +929,7 @@ func snmpTranslateCall(oid string) (mibName string, oidNum string, oidText strin
oidNum = oid
// ensure modules are loaded or node will be empty (might not error)
if err != nil {
return oid, oid, oid, "", err
return oid, oid, oid, oid, err
}
}

Expand All @@ -952,7 +952,7 @@ func snmpTranslateCall(oid string) (mibName string, oidNum string, oidText strin
oidText = out.RenderQualified()
i := strings.Index(oidText, "::")
if i == -1 {
return "", oid, oid, "", fmt.Errorf("not found")
return "", oid, oid, oid, fmt.Errorf("not found")
}
mibName = oidText[:i]
oidText = oidText[i+2:] + end
Expand Down
36 changes: 21 additions & 15 deletions plugins/inputs/snmp/snmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,47 +223,53 @@ func TestSnmpInit(t *testing.T) {
}

func TestSnmpInit_noTranslate(t *testing.T) {
testDataPath, err := filepath.Abs("./testdata")
require.NoError(t, err)

s := &Snmp{
Fields: []Field{
{Oid: ".1.1.1.1", Name: "one", IsTag: true},
{Oid: ".1.1.1.2", Name: "two"},
{Oid: ".1.1.1.3"},
{Oid: ".9.1.1.1.1", Name: "one", IsTag: true},
{Oid: ".9.1.1.1.2", Name: "two"},
{Oid: ".9.1.1.1.3"},
},
Tables: []Table{
{Name: "testing",
Fields: []Field{
{Oid: ".1.1.1.4", Name: "four", IsTag: true},
{Oid: ".1.1.1.5", Name: "five"},
{Oid: ".1.1.1.6"},
{Oid: ".9.1.1.1.4", Name: "four", IsTag: true},
{Oid: ".9.1.1.1.5", Name: "five"},
{Oid: ".9.1.1.1.6"},
}},
},
ClientConfig: snmp.ClientConfig{
Path: []string{testDataPath},
},
}

err := s.init()
err = s.init()
require.NoError(t, err)

assert.Equal(t, ".1.1.1.1", s.Fields[0].Oid)
assert.Equal(t, ".9.1.1.1.1", s.Fields[0].Oid)
assert.Equal(t, "one", s.Fields[0].Name)
assert.Equal(t, true, s.Fields[0].IsTag)

assert.Equal(t, ".1.1.1.2", s.Fields[1].Oid)
assert.Equal(t, ".9.1.1.1.2", s.Fields[1].Oid)
assert.Equal(t, "two", s.Fields[1].Name)
assert.Equal(t, false, s.Fields[1].IsTag)

assert.Equal(t, ".1.1.1.3", s.Fields[2].Oid)
assert.Equal(t, "iso", s.Fields[2].Name)
assert.Equal(t, ".9.1.1.1.3", s.Fields[2].Oid)
assert.Equal(t, ".9.1.1.1.3", s.Fields[2].Name)
assert.Equal(t, false, s.Fields[2].IsTag)

assert.Equal(t, ".1.1.1.4", s.Tables[0].Fields[0].Oid)
assert.Equal(t, ".9.1.1.1.4", s.Tables[0].Fields[0].Oid)
assert.Equal(t, "four", s.Tables[0].Fields[0].Name)
assert.Equal(t, true, s.Tables[0].Fields[0].IsTag)

assert.Equal(t, ".1.1.1.5", s.Tables[0].Fields[1].Oid)
assert.Equal(t, ".9.1.1.1.5", s.Tables[0].Fields[1].Oid)
assert.Equal(t, "five", s.Tables[0].Fields[1].Name)
assert.Equal(t, false, s.Tables[0].Fields[1].IsTag)

assert.Equal(t, ".1.1.1.6", s.Tables[0].Fields[2].Oid)
assert.Equal(t, "dod", s.Tables[0].Fields[2].Name)
assert.Equal(t, ".9.1.1.1.6", s.Tables[0].Fields[2].Oid)
assert.Equal(t, ".9.1.1.1.6", s.Tables[0].Fields[2].Name)
assert.Equal(t, false, s.Tables[0].Fields[2].IsTag)
}

Expand Down
3 changes: 3 additions & 0 deletions plugins/inputs/snmp/testdata/foo
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ fooTestMIB MODULE-IDENTITY
::= { iso 1 }

fooMIBObjects OBJECT IDENTIFIER ::= { iso 2 }
fooOne OBJECT IDENTIFIER ::= { iso 1 }
six OBJECT IDENTIFIER ::= { fooOne 1 }
three OBJECT IDENTIFIER ::= { six 3 }

foo OBJECT-TYPE
SYNTAX Integer32
Expand Down
57 changes: 57 additions & 0 deletions plugins/inputs/snmp/testdata/tableBuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
TEST DEFINITIONS ::= BEGIN

IMPORTS
MODULE-IDENTITY, OBJECT-TYPE, Integer32 FROM fooImports;

TestMIB MODULE-IDENTITY
LAST-UPDATED "2021090800Z"
ORGANIZATION "influx"
CONTACT-INFO
"EMail: influx@email.com"
DESCRIPTION
"MIB module for testing snmp plugin
for telegraf
"
::= { iso 1 }

testingObjects OBJECT IDENTIFIER ::= { iso 0 }
testObjects OBJECT IDENTIFIER ::= { testingObjects 0 }
hostnameone OBJECT IDENTIFIER ::= {testObjects 1 }
hostname OBJECT IDENTIFIER ::= { hostnameone 1 }
testTable OBJECT IDENTIFIER ::= { testObjects 0 }
testMIBObjects OBJECT IDENTIFIER ::= { testTable 1 }


myfield1 OBJECT-TYPE
SYNTAX Integer32
ACCESS read-only
STATUS current
DESCRIPTION
"server mib for testing"
::= { testMIBObjects 1 }

myfield2 OBJECT-TYPE
SYNTAX Integer32
ACCESS read-only
STATUS current
DESCRIPTION
"server mib for testing"
::= { testMIBObjects 2 }

myfield3 OBJECT-TYPE
SYNTAX Integer32
ACCESS read-only
STATUS current
DESCRIPTION
"server mib for testing"
::= { testMIBObjects 3 }

myfield4 OBJECT-TYPE
SYNTAX Integer32
ACCESS read-only
STATUS current
DESCRIPTION
"server mib for testing"
::= { testMIBObjects 4 }

END

0 comments on commit 6108859

Please sign in to comment.