Add configuration option offline_after

This commit is contained in:
Julian Kornberger 2017-01-29 21:04:10 +01:00
parent 5062b71318
commit 82c09ad952
4 changed files with 14 additions and 9 deletions

View File

@ -20,6 +20,9 @@ state_path = "/var/lib/collector/state.json"
# Export nodes and graph periodically # Export nodes and graph periodically
save_interval = "5s" save_interval = "5s"
# Set node to offline if not seen within this period
offline_after = "10m"
# Prune offline nodes after a time of inactivity # Prune offline nodes after a time of inactivity
prune_after = "7d" prune_after = "7d"

View File

@ -25,6 +25,7 @@ type Config struct {
GraphPath string GraphPath string
StatePath string StatePath string
SaveInterval Duration // Save nodes periodically SaveInterval Duration // Save nodes periodically
OfflineAfter Duration // Set node to offline if not seen within this period
PruneAfter Duration // Remove nodes after n days of inactivity PruneAfter Duration // Remove nodes after n days of inactivity
} }
Influxdb struct { Influxdb struct {

View File

@ -146,25 +146,25 @@ func (nodes *Nodes) worker() {
func (nodes *Nodes) expire() { func (nodes *Nodes) expire() {
now := jsontime.Now() now := jsontime.Now()
// Nodes last seen before expireTime will be removed // Nodes last seen before expireAfter will be removed
pruneAfter := nodes.config.Nodes.PruneAfter.Duration prunePeriod := nodes.config.Nodes.PruneAfter.Duration
if pruneAfter == 0 { if prunePeriod == 0 {
pruneAfter = time.Hour * 24 * 7 // our default prunePeriod = time.Hour * 24 * 7 // our default
} }
expireTime := now.Add(-pruneAfter) pruneAfter := now.Add(-prunePeriod)
// Nodes last seen before offlineTime are changed to 'offline' // Nodes last seen within OfflineAfter are changed to 'offline'
offlineTime := now.Add(-time.Minute * 10) offlineAfter := now.Add(-nodes.config.Nodes.OfflineAfter.Duration)
// Locking foo // Locking foo
nodes.Lock() nodes.Lock()
defer nodes.Unlock() defer nodes.Unlock()
for id, node := range nodes.List { for id, node := range nodes.List {
if node.Lastseen.Before(expireTime) { if node.Lastseen.Before(pruneAfter) {
// expire // expire
delete(nodes.List, id) delete(nodes.List, id)
} else if node.Lastseen.Before(offlineTime) { } else if node.Lastseen.Before(offlineAfter) {
// set to offline // set to offline
node.Flags.Online = false node.Flags.Online = false
} }

View File

@ -13,6 +13,7 @@ import (
func TestExpire(t *testing.T) { func TestExpire(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
config := &Config{} config := &Config{}
config.Nodes.OfflineAfter.Duration = time.Minute * 10
config.Nodes.PruneAfter.Duration = time.Hour * 24 * 6 config.Nodes.PruneAfter.Duration = time.Hour * 24 * 6
nodes := &Nodes{ nodes := &Nodes{
config: config, config: config,