Skip to content

Commit

Permalink
fix(inputs.snmp_trap): Handle octet strings (#14619)
Browse files Browse the repository at this point in the history
(cherry picked from commit 4c6df8d)
  • Loading branch information
JuhaKS authored and powersj committed Jan 31, 2024
1 parent 3b5e4c1 commit 9c51da4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
9 changes: 9 additions & 0 deletions plugins/inputs/snmp_trap/snmp_trap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package snmp_trap

import (
_ "embed"
"encoding/hex"
"fmt"
"net"
"strconv"
"strings"
"time"
"unicode/utf8"

"github.com/gosnmp/gosnmp"

Expand Down Expand Up @@ -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
}
Expand Down
13 changes: 13 additions & 0 deletions plugins/inputs/snmp_trap/snmp_trap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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{
Expand All @@ -357,6 +369,7 @@ func TestReceiveTrap(t *testing.T) {
map[string]interface{}{ // fields
"sysUpTimeInstance": uint(now),
"valueOID": "payload",
"valueHexOID": "07e801040e021900000e02",
},
fakeTime,
),
Expand Down

0 comments on commit 9c51da4

Please sign in to comment.