From 7c1afd8e28117f95503c91968f7d3d4c715ed113 Mon Sep 17 00:00:00 2001 From: Douglas Szarmach Date: Tue, 14 Feb 2023 11:27:52 -0600 Subject: [PATCH] dont change config - just use the context name if passed --- 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{