Use data package from node-informat
This commit is contained in:
parent
c416386683
commit
646f8c5a9a
43
main.go
43
main.go
@ -8,11 +8,10 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"reflect"
|
|
||||||
"strings"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/ffdo/node-informant/gluon-collector/data"
|
||||||
"github.com/monitormap/micro-daemon/models"
|
"github.com/monitormap/micro-daemon/models"
|
||||||
"github.com/monitormap/micro-daemon/respond"
|
"github.com/monitormap/micro-daemon/respond"
|
||||||
"github.com/monitormap/micro-daemon/websocketserver"
|
"github.com/monitormap/micro-daemon/websocketserver"
|
||||||
@ -51,27 +50,29 @@ func main() {
|
|||||||
go nodes.Saver(outputNodesFile, saveInterval)
|
go nodes.Saver(outputNodesFile, saveInterval)
|
||||||
go aliases.Saver(outputAliasesFile, saveInterval)
|
go aliases.Saver(outputAliasesFile, saveInterval)
|
||||||
respondDaemon = respond.NewDaemon(func(coll *respond.Collector, res *respond.Response) {
|
respondDaemon = respond.NewDaemon(func(coll *respond.Collector, res *respond.Response) {
|
||||||
var result map[string]interface{}
|
|
||||||
json.Unmarshal(res.Raw, &result)
|
|
||||||
|
|
||||||
nodeID, _ := result["node_id"].(string)
|
switch coll.CollectType {
|
||||||
|
case "neighbours":
|
||||||
if nodeID == "" {
|
result := &data.NeighbourStruct{}
|
||||||
log.Println("unable to parse node_id")
|
if json.Unmarshal(res.Raw, result) == nil {
|
||||||
return
|
node := nodes.Get(result.NodeId)
|
||||||
|
node.Neighbours = result
|
||||||
|
}
|
||||||
|
case "nodeinfo":
|
||||||
|
result := &data.NodeInfo{}
|
||||||
|
if json.Unmarshal(res.Raw, result) == nil {
|
||||||
|
node := nodes.Get(result.NodeId)
|
||||||
|
node.Nodeinfo = result
|
||||||
|
}
|
||||||
|
case "statistics":
|
||||||
|
result := &data.StatisticsStruct{}
|
||||||
|
if json.Unmarshal(res.Raw, result) == nil {
|
||||||
|
node := nodes.Get(result.NodeId)
|
||||||
|
node.Statistics = result
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
log.Println("unknown CollectType:", coll.CollectType)
|
||||||
}
|
}
|
||||||
|
|
||||||
node := nodes.Get(nodeID)
|
|
||||||
|
|
||||||
// Set result
|
|
||||||
elem := reflect.ValueOf(node).Elem()
|
|
||||||
field := elem.FieldByName(strings.Title(coll.CollectType))
|
|
||||||
|
|
||||||
if !reflect.DeepEqual(field, result) {
|
|
||||||
wsserverForNodes.SendAll(node)
|
|
||||||
}
|
|
||||||
|
|
||||||
field.Set(reflect.ValueOf(result))
|
|
||||||
})
|
})
|
||||||
go respondDaemon.ListenAndSend(collectInterval)
|
go respondDaemon.ListenAndSend(collectInterval)
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/ffdo/node-informant/gluon-collector/data"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
@ -9,24 +10,28 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
//Node struct
|
// Node struct
|
||||||
type Node struct {
|
type Node struct {
|
||||||
Firstseen time.Time `json:"firstseen"`
|
Firstseen time.Time `json:"firstseen"`
|
||||||
Lastseen time.Time `json:"lastseen"`
|
Lastseen time.Time `json:"lastseen"`
|
||||||
Statistics interface{} `json:"statistics"`
|
Statistics *data.StatisticsStruct `json:"statistics"`
|
||||||
Nodeinfo interface{} `json:"nodeinfo"`
|
Nodeinfo *data.NodeInfo `json:"nodeinfo"`
|
||||||
Neighbours interface{} `json:"neighbours"`
|
Neighbours *data.NeighbourStruct `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
//Nodes struct: cache DB of Node's structs
|
type NodeElement struct {
|
||||||
|
NodeId string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Nodes struct: cache DB of Node's structs
|
||||||
type Nodes struct {
|
type Nodes struct {
|
||||||
Version int `json:"version"`
|
Version int `json:"version"`
|
||||||
Timestamp time.Time `json:"timestamp"`
|
Timestamp time.Time `json:"timestamp"`
|
||||||
List map[string]*Node `json:"nodes"` // the current nodemap
|
List map[string]*Node `json:"nodes"` // the current nodemap, indexed by node ID
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
//NewNodes create Nodes structs (cache DB)
|
// NewNodes create Nodes structs (cache DB)
|
||||||
func NewNodes() *Nodes {
|
func NewNodes() *Nodes {
|
||||||
nodes := &Nodes{
|
nodes := &Nodes{
|
||||||
Version: 1,
|
Version: 1,
|
||||||
@ -36,7 +41,7 @@ func NewNodes() *Nodes {
|
|||||||
return nodes
|
return nodes
|
||||||
}
|
}
|
||||||
|
|
||||||
//Get a Node by nodeid
|
// Get a Node by nodeid
|
||||||
func (nodes *Nodes) Get(nodeID string) *Node {
|
func (nodes *Nodes) Get(nodeID string) *Node {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
|
|
||||||
@ -56,7 +61,7 @@ func (nodes *Nodes) Get(nodeID string) *Node {
|
|||||||
return node
|
return node
|
||||||
}
|
}
|
||||||
|
|
||||||
//Saver to save the cached DB to json file
|
// Saves the cached DB to json file periodically
|
||||||
func (nodes *Nodes) Saver(outputFile string, saveInterval time.Duration) {
|
func (nodes *Nodes) Saver(outputFile string, saveInterval time.Duration) {
|
||||||
c := time.Tick(saveInterval)
|
c := time.Tick(saveInterval)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user