fix(inputs.snmp): Translate field correctly when not in table (#15618)

This commit is contained in:
Thomas Casteleyn 2024-07-15 15:55:39 +02:00 committed by GitHub
parent 5a8af53447
commit 9297d77306
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 25 additions and 13 deletions

View File

@ -95,6 +95,17 @@ func (f *Field) Init(tr Translator) error {
// fieldConvert converts from any type according to the conv specification
func (f *Field) Convert(ent gosnmp.SnmpPDU) (interface{}, error) {
// snmptranslate table field value here
if f.Translate {
if entOid, ok := ent.Value.(string); ok {
_, _, oidText, _, err := f.translator.SnmpTranslate(entOid)
if err == nil {
// If no error translating, the original value for ent.Value should be replaced
ent.Value = oidText
}
}
}
if f.Conversion == "" {
// OctetStrings may contain hex data that needs its own conversion
if ent.Type == gosnmp.OctetString && !utf8.Valid(ent.Value.([]byte)[:]) {

View File

@ -205,17 +205,6 @@ func (t Table) Build(gs Connection, walk bool) (*RTable, error) {
}, idx)
}
// snmptranslate table field value here
if f.Translate {
if entOid, ok := ent.Value.(string); ok {
_, _, oidText, _, err := t.translator.SnmpTranslate(entOid)
if err == nil {
// If no error translating, the original value for ent.Value should be replaced
ent.Value = oidText
}
}
}
fv, err := f.Convert(ent)
if err != nil {
return &walkError{

View File

@ -274,15 +274,21 @@ func TestTableBuild_noWalkGosmi(t *testing.T) {
Name: "noexist",
Oid: ".1.2.3.4.5",
},
{
Name: "myfield4",
Oid: ".1.3.6.1.2.1.3.1.1.3.0",
Translate: true,
},
},
}
require.NoError(t, tbl.Init(getGosmiTr(t)))
tb, err := tbl.Build(tsc, false)
require.NoError(t, err)
rtr := RTableRow{
Tags: map[string]string{"myfield1": "baz", "myfield3": "234"},
Fields: map[string]interface{}{"myfield2": 234},
Fields: map[string]interface{}{"myfield2": 234, "myfield4": "atNetAddress"},
}
require.Len(t, tb.Rows, 1)
require.Contains(t, tb.Rows, rtr)

View File

@ -207,15 +207,21 @@ func TestTableBuild_noWalk(t *testing.T) {
Name: "noexist",
Oid: ".1.2.3.4.5",
},
{
Name: "myfield4",
Oid: ".1.0.0.0.1.6.0",
Translate: true,
},
},
}
require.NoError(t, tbl.Init(NewNetsnmpTranslator(testutil.Logger{})))
tb, err := tbl.Build(tsc, false)
require.NoError(t, err)
rtr := RTableRow{
Tags: map[string]string{"myfield1": "baz", "myfield3": "234"},
Fields: map[string]interface{}{"myfield2": 234},
Fields: map[string]interface{}{"myfield2": 234, "myfield4": "testTableEntry.7"},
}
require.Len(t, tb.Rows, 1)
require.Contains(t, tb.Rows, rtr)