Skip to content

Commit c35a87e

Browse files
authored
Add GoSNMP logger (#1157)
Add a debug logging to calls to GoSNMP. * Requires `--log.level=debug`. * Enable with `--snmp.debug-packets` or `snmp_debug_packets=true` URL param. Signed-off-by: SuperQ <superq@gmail.com>
1 parent 7201b0d commit c35a87e

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

collector/collector.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,22 @@ type Collector struct {
300300
metrics Metrics
301301
concurrency int
302302
snmpContext string
303+
debugSNMP bool
303304
}
304305

305-
func New(ctx context.Context, target, authName, snmpContext string, auth *config.Auth, modules []*NamedModule, logger log.Logger, metrics Metrics, conc int) *Collector {
306-
return &Collector{ctx: ctx, target: target, authName: authName, auth: auth, modules: modules, logger: logger, metrics: metrics, concurrency: conc, snmpContext: snmpContext}
306+
func New(ctx context.Context, target, authName, snmpContext string, auth *config.Auth, modules []*NamedModule, logger log.Logger, metrics Metrics, conc int, debugSNMP bool) *Collector {
307+
return &Collector{
308+
ctx: ctx,
309+
target: target,
310+
authName: authName,
311+
auth: auth,
312+
modules: modules,
313+
snmpContext: snmpContext,
314+
logger: log.With(logger, "source_address", *srcAddress),
315+
metrics: metrics,
316+
concurrency: conc,
317+
debugSNMP: debugSNMP,
318+
}
307319
}
308320

309321
// Describe implements Prometheus.Collector.
@@ -420,7 +432,7 @@ func (c Collector) Collect(ch chan<- prometheus.Metric) {
420432
go func(i int) {
421433
defer wg.Done()
422434
logger := log.With(c.logger, "worker", i)
423-
client, err := scraper.NewGoSNMP(logger, c.target, *srcAddress)
435+
client, err := scraper.NewGoSNMP(logger, c.target, *srcAddress, c.debugSNMP)
424436
if err != nil {
425437
level.Info(logger).Log("msg", err)
426438
cancel()

main.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var (
4949
configFile = kingpin.Flag("config.file", "Path to configuration file.").Default("snmp.yml").Strings()
5050
dryRun = kingpin.Flag("dry-run", "Only verify configuration is valid and exit.").Default("false").Bool()
5151
concurrency = kingpin.Flag("snmp.module-concurrency", "The number of modules to fetch concurrently per scrape").Default("1").Int()
52+
debugSNMP = kingpin.Flag("snmp.debug-packets", "Include a full debug trace of SNMP packet traffics.").Default("false").Bool()
5253
expandEnvVars = kingpin.Flag("config.expand-environment-variables", "Expand environment variables to source secrets").Default("false").Bool()
5354
metricsPath = kingpin.Flag(
5455
"web.telemetry-path",
@@ -86,6 +87,14 @@ const (
8687
func handler(w http.ResponseWriter, r *http.Request, logger log.Logger, exporterMetrics collector.Metrics) {
8788
query := r.URL.Query()
8889

90+
debug := *debugSNMP
91+
if query.Get("snmp_debug_packets") == "true" {
92+
debug = true
93+
// TODO: This doesn't work the way I want.
94+
// logger = level.NewFilter(logger, level.AllowDebug())
95+
level.Debug(logger).Log("msg", "Debug query param enabled")
96+
}
97+
8998
target := query.Get("target")
9099
if len(query["target"]) != 1 || target == "" {
91100
http.Error(w, "'target' parameter must be specified once", http.StatusBadRequest)
@@ -149,7 +158,7 @@ func handler(w http.ResponseWriter, r *http.Request, logger log.Logger, exporter
149158
sc.RUnlock()
150159
logger = log.With(logger, "auth", authName, "target", target)
151160
registry := prometheus.NewRegistry()
152-
c := collector.New(r.Context(), target, authName, snmpContext, auth, nmodules, logger, exporterMetrics, *concurrency)
161+
c := collector.New(r.Context(), target, authName, snmpContext, auth, nmodules, logger, exporterMetrics, *concurrency, debug)
153162
registry.MustRegister(c)
154163
// Delegate http serving to Prometheus client library, which will call collector.Collect.
155164
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
@@ -200,7 +209,7 @@ func main() {
200209
*concurrency = 1
201210
}
202211

203-
level.Info(logger).Log("msg", "Starting snmp_exporter", "version", version.Info(), "concurrency", concurrency)
212+
level.Info(logger).Log("msg", "Starting snmp_exporter", "version", version.Info(), "concurrency", concurrency, "debug_snmp", debugSNMP)
204213
level.Info(logger).Log("build_context", version.BuildContext())
205214

206215
prometheus.MustRegister(versioncollector.NewCollector("snmp_exporter"))

scraper/gosnmp.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package scraper
1616
import (
1717
"context"
1818
"fmt"
19+
stdlog "log"
1920
"net"
2021
"strconv"
2122
"strings"
@@ -31,7 +32,7 @@ type GoSNMPWrapper struct {
3132
logger log.Logger
3233
}
3334

34-
func NewGoSNMP(logger log.Logger, target, srcAddress string) (*GoSNMPWrapper, error) {
35+
func NewGoSNMP(logger log.Logger, target, srcAddress string, debug bool) (*GoSNMPWrapper, error) {
3536
transport := "udp"
3637
if s := strings.SplitN(target, "://", 2); len(s) == 2 {
3738
transport = s[0]
@@ -52,6 +53,9 @@ func NewGoSNMP(logger log.Logger, target, srcAddress string) (*GoSNMPWrapper, er
5253
Port: port,
5354
LocalAddr: srcAddress,
5455
}
56+
if debug {
57+
g.Logger = gosnmp.NewLogger(stdlog.New(log.NewStdlibAdapter(level.Debug(logger)), "", 0))
58+
}
5559
return &GoSNMPWrapper{c: g, logger: logger}, nil
5660
}
5761

0 commit comments

Comments
 (0)