forked from bosun-monitor/bosun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping.go
65 lines (58 loc) · 1.71 KB
/
ping.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package ping // import "bosun.org/cmd/bosun/ping"
import (
"net"
"time"
fastping "github.com/tatsushid/go-fastping"
"bosun.org/cmd/bosun/search"
"bosun.org/collect"
"bosun.org/metadata"
"bosun.org/opentsdb"
"bosun.org/slog"
)
func init() {
metadata.AddMetricMeta("bosun.ping.resolved", metadata.Gauge, metadata.Bool,
"1=Ping resolved to an IP Address. 0=Ping failed to resolve to an IP Address.")
metadata.AddMetricMeta("bosun.ping.rtt", metadata.Gauge, metadata.MilliSecond,
"The number of milliseconds for the echo reply to be received. Also known as Round Trip Time.")
metadata.AddMetricMeta("bosun.ping.timeout", metadata.Gauge, metadata.Ok,
"0=Ping responded before timeout. 1=Ping did not respond before 5 second timeout.")
}
const pingFreq = time.Second * 15
// PingHosts pings all hosts that bosun has indexed as recently as the PingDuration
// provided via the systemConf
func PingHosts(search *search.Search, duration time.Duration) {
for range time.Tick(pingFreq) {
hosts, err := search.TagValuesByTagKey("host", duration)
if err != nil {
slog.Error(err)
continue
}
for _, host := range hosts {
go pingHost(host)
}
}
}
func pingHost(host string) {
p := fastping.NewPinger()
tags := opentsdb.TagSet{"dst_host": host}
resolved := 0
defer func() {
collect.Put("ping.resolved", tags, resolved)
}()
ra, err := net.ResolveIPAddr("ip4:icmp", host)
if err != nil {
return
}
resolved = 1
p.AddIPAddr(ra)
p.MaxRTT = time.Second * 5
timeout := 1
p.OnRecv = func(addr *net.IPAddr, t time.Duration) {
collect.Put("ping.rtt", tags, float64(t)/float64(time.Millisecond))
timeout = 0
}
if err := p.Run(); err != nil {
slog.Errorln(err)
}
collect.Put("ping.timeout", tags, timeout)
}