2015-12-29 03:08:03 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-02-25 20:06:37 +00:00
|
|
|
"encoding/json"
|
2016-02-19 10:30:42 +00:00
|
|
|
"flag"
|
2015-12-29 03:08:03 +00:00
|
|
|
"log"
|
2016-02-25 21:06:15 +00:00
|
|
|
"net"
|
2016-02-19 10:13:30 +00:00
|
|
|
"net/http"
|
2016-02-25 20:06:37 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2016-03-07 00:37:38 +00:00
|
|
|
"github.com/ffdo/node-informant/gluon-collector/data"
|
2016-02-25 20:06:37 +00:00
|
|
|
"github.com/monitormap/micro-daemon/models"
|
2016-02-26 08:28:31 +00:00
|
|
|
"github.com/monitormap/micro-daemon/respond"
|
2016-02-25 20:06:37 +00:00
|
|
|
"github.com/monitormap/micro-daemon/websocketserver"
|
2015-12-29 13:05:47 +00:00
|
|
|
)
|
2016-02-25 20:06:37 +00:00
|
|
|
|
2016-02-19 10:30:42 +00:00
|
|
|
var (
|
2016-03-07 08:52:52 +00:00
|
|
|
configFile string
|
|
|
|
config *models.Config
|
|
|
|
wsserverForNodes *websocketserver.Server
|
|
|
|
respondDaemon *respond.Daemon
|
|
|
|
nodes = models.NewNodes()
|
|
|
|
aliases = models.NewNodes()
|
2016-02-19 10:30:42 +00:00
|
|
|
)
|
2016-02-25 20:06:37 +00:00
|
|
|
|
|
|
|
func main() {
|
2016-03-07 08:52:52 +00:00
|
|
|
flag.StringVar(&configFile, "c", "config.yml", "path of configuration file (default:config.yaml)")
|
2016-02-19 10:30:42 +00:00
|
|
|
flag.Parse()
|
2016-03-07 08:52:52 +00:00
|
|
|
config = models.ConfigReadFile(configFile)
|
2015-12-29 13:05:47 +00:00
|
|
|
|
2016-03-07 08:52:52 +00:00
|
|
|
collectInterval := time.Second * time.Duration(config.Responedd.CollectInterval)
|
|
|
|
saveInterval := time.Second * time.Duration(config.Nodes.SaveInterval)
|
2016-02-19 10:30:42 +00:00
|
|
|
|
2016-03-07 08:52:52 +00:00
|
|
|
if config.Nodes.Enable {
|
|
|
|
go nodes.Saver(config.Nodes.NodesPath, saveInterval)
|
|
|
|
}
|
|
|
|
if config.Nodes.AliasesEnable {
|
|
|
|
go aliases.Saver(config.Nodes.AliasesPath, saveInterval)
|
|
|
|
}
|
2016-02-19 10:30:42 +00:00
|
|
|
|
2016-03-07 08:52:52 +00:00
|
|
|
if config.Webserver.Enable {
|
|
|
|
if config.Webserver.WebsocketNode {
|
|
|
|
wsserverForNodes = websocketserver.NewServer("/nodes")
|
|
|
|
go wsserverForNodes.Listen()
|
2016-02-25 20:06:37 +00:00
|
|
|
}
|
2016-03-07 08:52:52 +00:00
|
|
|
http.Handle("/", http.FileServer(http.Dir(config.Webserver.Webroot)))
|
|
|
|
}
|
2016-02-25 20:06:37 +00:00
|
|
|
|
2016-03-07 08:52:52 +00:00
|
|
|
if config.Responedd.Enable {
|
|
|
|
respondDaemon = respond.NewDaemon(func(coll *respond.Collector, res *respond.Response) {
|
|
|
|
|
|
|
|
switch coll.CollectType {
|
|
|
|
case "neighbours":
|
|
|
|
result := &data.NeighbourStruct{}
|
|
|
|
if json.Unmarshal(res.Raw, result) == nil {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
go respondDaemon.ListenAndSend(collectInterval)
|
|
|
|
}
|
2016-02-25 20:06:37 +00:00
|
|
|
|
2016-03-07 08:52:52 +00:00
|
|
|
//TODO bad
|
|
|
|
if config.Webserver.Enable {
|
|
|
|
log.Fatal(http.ListenAndServe(net.JoinHostPort(config.Webserver.Address, config.Webserver.Port), nil))
|
|
|
|
}
|
2016-02-25 20:06:37 +00:00
|
|
|
// Wait for End
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
sig := <-sigs
|
|
|
|
log.Println("received", sig)
|
|
|
|
|
|
|
|
// Close everything at the end
|
|
|
|
wsserverForNodes.Close()
|
2016-02-26 08:28:31 +00:00
|
|
|
respondDaemon.Close()
|
2015-12-29 03:08:03 +00:00
|
|
|
}
|