Skip to content

Commit

Permalink
Add GoSNMP logger
Browse files Browse the repository at this point in the history
Add a debug logging to calls to GoSNMP.
* Enable with `--snmp.debug-packets`.

Signed-off-by: SuperQ <[email protected]>
  • Loading branch information
SuperQ committed Apr 12, 2024
1 parent 01f11ea commit bad0bb7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
7 changes: 4 additions & 3 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,11 @@ type Collector struct {
logger log.Logger
metrics Metrics
concurrency int
debugSNMP bool
}

func New(ctx context.Context, target, authName string, auth *config.Auth, modules []*NamedModule, logger log.Logger, metrics Metrics, conc int) *Collector {
return &Collector{ctx: ctx, target: target, authName: authName, auth: auth, modules: modules, logger: logger, metrics: metrics, concurrency: conc}
func New(ctx context.Context, target, authName string, auth *config.Auth, modules []*NamedModule, logger log.Logger, metrics Metrics, conc int, debugSNMP bool) *Collector {
return &Collector{ctx: ctx, target: target, authName: authName, auth: auth, modules: modules, logger: logger, metrics: metrics, concurrency: conc, debugSNMP: debugSNMP}
}

// Describe implements Prometheus.Collector.
Expand Down Expand Up @@ -419,7 +420,7 @@ func (c Collector) Collect(ch chan<- prometheus.Metric) {
go func(i int) {
defer wg.Done()
logger := log.With(c.logger, "worker", i)
client, err := scraper.NewGoSNMP(logger, c.target, *srcAddress)
client, err := scraper.NewGoSNMP(logger, c.target, *srcAddress, c.debugSNMP)
if err != nil {
level.Info(logger).Log("msg", err)
cancel()
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var (
configFile = kingpin.Flag("config.file", "Path to configuration file.").Default("snmp.yml").Strings()
dryRun = kingpin.Flag("dry-run", "Only verify configuration is valid and exit.").Default("false").Bool()
concurrency = kingpin.Flag("snmp.module-concurrency", "The number of modules to fetch concurrently per scrape").Default("1").Int()
debugSNMP = kingpin.Flag("snmp.debug-packets", "Include a full debug trace of SNMP packet traffics.").Default("false").Bool()
expandEnvVars = kingpin.Flag("config.expand-environment-variables", "Expand environment variables to source secrets").Default("false").Bool()
metricsPath = kingpin.Flag(
"web.telemetry-path",
Expand Down Expand Up @@ -141,7 +142,7 @@ func handler(w http.ResponseWriter, r *http.Request, logger log.Logger, exporter
sc.RUnlock()
logger = log.With(logger, "auth", authName, "target", target)
registry := prometheus.NewRegistry()
c := collector.New(r.Context(), target, authName, auth, nmodules, logger, exporterMetrics, *concurrency)
c := collector.New(r.Context(), target, authName, auth, nmodules, logger, exporterMetrics, *concurrency, *debugSNMP)
registry.MustRegister(c)
// Delegate http serving to Prometheus client library, which will call collector.Collect.
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
Expand Down Expand Up @@ -192,7 +193,7 @@ func main() {
*concurrency = 1
}

level.Info(logger).Log("msg", "Starting snmp_exporter", "version", version.Info(), "concurrency", concurrency)
level.Info(logger).Log("msg", "Starting snmp_exporter", "version", version.Info(), "concurrency", concurrency, "debug_snmp", debugSNMP)
level.Info(logger).Log("build_context", version.BuildContext())

prometheus.MustRegister(version.NewCollector("snmp_exporter"))
Expand Down
7 changes: 6 additions & 1 deletion scraper/gosnmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package scraper
import (
"context"
"fmt"
stdlog "log"
"net"
"strconv"
"strings"
Expand All @@ -31,7 +32,8 @@ type GoSNMPWrapper struct {
logger log.Logger
}

func NewGoSNMP(logger log.Logger, target, srcAddress string) (*GoSNMPWrapper, error) {
func NewGoSNMP(l log.Logger, target, srcAddress string, debug bool) (*GoSNMPWrapper, error) {
logger := log.With(l, "target", target, "source_address", srcAddress)
transport := "udp"
if s := strings.SplitN(target, "://", 2); len(s) == 2 {
transport = s[0]
Expand All @@ -52,6 +54,9 @@ func NewGoSNMP(logger log.Logger, target, srcAddress string) (*GoSNMPWrapper, er
Port: port,
LocalAddr: srcAddress,
}
if debug {
g.Logger = gosnmp.NewLogger(stdlog.New(log.NewStdlibAdapter(level.Debug(logger)), "", 0))
}
return &GoSNMPWrapper{c: g, logger: logger}, nil
}

Expand Down

0 comments on commit bad0bb7

Please sign in to comment.