From 209741790920f5ea3fb99beaa897055ce2ac4fe9 Mon Sep 17 00:00:00 2001 From: Kakuya Ando Date: Mon, 12 Feb 2024 14:24:15 +0900 Subject: [PATCH] Remove version retrieval from scraper and update to fetch from auth information Signed-off-by: Kakuya Ando --- collector/collector.go | 10 +++++----- scraper/gosnmp.go | 4 ---- scraper/scraper.go | 1 - 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/collector/collector.go b/collector/collector.go index 669bcb41..bef63a1c 100644 --- a/collector/collector.go +++ b/collector/collector.go @@ -81,7 +81,7 @@ type ScrapeResults struct { pdus []gosnmp.SnmpPDU } -func ScrapeTarget(snmp scraper.SNMPScraper, target string, module *config.Module, logger log.Logger, metrics Metrics) (ScrapeResults, error) { +func ScrapeTarget(snmp scraper.SNMPScraper, target string, auth *config.Auth, module *config.Module, logger log.Logger, metrics Metrics) (ScrapeResults, error) { results := ScrapeResults{} // Evaluate rules. newGet := module.Get @@ -109,11 +109,11 @@ func ScrapeTarget(snmp scraper.SNMPScraper, target string, module *config.Module newGet = newCfg } - version := snmp.GetVersion() + version := auth.Version getOids := newGet maxOids := int(module.WalkParams.MaxRepetitions) // Max Repetition can be 0, maxOids cannot. SNMPv1 can only report one OID error per call. - if maxOids == 0 || version == gosnmp.Version1 { + if maxOids == 0 || version == 1 { maxOids = 1 } for len(getOids) > 0 { @@ -127,7 +127,7 @@ func ScrapeTarget(snmp scraper.SNMPScraper, target string, module *config.Module return results, err } // SNMPv1 will return packet error for unsupported OIDs. - if packet.Error == gosnmp.NoSuchName && version == gosnmp.Version1 { + if packet.Error == gosnmp.NoSuchName && version == 1 { level.Debug(logger).Log("msg", "OID not supported by target", "oids", getOids[0]) getOids = getOids[oids:] continue @@ -346,7 +346,7 @@ func (c Collector) collect(ch chan<- prometheus.Metric, logger log.Logger, clien ) start := time.Now() moduleLabel := prometheus.Labels{"module": module.name} - results, err := ScrapeTarget(client, c.target, module.Module, logger, c.metrics) + results, err := ScrapeTarget(client, c.target, c.auth, module.Module, logger, c.metrics) if err != nil { level.Info(logger).Log("msg", "Error scraping target", "err", err) ch <- prometheus.NewInvalidMetric(prometheus.NewDesc("snmp_error", "Error scraping target", nil, moduleLabel), err) diff --git a/scraper/gosnmp.go b/scraper/gosnmp.go index 9ceb4b40..859bad5f 100644 --- a/scraper/gosnmp.go +++ b/scraper/gosnmp.go @@ -48,10 +48,6 @@ func (g *GoSNMPWrapper) SetOptions(fns ...func(*gosnmp.GoSNMP)) { } } -func (g *GoSNMPWrapper) GetVersion() gosnmp.SnmpVersion { - return g.c.Version -} - func (g *GoSNMPWrapper) Connect() error { st := time.Now() err := g.c.Connect() diff --git a/scraper/scraper.go b/scraper/scraper.go index 17af653c..eee05ecd 100644 --- a/scraper/scraper.go +++ b/scraper/scraper.go @@ -9,6 +9,5 @@ type SNMPScraper interface { WalkAll(string) ([]gosnmp.SnmpPDU, error) Connect() error Close() error - GetVersion() gosnmp.SnmpVersion SetOptions(...func(*gosnmp.GoSNMP)) }