0
0
Fork 0
mirror of https://github.com/netdata/netdata.git synced 2025-05-10 03:40:55 +00:00

improvement(go.d/sd/snmp): support device cache ttl 0 ()

* improvement(go.d/sd/snmp): support device cache ttl 0

* update config options description
This commit is contained in:
Ilya Mashchenko 2025-03-03 20:59:44 +02:00 committed by GitHub
parent 9692d1bb10
commit a3e54253a1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 18 additions and 11 deletions
src/go/plugin/go.d
agent/discovery/sd/discoverer/snmpsd
config/go.d/sd

View file

@ -6,7 +6,6 @@ import (
"context" "context"
"fmt" "fmt"
"log/slog" "log/slog"
"sync/atomic"
"time" "time"
"github.com/gohugoio/hashstructure" "github.com/gohugoio/hashstructure"
@ -55,7 +54,7 @@ func NewDiscoverer(cfg Config) (*Discoverer, error) {
status: newDiscoveryStatus(), status: newDiscoveryStatus(),
} }
if cfg.RescanInterval > 0 { if cfg.RescanInterval >= 0 {
d.rescanInterval = cfg.RescanInterval.Duration() d.rescanInterval = cfg.RescanInterval.Duration()
} }
if cfg.Timeout > 0 { if cfg.Timeout > 0 {
@ -64,7 +63,7 @@ func NewDiscoverer(cfg Config) (*Discoverer, error) {
if cfg.ParallelScansPerNetwork > 0 { if cfg.ParallelScansPerNetwork > 0 {
d.parallelScansPerNetwork = cfg.ParallelScansPerNetwork d.parallelScansPerNetwork = cfg.ParallelScansPerNetwork
} }
if cfg.DeviceCacheTTL > 0 { if cfg.DeviceCacheTTL >= 0 {
d.deviceCacheTTL = cfg.DeviceCacheTTL.Duration() d.deviceCacheTTL = cfg.DeviceCacheTTL.Duration()
} }
@ -90,7 +89,6 @@ type (
firstDiscovery bool firstDiscovery bool
status *discoveryStatus status *discoveryStatus
statusUpdated atomic.Bool
} }
subnet struct { subnet struct {
str string str string
@ -148,7 +146,7 @@ func (d *Discoverer) discoverNetworks(ctx context.Context, in chan<- []model.Tar
d.status.LastDiscoveryTime = now d.status.LastDiscoveryTime = now
} }
if d.statusUpdated.Swap(false) || d.status.ConfigHash != d.cfgHash { if d.status.updated.Swap(false) || d.status.ConfigHash != d.cfgHash {
d.status.ConfigHash = d.cfgHash d.status.ConfigHash = d.cfgHash
filepersister.Save(statusFileName(), d.status) filepersister.Save(statusFileName(), d.status)
} }
@ -199,11 +197,16 @@ func (d *Discoverer) probeIPAddress(ctx context.Context, sub subnet, ip string,
dev := d.status.get(sub, ip) dev := d.status.get(sub, ip)
// Use the cached device if available and not expired // Use the cached device if available and not expired
if dev != nil && now.Before(dev.DiscoverTime.Add(d.deviceCacheTTL)) { if dev != nil && (d.deviceCacheTTL == 0 || now.Before(dev.DiscoverTime.Add(d.deviceCacheTTL))) {
if d.firstDiscovery { if d.firstDiscovery {
untilProbe := dev.DiscoverTime.Add(d.deviceCacheTTL).Sub(now).Round(time.Second) if d.deviceCacheTTL == 0 {
d.Infof("device '%s': found in cache (sysName: '%s', network: '%s', next probe in %s)", d.Infof("device '%s': found in cache (sysName: '%s', network: '%s', cache never expires)",
ip, dev.SysInfo.Name, subKey(sub), untilProbe) ip, dev.SysInfo.Name, subKey(sub))
} else {
untilProbe := dev.DiscoverTime.Add(d.deviceCacheTTL).Sub(now).Round(time.Second)
d.Infof("device '%s': found in cache (sysName: '%s', network: '%s', next probe in %s)",
ip, dev.SysInfo.Name, subKey(sub), untilProbe)
}
} }
tg := newTarget(ip, sub.credential, dev.SysInfo) tg := newTarget(ip, sub.credential, dev.SysInfo)
tgg.addTarget(tg) tgg.addTarget(tg)
@ -221,13 +224,13 @@ func (d *Discoverer) probeIPAddress(ctx context.Context, sub subnet, ip string,
ip, dev.SysInfo.Name, subKey(sub), err) ip, dev.SysInfo.Name, subKey(sub), err)
} }
d.status.del(sub, ip) d.status.del(sub, ip)
d.statusUpdated.Store(dev != nil) d.status.updated.Store(dev != nil)
return return
} }
d.Infof("device '%s': successfully discovered (sysName: '%s', network: '%s')", ip, si.Name, subKey(sub)) d.Infof("device '%s': successfully discovered (sysName: '%s', network: '%s')", ip, si.Name, subKey(sub))
d.status.put(sub, ip, &discoveredDevice{DiscoverTime: now, SysInfo: *si}) d.status.put(sub, ip, &discoveredDevice{DiscoverTime: now, SysInfo: *si})
d.statusUpdated.Store(true) d.status.updated.Store(true)
tg := newTarget(ip, sub.credential, *si) tg := newTarget(ip, sub.credential, *si)
tgg.addTarget(tg) tgg.addTarget(tg)
} }

View file

@ -8,6 +8,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"sync" "sync"
"sync/atomic"
"time" "time"
) )
@ -50,6 +51,7 @@ func newDiscoveryStatus() *discoveryStatus {
type ( type (
discoveryStatus struct { discoveryStatus struct {
updated atomic.Bool
mux sync.RWMutex mux sync.RWMutex
Networks map[string]map[string]*discoveredDevice `json:"networks"` Networks map[string]map[string]*discoveredDevice `json:"networks"`
LastDiscoveryTime time.Time `json:"last_discovery_time"` LastDiscoveryTime time.Time `json:"last_discovery_time"`

View file

@ -12,12 +12,14 @@ discover:
- discoverer: snmp - discoverer: snmp
snmp: snmp:
## how often to scan the networks for devices (default: 30m) ## how often to scan the networks for devices (default: 30m)
## Set to 0 to perform a single discovery scan and exit
#rescan_interval: "30m" #rescan_interval: "30m"
## the maximum time to wait for SNMP device responses (default: 1s) ## the maximum time to wait for SNMP device responses (default: 1s)
#timeout: "1s" #timeout: "1s"
## How long to trust cached discovery results before requiring a new probe (default: 12h) ## How long to trust cached discovery results before requiring a new probe (default: 12h)
## Set to 0 to never expire cached results (devices will never be re-probed once discovered)
#device_cache_ttl: "12h" #device_cache_ttl: "12h"
## how many IPs to scan concurrently within each subnet (default: 32) ## how many IPs to scan concurrently within each subnet (default: 32)