add fallback for addresses

This commit is contained in:
Martin/Geno 2019-01-19 21:59:43 +01:00
parent 521af9429a
commit 8b75fb1b04
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
```
### 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
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

View File

@ -26,6 +26,12 @@ As root:
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
```sh

View File

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

View File

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

View File

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

View File

@ -27,7 +27,13 @@ func TestPing(t *testing.T) {
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"}
// error during ping
assert.False(nodes.ping(node))