Skip to content

Commit 58b4126

Browse files
authored
fix(inputs.snmp): Fix crash when trying to format fields from unknown OIDs (#16155)
1 parent 18b2d3c commit 58b4126

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

internal/snmp/translator_gosmi.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"github.com/influxdata/telegraf"
1313
)
1414

15+
var errCannotFormatUnkownType = errors.New("cannot format value, unknown type")
16+
1517
type gosmiTranslator struct {
1618
}
1719

@@ -64,14 +66,18 @@ func (g *gosmiTranslator) SnmpFormatEnum(oid string, value interface{}, full boo
6466
return "", err
6567
}
6668

69+
if node.Type == nil {
70+
return "", errCannotFormatUnkownType
71+
}
72+
6773
var v models.Value
6874
if full {
6975
v = node.FormatValue(value, models.FormatEnumName, models.FormatEnumValue)
7076
} else {
7177
v = node.FormatValue(value, models.FormatEnumName)
7278
}
7379

74-
return v.Formatted, nil
80+
return v.String(), nil
7581
}
7682

7783
func (g *gosmiTranslator) SnmpFormatDisplayHint(oid string, value interface{}) (string, error) {
@@ -85,9 +91,11 @@ func (g *gosmiTranslator) SnmpFormatDisplayHint(oid string, value interface{}) (
8591
return "", err
8692
}
8793

88-
v := node.FormatValue(value)
94+
if node.Type == nil {
95+
return "", errCannotFormatUnkownType
96+
}
8997

90-
return v.Formatted, nil
98+
return node.FormatValue(value).String(), nil
9199
}
92100

93101
func getIndex(mibPrefix string, node gosmi.SmiNode) (col []string, tagOids map[string]struct{}) {

0 commit comments

Comments
 (0)