-
Notifications
You must be signed in to change notification settings - Fork 1
/
icmp.go
54 lines (49 loc) · 1.26 KB
/
icmp.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
package main
import (
"github.com/go-ping/ping"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var (
icmpRequests = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "observer_icmp_requests",
Help: "Total number of sent ICMP requests",
}, []string{"target"})
icmpFailures = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "observer_icmp_failures",
Help: "Total number of failed ICMP requests",
}, []string{"target"})
icmpRtt = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "observer_icmp_rtt",
Help: "RTT of ICMP request.",
}, []string{"target"})
)
func init() {
prometheus.MustRegister(icmpRtt)
}
func sampleIcmp(targets []string, count int) {
for _, target := range targets {
icmpRequests.With(prometheus.Labels{
"target": target,
}).Add(float64(count))
f := icmpFailures.With(prometheus.Labels{
"target": target,
})
f.Add(float64(0))
pinger, err := ping.NewPinger(target)
pinger.SetPrivileged(true)
if err != nil {
f.Inc()
return
}
pinger.Count = count
pinger.Run()
stats := pinger.Statistics()
if stats.PacketsSent != stats.PacketsRecv {
f.Inc()
}
icmpRtt.With(prometheus.Labels{
"target": target,
}).Set(stats.AvgRtt.Seconds())
}
}