Expire nodes after n days of inactivity

closes #4
This commit is contained in:
Julian Kornberger 2016-10-08 10:50:41 +02:00
parent 54c39b7d67
commit cbe2577aa9
4 changed files with 60 additions and 21 deletions

View File

@ -2,7 +2,9 @@
respondd: respondd:
enable: true enable: true
interface: eth0 interface: eth0
collectinterval: 15
# Collected data every n seconds
collectinterval: 60
webserver: webserver:
enable: false enable: false
port: 8080 port: 8080
@ -16,9 +18,15 @@ nodes:
nodes_path: /var/www/html/meshviewer/data/nodes_all.json nodes_path: /var/www/html/meshviewer/data/nodes_all.json
nodesmini_path: /var/www/html/meshviewer/data/nodes.json nodesmini_path: /var/www/html/meshviewer/data/nodes.json
graphs_path: /var/www/html/meshviewer/data/graph.json graphs_path: /var/www/html/meshviewer/data/graph.json
saveinterval: 5
aliases_enable: false aliases_enable: false
aliases_path: /var/www/html/meshviewer/data/aliases.json aliases_path: /var/www/html/meshviewer/data/aliases.json
# Export nodes and graph every n seconds
saveinterval: 5
# Expire offline nodes after n days
max_age: 7
influxdb: influxdb:
enable: false enable: false
host: http://localhost:8086 host: http://localhost:8086

View File

@ -29,7 +29,7 @@ func (t *Time) UnmarshalJSON(data []byte) (err error) {
} }
return return
} }
func (t Time) GetTime() time.Time{ func (t Time) GetTime() time.Time {
return t.time return t.time
} }
func (t Time) Unix() int64 { func (t Time) Unix() int64 {
@ -46,3 +46,7 @@ func (t Time) Add(d time.Duration) Time {
func (t Time) After(u Time) bool { func (t Time) After(u Time) bool {
return t.time.After(u.GetTime()) return t.time.After(u.GetTime())
} }
func (t Time) Before(u Time) bool {
return t.time.Before(u.GetTime())
}

View File

@ -31,7 +31,8 @@ type Config struct {
NodesMiniPath string `yaml:"nodesmini_path"` NodesMiniPath string `yaml:"nodesmini_path"`
GraphsPath string `yaml:"graphs_path"` GraphsPath string `yaml:"graphs_path"`
AliasesPath string `yaml:"aliases_path"` AliasesPath string `yaml:"aliases_path"`
SaveInterval int `yaml:"saveinterval"` SaveInterval int `yaml:"saveinterval"` // Save nodes every n seconds
MaxAge int `yaml:"max_age"` // Remove nodes after n days of inactivity
} `yaml:"nodes"` } `yaml:"nodes"`
Influxdb struct { Influxdb struct {
Enable bool `yaml:"enable"` Enable bool `yaml:"enable"`

View File

@ -145,18 +145,47 @@ func (nodes *Nodes) worker() {
c := time.Tick(time.Second * time.Duration(nodes.config.Nodes.SaveInterval)) c := time.Tick(time.Second * time.Duration(nodes.config.Nodes.SaveInterval))
for range c { for range c {
log.Println("saving", len(nodes.List), "nodes") nodes.expire()
nodes.save()
}
}
// Expires nodes and set nodes offline
func (nodes *Nodes) expire() {
nodes.Timestamp = jsontime.Now() nodes.Timestamp = jsontime.Now()
// Nodes last seen before expireTime will be removed
maxAge := nodes.config.Nodes.MaxAge
if maxAge <= 0 {
maxAge = 7 // our default
}
expireTime := nodes.Timestamp.Add(-time.Duration(maxAge) * time.Hour * 24)
// Nodes last seen before offlineTime are changed to 'offline'
offlineTime := nodes.Timestamp.Add(-time.Minute * 10)
// Locking foo
nodes.Lock() nodes.Lock()
// defer nodes.Unlock()
// set node as offline (without statistics)
for _, node := range nodes.List { for id, node := range nodes.List {
if node.Statistics != nil && nodes.Timestamp.After(node.Lastseen.Add(time.Second*time.Duration(10*nodes.config.Respondd.CollectInterval))) { if node.Lastseen.Before(expireTime) {
// expire
delete(nodes.List, id)
} else if node.Lastseen.Before(offlineTime) {
// set to offline
if node.Flags != nil { if node.Flags != nil {
node.Flags.Online = false node.Flags.Online = false
} }
} }
} }
}
func (nodes *Nodes) save() {
// Locking foo
nodes.RLock()
defer nodes.RUnlock()
// serialize nodes // serialize nodes
save(nodes, nodes.config.Nodes.NodesPath) save(nodes, nodes.config.Nodes.NodesPath)
save(nodes.GetNodesMini(), nodes.config.Nodes.NodesMiniPath) save(nodes.GetNodesMini(), nodes.config.Nodes.NodesMiniPath)
@ -164,9 +193,6 @@ func (nodes *Nodes) worker() {
if path := nodes.config.Nodes.GraphsPath; path != "" { if path := nodes.config.Nodes.GraphsPath; path != "" {
save(nodes.BuildGraph(), path) save(nodes.BuildGraph(), path)
} }
nodes.Unlock()
}
} }
// Returns global statistics for InfluxDB // Returns global statistics for InfluxDB