2015-12-29 04:08:03 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
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-05-14 12:31:43 +02:00
|
|
|
"github.com/NYTimes/gziphandler"
|
2016-06-16 18:03:45 +02:00
|
|
|
"github.com/julienschmidt/httprouter"
|
2016-05-14 12:31:43 +02:00
|
|
|
|
2016-06-16 18:03:45 +02:00
|
|
|
"github.com/FreifunkBremen/respond-collector/api"
|
2016-03-15 23:26:30 +01:00
|
|
|
"github.com/FreifunkBremen/respond-collector/data"
|
|
|
|
"github.com/FreifunkBremen/respond-collector/models"
|
|
|
|
"github.com/FreifunkBremen/respond-collector/respond"
|
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-06-16 18:03:45 +02:00
|
|
|
configFile string
|
|
|
|
config *models.Config
|
|
|
|
collector *respond.Collector
|
|
|
|
statsDb *StatsDb
|
|
|
|
nodes *models.Nodes
|
2016-02-19 11:30:42 +01:00
|
|
|
)
|
2016-02-25 21:06:37 +01:00
|
|
|
|
|
|
|
func main() {
|
2016-03-12 16:40:41 +01:00
|
|
|
flag.StringVar(&configFile, "config", "config.yml", "path of configuration file (default:config.yaml)")
|
2016-02-19 11:30:42 +01:00
|
|
|
flag.Parse()
|
2016-03-20 19:54:43 +01:00
|
|
|
config = models.ReadConfigFile(configFile)
|
2016-03-20 18:30:44 +01:00
|
|
|
nodes = models.NewNodes(config)
|
2016-02-25 21:06:37 +01:00
|
|
|
|
2016-03-12 03:36:02 +01:00
|
|
|
if config.Influxdb.Enable {
|
|
|
|
statsDb = NewStatsDb()
|
|
|
|
}
|
|
|
|
|
2016-03-09 03:26:08 +01:00
|
|
|
if config.Respondd.Enable {
|
2016-03-12 03:58:36 +01:00
|
|
|
collectInterval := time.Second * time.Duration(config.Respondd.CollectInterval)
|
2016-06-16 18:03:45 +02:00
|
|
|
collector = respond.NewCollector("nodeinfo statistics neighbours", collectInterval, onReceive, config.Respondd.Interface)
|
2016-03-07 09:52:52 +01:00
|
|
|
}
|
2016-02-25 21:06:37 +01:00
|
|
|
|
2016-03-07 09:52:52 +01:00
|
|
|
if config.Webserver.Enable {
|
2016-05-14 12:31:43 +02:00
|
|
|
router := httprouter.New()
|
2016-05-17 11:07:14 +02:00
|
|
|
if config.Webserver.Api.NewNodes {
|
2016-06-16 18:03:45 +02:00
|
|
|
api.NewNodes(config, router, "/api/nodes", nodes)
|
2016-05-17 10:59:34 +02:00
|
|
|
log.Println("api nodes started")
|
2016-05-14 12:31:43 +02:00
|
|
|
}
|
|
|
|
if config.Webserver.Api.Aliases {
|
2016-06-16 18:03:45 +02:00
|
|
|
api.NewAliases(config, router, "/api/aliases", nodes)
|
2016-05-17 10:59:34 +02:00
|
|
|
log.Println("api aliases started")
|
2016-05-14 12:31:43 +02:00
|
|
|
}
|
|
|
|
router.NotFound = gziphandler.GzipHandler(http.FileServer(http.Dir(config.Webserver.Webroot)))
|
2016-03-20 18:30:44 +01:00
|
|
|
|
|
|
|
address := net.JoinHostPort(config.Webserver.Address, config.Webserver.Port)
|
|
|
|
log.Println("starting webserver on", address)
|
|
|
|
// TODO bad
|
2016-05-14 12:31:43 +02:00
|
|
|
log.Fatal(http.ListenAndServe(address, router))
|
2016-03-07 09:52:52 +01:00
|
|
|
}
|
2016-03-12 03:58:36 +01:00
|
|
|
|
|
|
|
// Wait for INT/TERM
|
2016-02-25 21:06:37 +01:00
|
|
|
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-19 23:18:26 +01:00
|
|
|
if collector != nil {
|
|
|
|
collector.Close()
|
2016-03-07 10:28:24 +01:00
|
|
|
}
|
2016-03-12 03:36:02 +01:00
|
|
|
if statsDb != nil {
|
|
|
|
statsDb.Close()
|
|
|
|
}
|
2015-12-29 04:08:03 +01:00
|
|
|
}
|
2016-03-12 03:58:36 +01:00
|
|
|
|
|
|
|
// called for every parsed announced-message
|
2016-03-19 23:18:26 +01:00
|
|
|
func onReceive(addr net.UDPAddr, res *data.ResponseData) {
|
2016-03-12 03:58:36 +01:00
|
|
|
|
2016-03-20 16:25:33 +01:00
|
|
|
// Search for NodeID
|
|
|
|
var nodeId string
|
2016-03-19 23:18:26 +01:00
|
|
|
if val := res.NodeInfo; val != nil {
|
2016-03-20 16:25:33 +01: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 23:18:26 +01:00
|
|
|
}
|
2016-03-12 03:58:36 +01:00
|
|
|
|
2016-03-20 16:25:33 +01:00
|
|
|
// Updates nodes if NodeID found
|
2016-03-20 17:16:49 +01:00
|
|
|
if len(nodeId) != 12 {
|
|
|
|
log.Printf("invalid NodeID '%s' from %s", nodeId, addr.String())
|
|
|
|
return
|
2016-03-20 16:25:33 +01:00
|
|
|
}
|
2016-03-12 03:58:36 +01:00
|
|
|
|
2016-07-14 01:19:03 +02:00
|
|
|
node := nodes.Update(nodeId, res)
|
2016-03-20 17:16:49 +01:00
|
|
|
|
2016-07-14 01:19:03 +02:00
|
|
|
if statsDb != nil && node.Statistics != nil {
|
|
|
|
statsDb.Add(nodeId, node)
|
2016-03-12 03:58:36 +01:00
|
|
|
}
|
|
|
|
}
|