From 4c6df8dbd2fb88c9295e28cc75372fa06f340944 Mon Sep 17 00:00:00 2001 From: Juha Keski-Saari Date: Tue, 30 Jan 2024 11:02:40 +0200 Subject: [PATCH] fix(inputs.snmp_trap): Handle octet strings (#14619) --- plugins/inputs/snmp_trap/snmp_trap.go | 9 +++++++++ plugins/inputs/snmp_trap/snmp_trap_test.go | 13 +++++++++++++ 2 files changed, 22 insertions(+) diff --git a/plugins/inputs/snmp_trap/snmp_trap.go b/plugins/inputs/snmp_trap/snmp_trap.go index 35c8bff9f..17c5417a9 100644 --- a/plugins/inputs/snmp_trap/snmp_trap.go +++ b/plugins/inputs/snmp_trap/snmp_trap.go @@ -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 } diff --git a/plugins/inputs/snmp_trap/snmp_trap_test.go b/plugins/inputs/snmp_trap/snmp_trap_test.go index ca60a1106..0af1ae83d 100644 --- a/plugins/inputs/snmp_trap/snmp_trap_test.go +++ b/plugins/inputs/snmp_trap/snmp_trap_test.go @@ -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, ),