From 2e8d0c7056061ff225eb8f810b1728c756741079 Mon Sep 17 00:00:00 2001 From: Tuomas Tammero Date: Tue, 24 Nov 2020 10:04:18 +0200 Subject: [PATCH 1/8] Enable translating snmp table field value --- plugins/inputs/snmp/snmp.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/plugins/inputs/snmp/snmp.go b/plugins/inputs/snmp/snmp.go index 103b23d214485..b6ee4c4c8b6ae 100644 --- a/plugins/inputs/snmp/snmp.go +++ b/plugins/inputs/snmp/snmp.go @@ -237,6 +237,8 @@ type Field struct { // "hwaddr" will convert a 6-byte string to a MAC address. // "ipaddr" will convert the value to an IPv4 or IPv6 address. Conversion string + // Translate tells if the value of the field should be snmptranslated + Translate bool initialized bool } @@ -427,6 +429,7 @@ func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) { return nil, fmt.Errorf("performing get on field %s: %w", f.Name, err) } else if pkt != nil && len(pkt.Variables) > 0 && pkt.Variables[0].Type != gosnmp.NoSuchObject && pkt.Variables[0].Type != gosnmp.NoSuchInstance { ent := pkt.Variables[0] + fv, err := fieldConvert(f.Conversion, ent.Value) if err != nil { return nil, fmt.Errorf("converting %q (OID %s) for field %s: %w", ent.Value, ent.Name, f.Name, err) @@ -460,6 +463,16 @@ func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) { }, idx) } + // snmptranslate table field value here + if f.Translate { + entOid := ent.Value.(string) + _, _, oidText, _, err := SnmpTranslate(entOid) + if err == nil { + // If no error translating, the original value for ent.Value should be replaced + ent.Value = oidText + } + } + fv, err := fieldConvert(f.Conversion, ent.Value) if err != nil { return &walkError{ From df30df16e498b03c7c0e96d6561b8085aba0ee49 Mon Sep 17 00:00:00 2001 From: Tuomas Tammero Date: Tue, 24 Nov 2020 12:56:29 +0200 Subject: [PATCH 2/8] Check entity type before translating --- plugins/inputs/snmp/snmp.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/plugins/inputs/snmp/snmp.go b/plugins/inputs/snmp/snmp.go index b6ee4c4c8b6ae..055db0791eb77 100644 --- a/plugins/inputs/snmp/snmp.go +++ b/plugins/inputs/snmp/snmp.go @@ -464,12 +464,16 @@ func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) { } // snmptranslate table field value here - if f.Translate { - entOid := ent.Value.(string) - _, _, oidText, _, err := SnmpTranslate(entOid) - if err == nil { - // If no error translating, the original value for ent.Value should be replaced - ent.Value = oidText + if f.Translate && !f.IsTag{ + switch ent.Value.(interface{}).(type) { + case string: { + entOid := ent.Value.(string) + _, _, oidText, _, err := SnmpTranslate(entOid) + if err == nil { + // If no error translating, the original value for ent.Value should be replaced + ent.Value = oidText + } + } } } From 9a4743fdbc780fd0e1ca3764752bd714241f22f1 Mon Sep 17 00:00:00 2001 From: Tuomas Tammero Date: Tue, 24 Nov 2020 12:57:06 +0200 Subject: [PATCH 3/8] Update testing table build with walk --- plugins/inputs/snmp/snmp.go | 1 - plugins/inputs/snmp/snmp_test.go | 8 ++++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/plugins/inputs/snmp/snmp.go b/plugins/inputs/snmp/snmp.go index 055db0791eb77..ba10caff6a547 100644 --- a/plugins/inputs/snmp/snmp.go +++ b/plugins/inputs/snmp/snmp.go @@ -429,7 +429,6 @@ func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) { return nil, fmt.Errorf("performing get on field %s: %w", f.Name, err) } else if pkt != nil && len(pkt.Variables) > 0 && pkt.Variables[0].Type != gosnmp.NoSuchObject && pkt.Variables[0].Type != gosnmp.NoSuchInstance { ent := pkt.Variables[0] - fv, err := fieldConvert(f.Conversion, ent.Value) if err != nil { return nil, fmt.Errorf("converting %q (OID %s) for field %s: %w", ent.Value, ent.Name, f.Name, err) diff --git a/plugins/inputs/snmp/snmp_test.go b/plugins/inputs/snmp/snmp_test.go index 583b2dc847282..68b476464499d 100644 --- a/plugins/inputs/snmp/snmp_test.go +++ b/plugins/inputs/snmp/snmp_test.go @@ -493,6 +493,11 @@ func TestTableBuild_walk(t *testing.T) { Oid: ".1.0.0.2.1.5", OidIndexLength: 1, }, + { + Name: "myfield6", + Oid: ".1.0.0.0.1.2", + Translate: true, + }, }, } @@ -510,6 +515,7 @@ func TestTableBuild_walk(t *testing.T) { "myfield3": float64(0.123), "myfield4": 11, "myfield5": 11, + "myfield6": 1, }, } rtr2 := RTableRow{ @@ -522,6 +528,7 @@ func TestTableBuild_walk(t *testing.T) { "myfield3": float64(0.456), "myfield4": 22, "myfield5": 22, + "myfield6": 2, }, } rtr3 := RTableRow{ @@ -531,6 +538,7 @@ func TestTableBuild_walk(t *testing.T) { Fields: map[string]interface{}{ "myfield2": 0, "myfield3": float64(0.0), + "myfield6": 0, }, } rtr4 := RTableRow{ From a96f3e461b7fdde2e5f9123a8522c6f3ec8a762a Mon Sep 17 00:00:00 2001 From: Tuomas Tammero Date: Tue, 24 Nov 2020 13:06:15 +0200 Subject: [PATCH 4/8] Enable translating for tagged fields --- plugins/inputs/snmp/snmp.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/inputs/snmp/snmp.go b/plugins/inputs/snmp/snmp.go index ba10caff6a547..43b2ad8460b60 100644 --- a/plugins/inputs/snmp/snmp.go +++ b/plugins/inputs/snmp/snmp.go @@ -463,7 +463,7 @@ func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) { } // snmptranslate table field value here - if f.Translate && !f.IsTag{ + if f.Translate { switch ent.Value.(interface{}).(type) { case string: { entOid := ent.Value.(string) From 0d17c4109068cf2436ec15ff4d8e112faef2fb4b Mon Sep 17 00:00:00 2001 From: Tuomas Tammero Date: Tue, 24 Nov 2020 13:09:45 +0200 Subject: [PATCH 5/8] Update snmp input plugin docs --- plugins/inputs/snmp/README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/plugins/inputs/snmp/README.md b/plugins/inputs/snmp/README.md index a0c9155db5432..ea6e7a95bbfa4 100644 --- a/plugins/inputs/snmp/README.md +++ b/plugins/inputs/snmp/README.md @@ -184,6 +184,10 @@ One [metric][] is created for each row of the SNMP table. ## path segments). Truncates the index after this point to remove non-fixed ## value or length index suffixes. # oid_index_length = 0 + + ## Specifies if the value of given field should be snmptranslated + ## by default no field values are translated + # translate = true ``` ### Troubleshooting From 8fc235dc6c7c2f510d1f10c3e811a696ce015dbb Mon Sep 17 00:00:00 2001 From: Tuomas Tammero Date: Tue, 24 Nov 2020 13:32:00 +0200 Subject: [PATCH 6/8] Fix tests --- plugins/inputs/snmp/snmp.go | 3 ++- plugins/inputs/snmp/snmp_test.go | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/plugins/inputs/snmp/snmp.go b/plugins/inputs/snmp/snmp.go index 43b2ad8460b60..0c0c25087a7ba 100644 --- a/plugins/inputs/snmp/snmp.go +++ b/plugins/inputs/snmp/snmp.go @@ -465,7 +465,8 @@ func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) { // snmptranslate table field value here if f.Translate { switch ent.Value.(interface{}).(type) { - case string: { + case string: + { entOid := ent.Value.(string) _, _, oidText, _, err := SnmpTranslate(entOid) if err == nil { diff --git a/plugins/inputs/snmp/snmp_test.go b/plugins/inputs/snmp/snmp_test.go index 68b476464499d..778245ddfdedb 100644 --- a/plugins/inputs/snmp/snmp_test.go +++ b/plugins/inputs/snmp/snmp_test.go @@ -494,9 +494,9 @@ func TestTableBuild_walk(t *testing.T) { OidIndexLength: 1, }, { - Name: "myfield6", - Oid: ".1.0.0.0.1.2", - Translate: true, + Name: "myfield6", + Oid: ".1.0.0.0.1.2", + Translate: true, }, }, } From 137781811a772ff517584bbf92128df3d03cbd6c Mon Sep 17 00:00:00 2001 From: Tuomas Tammero Date: Thu, 26 Nov 2020 12:48:31 +0200 Subject: [PATCH 7/8] Remove redundant switch and improve tests --- plugins/inputs/snmp/snmp.go | 14 +++++--------- plugins/inputs/snmp/snmp_mocks_generate.go | 1 + plugins/inputs/snmp/snmp_mocks_test.go | 1 + plugins/inputs/snmp/snmp_test.go | 13 +++++++++---- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/plugins/inputs/snmp/snmp.go b/plugins/inputs/snmp/snmp.go index 0c0c25087a7ba..623c9ba61ce23 100644 --- a/plugins/inputs/snmp/snmp.go +++ b/plugins/inputs/snmp/snmp.go @@ -464,15 +464,11 @@ func (t Table) Build(gs snmpConnection, walk bool) (*RTable, error) { // snmptranslate table field value here if f.Translate { - switch ent.Value.(interface{}).(type) { - case string: - { - entOid := ent.Value.(string) - _, _, oidText, _, err := SnmpTranslate(entOid) - if err == nil { - // If no error translating, the original value for ent.Value should be replaced - ent.Value = oidText - } + if entOid, ok := ent.Value.(string); ok { + _, _, oidText, _, err := SnmpTranslate(entOid) + if err == nil { + // If no error translating, the original value for ent.Value should be replaced + ent.Value = oidText } } } diff --git a/plugins/inputs/snmp/snmp_mocks_generate.go b/plugins/inputs/snmp/snmp_mocks_generate.go index c09dd004580da..7227771a7e4fa 100644 --- a/plugins/inputs/snmp/snmp_mocks_generate.go +++ b/plugins/inputs/snmp/snmp_mocks_generate.go @@ -23,6 +23,7 @@ var mockedCommands = [][]string{ {"snmptranslate", "-Td", "-Ob", "-m", "all", ".1.0.0.0.1.1.0"}, {"snmptranslate", "-Td", "-Ob", "-m", "all", ".1.0.0.0.1.5"}, {"snmptranslate", "-Td", "-Ob", "-m", "all", ".1.2.3"}, + {"snmptranslate", "-Td", "-Ob", "-m", "all", ".1.0.0.0.1.7"}, {"snmptranslate", "-Td", "-Ob", ".iso.2.3"}, {"snmptranslate", "-Td", "-Ob", "-m", "all", ".999"}, {"snmptranslate", "-Td", "-Ob", "TEST::server"}, diff --git a/plugins/inputs/snmp/snmp_mocks_test.go b/plugins/inputs/snmp/snmp_mocks_test.go index 56d9326f1d639..5b3bbd767e67d 100644 --- a/plugins/inputs/snmp/snmp_mocks_test.go +++ b/plugins/inputs/snmp/snmp_mocks_test.go @@ -69,6 +69,7 @@ var mockedCommandResults = map[string]mockedCommandResult{ "snmptranslate\x00-Td\x00-Ob\x00-m\x00all\x00.1.0.0.0.1.1.0": {stdout: "TEST::server.0\nserver OBJECT-TYPE\n -- FROM\tTEST\n SYNTAX\tOCTET STRING\n MAX-ACCESS\tread-only\n STATUS\tcurrent\n::= { iso(1) 0 testOID(0) testTable(0) testTableEntry(1) server(1) 0 }\n", stderr: "", exitError: false}, "snmptranslate\x00-Td\x00-Ob\x00-m\x00all\x00.1.0.0.0.1.5": {stdout: "TEST::testTableEntry.5\ntestTableEntry OBJECT-TYPE\n -- FROM\tTEST\n MAX-ACCESS\tnot-accessible\n STATUS\tcurrent\n INDEX\t\t{ server }\n::= { iso(1) 0 testOID(0) testTable(0) testTableEntry(1) 5 }\n", stderr: "", exitError: false}, "snmptranslate\x00-Td\x00-Ob\x00-m\x00all\x00.1.2.3": {stdout: "iso.2.3\niso OBJECT-TYPE\n -- FROM\t#-1\n::= { iso(1) 2 3 }\n", stderr: "", exitError: false}, + "snmptranslate\x00-Td\x00-Ob\x00-m\x00all\x00.1.0.0.0.1.7": {stdout: "TEST::testTableEntry.7\ntestTableEntry OBJECT-TYPE\n -- FROM\tTEST\n MAX-ACCESS\tnot-accessible\n STATUS\tcurrent\n INDEX\t\t{ server }\n::= { iso(1) std(0) testOID(0) testTable(0) testTableEntry(1) 7 }\n", stderr: "", exitError: false}, "snmptranslate\x00-Td\x00-Ob\x00.iso.2.3": {stdout: "iso.2.3\niso OBJECT-TYPE\n -- FROM\t#-1\n::= { iso(1) 2 3 }\n", stderr: "", exitError: false}, "snmptranslate\x00-Td\x00-Ob\x00-m\x00all\x00.999": {stdout: ".999\n [TRUNCATED]\n", stderr: "", exitError: false}, "snmptranslate\x00-Td\x00-Ob\x00TEST::server": {stdout: "TEST::server\nserver OBJECT-TYPE\n -- FROM\tTEST\n SYNTAX\tOCTET STRING\n MAX-ACCESS\tread-only\n STATUS\tcurrent\n::= { iso(1) 0 testOID(0) testTable(0) testTableEntry(1) 1 }\n", stderr: "", exitError: false}, diff --git a/plugins/inputs/snmp/snmp_test.go b/plugins/inputs/snmp/snmp_test.go index 778245ddfdedb..69c88f29e430b 100644 --- a/plugins/inputs/snmp/snmp_test.go +++ b/plugins/inputs/snmp/snmp_test.go @@ -81,6 +81,7 @@ var tsc = &testSNMPConnection{ ".1.0.0.1.3": []byte("byte slice"), ".1.0.0.2.1.5.0.9.9": 11, ".1.0.0.2.1.5.1.9.9": 22, + ".1.0.0.0.1.6.0": ".1.0.0.0.1.7", }, } @@ -495,9 +496,14 @@ func TestTableBuild_walk(t *testing.T) { }, { Name: "myfield6", - Oid: ".1.0.0.0.1.2", + Oid: ".1.0.0.0.1.6", Translate: true, }, + { + Name: "myfield7", + Oid: ".1.0.0.0.1.6", + Translate: false, + }, }, } @@ -515,7 +521,8 @@ func TestTableBuild_walk(t *testing.T) { "myfield3": float64(0.123), "myfield4": 11, "myfield5": 11, - "myfield6": 1, + "myfield6": "testTableEntry.7", + "myfield7": ".1.0.0.0.1.7", }, } rtr2 := RTableRow{ @@ -528,7 +535,6 @@ func TestTableBuild_walk(t *testing.T) { "myfield3": float64(0.456), "myfield4": 22, "myfield5": 22, - "myfield6": 2, }, } rtr3 := RTableRow{ @@ -538,7 +544,6 @@ func TestTableBuild_walk(t *testing.T) { Fields: map[string]interface{}{ "myfield2": 0, "myfield3": float64(0.0), - "myfield6": 0, }, } rtr4 := RTableRow{ From a51a9a86a7b9f1be510b6c1a9e8ef9ef17aad9a2 Mon Sep 17 00:00:00 2001 From: Tuomas Tammero Date: Thu, 26 Nov 2020 12:52:57 +0200 Subject: [PATCH 8/8] Fix formatting --- plugins/inputs/snmp/snmp_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/inputs/snmp/snmp_test.go b/plugins/inputs/snmp/snmp_test.go index 69c88f29e430b..80651d1d86161 100644 --- a/plugins/inputs/snmp/snmp_test.go +++ b/plugins/inputs/snmp/snmp_test.go @@ -81,7 +81,7 @@ var tsc = &testSNMPConnection{ ".1.0.0.1.3": []byte("byte slice"), ".1.0.0.2.1.5.0.9.9": 11, ".1.0.0.2.1.5.1.9.9": 22, - ".1.0.0.0.1.6.0": ".1.0.0.0.1.7", + ".1.0.0.0.1.6.0": ".1.0.0.0.1.7", }, }