Skip to content

Commit

Permalink
feat: support snmp enum
Browse files Browse the repository at this point in the history
  • Loading branch information
kongfei605 committed May 14, 2024
1 parent 451b785 commit 4e89374
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions inputs/snmp/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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 {
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}

0 comments on commit 4e89374

Please sign in to comment.