0
0
Fork 0
mirror of https://github.com/netdata/netdata.git synced 2025-04-23 04:50:22 +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"
"fmt"
"log/slog"
"sync/atomic"
"time"
"github.com/gohugoio/hashstructure"
@ -55,7 +54,7 @@ func NewDiscoverer(cfg Config) (*Discoverer, error) {
status: newDiscoveryStatus(),
}
if cfg.RescanInterval > 0 {
if cfg.RescanInterval >= 0 {
d.rescanInterval = cfg.RescanInterval.Duration()
}
if cfg.Timeout > 0 {
@ -64,7 +63,7 @@ func NewDiscoverer(cfg Config) (*Discoverer, error) {
if cfg.ParallelScansPerNetwork > 0 {
d.parallelScansPerNetwork = cfg.ParallelScansPerNetwork
}
if cfg.DeviceCacheTTL > 0 {
if cfg.DeviceCacheTTL >= 0 {
d.deviceCacheTTL = cfg.DeviceCacheTTL.Duration()
}
@ -90,7 +89,6 @@ type (
firstDiscovery bool
status *discoveryStatus
statusUpdated atomic.Bool
}
subnet struct {
str string
@ -148,7 +146,7 @@ func (d *Discoverer) discoverNetworks(ctx context.Context, in chan<- []model.Tar
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
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)
// 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 {
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)
if d.deviceCacheTTL == 0 {
d.Infof("device '%s': found in cache (sysName: '%s', network: '%s', cache never expires)",
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)
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)
}
d.status.del(sub, ip)
d.statusUpdated.Store(dev != nil)
d.status.updated.Store(dev != nil)
return
}
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.statusUpdated.Store(true)
d.status.updated.Store(true)
tg := newTarget(ip, sub.credential, *si)
tgg.addTarget(tg)
}

View file

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

View file

@ -12,12 +12,14 @@ discover:
- discoverer: snmp
snmp:
## how often to scan the networks for devices (default: 30m)
## Set to 0 to perform a single discovery scan and exit
#rescan_interval: "30m"
## the maximum time to wait for SNMP device responses (default: 1s)
#timeout: "1s"
## 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"
## how many IPs to scan concurrently within each subnet (default: 32)