2019-01-19 18:45:59 +01:00
|
|
|
package runtime
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/bdlm/log"
|
|
|
|
"github.com/sparrc/go-ping"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (nodes *Nodes) ping(node *Node) bool {
|
|
|
|
logNode := log.WithField("node_id", "unknown")
|
|
|
|
if node.Nodeinfo != nil {
|
|
|
|
logNode = logNode.WithField("node_id", node.Nodeinfo.NodeID)
|
|
|
|
}
|
2019-01-19 21:59:43 +01:00
|
|
|
var addr string
|
|
|
|
if node.Address != nil {
|
|
|
|
addr = node.Address.IP.String()
|
|
|
|
if node.Address.IP.IsLinkLocalUnicast() {
|
|
|
|
addr += "%" + node.Address.Zone
|
|
|
|
}
|
|
|
|
} else {
|
2019-01-19 18:45:59 +01:00
|
|
|
logNode.Debug("error no address found")
|
2019-01-19 21:59:43 +01:00
|
|
|
if node.Nodeinfo != nil {
|
|
|
|
for _, addrMaybe := range node.Nodeinfo.Network.Addresses {
|
|
|
|
if len(addrMaybe) >= 5 && addrMaybe[:5] != "fe80:" {
|
|
|
|
addr = addrMaybe
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-19 18:45:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
logAddr := logNode.WithField("addr", addr)
|
|
|
|
|
|
|
|
pinger, err := ping.NewPinger(addr)
|
|
|
|
if err != nil {
|
|
|
|
logAddr.Debugf("error during ping: %s", err)
|
|
|
|
return false
|
|
|
|
}
|
2019-01-19 21:59:43 +01:00
|
|
|
pinger.SetPrivileged(true)
|
2019-01-19 18:45:59 +01:00
|
|
|
pinger.Count = nodes.config.PingCount
|
|
|
|
pinger.Timeout = nodes.config.PingTimeout.Duration
|
2019-01-19 21:59:43 +01:00
|
|
|
pinger.Interval = pinger.Timeout / pinger.Count
|
2019-01-19 18:45:59 +01:00
|
|
|
pinger.Run() // blocks until finished
|
|
|
|
stats := pinger.Statistics()
|
|
|
|
logAddr.WithFields(map[string]interface{}{
|
|
|
|
"pkg_lost": stats.PacketLoss,
|
|
|
|
}).Debug("pong")
|
|
|
|
return stats.PacketLoss < 100
|
|
|
|
}
|