2015-12-29 04:08:03 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-02-25 21:06:37 +01:00
|
|
|
"encoding/json"
|
2016-02-19 11:30:42 +01:00
|
|
|
"flag"
|
2015-12-29 04:08:03 +01:00
|
|
|
"log"
|
2016-02-25 22:06:15 +01:00
|
|
|
"net"
|
2016-02-19 11:13:30 +01:00
|
|
|
"net/http"
|
2016-02-25 21:06:37 +01:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2016-03-07 01:37:38 +01:00
|
|
|
"github.com/ffdo/node-informant/gluon-collector/data"
|
2016-02-25 21:06:37 +01:00
|
|
|
"github.com/monitormap/micro-daemon/models"
|
2016-02-26 09:28:31 +01:00
|
|
|
"github.com/monitormap/micro-daemon/respond"
|
2016-02-25 21:06:37 +01:00
|
|
|
"github.com/monitormap/micro-daemon/websocketserver"
|
2015-12-29 14:05:47 +01:00
|
|
|
)
|
2016-02-25 21:06:37 +01:00
|
|
|
|
2016-02-19 11:30:42 +01:00
|
|
|
var (
|
2016-02-25 21:42:58 +01:00
|
|
|
wsserverForNodes = websocketserver.NewServer("/nodes")
|
2016-02-26 09:28:31 +01:00
|
|
|
respondDaemon *respond.Daemon
|
2016-02-25 21:42:58 +01:00
|
|
|
nodes = models.NewNodes()
|
|
|
|
aliases = models.NewNodes()
|
2016-02-26 09:33:07 +01:00
|
|
|
listenAddr string
|
|
|
|
listenPort string
|
|
|
|
collectInterval time.Duration
|
|
|
|
httpDir string
|
2016-02-25 21:42:58 +01:00
|
|
|
outputNodesFile string
|
|
|
|
outputAliasesFile string
|
|
|
|
saveInterval time.Duration
|
2016-02-19 11:30:42 +01:00
|
|
|
)
|
2016-02-25 21:06:37 +01:00
|
|
|
|
|
|
|
func main() {
|
2016-02-19 11:30:42 +01:00
|
|
|
var collectSeconds, saveSeconds int
|
|
|
|
|
2016-02-26 09:33:07 +01:00
|
|
|
flag.StringVar(&listenAddr, "host", "", "path aliases.json file")
|
|
|
|
flag.StringVar(&listenPort, "port", "8080", "path aliases.json file")
|
2016-02-25 21:42:58 +01:00
|
|
|
flag.IntVar(&collectSeconds, "collectInterval", 15, "interval for data collections")
|
2016-02-26 09:33:07 +01:00
|
|
|
flag.StringVar(&httpDir, "httpdir", "webroot", "a implemented static file webserver")
|
|
|
|
flag.StringVar(&outputNodesFile, "path-nodes", "webroot/nodes.json", "path nodes.json file")
|
|
|
|
flag.StringVar(&outputAliasesFile, "path-aliases", "webroot/aliases.json", "path aliases.json file")
|
|
|
|
flag.IntVar(&saveSeconds, "saveInterval", 60, "interval for data saving")
|
2016-02-19 11:30:42 +01:00
|
|
|
flag.Parse()
|
2015-12-29 14:05:47 +01:00
|
|
|
|
2016-02-19 11:30:42 +01:00
|
|
|
collectInterval = time.Second * time.Duration(collectSeconds)
|
|
|
|
saveInterval = time.Second * time.Duration(saveSeconds)
|
|
|
|
|
2016-02-25 21:06:37 +01:00
|
|
|
go wsserverForNodes.Listen()
|
2016-02-25 21:42:58 +01:00
|
|
|
go nodes.Saver(outputNodesFile, saveInterval)
|
|
|
|
go aliases.Saver(outputAliasesFile, saveInterval)
|
2016-02-26 09:28:31 +01:00
|
|
|
respondDaemon = respond.NewDaemon(func(coll *respond.Collector, res *respond.Response) {
|
2016-02-19 11:30:42 +01:00
|
|
|
|
2016-03-07 01:37:38 +01:00
|
|
|
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)
|
2016-02-25 21:06:37 +01:00
|
|
|
}
|
|
|
|
})
|
2016-02-26 09:28:31 +01:00
|
|
|
go respondDaemon.ListenAndSend(collectInterval)
|
2016-02-25 21:06:37 +01:00
|
|
|
|
2016-02-26 09:33:07 +01:00
|
|
|
http.Handle("/", http.FileServer(http.Dir(httpDir)))
|
2016-02-25 21:06:37 +01:00
|
|
|
//TODO bad
|
2016-02-25 22:06:15 +01:00
|
|
|
log.Fatal(http.ListenAndServe(net.JoinHostPort(listenAddr, listenPort), nil))
|
2016-02-25 21:06:37 +01: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 09:28:31 +01:00
|
|
|
respondDaemon.Close()
|
2015-12-29 04:08:03 +01:00
|
|
|
}
|