From 7904187333b5a474d56b0398345a2e3707fbcc42 Mon Sep 17 00:00:00 2001 From: Ian Date: Mon, 28 Oct 2024 13:07:14 -0700 Subject: [PATCH] Recover from a panic in disco if things do not boogie right (#770) * Recover from a panic in disco if things do not boogie right * also update otel example to show how to send logs and metrics in --- hack/grafana-cloud/docker-compose.yml | 3 +++ pkg/inputs/snmp/disco.go | 3 +++ pkg/inputs/snmp/metadata/device_metadata.go | 12 ++++++++++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/hack/grafana-cloud/docker-compose.yml b/hack/grafana-cloud/docker-compose.yml index 26e2c701..ab0c0e39 100644 --- a/hack/grafana-cloud/docker-compose.yml +++ b/hack/grafana-cloud/docker-compose.yml @@ -14,6 +14,9 @@ services: - alloy command: - --format=otel + - --sinks=otel + - --metrics=jchf + - --tee_logs=true - --otel.protocol=grpc - --otel.endpoint=http://alloy:4317/ - --snmp=/snmp.yml diff --git a/pkg/inputs/snmp/disco.go b/pkg/inputs/snmp/disco.go index 15aac3d1..9be8f718 100644 --- a/pkg/inputs/snmp/disco.go +++ b/pkg/inputs/snmp/disco.go @@ -196,6 +196,9 @@ func doubleCheckHost(result scan.Result, timeout time.Duration, ctl chan bool, m // Get the token to allow us to run. _ = <-ctl defer func() { + if r := recover(); r != nil { + log.Errorf("Recovered from panic, skipping device: %v", r) + } wg.Done() ctl <- true }() diff --git a/pkg/inputs/snmp/metadata/device_metadata.go b/pkg/inputs/snmp/metadata/device_metadata.go index c69bd86b..9b3a0d70 100644 --- a/pkg/inputs/snmp/metadata/device_metadata.go +++ b/pkg/inputs/snmp/metadata/device_metadata.go @@ -164,7 +164,11 @@ func (dm *DeviceMetadata) poll(ctx context.Context, server *gosnmp.GoSNMP) (*kt. case SNMP_sysDescr: md.SysDescr = string(value.([]byte)) case SNMP_sysObjectID: - md.SysObjectID = value.(string) + if s, ok := value.(string); ok { + md.SysObjectID = s + } else if s, ok := value.([]byte); ok { + md.SysObjectID = string(s) + } case SNMP_sysContact: md.SysContact = string(value.([]byte)) case SNMP_sysName: @@ -315,7 +319,11 @@ func GetBasicDeviceMetadata(log logger.ContextL, server *gosnmp.GoSNMP) (*kt.Dev case SNMP_sysDescr: md.SysDescr = string(value.([]byte)) case SNMP_sysObjectID: - md.SysObjectID = value.(string) + if s, ok := value.(string); ok { + md.SysObjectID = s + } else if s, ok := value.([]byte); ok { + md.SysObjectID = string(s) + } case SNMP_sysContact: md.SysContact = string(value.([]byte)) case SNMP_sysName: