fix(inputs.snmp_trap): Handle octet strings (#14619)
This commit is contained in:
parent
18f57e30f3
commit
4c6df8dbd2
|
|
@ -3,11 +3,13 @@ package snmp_trap
|
|||
|
||||
import (
|
||||
_ "embed"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/gosnmp/gosnmp"
|
||||
|
||||
|
|
@ -343,6 +345,13 @@ func makeTrapHandler(s *SnmpTrap) gosnmp.TrapHandlerFunc {
|
|||
setTrapOid(tags, val, e)
|
||||
continue
|
||||
}
|
||||
case gosnmp.OctetString:
|
||||
// OctetStrings may contain hex data that needs its own conversion
|
||||
if !utf8.ValidString(string(v.Value.([]byte)[:])) {
|
||||
value = hex.EncodeToString(v.Value.([]byte))
|
||||
} else {
|
||||
value = v.Value
|
||||
}
|
||||
default:
|
||||
value = v.Value
|
||||
}
|
||||
|
|
|
|||
|
|
@ -319,6 +319,11 @@ func TestReceiveTrap(t *testing.T) {
|
|||
Type: gosnmp.OctetString,
|
||||
Value: "payload",
|
||||
},
|
||||
{
|
||||
Name: ".1.2.3.4.6",
|
||||
Type: gosnmp.OctetString,
|
||||
Value: []byte{0x7, 0xe8, 0x1, 0x4, 0xe, 0x2, 0x19, 0x0, 0x0, 0xe, 0x2},
|
||||
},
|
||||
},
|
||||
Enterprise: ".1.2.3",
|
||||
AgentAddress: "10.20.30.40",
|
||||
|
|
@ -334,6 +339,13 @@ func TestReceiveTrap(t *testing.T) {
|
|||
OidText: "valueOID",
|
||||
},
|
||||
},
|
||||
{
|
||||
".1.2.3.4.6",
|
||||
snmp.MibEntry{
|
||||
MibName: "valueMIB",
|
||||
OidText: "valueHexOID",
|
||||
},
|
||||
},
|
||||
{
|
||||
".1.3.6.1.6.3.1.1.5.1",
|
||||
snmp.MibEntry{
|
||||
|
|
@ -357,6 +369,7 @@ func TestReceiveTrap(t *testing.T) {
|
|||
map[string]interface{}{ // fields
|
||||
"sysUpTimeInstance": uint(now),
|
||||
"valueOID": "payload",
|
||||
"valueHexOID": "07e801040e021900000e02",
|
||||
},
|
||||
fakeTime,
|
||||
),
|
||||
|
|
|
|||
Loading…
Reference in New Issue