2016-02-26 08:28:31 +00:00
|
|
|
package respond
|
2015-12-29 03:08:03 +00:00
|
|
|
|
|
|
|
import (
|
2016-03-19 00:50:23 +00:00
|
|
|
"bytes"
|
|
|
|
"compress/flate"
|
2016-03-11 23:59:36 +00:00
|
|
|
"encoding/json"
|
2016-12-22 02:06:46 +00:00
|
|
|
"io/ioutil"
|
2015-12-29 03:08:03 +00:00
|
|
|
"log"
|
|
|
|
"net"
|
2016-03-11 23:59:36 +00:00
|
|
|
"reflect"
|
2015-12-29 03:08:03 +00:00
|
|
|
"time"
|
2016-03-19 22:18:26 +00:00
|
|
|
|
|
|
|
"github.com/FreifunkBremen/respond-collector/data"
|
2016-10-03 17:55:37 +00:00
|
|
|
"github.com/FreifunkBremen/respond-collector/database"
|
|
|
|
"github.com/FreifunkBremen/respond-collector/models"
|
2015-12-29 03:08:03 +00:00
|
|
|
)
|
|
|
|
|
2016-02-26 08:28:31 +00:00
|
|
|
//Collector for a specificle respond messages
|
2015-12-29 03:08:03 +00:00
|
|
|
type Collector struct {
|
2016-10-03 23:05:18 +00:00
|
|
|
CollectType string
|
|
|
|
connection *net.UDPConn // UDP socket
|
|
|
|
queue chan *Response // received responses
|
|
|
|
msgType reflect.Type
|
|
|
|
multicastAddr string
|
|
|
|
db *database.DB
|
|
|
|
nodes *models.Nodes
|
2016-03-11 22:56:23 +00:00
|
|
|
// Ticker and stopper
|
|
|
|
ticker *time.Ticker
|
|
|
|
stop chan interface{}
|
2015-12-29 03:08:03 +00:00
|
|
|
}
|
|
|
|
|
2016-10-03 17:55:37 +00:00
|
|
|
// Creates a Collector struct
|
2016-12-22 02:06:46 +00:00
|
|
|
func NewCollector(db *database.DB, nodes *models.Nodes, iface string) *Collector {
|
2015-12-29 03:08:03 +00:00
|
|
|
// Parse address
|
2016-02-25 20:06:37 +00:00
|
|
|
addr, err := net.ResolveUDPAddr("udp", "[::]:0")
|
2015-12-29 03:08:03 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open socket
|
|
|
|
conn, err := net.ListenUDP("udp", addr)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2016-02-25 20:06:37 +00:00
|
|
|
conn.SetReadBuffer(maxDataGramSize)
|
2015-12-29 03:08:03 +00:00
|
|
|
|
|
|
|
collector := &Collector{
|
2016-10-03 23:05:18 +00:00
|
|
|
connection: conn,
|
2016-10-04 12:54:19 +00:00
|
|
|
db: db,
|
2016-10-03 23:05:18 +00:00
|
|
|
nodes: nodes,
|
|
|
|
multicastAddr: net.JoinHostPort(multiCastGroup+"%"+iface, port),
|
|
|
|
queue: make(chan *Response, 400),
|
|
|
|
stop: make(chan interface{}, 1),
|
2015-12-29 03:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go collector.receiver()
|
|
|
|
go collector.parser()
|
2016-03-12 00:04:22 +00:00
|
|
|
|
2016-12-15 10:10:09 +00:00
|
|
|
if collector.db != nil {
|
|
|
|
go collector.globalStatsWorker()
|
|
|
|
}
|
|
|
|
|
2016-12-22 02:06:46 +00:00
|
|
|
return collector
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start Collector
|
|
|
|
func (coll *Collector) Start(interval time.Duration) {
|
|
|
|
if coll.ticker != nil {
|
|
|
|
panic("already started")
|
|
|
|
}
|
|
|
|
|
|
|
|
coll.ticker = time.NewTicker(interval)
|
2016-03-12 17:26:51 +00:00
|
|
|
go func() {
|
2016-12-22 02:06:46 +00:00
|
|
|
coll.sendOnce() // immediately
|
|
|
|
coll.sender() // periodically
|
2016-03-12 17:26:51 +00:00
|
|
|
}()
|
2015-12-29 03:08:03 +00:00
|
|
|
}
|
|
|
|
|
2016-03-11 22:56:23 +00:00
|
|
|
// Close Collector
|
2015-12-29 03:08:03 +00:00
|
|
|
func (coll *Collector) Close() {
|
2016-03-11 22:56:23 +00:00
|
|
|
// stop ticker
|
2016-12-22 02:06:46 +00:00
|
|
|
if coll.ticker != nil {
|
|
|
|
coll.ticker.Stop()
|
|
|
|
close(coll.stop)
|
|
|
|
}
|
2016-03-11 22:56:23 +00:00
|
|
|
|
2015-12-29 03:08:03 +00:00
|
|
|
coll.connection.Close()
|
|
|
|
close(coll.queue)
|
|
|
|
}
|
|
|
|
|
2015-12-29 13:05:47 +00:00
|
|
|
func (coll *Collector) sendOnce() {
|
2016-12-22 02:06:46 +00:00
|
|
|
coll.SendPacket(coll.multicastAddr)
|
2015-12-29 13:05:47 +00:00
|
|
|
}
|
|
|
|
|
2016-12-22 02:06:46 +00:00
|
|
|
// Sends a UDP request to the given unicast or multicast address
|
|
|
|
func (coll *Collector) SendPacket(address string) {
|
2015-12-29 03:08:03 +00:00
|
|
|
addr, err := net.ResolveUDPAddr("udp", address)
|
2016-02-19 10:13:30 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2015-12-29 13:05:47 +00:00
|
|
|
|
2016-10-03 23:05:18 +00:00
|
|
|
if _, err := coll.connection.WriteToUDP([]byte("GET nodeinfo statistics neighbours"), addr); err != nil {
|
2016-03-07 00:37:07 +00:00
|
|
|
log.Println("WriteToUDP failed:", err)
|
|
|
|
}
|
2015-12-29 03:08:03 +00:00
|
|
|
}
|
|
|
|
|
2016-03-11 22:56:23 +00:00
|
|
|
// send packets continously
|
|
|
|
func (coll *Collector) sender() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-coll.stop:
|
|
|
|
return
|
|
|
|
case <-coll.ticker.C:
|
2016-10-03 23:05:18 +00:00
|
|
|
// send the multicast packet to request per-node statistics
|
2016-03-11 22:56:23 +00:00
|
|
|
coll.sendOnce()
|
|
|
|
}
|
2015-12-29 03:08:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (coll *Collector) parser() {
|
2016-01-04 01:07:09 +00:00
|
|
|
for obj := range coll.queue {
|
2016-10-03 17:55:37 +00:00
|
|
|
if data, err := obj.parse(); err != nil {
|
2016-03-12 12:32:55 +00:00
|
|
|
log.Println("unable to decode response from", obj.Address.String(), err, "\n", string(obj.Raw))
|
2016-10-03 17:55:37 +00:00
|
|
|
} else {
|
|
|
|
coll.saveResponse(obj.Address, data)
|
2016-03-11 23:59:36 +00:00
|
|
|
}
|
2015-12-29 03:08:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 17:55:37 +00:00
|
|
|
func (res *Response) parse() (*data.ResponseData, error) {
|
2016-03-19 22:27:07 +00:00
|
|
|
// Deflate
|
2016-10-03 17:55:37 +00:00
|
|
|
deflater := flate.NewReader(bytes.NewReader(res.Raw))
|
2016-03-19 22:27:07 +00:00
|
|
|
defer deflater.Close()
|
2016-03-19 14:07:44 +00:00
|
|
|
|
2016-03-19 22:27:07 +00:00
|
|
|
// Unmarshal
|
2016-10-03 17:55:37 +00:00
|
|
|
rdata := &data.ResponseData{}
|
|
|
|
err := json.NewDecoder(deflater).Decode(rdata)
|
|
|
|
|
|
|
|
return rdata, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (coll *Collector) saveResponse(addr net.UDPAddr, res *data.ResponseData) {
|
|
|
|
// Search for NodeID
|
|
|
|
var nodeId string
|
|
|
|
if val := res.NodeInfo; val != nil {
|
|
|
|
nodeId = val.NodeId
|
|
|
|
} else if val := res.Neighbours; val != nil {
|
|
|
|
nodeId = val.NodeId
|
|
|
|
} else if val := res.Statistics; val != nil {
|
|
|
|
nodeId = val.NodeId
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updates nodes if NodeID found
|
|
|
|
if len(nodeId) != 12 {
|
|
|
|
log.Printf("invalid NodeID '%s' from %s", nodeId, addr.String())
|
|
|
|
return
|
2016-03-19 14:07:44 +00:00
|
|
|
}
|
2016-10-03 17:55:37 +00:00
|
|
|
node := coll.nodes.Update(nodeId, res)
|
2016-03-19 14:07:44 +00:00
|
|
|
|
2016-10-03 17:55:37 +00:00
|
|
|
if coll.db != nil && node.Statistics != nil {
|
|
|
|
coll.db.Add(nodeId, node)
|
|
|
|
}
|
2016-03-19 00:50:23 +00:00
|
|
|
}
|
|
|
|
|
2015-12-29 03:08:03 +00:00
|
|
|
func (coll *Collector) receiver() {
|
2016-02-25 20:06:37 +00:00
|
|
|
buf := make([]byte, maxDataGramSize)
|
2015-12-29 03:08:03 +00:00
|
|
|
for {
|
2016-01-04 01:07:09 +00:00
|
|
|
n, src, err := coll.connection.ReadFromUDP(buf)
|
2015-12-29 03:08:03 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println("ReadFromUDP failed:", err)
|
2015-12-29 13:05:47 +00:00
|
|
|
return
|
2015-12-29 03:08:03 +00:00
|
|
|
}
|
2016-01-04 01:07:09 +00:00
|
|
|
|
|
|
|
raw := make([]byte, n)
|
|
|
|
copy(raw, buf)
|
|
|
|
|
|
|
|
coll.queue <- &Response{
|
|
|
|
Address: *src,
|
|
|
|
Raw: raw,
|
|
|
|
}
|
2015-12-29 03:08:03 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-15 10:10:09 +00:00
|
|
|
|
|
|
|
func (coll *Collector) globalStatsWorker() {
|
|
|
|
ticker := time.NewTicker(time.Minute)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-coll.stop:
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
coll.saveGlobalStats()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// saves global statistics
|
|
|
|
func (coll *Collector) saveGlobalStats() {
|
|
|
|
stats := models.NewGlobalStats(coll.nodes)
|
|
|
|
|
|
|
|
coll.db.AddPoint(database.MeasurementGlobal, nil, stats.Fields(), time.Now())
|
|
|
|
coll.db.AddCounterMap(database.MeasurementFirmware, stats.Firmwares)
|
|
|
|
coll.db.AddCounterMap(database.MeasurementModel, stats.Models)
|
|
|
|
}
|