yanic/respond/collector.go

360 lines
8.2 KiB
Go
Raw Normal View History

2016-02-26 09:28:31 +01:00
package respond
2015-12-29 04:08:03 +01:00
import (
2016-03-19 01:50:23 +01:00
"bytes"
"compress/flate"
2016-03-12 00:59:36 +01:00
"encoding/json"
"fmt"
2015-12-29 04:08:03 +01:00
"net"
"time"
2019-01-17 13:26:16 +01:00
"github.com/bdlm/log"
2017-03-03 16:19:35 +01:00
"github.com/FreifunkBremen/yanic/data"
"github.com/FreifunkBremen/yanic/database"
"github.com/FreifunkBremen/yanic/lib/jsontime"
"github.com/FreifunkBremen/yanic/runtime"
2015-12-29 04:08:03 +01:00
)
// Collector for a specificle respond messages
2015-12-29 04:08:03 +01:00
type Collector struct {
connections []multicastConn // UDP sockets
queue chan *Response // received responses
db database.Connection
nodes *runtime.Nodes
sitesDomains map[string][]string
interval time.Duration // Interval for multicast packets
stop chan interface{}
2015-12-29 04:08:03 +01:00
}
type multicastConn struct {
Conn *net.UDPConn
SendRequest bool
MulticastAddress net.IP
}
// NewCollector creates a Collector struct
func NewCollector(db database.Connection, nodes *runtime.Nodes, sitesDomains map[string][]string, ifaces []InterfaceConfig) *Collector {
coll := &Collector{
db: db,
nodes: nodes,
sitesDomains: sitesDomains,
queue: make(chan *Response, 400),
stop: make(chan interface{}),
}
for _, iface := range ifaces {
coll.listenUDP(iface)
}
go coll.parser()
if coll.db != nil {
go coll.globalStatsWorker()
}
return coll
}
func (coll *Collector) listenUDP(iface InterfaceConfig) {
var addr net.IP
var err error
if iface.IPAddress != "" {
addr = net.ParseIP(iface.IPAddress)
} else {
addr, err = getUnicastAddr(iface.InterfaceName, iface.MulticastAddress == "")
if err != nil {
2019-01-17 13:26:16 +01:00
log.WithField("iface", iface.InterfaceName).Panic(err)
}
}
multicastAddress := multicastAddressDefault
if iface.MulticastAddress != "" {
multicastAddress = iface.MulticastAddress
2015-12-29 04:08:03 +01:00
}
// Open socket
conn, err := net.ListenUDP("udp", &net.UDPAddr{
IP: addr,
Port: iface.Port,
Zone: iface.InterfaceName,
})
2015-12-29 04:08:03 +01:00
if err != nil {
log.Panic(err)
}
conn.SetReadBuffer(maxDataGramSize)
2015-12-29 04:08:03 +01:00
coll.connections = append(coll.connections, multicastConn{
Conn: conn,
SendRequest: !iface.SendNoRequest,
MulticastAddress: net.ParseIP(multicastAddress),
})
2016-03-12 01:04:22 +01:00
// Start receiver
go coll.receiver(conn)
}
// Returns a unicast address of given interface (linklocal or global unicast address)
func getUnicastAddr(ifname string, linklocal bool) (net.IP, error) {
iface, err := net.InterfaceByName(ifname)
if err != nil {
return nil, err
}
addresses, err := iface.Addrs()
if err != nil {
return nil, err
}
var ip net.IP
for _, addr := range addresses {
ipnet, ok := addr.(*net.IPNet)
if !ok {
continue
}
if (!linklocal && ipnet.IP.IsGlobalUnicast()) || (linklocal && ipnet.IP.IsLinkLocalUnicast()) {
ip = ipnet.IP
}
}
if ip != nil {
return ip, nil
}
2019-01-17 13:26:16 +01:00
return nil, fmt.Errorf("unable to find a unicast address")
}
// Start Collector
func (coll *Collector) Start(interval time.Duration) {
2017-01-29 22:26:16 +01:00
if coll.interval != 0 {
2019-01-17 13:26:16 +01:00
log.Panic("already started")
}
2017-01-29 22:26:16 +01:00
if interval <= 0 {
2019-01-17 13:26:16 +01:00
log.Panic("invalid collector interval")
2017-01-29 22:26:16 +01:00
}
coll.interval = interval
2016-03-12 18:26:51 +01:00
go func() {
coll.sendOnce() // immediately
coll.sender() // periodically
2016-03-12 18:26:51 +01:00
}()
2015-12-29 04:08:03 +01:00
}
2016-03-11 23:56:23 +01:00
// Close Collector
2015-12-29 04:08:03 +01:00
func (coll *Collector) Close() {
2017-01-29 22:26:16 +01:00
close(coll.stop)
for _, conn := range coll.connections {
conn.Conn.Close()
}
2015-12-29 04:08:03 +01:00
close(coll.queue)
}
2015-12-29 14:05:47 +01:00
func (coll *Collector) sendOnce() {
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 14:05:47 +01:00
}
func (coll *Collector) sendMulticast() {
2019-01-17 13:26:16 +01:00
log.Info("sending multicasts")
for _, conn := range coll.connections {
if conn.SendRequest {
coll.sendPacket(conn.Conn, conn.MulticastAddress)
}
}
}
// 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
nodes := coll.nodes.Select(func(n *runtime.Node) bool {
return n.Lastseen.After(seenAfter) && n.Lastseen.Before(seenBefore) && n.Address != nil
})
// Send unicast packets
count := 0
for _, node := range nodes {
send := 0
for _, conn := range coll.connections {
2018-07-27 20:42:03 +02:00
if node.Address.Zone != "" && conn.Conn.LocalAddr().(*net.UDPAddr).Zone != node.Address.Zone && conn.SendRequest {
continue
}
coll.sendPacket(conn.Conn, node.Address.IP)
send++
}
if send == 0 {
2019-01-17 13:26:16 +01:00
log.WithField("iface", node.Address.Zone).Error("unable to find connection")
} else {
time.Sleep(10 * time.Millisecond)
count += send
}
}
2019-01-17 13:26:16 +01:00
log.WithFields(map[string]interface{}{
"pkg_count": count,
"nodes_count": len(nodes),
}).Info("sending unicast pkg")
}
// SendPacket sends a UDP request to the given unicast or multicast address on the first UDP socket
func (coll *Collector) SendPacket(destination net.IP) {
coll.sendPacket(coll.connections[0].Conn, destination)
}
// sendPacket sends a UDP request to the given unicast or multicast address on the given UDP socket
func (coll *Collector) sendPacket(conn *net.UDPConn, destination net.IP) {
addr := net.UDPAddr{
IP: destination,
Port: port,
Zone: conn.LocalAddr().(*net.UDPAddr).Zone,
2016-02-19 11:13:30 +01:00
}
2015-12-29 14:05:47 +01:00
if _, err := conn.WriteToUDP([]byte("GET nodeinfo statistics neighbours"), &addr); err != nil {
2019-01-17 13:26:16 +01:00
log.WithField("address", addr.String()).Errorf("WriteToUDP failed: %s", err)
2016-03-07 01:37:07 +01:00
}
2015-12-29 04:08:03 +01:00
}
2018-01-13 16:54:48 +01:00
// send packets continuously
2016-03-11 23:56:23 +01:00
func (coll *Collector) sender() {
2017-01-29 22:26:16 +01:00
ticker := time.NewTicker(coll.interval)
2016-03-11 23:56:23 +01:00
for {
select {
case <-coll.stop:
2017-01-29 22:26:16 +01:00
ticker.Stop()
2016-03-11 23:56:23 +01:00
return
2017-01-29 22:26:16 +01:00
case <-ticker.C:
2016-10-04 01:05:18 +02:00
// send the multicast packet to request per-node statistics
2016-03-11 23:56:23 +01:00
coll.sendOnce()
}
2015-12-29 04:08:03 +01:00
}
}
func (coll *Collector) parser() {
2016-01-04 02:07:09 +01:00
for obj := range coll.queue {
2016-10-03 19:55:37 +02:00
if data, err := obj.parse(); err != nil {
2019-01-17 13:26:16 +01:00
log.WithField("address", obj.Address.String()).Errorf("unable to decode response %s", err)
2016-10-03 19:55:37 +02:00
} else {
coll.saveResponse(obj.Address, data)
2016-03-12 00:59:36 +01:00
}
2015-12-29 04:08:03 +01:00
}
}
2016-10-03 19:55:37 +02:00
func (res *Response) parse() (*data.ResponseData, error) {
2016-03-19 23:27:07 +01:00
// Deflate
2016-10-03 19:55:37 +02:00
deflater := flate.NewReader(bytes.NewReader(res.Raw))
2016-03-19 23:27:07 +01:00
defer deflater.Close()
2016-03-19 15:07:44 +01:00
2016-03-19 23:27:07 +01:00
// Unmarshal
2016-10-03 19:55:37 +02:00
rdata := &data.ResponseData{}
err := json.NewDecoder(deflater).Decode(rdata)
return rdata, err
}
func (coll *Collector) saveResponse(addr *net.UDPAddr, res *data.ResponseData) {
2016-10-03 19:55:37 +02:00
// Search for NodeID
var nodeID string
if val := res.Nodeinfo; val != nil {
nodeID = val.NodeID
2016-10-03 19:55:37 +02:00
} else if val := res.Neighbours; val != nil {
nodeID = val.NodeID
2016-10-03 19:55:37 +02:00
} else if val := res.Statistics; val != nil {
nodeID = val.NodeID
2016-10-03 19:55:37 +02:00
}
2017-01-29 22:26:16 +01:00
// Check length of nodeID
if len(nodeID) != 12 {
2019-01-17 13:26:16 +01:00
log.WithFields(map[string]interface{}{
"node_id": nodeID,
"address": addr.String(),
}).Warn("invalid NodeID")
2016-10-03 19:55:37 +02:00
return
2016-03-19 15:07:44 +01:00
}
2017-01-29 22:26:16 +01:00
// Set fields to nil if nodeID is inconsistent
if res.Statistics != nil && res.Statistics.NodeID != nodeID {
res.Statistics = nil
}
if res.Neighbours != nil && res.Neighbours.NodeID != nodeID {
res.Neighbours = nil
}
if res.Nodeinfo != nil && res.Nodeinfo.NodeID != nodeID {
res.Nodeinfo = nil
}
// Process the data and update IP address
node := coll.nodes.Update(nodeID, res)
node.Address = addr
2016-03-19 15:07:44 +01:00
2017-04-18 01:48:38 +02:00
// Store statistics in database
if db := coll.db; db != nil {
db.InsertNode(node)
// Store link data
if neighbours := node.Neighbours; neighbours != nil {
coll.nodes.RLock()
for _, link := range coll.nodes.NodeLinks(node) {
db.InsertLink(&link, node.Lastseen.GetTime())
}
coll.nodes.RUnlock()
}
2016-10-03 19:55:37 +02:00
}
2016-03-19 01:50:23 +01:00
}
func (coll *Collector) receiver(conn *net.UDPConn) {
buf := make([]byte, maxDataGramSize)
2015-12-29 04:08:03 +01:00
for {
n, src, err := conn.ReadFromUDP(buf)
2015-12-29 04:08:03 +01:00
if err != nil {
2019-01-17 13:26:16 +01:00
if conn != nil {
log.WithFields(map[string]interface{}{
"local": conn.LocalAddr(),
"remote": conn.RemoteAddr(),
}).Errorf("ReadFromUDP failed: %s", err)
} else {
log.Errorf("ReadFromUDP failed: %s", err)
}
2015-12-29 14:05:47 +01:00
return
2015-12-29 04:08:03 +01:00
}
2016-01-04 02:07:09 +01:00
raw := make([]byte, n)
copy(raw, buf)
coll.queue <- &Response{
Address: src,
2016-01-04 02:07:09 +01:00
Raw: raw,
}
2015-12-29 04:08:03 +01:00
}
}
func (coll *Collector) globalStatsWorker() {
ticker := time.NewTicker(time.Minute)
for {
select {
case <-coll.stop:
2017-01-29 22:26:16 +01:00
ticker.Stop()
return
case <-ticker.C:
coll.saveGlobalStats()
}
}
}
// saves global statistics
func (coll *Collector) saveGlobalStats() {
stats := runtime.NewGlobalStats(coll.nodes, coll.sitesDomains)
for site, domains := range stats {
for domain, stat := range domains {
coll.db.InsertGlobals(stat, time.Now(), site, domain)
}
}
}