fix(inputs.snmp): Translate field correctly when not in table (#15618)
This commit is contained in:
parent
5a8af53447
commit
9297d77306
|
|
@ -95,6 +95,17 @@ func (f *Field) Init(tr Translator) error {
|
||||||
|
|
||||||
// fieldConvert converts from any type according to the conv specification
|
// fieldConvert converts from any type according to the conv specification
|
||||||
func (f *Field) Convert(ent gosnmp.SnmpPDU) (interface{}, error) {
|
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 == "" {
|
if f.Conversion == "" {
|
||||||
// OctetStrings may contain hex data that needs its own conversion
|
// OctetStrings may contain hex data that needs its own conversion
|
||||||
if ent.Type == gosnmp.OctetString && !utf8.Valid(ent.Value.([]byte)[:]) {
|
if ent.Type == gosnmp.OctetString && !utf8.Valid(ent.Value.([]byte)[:]) {
|
||||||
|
|
|
||||||
|
|
@ -205,17 +205,6 @@ func (t Table) Build(gs Connection, walk bool) (*RTable, error) {
|
||||||
}, idx)
|
}, 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)
|
fv, err := f.Convert(ent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &walkError{
|
return &walkError{
|
||||||
|
|
|
||||||
|
|
@ -274,15 +274,21 @@ func TestTableBuild_noWalkGosmi(t *testing.T) {
|
||||||
Name: "noexist",
|
Name: "noexist",
|
||||||
Oid: ".1.2.3.4.5",
|
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)
|
tb, err := tbl.Build(tsc, false)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
rtr := RTableRow{
|
rtr := RTableRow{
|
||||||
Tags: map[string]string{"myfield1": "baz", "myfield3": "234"},
|
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.Len(t, tb.Rows, 1)
|
||||||
require.Contains(t, tb.Rows, rtr)
|
require.Contains(t, tb.Rows, rtr)
|
||||||
|
|
|
||||||
|
|
@ -207,15 +207,21 @@ func TestTableBuild_noWalk(t *testing.T) {
|
||||||
Name: "noexist",
|
Name: "noexist",
|
||||||
Oid: ".1.2.3.4.5",
|
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)
|
tb, err := tbl.Build(tsc, false)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
rtr := RTableRow{
|
rtr := RTableRow{
|
||||||
Tags: map[string]string{"myfield1": "baz", "myfield3": "234"},
|
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.Len(t, tb.Rows, 1)
|
||||||
require.Contains(t, tb.Rows, rtr)
|
require.Contains(t, tb.Rows, rtr)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue