Skip to content

Commit

Permalink
fix(inputs.snmp): Fix crash when trying to format fields from unknown…
Browse files Browse the repository at this point in the history
… OIDs (#16155)
  • Loading branch information
Hipska authored Nov 7, 2024
1 parent 18b2d3c commit 58b4126
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions internal/snmp/translator_gosmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/influxdata/telegraf"
)

var errCannotFormatUnkownType = errors.New("cannot format value, unknown type")

type gosmiTranslator struct {
}

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

if node.Type == nil {
return "", errCannotFormatUnkownType
}

var v models.Value
if full {
v = node.FormatValue(value, models.FormatEnumName, models.FormatEnumValue)
} else {
v = node.FormatValue(value, models.FormatEnumName)
}

return v.Formatted, nil
return v.String(), nil
}

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

v := node.FormatValue(value)
if node.Type == nil {
return "", errCannotFormatUnkownType
}

return v.Formatted, nil
return node.FormatValue(value).String(), nil
}

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

0 comments on commit 58b4126

Please sign in to comment.