-
Notifications
You must be signed in to change notification settings - Fork 24
/
icmp_impl.go
179 lines (137 loc) · 5.3 KB
/
icmp_impl.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package icmp
import (
"context"
"fmt"
"strconv"
"time"
"github.com/go-kit/kit/log" //nolint:staticcheck // TODO(mem): replace in BBE
"github.com/go-kit/kit/log/level" //nolint:staticcheck // TODO(mem): replace in BBE
"github.com/go-ping/ping"
"github.com/prometheus/client_golang/prometheus"
)
func probeICMP(ctx context.Context, target string, module Module, registry *prometheus.Registry, logger log.Logger) (success bool) {
var (
durationGaugeVec = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "probe_icmp_duration_seconds",
Help: "Duration of icmp request by phase",
}, []string{"phase"})
durationMaxGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "probe_icmp_duration_rtt_max_seconds",
Help: "Maximum duration of round trip time phase",
})
durationMinGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "probe_icmp_duration_rtt_min_seconds",
Help: "Minimum duration of round trip time phase",
})
durationStddevGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "probe_icmp_duration_rtt_stddev_seconds",
Help: "Standard deviation of round trip time phase",
})
packetsSentGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "probe_icmp_packets_sent_count",
Help: "Number of ICMP packets sent",
})
packetsReceivedGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "probe_icmp_packets_received_count",
Help: "Number of ICMP packets received",
})
hopLimitGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "probe_icmp_reply_hop_limit",
Help: "Replied packet hop limit (TTL for ipv4)",
})
)
for _, lv := range []string{"resolve", "setup", "rtt"} {
durationGaugeVec.WithLabelValues(lv)
}
registry.MustRegister(durationGaugeVec)
registry.MustRegister(durationMaxGauge)
registry.MustRegister(durationMinGauge)
registry.MustRegister(durationStddevGauge)
registry.MustRegister(packetsSentGauge)
registry.MustRegister(packetsReceivedGauge)
dstIPAddr, lookupTime, err := chooseProtocol(ctx, module.ICMP.IPProtocol, module.ICMP.IPProtocolFallback, target, registry, logger)
if err != nil {
_ = level.Warn(logger).Log("msg", "Error resolving address", "err", err)
return false
}
durationGaugeVec.WithLabelValues("resolve").Add(lookupTime)
pinger := ping.New(dstIPAddr.String())
pinger.SetPrivileged(module.Privileged)
if err := pinger.Resolve(); err != nil {
// This should never happe, the address is already resolved.
_ = level.Error(logger).Log("msg", "Error resolving address", "err", err)
return false
}
pinger.Timeout = module.Timeout
pinger.SetLogger(icmpLogger{logger})
var (
setupStart time.Time
setupDone bool
)
pinger.OnSetup = func() {
if !setupDone {
durationGaugeVec.WithLabelValues("setup").Add(time.Since(setupStart).Seconds())
setupDone = true
}
_ = level.Info(logger).Log("msg", "Using source address", "srcIP", pinger.Source)
}
pinger.OnSend = func(pkt *ping.Packet) {
_ = level.Info(logger).Log("msg", "Creating ICMP packet", "seq", strconv.Itoa(pkt.Seq))
_ = level.Info(logger).Log("msg", "Waiting for reply packets")
}
pinger.OnRecv = func(pkt *ping.Packet) {
if pkt.Seq == 0 && pkt.Ttl >= 0 {
registry.MustRegister(hopLimitGauge)
hopLimitGauge.Set(float64(pkt.Ttl))
}
_ = level.Info(logger).Log("msg", "Found matching reply packet", "seq", strconv.Itoa(pkt.Seq))
}
pinger.OnDuplicateRecv = func(pkt *ping.Packet) {
_ = level.Info(logger).Log("msg", "Duplicate packet received", "seq", strconv.Itoa(pkt.Seq))
}
pinger.OnFinish = func(stats *ping.Statistics) {
durationGaugeVec.WithLabelValues("rtt").Set(stats.AvgRtt.Seconds())
durationMaxGauge.Set(stats.MaxRtt.Seconds())
durationMinGauge.Set(stats.MinRtt.Seconds())
durationStddevGauge.Set(stats.StdDevRtt.Seconds())
packetsSentGauge.Set(float64(stats.PacketsSent))
packetsReceivedGauge.Set(float64(stats.PacketsRecv))
_ = level.Info(logger).Log("msg", "Probe finished", "packets_sent", stats.PacketsSent, "packets_received", stats.PacketsRecv)
}
// TODO: module.ICMP.DontFragment
if module.ICMP.DontFragment {
_ = level.Warn(logger).Log("msg", "ignoring DontFragment option")
}
if module.ICMP.PayloadSize != 0 {
pinger.Size = module.ICMP.PayloadSize
}
pinger.Count = int(module.PacketCount)
pinger.Interval = 50 * time.Millisecond
pinger.RecordRtts = false
pinger.Source = module.ICMP.SourceIPAddress
setupStart = time.Now()
_ = level.Info(logger).Log("msg", "Creating socket")
if err := pinger.Run(); err != nil {
_ = level.Info(logger).Log("msg", "failed to run ping", "err", err.Error())
return false
}
return pinger.Count == pinger.PacketsSent && pinger.PacketsRecv == pinger.PacketsSent
}
type icmpLogger struct {
logger log.Logger
}
func (l icmpLogger) Fatalf(format string, v ...interface{}) {
_ = level.Error(l.logger).Log("msg", fmt.Sprintf(format, v...))
}
func (l icmpLogger) Errorf(format string, v ...interface{}) {
_ = level.Error(l.logger).Log("msg", fmt.Sprintf(format, v...))
}
func (l icmpLogger) Warnf(format string, v ...interface{}) {
_ = level.Warn(l.logger).Log("msg", fmt.Sprintf(format, v...))
}
func (l icmpLogger) Infof(format string, v ...interface{}) {
_ = level.Info(l.logger).Log("msg", fmt.Sprintf(format, v...))
}
func (l icmpLogger) Debugf(format string, v ...interface{}) {
_ = level.Debug(l.logger).Log("msg", fmt.Sprintf(format, v...))
}