Skip to content

Commit 3d5e3b3

Browse files
authored
turn on gosnmp logging in debug mode (#213)
1 parent 44af518 commit 3d5e3b3

File tree

5 files changed

+48
-15
lines changed

5 files changed

+48
-15
lines changed

snmp-discovery/policy/manager.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"log/slog"
9+
"time"
910

1011
"github.com/netboxlabs/diode-sdk-go/diode"
1112
"github.com/netboxlabs/orb-discovery/snmp-discovery/config"
@@ -193,7 +194,12 @@ func (m *Manager) StartPolicy(name string, policy config.Policy) error {
193194
m.logger.Info("Loaded device lookup extensions", "directory", policy.Config.LookupExtensionsDir)
194195
}
195196

196-
r, err := NewRunner(m.ctx, m.logger, name, policy, m.client, snmp.NewClient, &m.mappingConfig, m.manufacturers, deviceLookup)
197+
// Create logger-aware ClientFactory wrapper
198+
clientFactory := func(host string, port uint16, retries int, timeout time.Duration, authentication *config.Authentication, logger *slog.Logger) (snmp.Walker, error) {
199+
return snmp.NewClient(host, port, retries, timeout, authentication, logger)
200+
}
201+
202+
r, err := NewRunner(m.ctx, m.logger, name, policy, m.client, clientFactory, &m.mappingConfig, m.manufacturers, deviceLookup)
197203
if err != nil {
198204
return err
199205
}

snmp-discovery/policy/runner_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ func TestRunnerWalkError(t *testing.T) {
353353
mockHost.On("Walk", mock.Anything, mock.Anything).Return(nil, errors.New("walk error"))
354354

355355
// Create a mock client factory that returns the mock host
356-
mockClientFactory := func(_ string, _ uint16, _ int, _ time.Duration, _ *config.Authentication) (snmp.Walker, error) {
356+
mockClientFactory := func(_ string, _ uint16, _ int, _ time.Duration, _ *config.Authentication, _ *slog.Logger) (snmp.Walker, error) {
357357
return mockHost, nil
358358
}
359359

snmp-discovery/snmp/mocks.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package snmp
22

33
import (
4+
"log/slog"
45
"time"
56

67
"github.com/gosnmp/gosnmp"
@@ -51,6 +52,6 @@ func (n *FakeSNMPWalker) Walk(oid string, _ int) (map[string]PDU, error) {
5152
}
5253

5354
// NewFakeSNMPWalker creates a new FakeSNMPWalker
54-
func NewFakeSNMPWalker(_ string, _ uint16, _ int, _ time.Duration, _ *config.Authentication) (Walker, error) {
55+
func NewFakeSNMPWalker(_ string, _ uint16, _ int, _ time.Duration, _ *config.Authentication, _ *slog.Logger) (Walker, error) {
5556
return &FakeSNMPWalker{}, nil
5657
}

snmp-discovery/snmp/snmp.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package snmp
22

33
import (
4+
"context"
45
"fmt"
56
"log/slog"
67
"reflect"
@@ -11,6 +12,21 @@ import (
1112
"github.com/netboxlabs/orb-discovery/snmp-discovery/mapping"
1213
)
1314

15+
// SlogAdapter adapts slog.Logger to implement gosnmp.LoggerInterface
16+
type SlogAdapter struct {
17+
logger *slog.Logger
18+
}
19+
20+
// Print implements gosnmp.LoggerInterface by logging at Debug level
21+
func (s *SlogAdapter) Print(v ...interface{}) {
22+
s.logger.Debug(fmt.Sprint(v...))
23+
}
24+
25+
// Printf implements gosnmp.LoggerInterface by logging at Debug level
26+
func (s *SlogAdapter) Printf(format string, v ...interface{}) {
27+
s.logger.Debug(fmt.Sprintf(format, v...))
28+
}
29+
1430
// Host is a struct that represents an SNMP host
1531
type Host struct {
1632
address string
@@ -39,7 +55,7 @@ func NewHost(host string, port uint16, retries int, timeout time.Duration, authe
3955
func (s *Host) Walk(objectIDs map[string]int) (mapping.ObjectIDValueMap, error) {
4056
s.logger.Info("Scanning", "host", s.address)
4157

42-
snmpClient, err := s.ClientFactory(s.address, s.port, s.retries, s.timeout, s.authentication)
58+
snmpClient, err := s.ClientFactory(s.address, s.port, s.retries, s.timeout, s.authentication, s.logger)
4359
if err != nil {
4460
return nil, err
4561
}
@@ -167,10 +183,16 @@ const (
167183
)
168184

169185
// ClientFactory is a function that creates a new SNMPClient
170-
type ClientFactory func(host string, port uint16, retries int, timeout time.Duration, authentication *config.Authentication) (Walker, error)
186+
type ClientFactory func(host string, port uint16, retries int, timeout time.Duration, authentication *config.Authentication, logger *slog.Logger) (Walker, error)
171187

172188
// NewClient creates a new SNMPClient for the given target host
173-
func NewClient(host string, port uint16, retries int, timeout time.Duration, authentication *config.Authentication) (Walker, error) {
189+
func NewClient(host string, port uint16, retries int, timeout time.Duration, authentication *config.Authentication, logger *slog.Logger) (Walker, error) {
190+
// Check if debug logging is enabled
191+
var gosnmpLogger gosnmp.Logger
192+
if logger.Enabled(context.Background(), slog.LevelDebug) {
193+
gosnmpLogger = gosnmp.NewLogger(&SlogAdapter{logger})
194+
}
195+
174196
switch authentication.ProtocolVersion {
175197
case ProtocolVersion1:
176198
return &Client{
@@ -181,6 +203,7 @@ func NewClient(host string, port uint16, retries int, timeout time.Duration, aut
181203
Version: gosnmp.Version1,
182204
Timeout: timeout,
183205
Retries: retries,
206+
Logger: gosnmpLogger,
184207
},
185208
}, nil
186209
case ProtocolVersion2c:
@@ -192,6 +215,7 @@ func NewClient(host string, port uint16, retries int, timeout time.Duration, aut
192215
Version: gosnmp.Version2c,
193216
Timeout: timeout,
194217
Retries: retries,
218+
Logger: gosnmpLogger,
195219
},
196220
}, nil
197221
case ProtocolVersion3:
@@ -211,6 +235,7 @@ func NewClient(host string, port uint16, retries int, timeout time.Duration, aut
211235
Timeout: timeout,
212236
Retries: retries,
213237
SecurityModel: gosnmp.UserSecurityModel,
238+
Logger: gosnmpLogger,
214239
SecurityParameters: &gosnmp.UsmSecurityParameters{
215240
UserName: authentication.Username,
216241
AuthenticationProtocol: authProtocol,

snmp-discovery/snmp/snmp_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ func TestSNMPHost(t *testing.T) {
7878

7979
t.Run("Successfully walks a host", func(t *testing.T) {
8080
// Setup
81-
snmpClientFactory := func(_ string, _ uint16, _ int, _ time.Duration, _ *config.Authentication) (snmp.Walker, error) {
82-
fakeWalker, _ := snmp.NewFakeSNMPWalker("192.168.1.1", 161, 3, 1*time.Second, nil)
81+
snmpClientFactory := func(_ string, _ uint16, _ int, _ time.Duration, _ *config.Authentication, logger *slog.Logger) (snmp.Walker, error) {
82+
fakeWalker, _ := snmp.NewFakeSNMPWalker("192.168.1.1", 161, 3, 1*time.Second, nil, logger)
8383
return fakeWalker, nil
8484
}
8585
host := snmp.NewHost("192.168.1.1", 161, 3, 1*time.Second, nil, logger, snmpClientFactory)
@@ -110,7 +110,7 @@ func TestSNMPHost(t *testing.T) {
110110
interfaceSpeedOID + ".1": {Value: 1000000, Type: gosnmp.Integer, IdentifierSize: 1},
111111
}, nil)
112112

113-
snmpClientFactory := func(_ string, _ uint16, _ int, _ time.Duration, _ *config.Authentication) (snmp.Walker, error) {
113+
snmpClientFactory := func(_ string, _ uint16, _ int, _ time.Duration, _ *config.Authentication, _ *slog.Logger) (snmp.Walker, error) {
114114
return mockWalker, nil
115115
}
116116
host := snmp.NewHost("192.168.1.1", 161, 3, 1*time.Second, nil, logger, snmpClientFactory)
@@ -136,7 +136,7 @@ func TestSNMPHost(t *testing.T) {
136136
ipAddressObjectID: {Value: "192.168.1.1", Type: gosnmp.IPAddress, IdentifierSize: 4},
137137
}, nil)
138138

139-
snmpClientFactory := func(_ string, _ uint16, _ int, _ time.Duration, _ *config.Authentication) (snmp.Walker, error) {
139+
snmpClientFactory := func(_ string, _ uint16, _ int, _ time.Duration, _ *config.Authentication, _ *slog.Logger) (snmp.Walker, error) {
140140
return mockWalker, nil
141141
}
142142
host := snmp.NewHost("192.168.1.1", 161, 3, 1*time.Second, nil, logger, snmpClientFactory)
@@ -158,7 +158,7 @@ func TestSNMPHost(t *testing.T) {
158158
mockWalker := &MockSNMP{}
159159
mockWalker.On("Connect").Return(assert.AnError)
160160
mockWalker.On("Close").Return(nil)
161-
snmpClientFactory := func(_ string, _ uint16, _ int, _ time.Duration, _ *config.Authentication) (snmp.Walker, error) {
161+
snmpClientFactory := func(_ string, _ uint16, _ int, _ time.Duration, _ *config.Authentication, _ *slog.Logger) (snmp.Walker, error) {
162162
return mockWalker, nil
163163
}
164164
host := snmp.NewHost("192.168.1.1", 161, 3, 1*time.Second, nil, logger, snmpClientFactory)
@@ -178,7 +178,7 @@ func TestSNMPHost(t *testing.T) {
178178
mockWalker.On("Connect").Return(nil)
179179
mockWalker.On("Close").Return(nil)
180180
mockWalker.On("Walk", mock.Anything, mock.Anything).Return(make(map[string]snmp.PDU), assert.AnError)
181-
snmpClientFactory := func(_ string, _ uint16, _ int, _ time.Duration, _ *config.Authentication) (snmp.Walker, error) {
181+
snmpClientFactory := func(_ string, _ uint16, _ int, _ time.Duration, _ *config.Authentication, _ *slog.Logger) (snmp.Walker, error) {
182182
return mockWalker, nil
183183
}
184184
host := snmp.NewHost("192.168.1.1", 161, 3, 1*time.Second, nil, logger, snmpClientFactory)
@@ -207,7 +207,7 @@ func TestSNMPHost(t *testing.T) {
207207
interfaceSpeedOID + ".1": {Value: "invalid", Type: gosnmp.Asn1BER(255), IdentifierSize: 1}, // Invalid type
208208
}, nil)
209209

210-
snmpClientFactory := func(_ string, _ uint16, _ int, _ time.Duration, _ *config.Authentication) (snmp.Walker, error) {
210+
snmpClientFactory := func(_ string, _ uint16, _ int, _ time.Duration, _ *config.Authentication, _ *slog.Logger) (snmp.Walker, error) {
211211
return mockWalker, nil
212212
}
213213
host := snmp.NewHost("192.168.1.1", 161, 3, 1*time.Second, nil, logger, snmpClientFactory)
@@ -232,7 +232,7 @@ func TestSNMPHost(t *testing.T) {
232232
mockWalker.On("Connect").Return(nil)
233233
mockWalker.On("Close").Return(nil)
234234
mockWalker.On("Walk", mock.Anything, mock.Anything).Return(nil, assert.AnError)
235-
snmpClientFactory := func(_ string, _ uint16, _ int, _ time.Duration, _ *config.Authentication) (snmp.Walker, error) {
235+
snmpClientFactory := func(_ string, _ uint16, _ int, _ time.Duration, _ *config.Authentication, _ *slog.Logger) (snmp.Walker, error) {
236236
return nil, fmt.Errorf("error creating client")
237237
}
238238
host := snmp.NewHost("192.168.1.1", 161, 3, 1*time.Second, nil, logger, snmpClientFactory)
@@ -434,7 +434,8 @@ func TestNewClient(t *testing.T) {
434434

435435
for _, tc := range testCases {
436436
t.Run(tc.name, func(t *testing.T) {
437-
client, err := snmp.NewClient("192.168.1.1", 161, 3, 1*time.Second, tc.auth)
437+
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
438+
client, err := snmp.NewClient("192.168.1.1", 161, 3, 1*time.Second, tc.auth, logger)
438439

439440
if tc.expectError {
440441
assert.Error(t, err)

0 commit comments

Comments
 (0)