yanic/models/nodes.go

180 lines
4.0 KiB
Go
Raw Normal View History

package models
2015-12-29 13:05:47 +00:00
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"sync"
"time"
2016-03-12 02:23:12 +00:00
2016-03-15 22:26:30 +00:00
"github.com/FreifunkBremen/respond-collector/data"
2016-03-20 16:10:39 +00:00
"github.com/FreifunkBremen/respond-collector/jsontime"
2015-12-29 13:05:47 +00:00
)
2016-03-07 00:37:38 +00:00
// Node struct
2015-12-29 13:05:47 +00:00
type Node struct {
2016-03-20 16:10:39 +00:00
Firstseen jsontime.Time `json:"firstseen"`
Lastseen jsontime.Time `json:"lastseen"`
Flags *Flags `json:"flags,omitempty"`
//Statistics *MeshviewerStatistics `json:"statistics"`
Statistics *data.Statistics `json:"statistics"`
Nodeinfo *data.NodeInfo `json:"nodeinfo"`
Neighbours *data.Neighbours `json:"-"`
2015-12-29 13:05:47 +00:00
}
type Flags struct {
Online bool `json:"online"`
Gateway bool `json:"gateway"`
2016-03-07 00:37:38 +00:00
}
// Nodes struct: cache DB of Node's structs
2015-12-29 13:05:47 +00:00
type Nodes struct {
Version int `json:"version"`
2016-03-20 16:10:39 +00:00
Timestamp jsontime.Time `json:"timestamp"`
2016-03-07 00:37:38 +00:00
List map[string]*Node `json:"nodes"` // the current nodemap, indexed by node ID
2016-03-20 17:30:44 +00:00
config *Config
2015-12-29 13:05:47 +00:00
sync.Mutex
}
2016-03-20 17:30:44 +00:00
// NewNodes create Nodes structs
func NewNodes(config *Config) *Nodes {
2015-12-29 13:05:47 +00:00
nodes := &Nodes{
2016-03-20 17:30:44 +00:00
List: make(map[string]*Node),
config: config,
2015-12-29 13:05:47 +00:00
}
2016-03-20 17:30:44 +00:00
if config.Nodes.NodesPath != "" {
nodes.load()
}
go nodes.worker()
nodes.Version = 2
2015-12-29 13:05:47 +00:00
return nodes
}
2016-03-20 15:25:33 +00:00
// Update a Node
func (nodes *Nodes) Update(nodeID string, res *data.ResponseData) {
2016-03-20 16:10:39 +00:00
now := jsontime.Now()
2015-12-29 13:05:47 +00:00
nodes.Lock()
2016-02-25 20:24:54 +00:00
node, _ := nodes.List[nodeID]
2015-12-29 13:05:47 +00:00
if node == nil {
node = &Node{
2016-03-20 15:25:33 +00:00
Firstseen: now,
Flags: &Flags{
Online: true,
Gateway: false,
},
2015-12-29 13:05:47 +00:00
}
2016-02-25 20:24:54 +00:00
nodes.List[nodeID] = node
2015-12-29 13:05:47 +00:00
}
nodes.Unlock()
node.Lastseen = now
2016-05-23 12:20:58 +00:00
if node.Flags !=nil {
node.Flags.Online = true
}
2016-03-20 15:25:33 +00:00
// Update neighbours
if val := res.Neighbours; val != nil {
node.Neighbours = val
}
// Update nodeinfo
if val := res.NodeInfo; val != nil {
node.Nodeinfo = val
node.Flags.Gateway = val.VPN
2016-03-20 15:25:33 +00:00
}
// Update statistics
if val := res.Statistics; val != nil {
//node.Statistics = &MeshviewerStatistics{
node.Statistics = &data.Statistics{
NodeId: val.NodeId,
// Clients: 0,
Gateway: val.Gateway,
RootFsUsage: val.RootFsUsage,
LoadAverage: val.LoadAverage,
Memory: val.Memory,
Uptime: val.Uptime,
Idletime: val.Idletime,
Processes: val.Processes,
MeshVpn: val.MeshVpn,
Traffic: val.Traffic,
}
//node.Statistics.Clients = val.Clients.Total
node.Statistics.Clients = val.Clients
2016-03-20 15:25:33 +00:00
}
2015-12-29 13:05:47 +00:00
}
2016-03-20 17:30:44 +00:00
// Periodically saves the cached DB to json file
func (nodes *Nodes) worker() {
2016-05-21 11:11:29 +00:00
c := time.Tick(time.Second * time.Duration(nodes.config.Nodes.SaveInterval))
2015-12-29 13:05:47 +00:00
for range c {
2016-03-07 01:29:31 +00:00
log.Println("saving", len(nodes.List), "nodes")
2016-03-20 17:30:44 +00:00
nodes.Timestamp = jsontime.Now()
2016-03-07 01:29:31 +00:00
nodes.Lock()
2016-03-20 17:30:44 +00:00
// set node as offline (without statistics)
for _,node := range nodes.List {
2016-05-21 11:11:29 +00:00
if node.Statistics != nil && node.Lastseen.Unix()+int64(1000*nodes.config.Respondd.CollectInterval) < nodes.Timestamp.Unix() {
// node.Statistics.Clients = data.Clients{Wifi: 0, Wifi24: 0, Wifi5: 0, Total: 0}
// node.Statistics = &MeshviewerStatistics{
node.Statistics = &data.Statistics{
NodeId: node.Statistics.NodeId,
//Clients: 0,
Clients: data.Clients{Wifi: 0, Wifi24: 0, Wifi5: 0, Total: 0},
}
if node.Flags != nil {
node.Flags.Online = false
}
}
}
2016-03-20 17:30:44 +00:00
// serialize nodes
save(nodes, nodes.config.Nodes.NodesPath)
if path := nodes.config.Nodes.GraphsPath; path != "" {
2016-03-23 15:22:12 +00:00
save(nodes.BuildGraph(), path)
2016-03-07 11:05:53 +00:00
}
2016-03-20 17:30:44 +00:00
2016-03-07 01:29:31 +00:00
nodes.Unlock()
2015-12-29 13:05:47 +00:00
}
}
2016-03-20 17:30:44 +00:00
func (nodes *Nodes) load() {
path := nodes.config.Nodes.NodesPath
log.Println("loading", path)
if data, err := ioutil.ReadFile(path); err == nil {
if err := json.Unmarshal(data, nodes); err == nil {
log.Println("loaded", len(nodes.List), "nodes")
} else {
log.Println("failed to unmarshal nodes:", err)
}
} else {
log.Println("failed loading cached nodes:", err)
}
}
2016-03-07 01:29:31 +00:00
func save(input interface{}, outputFile string) {
data, err := json.Marshal(input)
if err != nil {
2016-02-19 10:30:42 +00:00
log.Panic(err)
2016-02-19 10:13:30 +00:00
}
2015-12-29 13:05:47 +00:00
tmpFile := outputFile + ".tmp"
2016-03-20 17:30:44 +00:00
if err := ioutil.WriteFile(tmpFile, data, 0644); err != nil {
2016-02-19 10:30:42 +00:00
log.Panic(err)
}
2016-03-20 17:30:44 +00:00
if err := os.Rename(tmpFile, outputFile); err != nil {
2016-02-19 10:30:42 +00:00
log.Panic(err)
}
2015-12-29 13:05:47 +00:00
}