Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions probe/ping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package probe

import (
"time"

ping "github.com/sparrc/go-ping"
)

const packetCount = 3

// Ping probe, check network avaibility
type Ping struct {
addr string
warning time.Duration
fail time.Duration
}

// NewPing returns a ping probe
func NewPing(addr string, warning, fail time.Duration) *Ping {
return &Ping{
addr: addr,
warning: warning,
fail: fail,
}
}

// Probe send 3 ICMP packets and wait for replies
// Warning is issued when a reply is missing or if rtt exeed `warning`
// Failure occure when target is unreachable
func (p *Ping) Probe() (status Status, message string) {
pinger := &ping.Pinger{
Timeout: p.fail,
Count: packetCount,
}
pinger.SetAddr(p.addr)

pinger.Run()
stats := pinger.Statistics()

if stats.PacketsRecv == 0 {
return StatusError, "Unreacheable"
}

if stats.PacketsRecv < packetCount {
return StatusWarning, "Packet loss"
}
return EvaluateDuration(stats.AvgRtt, p.warning)
}