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"
|
2017-04-10 16:54:12 +00:00
|
|
|
"fmt"
|
2015-12-29 03:08:03 +00:00
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"time"
|
2017-02-01 17:37:39 +00:00
|
|
|
|
2017-03-03 15:19:35 +00:00
|
|
|
"github.com/FreifunkBremen/yanic/data"
|
|
|
|
"github.com/FreifunkBremen/yanic/database"
|
|
|
|
"github.com/FreifunkBremen/yanic/jsontime"
|
2017-04-10 16:54:12 +00:00
|
|
|
"github.com/FreifunkBremen/yanic/runtime"
|
2015-12-29 03:08:03 +00:00
|
|
|
)
|
|
|
|
|
2017-01-29 21:14:40 +00:00
|
|
|
// Collector for a specificle respond messages
|
2015-12-29 03:08:03 +00:00
|
|
|
type Collector struct {
|
2017-06-29 17:06:53 +00:00
|
|
|
connection *net.UDPConn // UDP socket
|
|
|
|
queue chan *Response // received responses
|
|
|
|
ifaceSendUnicast string
|
|
|
|
ifaceSendMulticast string
|
|
|
|
db database.Connection
|
|
|
|
nodes *runtime.Nodes
|
|
|
|
interval time.Duration // Interval for multicast packets
|
|
|
|
stop chan interface{}
|
2015-12-29 03:08:03 +00:00
|
|
|
}
|
|
|
|
|
2017-01-20 21:27:44 +00:00
|
|
|
// NewCollector creates a Collector struct
|
2017-06-29 17:06:53 +00:00
|
|
|
func NewCollector(db database.Connection, nodes *runtime.Nodes, ifaceListen string, ifaceSendUnicast string, ifaceSendMulticast string, port int) *Collector {
|
|
|
|
linkLocalAddr, err := getLinkLocalAddr(ifaceListen)
|
2015-12-29 03:08:03 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
2017-07-06 10:38:14 +00:00
|
|
|
// Open link local socket
|
2017-02-01 17:37:39 +00:00
|
|
|
conn, err := net.ListenUDP("udp", &net.UDPAddr{
|
|
|
|
IP: linkLocalAddr,
|
2017-03-20 23:53:02 +00:00
|
|
|
Port: port,
|
2017-06-29 17:06:53 +00:00
|
|
|
Zone: ifaceListen,
|
2017-02-01 17:37:39 +00:00
|
|
|
})
|
2015-12-29 03:08:03 +00:00
|
|
|
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{
|
2017-06-29 17:06:53 +00:00
|
|
|
connection: conn,
|
|
|
|
db: db,
|
|
|
|
nodes: nodes,
|
|
|
|
ifaceSendUnicast: ifaceSendUnicast,
|
|
|
|
ifaceSendMulticast: ifaceSendMulticast,
|
|
|
|
queue: make(chan *Response, 400),
|
|
|
|
stop: make(chan interface{}),
|
2015-12-29 03:08:03 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 10:38:14 +00:00
|
|
|
go collector.receiver(conn)
|
2015-12-29 03:08:03 +00:00
|
|
|
go collector.parser()
|
2017-07-06 10:38:14 +00:00
|
|
|
collector.listenPublic()
|
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
|
|
|
|
}
|
|
|
|
|
2017-02-01 17:37:39 +00:00
|
|
|
// Returns the first link local unicast address for the given interface name
|
|
|
|
func getLinkLocalAddr(ifname string) (net.IP, error) {
|
|
|
|
iface, err := net.InterfaceByName(ifname)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
addresses, err := iface.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range addresses {
|
|
|
|
if ipnet := addr.(*net.IPNet); ipnet.IP.IsLinkLocalUnicast() {
|
|
|
|
return ipnet.IP, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("unable to find link local unicast address for %s", ifname)
|
|
|
|
}
|
|
|
|
|
2017-07-06 10:38:14 +00:00
|
|
|
func (coll *Collector) listenPublic() {
|
|
|
|
conn, err := net.ListenUDP("udp", &net.UDPAddr{
|
|
|
|
IP: net.ParseIP("::"),
|
|
|
|
Port: 12345,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
conn.SetReadBuffer(maxDataGramSize)
|
|
|
|
go coll.receiver(conn)
|
|
|
|
}
|
|
|
|
|
2016-12-22 02:06:46 +00:00
|
|
|
// Start Collector
|
|
|
|
func (coll *Collector) Start(interval time.Duration) {
|
2017-01-29 21:26:16 +00:00
|
|
|
if coll.interval != 0 {
|
2016-12-22 02:06:46 +00:00
|
|
|
panic("already started")
|
|
|
|
}
|
2017-01-29 21:26:16 +00:00
|
|
|
if interval <= 0 {
|
|
|
|
panic("invalid collector interval")
|
|
|
|
}
|
|
|
|
coll.interval = interval
|
2016-12-22 02:06:46 +00:00
|
|
|
|
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() {
|
2017-01-29 21:26:16 +00:00
|
|
|
close(coll.stop)
|
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() {
|
2017-01-29 21:14:40 +00:00
|
|
|
now := jsontime.Now()
|
|
|
|
coll.sendMulticast()
|
|
|
|
|
|
|
|
// Wait for the multicast responses to be processed and send unicasts
|
|
|
|
time.Sleep(coll.interval / 2)
|
|
|
|
coll.sendUnicasts(now)
|
2015-12-29 13:05:47 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 21:14:40 +00:00
|
|
|
func (coll *Collector) sendMulticast() {
|
|
|
|
log.Println("sending multicast")
|
2017-06-29 17:06:53 +00:00
|
|
|
coll.SendPacket(net.UDPAddr{
|
2017-07-05 13:51:06 +00:00
|
|
|
IP: net.ParseIP(MulticastGroup),
|
2017-06-29 17:06:53 +00:00
|
|
|
Zone: coll.ifaceSendMulticast,
|
|
|
|
})
|
2017-01-29 21:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send unicast packets to nodes that did not answer the multicast
|
|
|
|
func (coll *Collector) sendUnicasts(seenBefore jsontime.Time) {
|
|
|
|
seenAfter := seenBefore.Add(-time.Minute * 10)
|
|
|
|
|
|
|
|
// Select online nodes that has not been seen recently
|
2017-04-10 16:54:12 +00:00
|
|
|
nodes := coll.nodes.Select(func(n *runtime.Node) bool {
|
2017-01-29 21:14:40 +00:00
|
|
|
return n.Lastseen.After(seenAfter) && n.Lastseen.Before(seenBefore) && n.Address != nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// Send unicast packets
|
|
|
|
log.Printf("sending unicast to %d nodes", len(nodes))
|
|
|
|
for _, node := range nodes {
|
2017-06-29 17:06:53 +00:00
|
|
|
coll.SendPacket(net.UDPAddr{
|
|
|
|
IP: node.Address,
|
|
|
|
Zone: coll.ifaceSendUnicast,
|
|
|
|
})
|
2017-01-29 21:14:40 +00:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendPacket sends a UDP request to the given unicast or multicast address
|
2017-06-29 17:06:53 +00:00
|
|
|
func (coll *Collector) SendPacket(addr net.UDPAddr) {
|
|
|
|
addr.Port = port
|
2017-01-29 21:14:40 +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() {
|
2017-01-29 21:26:16 +00:00
|
|
|
ticker := time.NewTicker(coll.interval)
|
2016-03-11 22:56:23 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-coll.stop:
|
2017-01-29 21:26:16 +00:00
|
|
|
ticker.Stop()
|
2016-03-11 22:56:23 +00:00
|
|
|
return
|
2017-01-29 21:26:16 +00:00
|
|
|
case <-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 {
|
2017-07-06 10:38:14 +00:00
|
|
|
|
2017-07-28 06:55:11 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 06:55:11 +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
|
2017-01-20 21:27:44 +00:00
|
|
|
var nodeID string
|
2016-10-03 17:55:37 +00:00
|
|
|
if val := res.NodeInfo; val != nil {
|
2017-01-20 21:27:44 +00:00
|
|
|
nodeID = val.NodeID
|
2016-10-03 17:55:37 +00:00
|
|
|
} else if val := res.Neighbours; val != nil {
|
2017-01-20 21:27:44 +00:00
|
|
|
nodeID = val.NodeID
|
2016-10-03 17:55:37 +00:00
|
|
|
} else if val := res.Statistics; val != nil {
|
2017-01-20 21:27:44 +00:00
|
|
|
nodeID = val.NodeID
|
2016-10-03 17:55:37 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 21:26:16 +00:00
|
|
|
// Check length of nodeID
|
2017-01-20 21:27:44 +00:00
|
|
|
if len(nodeID) != 12 {
|
|
|
|
log.Printf("invalid NodeID '%s' from %s", nodeID, addr.String())
|
2016-10-03 17:55:37 +00:00
|
|
|
return
|
2016-03-19 14:07:44 +00:00
|
|
|
}
|
2017-01-29 21:26:16 +00:00
|
|
|
|
2017-01-29 21:14:40 +00:00
|
|
|
// Process the data and update IP address
|
2017-01-20 21:27:44 +00:00
|
|
|
node := coll.nodes.Update(nodeID, res)
|
2017-01-29 21:14:40 +00:00
|
|
|
node.Address = addr.IP
|
2016-03-19 14:07:44 +00:00
|
|
|
|
2017-04-17 23:48:38 +00:00
|
|
|
// Store statistics in database
|
2016-10-03 17:55:37 +00:00
|
|
|
if coll.db != nil && node.Statistics != nil {
|
2017-04-17 23:48:38 +00:00
|
|
|
node.Statistics.NodeID = nodeID
|
|
|
|
coll.db.InsertNode(node)
|
2016-10-03 17:55:37 +00:00
|
|
|
}
|
2016-03-19 00:50:23 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 10:38:14 +00:00
|
|
|
func (coll *Collector) receiver(conn *net.UDPConn) {
|
2016-02-25 20:06:37 +00:00
|
|
|
buf := make([]byte, maxDataGramSize)
|
2015-12-29 03:08:03 +00:00
|
|
|
for {
|
2017-07-06 10:38:14 +00:00
|
|
|
n, src, err := conn.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:
|
2017-01-29 21:26:16 +00:00
|
|
|
ticker.Stop()
|
2016-12-15 10:10:09 +00:00
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
coll.saveGlobalStats()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// saves global statistics
|
|
|
|
func (coll *Collector) saveGlobalStats() {
|
2017-04-10 16:54:12 +00:00
|
|
|
stats := runtime.NewGlobalStats(coll.nodes)
|
2016-12-15 10:10:09 +00:00
|
|
|
|
2017-04-17 23:48:38 +00:00
|
|
|
coll.db.InsertGlobals(stats, time.Now())
|
2016-12-15 10:10:09 +00:00
|
|
|
}
|