From c9379db17ed3959fb3c265100107235842c26b5f Mon Sep 17 00:00:00 2001 From: PrometheusBot Date: Mon, 13 Feb 2023 09:59:08 +0100 Subject: [PATCH 1/6] Update common Prometheus files (#826) Signed-off-by: prombot Signed-off-by: Douglas --- Makefile.common | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile.common b/Makefile.common index 7642c448..e358db69 100644 --- a/Makefile.common +++ b/Makefile.common @@ -55,7 +55,7 @@ ifneq ($(shell which gotestsum),) endif endif -PROMU_VERSION ?= 0.13.0 +PROMU_VERSION ?= 0.14.0 PROMU_URL := https://github.com/prometheus/promu/releases/download/v$(PROMU_VERSION)/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM).tar.gz SKIP_GOLANGCI_LINT := From f7fc8a6957bc922d160c206dd14b200a78efbbab Mon Sep 17 00:00:00 2001 From: Douglas Szarmach Date: Tue, 14 Feb 2023 11:14:46 -0600 Subject: [PATCH 2/6] add snmp_context as optional param Signed-off-by: Douglas --- main.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index c249ffd6..2c4ee75d 100644 --- a/main.go +++ b/main.go @@ -84,6 +84,15 @@ func handler(w http.ResponseWriter, r *http.Request, logger log.Logger) { if moduleName == "" { moduleName = "if_mib" } + + + snmp_context := query.Get("snmp_context") + if len(query["snmp_context"]) > 1 { + http.Error(w, "'snmp_context' parameter must only be specified once", 400) + snmpRequestErrors.Inc() + return + } + sc.RLock() module, ok := (*(sc.C))[moduleName] sc.RUnlock() @@ -94,11 +103,11 @@ func handler(w http.ResponseWriter, r *http.Request, logger log.Logger) { } logger = log.With(logger, "module", moduleName, "target", target) - level.Debug(logger).Log("msg", "Starting scrape") + level.Debug(logger).Log("msg", "Starting scrape","snmp_context",snmp_context) start := time.Now() registry := prometheus.NewRegistry() - c := collector.New(r.Context(), target, module, logger) + c := collector.New(r.Context(), target, snmp_context, module, logger) registry.MustRegister(c) // Delegate http serving to Prometheus client library, which will call collector.Collect. h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{}) From 302e259479673c9b8a7fe61de574fa28fdc5c393 Mon Sep 17 00:00:00 2001 From: Douglas Szarmach Date: Tue, 14 Feb 2023 11:27:52 -0600 Subject: [PATCH 3/6] dont change config - just use the context name if passed Signed-off-by: Douglas --- collector/collector.go | 12 ++++++------ config/config.go | 9 ++++++--- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/collector/collector.go b/collector/collector.go index 0ffe7ece..04c6b5ad 100644 --- a/collector/collector.go +++ b/collector/collector.go @@ -115,7 +115,7 @@ type ScrapeResults struct { retries uint64 } -func ScrapeTarget(ctx context.Context, target string, config *config.Module, logger log.Logger) (ScrapeResults, error) { +func ScrapeTarget(ctx context.Context, target string, snmp_context string, config *config.Module, logger log.Logger) (ScrapeResults, error) { results := ScrapeResults{} // Set the options. snmp := gosnmp.GoSNMP{} @@ -125,7 +125,6 @@ func ScrapeTarget(ctx context.Context, target string, config *config.Module, log snmp.Timeout = config.WalkParams.Timeout snmp.UseUnconnectedUDPSocket = config.WalkParams.UseUnconnectedUDPSocket snmp.LocalAddr = *srcAddress - // Allow a set of OIDs that aren't in a strictly increasing order if config.WalkParams.AllowNonIncreasingOIDs { snmp.AppOpts = make(map[string]interface{}) @@ -158,7 +157,7 @@ func ScrapeTarget(ctx context.Context, target string, config *config.Module, log } // Configure auth. - config.WalkParams.ConfigureSNMP(&snmp) + config.WalkParams.ConfigureSNMP(&snmp,snmp_context) // Do the actual walk. err := snmp.Connect() @@ -261,12 +260,13 @@ func buildMetricTree(metrics []*config.Metric) *MetricNode { type collector struct { ctx context.Context target string + snmp_context string module *config.Module logger log.Logger } -func New(ctx context.Context, target string, module *config.Module, logger log.Logger) *collector { - return &collector{ctx: ctx, target: target, module: module, logger: logger} +func New(ctx context.Context, target string, snmp_context string, module *config.Module, logger log.Logger) *collector { + return &collector{ctx: ctx, target: target, snmp_context: snmp_context, module: module, logger: logger} } // Describe implements Prometheus.Collector. @@ -277,7 +277,7 @@ func (c collector) Describe(ch chan<- *prometheus.Desc) { // Collect implements Prometheus.Collector. func (c collector) Collect(ch chan<- prometheus.Metric) { start := time.Now() - results, err := ScrapeTarget(c.ctx, c.target, c.module, c.logger) + results, err := ScrapeTarget(c.ctx, c.target, c.snmp_context, c.module, c.logger) if err != nil { level.Info(c.logger).Log("msg", "Error scraping target", "err", err) ch <- prometheus.NewInvalidMetric(prometheus.NewDesc("snmp_error", "Error scraping target", nil, nil), err) diff --git a/config/config.go b/config/config.go index f8fd56d1..f96ef6d6 100644 --- a/config/config.go +++ b/config/config.go @@ -125,7 +125,7 @@ func (c *Module) UnmarshalYAML(unmarshal func(interface{}) error) error { } // ConfigureSNMP sets the various version and auth settings. -func (c WalkParams) ConfigureSNMP(g *gosnmp.GoSNMP) { +func (c WalkParams) ConfigureSNMP(g *gosnmp.GoSNMP,snmp_context string) { switch c.Version { case 1: g.Version = gosnmp.Version1 @@ -135,8 +135,11 @@ func (c WalkParams) ConfigureSNMP(g *gosnmp.GoSNMP) { g.Version = gosnmp.Version3 } g.Community = string(c.Auth.Community) - g.ContextName = c.Auth.ContextName - + if snmp_context == ""{ + g.ContextName = c.Auth.ContextName + } else{ + g.ContextName = snmp_context + } // v3 security settings. g.SecurityModel = gosnmp.UserSecurityModel usm := &gosnmp.UsmSecurityParameters{ From afe9f37774beea95743f4fa36b357d7998ef9341 Mon Sep 17 00:00:00 2001 From: Douglas Szarmach Date: Tue, 14 Feb 2023 11:37:07 -0600 Subject: [PATCH 4/6] restore blank line Signed-off-by: Douglas --- collector/collector.go | 1 + 1 file changed, 1 insertion(+) diff --git a/collector/collector.go b/collector/collector.go index 04c6b5ad..1a7b6f13 100644 --- a/collector/collector.go +++ b/collector/collector.go @@ -125,6 +125,7 @@ func ScrapeTarget(ctx context.Context, target string, snmp_context string, confi snmp.Timeout = config.WalkParams.Timeout snmp.UseUnconnectedUDPSocket = config.WalkParams.UseUnconnectedUDPSocket snmp.LocalAddr = *srcAddress + // Allow a set of OIDs that aren't in a strictly increasing order if config.WalkParams.AllowNonIncreasingOIDs { snmp.AppOpts = make(map[string]interface{}) From ef28cefde8dc78a409d577ff4db466599b55478f Mon Sep 17 00:00:00 2001 From: Douglas Szarmach Date: Tue, 14 Feb 2023 11:14:46 -0600 Subject: [PATCH 5/6] add snmp_context as optional param Signed-off-by: Douglas --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 2c4ee75d..964e2834 100644 --- a/main.go +++ b/main.go @@ -103,7 +103,7 @@ func handler(w http.ResponseWriter, r *http.Request, logger log.Logger) { } logger = log.With(logger, "module", moduleName, "target", target) - level.Debug(logger).Log("msg", "Starting scrape","snmp_context",snmp_context) + level.Debug(logger).Log("msg", "Starting scrape") start := time.Now() registry := prometheus.NewRegistry() From 8e8b6bb67a8afa3a2de2426306c8b6377111038b Mon Sep 17 00:00:00 2001 From: Douglas Date: Thu, 4 May 2023 10:43:33 -0500 Subject: [PATCH 6/6] fix formatting Signed-off-by: Douglas --- collector/collector.go | 12 ++++++------ config/config.go | 12 ++++++------ main.go | 13 ++++++------- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/collector/collector.go b/collector/collector.go index 1a7b6f13..4532e1ad 100644 --- a/collector/collector.go +++ b/collector/collector.go @@ -158,7 +158,7 @@ func ScrapeTarget(ctx context.Context, target string, snmp_context string, confi } // Configure auth. - config.WalkParams.ConfigureSNMP(&snmp,snmp_context) + config.WalkParams.ConfigureSNMP(&snmp, snmp_context) // Do the actual walk. err := snmp.Connect() @@ -259,11 +259,11 @@ func buildMetricTree(metrics []*config.Metric) *MetricNode { } type collector struct { - ctx context.Context - target string - snmp_context string - module *config.Module - logger log.Logger + ctx context.Context + target string + snmp_context string + module *config.Module + logger log.Logger } func New(ctx context.Context, target string, snmp_context string, module *config.Module, logger log.Logger) *collector { diff --git a/config/config.go b/config/config.go index f96ef6d6..efcba266 100644 --- a/config/config.go +++ b/config/config.go @@ -125,7 +125,7 @@ func (c *Module) UnmarshalYAML(unmarshal func(interface{}) error) error { } // ConfigureSNMP sets the various version and auth settings. -func (c WalkParams) ConfigureSNMP(g *gosnmp.GoSNMP,snmp_context string) { +func (c WalkParams) ConfigureSNMP(g *gosnmp.GoSNMP, snmp_context string) { switch c.Version { case 1: g.Version = gosnmp.Version1 @@ -135,11 +135,11 @@ func (c WalkParams) ConfigureSNMP(g *gosnmp.GoSNMP,snmp_context string) { g.Version = gosnmp.Version3 } g.Community = string(c.Auth.Community) - if snmp_context == ""{ - g.ContextName = c.Auth.ContextName - } else{ - g.ContextName = snmp_context - } + if snmp_context == "" { + g.ContextName = c.Auth.ContextName + } else { + g.ContextName = snmp_context + } // v3 security settings. g.SecurityModel = gosnmp.UserSecurityModel usm := &gosnmp.UsmSecurityParameters{ diff --git a/main.go b/main.go index 964e2834..e9808239 100644 --- a/main.go +++ b/main.go @@ -85,13 +85,12 @@ func handler(w http.ResponseWriter, r *http.Request, logger log.Logger) { moduleName = "if_mib" } - - snmp_context := query.Get("snmp_context") - if len(query["snmp_context"]) > 1 { - http.Error(w, "'snmp_context' parameter must only be specified once", 400) - snmpRequestErrors.Inc() - return - } + snmp_context := query.Get("snmp_context") + if len(query["snmp_context"]) > 1 { + http.Error(w, "'snmp_context' parameter must only be specified once", 400) + snmpRequestErrors.Inc() + return + } sc.RLock() module, ok := (*(sc.C))[moduleName]