fix(inputs.snmp_trap): Handle octet strings (#14619)

This commit is contained in:
Juha Keski-Saari 2024-01-30 11:02:40 +02:00 committed by GitHub
parent 18f57e30f3
commit 4c6df8dbd2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 0 deletions

View File

@ -3,11 +3,13 @@ package snmp_trap
import ( import (
_ "embed" _ "embed"
"encoding/hex"
"fmt" "fmt"
"net" "net"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"unicode/utf8"
"github.com/gosnmp/gosnmp" "github.com/gosnmp/gosnmp"
@ -343,6 +345,13 @@ func makeTrapHandler(s *SnmpTrap) gosnmp.TrapHandlerFunc {
setTrapOid(tags, val, e) setTrapOid(tags, val, e)
continue 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: default:
value = v.Value value = v.Value
} }

View File

@ -319,6 +319,11 @@ func TestReceiveTrap(t *testing.T) {
Type: gosnmp.OctetString, Type: gosnmp.OctetString,
Value: "payload", 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", Enterprise: ".1.2.3",
AgentAddress: "10.20.30.40", AgentAddress: "10.20.30.40",
@ -334,6 +339,13 @@ func TestReceiveTrap(t *testing.T) {
OidText: "valueOID", OidText: "valueOID",
}, },
}, },
{
".1.2.3.4.6",
snmp.MibEntry{
MibName: "valueMIB",
OidText: "valueHexOID",
},
},
{ {
".1.3.6.1.6.3.1.1.5.1", ".1.3.6.1.6.3.1.1.5.1",
snmp.MibEntry{ snmp.MibEntry{
@ -357,6 +369,7 @@ func TestReceiveTrap(t *testing.T) {
map[string]interface{}{ // fields map[string]interface{}{ // fields
"sysUpTimeInstance": uint(now), "sysUpTimeInstance": uint(now),
"valueOID": "payload", "valueOID": "payload",
"valueHexOID": "07e801040e021900000e02",
}, },
fakeTime, fakeTime,
), ),