add fallback for addresses

This commit is contained in:
Martin/Geno 2019-01-19 21:59:43 +01:00
parent 4faf990845
commit 7295277334
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
6 changed files with 56 additions and 19 deletions

View File

@ -25,6 +25,12 @@ As root:
go get -v -u github.com/FreifunkBremen/yanic go get -v -u github.com/FreifunkBremen/yanic
``` ```
### allow to ping
only needed if config has `nodes.ping_count` > 0
```sh
sudo setcap cap_net_raw=+ep /opt/go/bin/yanic
```
#### Work with other databases #### Work with other databases
If you like to use another database solution than influxdb, Pull Requests are If you like to use another database solution than influxdb, Pull Requests are
welcome. Just fork this project and create another subpackage within the folder welcome. Just fork this project and create another subpackage within the folder

View File

@ -26,6 +26,12 @@ As root:
go get -v -u github.com/FreifunkBremen/yanic go get -v -u github.com/FreifunkBremen/yanic
``` ```
### allow to ping
only needed if config has `nodes.ping_count` > 0
```sh
sudo setcap cap_net_raw=+ep /opt/go/bin/yanic
```
### Install ### Install
```sh ```sh

View File

@ -13,7 +13,7 @@ type Node struct {
Firstseen jsontime.Time `json:"firstseen"` Firstseen jsontime.Time `json:"firstseen"`
Lastseen jsontime.Time `json:"lastseen"` Lastseen jsontime.Time `json:"lastseen"`
Online bool `json:"online"` Online bool `json:"online"`
NoRespondd bool `json:"-"` NoRespondd bool `json:"no_respondd"`
Statistics *data.Statistics `json:"statistics"` Statistics *data.Statistics `json:"statistics"`
Nodeinfo *data.Nodeinfo `json:"nodeinfo"` Nodeinfo *data.Nodeinfo `json:"nodeinfo"`
Neighbours *data.Neighbours `json:"-"` Neighbours *data.Neighbours `json:"-"`

View File

@ -80,9 +80,10 @@ func (nodes *Nodes) Update(nodeID string, res *data.ResponseData) *Node {
// Update fields // Update fields
node.Lastseen = now node.Lastseen = now
node.Online = true node.Online = true
node.Neighbours = res.Neighbours
node.Nodeinfo = res.Nodeinfo node.Nodeinfo = res.Nodeinfo
node.Statistics = res.Statistics node.Statistics = res.Statistics
node.Neighbours = res.Neighbours
node.NoRespondd = res.Statistics == nil && res.Neighbours == nil
return node return node
} }
@ -170,24 +171,32 @@ func (nodes *Nodes) expire() {
nodes.Lock() nodes.Lock()
defer nodes.Unlock() defer nodes.Unlock()
wg := sync.WaitGroup{}
for id, node := range nodes.List { for id, node := range nodes.List {
if node.Lastseen.Before(pruneAfter) { if node.Lastseen.Before(pruneAfter) {
// expire // expire
delete(nodes.List, id) delete(nodes.List, id)
} else if node.Lastseen.Before(offlineAfter) { } else if node.Lastseen.Before(offlineAfter) {
// set to offline // set to offline
if nodes.config.PingCount > 0 && nodes.ping(node) { wg.Add(1)
node.Online = true go func(node *Node) {
node.NoRespondd = true defer wg.Done()
if nodes.config.PingCount > 0 && nodes.ping(node) {
node.Online = true
node.NoRespondd = true
node.Statistics = nil node.Statistics = nil
node.Neighbours = nil node.Neighbours = nil
} else { } else {
node.Online = false node.Online = false
node.NoRespondd = false node.NoRespondd = false
} }
}(node)
} }
} }
wg.Wait()
log.WithField("nodes", "expire").Debug("end")
} }
// adds the nodes interface addresses to the internal map // adds the nodes interface addresses to the internal map
@ -249,6 +258,7 @@ func (nodes *Nodes) save() {
// serialize nodes // serialize nodes
SaveJSON(nodes, nodes.config.StatePath) SaveJSON(nodes, nodes.config.StatePath)
log.WithField("nodes", "save").Debug("end")
} }
// SaveJSON to path // SaveJSON to path

View File

@ -10,13 +10,21 @@ func (nodes *Nodes) ping(node *Node) bool {
if node.Nodeinfo != nil { if node.Nodeinfo != nil {
logNode = logNode.WithField("node_id", node.Nodeinfo.NodeID) logNode = logNode.WithField("node_id", node.Nodeinfo.NodeID)
} }
if node.Address == nil { var addr string
if node.Address != nil {
addr = node.Address.IP.String()
if node.Address.IP.IsLinkLocalUnicast() {
addr += "%" + node.Address.Zone
}
} else {
logNode.Debug("error no address found") logNode.Debug("error no address found")
return false if node.Nodeinfo != nil {
} for _, addrMaybe := range node.Nodeinfo.Network.Addresses {
addr := node.Address.IP.String() if len(addrMaybe) >= 5 && addrMaybe[:5] != "fe80:" {
if node.Address.IP.IsLinkLocalUnicast() { addr = addrMaybe
addr += "%" + node.Address.Zone }
}
}
} }
logAddr := logNode.WithField("addr", addr) logAddr := logNode.WithField("addr", addr)
@ -26,9 +34,10 @@ func (nodes *Nodes) ping(node *Node) bool {
logAddr.Debugf("error during ping: %s", err) logAddr.Debugf("error during ping: %s", err)
return false return false
} }
//pinger.SetPrivileged(true) pinger.SetPrivileged(true)
pinger.Count = nodes.config.PingCount pinger.Count = nodes.config.PingCount
pinger.Timeout = nodes.config.PingTimeout.Duration pinger.Timeout = nodes.config.PingTimeout.Duration
pinger.Interval = pinger.Timeout / pinger.Count
pinger.Run() // blocks until finished pinger.Run() // blocks until finished
stats := pinger.Statistics() stats := pinger.Statistics()
logAddr.WithFields(map[string]interface{}{ logAddr.WithFields(map[string]interface{}{

View File

@ -27,7 +27,13 @@ func TestPing(t *testing.T) {
ifaceToNodeID: make(map[string]string), ifaceToNodeID: make(map[string]string),
} }
node := nodes.Update("expire", &data.ResponseData{NodeInfo: &data.NodeInfo{NodeID: "nodeID-Lola"}}) node := nodes.Update("expire", &data.ResponseData{NodeInfo: &data.NodeInfo{
NodeID: "nodeID-Lola",
Network: data.Network{Addresses: []string{"fe80::1", "fd2f::1"}},
}})
// get fallback
assert.False(nodes.ping(node))
node.Address = &net.UDPAddr{Zone: "bat0"} node.Address = &net.UDPAddr{Zone: "bat0"}
// error during ping // error during ping
assert.False(nodes.ping(node)) assert.False(nodes.ping(node))