diff --git a/plugins/inputs/snmp/snmp.go b/plugins/inputs/snmp/snmp.go index 8843e9f90e18f..303aa8dd6c7f4 100644 --- a/plugins/inputs/snmp/snmp.go +++ b/plugins/inputs/snmp/snmp.go @@ -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 @@ -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() } @@ -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 } } @@ -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 diff --git a/plugins/inputs/snmp/snmp_test.go b/plugins/inputs/snmp/snmp_test.go index 843a85c9e36ea..64f5aeb194286 100644 --- a/plugins/inputs/snmp/snmp_test.go +++ b/plugins/inputs/snmp/snmp_test.go @@ -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) } diff --git a/plugins/inputs/snmp/testdata/foo b/plugins/inputs/snmp/testdata/foo index f2e4c0b700f80..4e9bf7f9d16f9 100644 --- a/plugins/inputs/snmp/testdata/foo +++ b/plugins/inputs/snmp/testdata/foo @@ -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 diff --git a/plugins/inputs/snmp/testdata/tableBuild b/plugins/inputs/snmp/testdata/tableBuild new file mode 100644 index 0000000000000..0551bfd6dd1d4 --- /dev/null +++ b/plugins/inputs/snmp/testdata/tableBuild @@ -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 \ No newline at end of file