From dd55a8999026000fbe63a5c8a5ed9d4391262614 Mon Sep 17 00:00:00 2001 From: Ian Pye Date: Mon, 6 May 2024 14:02:11 -0700 Subject: [PATCH] Logging poll interval for snmp --- pkg/inputs/snmp/metadata/poll.go | 9 ++++++- pkg/inputs/snmp/metrics/poll.go | 40 ++++---------------------------- pkg/inputs/snmp/util/util.go | 17 ++++++++++++++ 3 files changed, 29 insertions(+), 37 deletions(-) diff --git a/pkg/inputs/snmp/metadata/poll.go b/pkg/inputs/snmp/metadata/poll.go index ee0c9a34..65533449 100644 --- a/pkg/inputs/snmp/metadata/poll.go +++ b/pkg/inputs/snmp/metadata/poll.go @@ -2,6 +2,7 @@ package metadata import ( "context" + "fmt" "regexp" "strconv" "strings" @@ -12,6 +13,7 @@ import ( "github.com/kentik/ktranslate/pkg/eggs/logger" "github.com/kentik/ktranslate/pkg/inputs/snmp/mibs" + "github.com/kentik/ktranslate/pkg/inputs/snmp/util" "github.com/kentik/ktranslate/pkg/kt" "github.com/kentik/ktranslate/pkg/util/tick" ) @@ -29,6 +31,7 @@ type Poller struct { deviceMetadataMibs map[string]*kt.Mib interfaceMetadataMibs map[string]*kt.Mib matchAttr map[string]*regexp.Regexp + counterTimeSec string } const ( @@ -87,6 +90,8 @@ func NewPoller(server *gosnmp.GoSNMP, gconf *kt.SnmpGlobalConfig, conf *kt.SnmpD log.Infof("Added %d Match Attribute(s)", len(attrMap)) } + counterTimeSec := util.GetPollRate(gconf, conf, log) + return &Poller{ gconf: gconf, conf: conf, @@ -100,6 +105,7 @@ func NewPoller(server *gosnmp.GoSNMP, gconf *kt.SnmpGlobalConfig, conf *kt.SnmpD deviceMetadataMibs: deviceMetadataMibs, interfaceMetadataMibs: interfaceMetadataMibs, matchAttr: attrMap, + counterTimeSec: fmt.Sprintf("%v", time.Duration(counterTimeSec)*time.Second), } } @@ -256,7 +262,8 @@ func (p *Poller) toFlows(dd *kt.DeviceData) ([]*kt.JCHF, error) { // Also any device level tags cs := map[string]string{ // Set log level as a SysLogLevel key here so that Uptime metrics get it only. - "SysLogLevel": p.log.GetLogger().GetUnderlyingLogger().GetLogLevel(), + "SysLogLevel": p.log.GetLogger().GetUnderlyingLogger().GetLogLevel(), + "SysPollInterval": p.counterTimeSec, } p.conf.SetUserTags(cs) for k, v := range cs { diff --git a/pkg/inputs/snmp/metrics/poll.go b/pkg/inputs/snmp/metrics/poll.go index c8401b6d..5e7d32a3 100644 --- a/pkg/inputs/snmp/metrics/poll.go +++ b/pkg/inputs/snmp/metrics/poll.go @@ -9,6 +9,7 @@ import ( "github.com/kentik/ktranslate/pkg/eggs/logger" "github.com/kentik/ktranslate/pkg/inputs/snmp/mibs" "github.com/kentik/ktranslate/pkg/inputs/snmp/ping" + "github.com/kentik/ktranslate/pkg/inputs/snmp/util" extension "github.com/kentik/ktranslate/pkg/inputs/snmp/x" "github.com/kentik/ktranslate/pkg/kt" "github.com/kentik/ktranslate/pkg/util/tick" @@ -35,18 +36,7 @@ type Poller struct { } func NewPoller(server *gosnmp.GoSNMP, gconf *kt.SnmpGlobalConfig, conf *kt.SnmpDeviceConfig, jchfChan chan []*kt.JCHF, metrics *kt.SnmpDeviceMetric, profile *mibs.Profile, log logger.ContextL, logchan chan string) *Poller { - // Default poll rate is 5 min. This is what a lot of SNMP billing is on. - counterTimeSec := 5 * 60 - if conf != nil && conf.PollTimeSec > 0 { - counterTimeSec = conf.PollTimeSec - } else if gconf != nil && gconf.PollTimeSec > 0 { - counterTimeSec = gconf.PollTimeSec - } - // Lastly, enforece a min polling interval. - if counterTimeSec < 30 { - log.Warnf("%d poll time is below min of 30. Raising to 30 seconds", counterTimeSec) - counterTimeSec = 30 - } + counterTimeSec := util.GetPollRate(gconf, conf, log) jitterTimeSec := 15 // This is how long to spead the polling load out across. if gconf.JitterTimeSec > 0 { @@ -96,18 +86,7 @@ func NewPoller(server *gosnmp.GoSNMP, gconf *kt.SnmpGlobalConfig, conf *kt.SnmpD } func NewPollerForPing(gconf *kt.SnmpGlobalConfig, conf *kt.SnmpDeviceConfig, jchfChan chan []*kt.JCHF, metrics *kt.SnmpDeviceMetric, profile *mibs.Profile, log logger.ContextL) *Poller { - // Default poll rate is 5 min. This is what a lot of SNMP billing is on. - counterTimeSec := 5 * 60 - if conf != nil && conf.PollTimeSec > 0 { - counterTimeSec = conf.PollTimeSec - } else if gconf != nil && gconf.PollTimeSec > 0 { - counterTimeSec = gconf.PollTimeSec - } - // Lastly, enforece a min polling interval. - if counterTimeSec < 30 { - log.Warnf("%d poll time is below min of 30. Raising to 30 seconds", counterTimeSec) - counterTimeSec = 30 - } + counterTimeSec := util.GetPollRate(gconf, conf, log) jitterTimeSec := 15 // This is how long to spead the polling load out across. if gconf.JitterTimeSec > 0 { @@ -145,18 +124,7 @@ func NewPollerForPing(gconf *kt.SnmpGlobalConfig, conf *kt.SnmpDeviceConfig, jch } func NewPollerForExtention(gconf *kt.SnmpGlobalConfig, conf *kt.SnmpDeviceConfig, jchfChan chan []*kt.JCHF, metrics *kt.SnmpDeviceMetric, profile *mibs.Profile, log logger.ContextL, logchan chan string) *Poller { - // Default poll rate is 5 min. This is what a lot of SNMP billing is on. - counterTimeSec := 5 * 60 - if conf != nil && conf.PollTimeSec > 0 { - counterTimeSec = conf.PollTimeSec - } else if gconf != nil && gconf.PollTimeSec > 0 { - counterTimeSec = gconf.PollTimeSec - } - // Lastly, enforece a min polling interval. - if counterTimeSec < 30 { - log.Warnf("%d poll time is below min of 30. Raising to 30 seconds", counterTimeSec) - counterTimeSec = 30 - } + counterTimeSec := util.GetPollRate(gconf, conf, log) jitterTimeSec := 15 // This is how long to spead the polling load out across. if gconf.JitterTimeSec > 0 { diff --git a/pkg/inputs/snmp/util/util.go b/pkg/inputs/snmp/util/util.go index bf137136..cfde9bdf 100644 --- a/pkg/inputs/snmp/util/util.go +++ b/pkg/inputs/snmp/util/util.go @@ -260,6 +260,23 @@ func DoWalk(device string, baseOid string, format string, conf *kt.SnmpConfig, c return fmt.Errorf("ok") } +func GetPollRate(gconf *kt.SnmpGlobalConfig, conf *kt.SnmpDeviceConfig, log logger.ContextL) int { + // Default poll rate is 5 min. This is what a lot of SNMP billing is on. + counterTimeSec := 5 * 60 + if conf != nil && conf.PollTimeSec > 0 { + counterTimeSec = conf.PollTimeSec + } else if gconf != nil && gconf.PollTimeSec > 0 { + counterTimeSec = gconf.PollTimeSec + } + // Lastly, enforece a min polling interval. + if counterTimeSec < 30 { + log.Warnf("%d poll time is below min of 30. Raising to 30 seconds", counterTimeSec) + counterTimeSec = 30 + } + + return counterTimeSec +} + // Handle the case of wierd ints encoded as byte arrays. func GetFromConv(pdu gosnmp.SnmpPDU, conv string, log logger.ContextL) (int64, string, map[string]string) { defer func() {