-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathicmp.go
More file actions
82 lines (71 loc) · 2.07 KB
/
icmp.go
File metadata and controls
82 lines (71 loc) · 2.07 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
probing "github.com/prometheus-community/pro-bing"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var (
icmpSetupFailures = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "observer_icmp_setup_failures",
Help: "Total number of failed ICMP tests due to errors setting up the measurement",
}, []string{"target"})
icmpAvgRtt = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "observer_icmp_avg_rtt",
Help: "Average round trip time of ICMP request.",
}, []string{"target", "resolved_addr"})
icmpSentRequests = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "observer_sent_icmp_requests",
Help: "Total number of sent ICMP echo requests",
}, []string{"target", "resolved_addr"})
icmpReceivedRequests = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "observer_received_icmp_requests",
Help: "Total number of received ICMP echo replies",
}, []string{"target", "resolved_addr"})
)
func init() {
prometheus.MustRegister(icmpAvgRtt)
}
func sampleIcmp(targets []string, count int) error {
for _, target := range targets {
f := icmpSetupFailures.With(prometheus.Labels{
"target": target,
})
f.Add(float64(0))
pinger, err := probing.NewPinger(target)
if err != nil {
f.Inc()
return err
}
pinger.SetPrivileged(true)
pinger.Count = count
pinger.InterfaceName = iface
var af string
if !disable4 {
af = "ip6"
} else if !disable6 {
af = "ip4"
} else {
af = "ip"
}
pinger.SetNetwork(af)
err = pinger.Run()
if err != nil {
f.Inc()
return err
}
stats := pinger.Statistics()
icmpSentRequests.With(prometheus.Labels{
"target": target,
"resolved_addr": stats.IPAddr.String(),
}).Add(float64(stats.PacketsSent))
icmpReceivedRequests.With(prometheus.Labels{
"target": target,
"resolved_addr": stats.IPAddr.String(),
}).Add(float64(stats.PacketsRecv))
icmpAvgRtt.With(prometheus.Labels{
"target": target,
"resolved_addr": stats.IPAddr.String(),
}).Set(stats.AvgRtt.Seconds())
}
return nil
}