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
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_after = "7d"

View File

@ -25,6 +25,7 @@ type Config struct {
GraphPath string
StatePath string
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
}
Influxdb struct {

View File

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

View File

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