feat(inputs.snmp): Convert uneven bytes to int (#16027)
This commit is contained in:
parent
c4bce2d211
commit
3951d894ee
|
|
@ -253,33 +253,38 @@ func (f *Field) Convert(ent gosnmp.SnmpPDU) (interface{}, error) {
|
||||||
return v, nil
|
return v, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var b []byte
|
||||||
|
switch bit {
|
||||||
|
case "uint64":
|
||||||
|
b = make([]byte, 8)
|
||||||
|
case "uint32":
|
||||||
|
b = make([]byte, 4)
|
||||||
|
case "uint16":
|
||||||
|
b = make([]byte, 2)
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("invalid bit value (%s) for hex to int conversion", bit)
|
||||||
|
}
|
||||||
|
copy(b, bv)
|
||||||
|
|
||||||
|
var byteOrder binary.ByteOrder
|
||||||
switch endian {
|
switch endian {
|
||||||
case "LittleEndian":
|
case "LittleEndian":
|
||||||
switch bit {
|
byteOrder = binary.LittleEndian
|
||||||
case "uint64":
|
|
||||||
v = binary.LittleEndian.Uint64(bv)
|
|
||||||
case "uint32":
|
|
||||||
v = binary.LittleEndian.Uint32(bv)
|
|
||||||
case "uint16":
|
|
||||||
v = binary.LittleEndian.Uint16(bv)
|
|
||||||
default:
|
|
||||||
return nil, fmt.Errorf("invalid bit value (%s) for hex to int conversion", bit)
|
|
||||||
}
|
|
||||||
case "BigEndian":
|
case "BigEndian":
|
||||||
switch bit {
|
byteOrder = binary.BigEndian
|
||||||
case "uint64":
|
|
||||||
v = binary.BigEndian.Uint64(bv)
|
|
||||||
case "uint32":
|
|
||||||
v = binary.BigEndian.Uint32(bv)
|
|
||||||
case "uint16":
|
|
||||||
v = binary.BigEndian.Uint16(bv)
|
|
||||||
default:
|
|
||||||
return nil, fmt.Errorf("invalid bit value (%s) for hex to int conversion", bit)
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("invalid Endian value (%s) for hex to int conversion", endian)
|
return nil, fmt.Errorf("invalid Endian value (%s) for hex to int conversion", endian)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch bit {
|
||||||
|
case "uint64":
|
||||||
|
v = byteOrder.Uint64(b)
|
||||||
|
case "uint32":
|
||||||
|
v = byteOrder.Uint32(b)
|
||||||
|
case "uint16":
|
||||||
|
v = byteOrder.Uint16(b)
|
||||||
|
}
|
||||||
|
|
||||||
return v, nil
|
return v, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -211,6 +211,15 @@ func TestConvertHextoint(t *testing.T) {
|
||||||
},
|
},
|
||||||
expected: uint16(0xc884),
|
expected: uint16(0xc884),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "little endian single byte",
|
||||||
|
conversion: "hextoint:LittleEndian:uint16",
|
||||||
|
ent: gosnmp.SnmpPDU{
|
||||||
|
Type: gosnmp.OctetString,
|
||||||
|
Value: []byte{0x84},
|
||||||
|
},
|
||||||
|
expected: uint16(0x84),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "little endian invalid",
|
name: "little endian invalid",
|
||||||
conversion: "hextoint:LittleEndian:invalid",
|
conversion: "hextoint:LittleEndian:invalid",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue