yanic/main.go

102 lines
2.4 KiB
Go
Raw Normal View History

2015-12-29 03:08:03 +00:00
package main
import (
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"
"os"
"os/signal"
"syscall"
"time"
2016-03-15 22:26:30 +00:00
"github.com/FreifunkBremen/respond-collector/data"
"github.com/FreifunkBremen/respond-collector/models"
"github.com/FreifunkBremen/respond-collector/respond"
"github.com/FreifunkBremen/respond-collector/websocketserver"
2016-03-20 18:40:03 +00:00
"github.com/NYTimes/gziphandler"
2015-12-29 13:05:47 +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
2016-03-19 22:18:26 +00:00
collector *respond.Collector
2016-03-12 02:36:02 +00:00
statsDb *StatsDb
2016-03-20 17:30:44 +00:00
nodes *models.Nodes
2016-03-07 01:29:31 +00:00
//aliases = models.NewNodes()
2016-02-19 10:30:42 +00:00
)
func main() {
2016-03-12 15:40:41 +00:00
flag.StringVar(&configFile, "config", "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)
2016-03-20 17:30:44 +00:00
nodes = models.NewNodes(config)
2016-03-12 02:36:02 +00:00
if config.Influxdb.Enable {
statsDb = NewStatsDb()
}
2016-03-09 02:26:08 +00:00
if config.Respondd.Enable {
2016-03-12 02:58:36 +00:00
collectInterval := time.Second * time.Duration(config.Respondd.CollectInterval)
2016-03-19 22:18:26 +00:00
collector = respond.NewCollector("nodeinfo statistics neighbours", collectInterval, onReceive)
2016-03-07 08:52:52 +00:00
}
2016-03-20 17:30:44 +00:00
if config.Webserver.WebsocketNode {
wsserverForNodes = websocketserver.NewServer("/nodes")
go wsserverForNodes.Listen()
}
2016-03-07 08:52:52 +00:00
if config.Webserver.Enable {
2016-03-20 18:40:03 +00:00
http.Handle("/", gziphandler.GzipHandler(http.FileServer(http.Dir(config.Webserver.Webroot))))
2016-03-20 17:30:44 +00:00
address := net.JoinHostPort(config.Webserver.Address, config.Webserver.Port)
log.Println("starting webserver on", address)
// TODO bad
log.Fatal(http.ListenAndServe(address, nil))
2016-03-07 08:52:52 +00:00
}
2016-03-12 02:58:36 +00:00
// Wait for INT/TERM
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
sig := <-sigs
log.Println("received", sig)
// Close everything at the end
2016-03-07 09:28:24 +00:00
if wsserverForNodes != nil {
wsserverForNodes.Close()
}
2016-03-19 22:18:26 +00:00
if collector != nil {
collector.Close()
2016-03-07 09:28:24 +00:00
}
2016-03-12 02:36:02 +00:00
if statsDb != nil {
statsDb.Close()
}
2015-12-29 03:08:03 +00:00
}
2016-03-12 02:58:36 +00:00
// called for every parsed announced-message
2016-03-19 22:18:26 +00:00
func onReceive(addr net.UDPAddr, res *data.ResponseData) {
2016-03-12 02:58:36 +00:00
2016-03-20 15:25:33 +00:00
// Search for NodeID
var nodeId string
2016-03-19 22:18:26 +00:00
if val := res.NodeInfo; val != nil {
2016-03-20 15:25:33 +00:00
nodeId = val.NodeId
} else if val := res.Neighbours; val != nil {
nodeId = val.NodeId
} else if val := res.Statistics; val != nil {
nodeId = val.NodeId
2016-03-19 22:18:26 +00:00
}
2016-03-12 02:58:36 +00:00
2016-03-20 15:25:33 +00:00
// Updates nodes if NodeID found
2016-03-20 16:16:49 +00:00
if len(nodeId) != 12 {
log.Printf("invalid NodeID '%s' from %s", nodeId, addr.String())
return
2016-03-20 15:25:33 +00:00
}
2016-03-12 02:58:36 +00:00
2016-03-20 16:16:49 +00:00
nodes.Update(nodeId, res)
2016-03-20 15:25:33 +00:00
if val := res.Statistics; val != nil && statsDb != nil {
statsDb.Add(val)
2016-03-12 02:58:36 +00:00
}
}