diff --git a/inputs/snmp/table.go b/inputs/snmp/table.go index 87126a66..204a919d 100644 --- a/inputs/snmp/table.go +++ b/inputs/snmp/table.go @@ -338,7 +338,7 @@ func (t Table) Build(gs snmpConnection, walk bool, tr Translator) (*RTable, erro pkt.Variables[0].Type != gosnmp.NoSuchObject && pkt.Variables[0].Type != gosnmp.NoSuchInstance { ent := pkt.Variables[0] - fv, err := fieldConvert(f.Conversion, ent.Value) + fv, err := fieldConvert(tr, f.Conversion, ent) if err != nil { return nil, fmt.Errorf("converting %q (OID %s) for field %s: %w", ent.Value, ent.Name, f.Name, err) } @@ -386,7 +386,7 @@ func (t Table) Build(gs snmpConnection, walk bool, tr Translator) (*RTable, erro } } - fv, err := fieldConvert(f.Conversion, ent.Value) + fv, err := fieldConvert(tr, f.Conversion, ent) if err != nil { return &walkError{ msg: fmt.Sprintf("converting %q (OID %s) for field %s", ent.Value, ent.Name, f.Name), @@ -547,16 +547,17 @@ func fieldNonStandardConvertInt64(v string) int64 { } // fieldConvert converts from any type according to the conv specification -func fieldConvert(conv string, v interface{}) (interface{}, error) { +func fieldConvert(tr Translator, conv string, ent gosnmp.SnmpPDU) (v interface{}, err error) { if conv == "" { - if bs, ok := v.([]byte); ok { + if bs, ok := ent.Value.([]byte); ok { return string(bs), nil } - return v, nil + return ent.Value, nil } var d int if _, err := fmt.Sscanf(conv, "float(%d)", &d); err == nil || conv == "float" { + v = ent.Value switch vt := v.(type) { case float32: v = float64(vt) / math.Pow10(d) @@ -593,6 +594,7 @@ func fieldConvert(conv string, v interface{}) (interface{}, error) { } if conv == "int" { + v = ent.Value switch vt := v.(type) { case float32: v = int64(vt) @@ -627,7 +629,7 @@ func fieldConvert(conv string, v interface{}) (interface{}, error) { } if conv == "hwaddr" { - switch vt := v.(type) { + switch vt := ent.Value.(type) { case string: v = net.HardwareAddr(vt).String() case []byte: @@ -643,9 +645,9 @@ func fieldConvert(conv string, v interface{}) (interface{}, error) { endian := split[1] bit := split[2] - bv, ok := v.([]byte) + bv, ok := ent.Value.([]byte) if !ok { - return v, nil + return ent.Value, nil } switch endian { @@ -681,7 +683,7 @@ func fieldConvert(conv string, v interface{}) (interface{}, error) { if conv == "ipaddr" { var ipbs []byte - switch vt := v.(type) { + switch vt := ent.Value.(type) { case string: ipbs = []byte(vt) case []byte: @@ -702,7 +704,7 @@ func fieldConvert(conv string, v interface{}) (interface{}, error) { // 55 57 57 51 46 56 32 77 66 may be the ascii arr for string -> 7993.8 MB if conv == "asciitobytes" { - + v = ent.Value input, ok := v.([]uint8) if !ok { return nil, fmt.Errorf("invalid type of %v (not rune arr)", v) @@ -759,6 +761,13 @@ func fieldConvert(conv string, v interface{}) (interface{}, error) { return result, nil } + if conv == "enum" { + return tr.SnmpFormatEnum(ent.Name, ent.Value, false) + } + + if conv == "enum(1)" { + return tr.SnmpFormatEnum(ent.Name, ent.Value, true) + } return nil, fmt.Errorf("invalid conversion type '%s'", conv) }