Skip to content
Draft
Show file tree
Hide file tree
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
36 changes: 24 additions & 12 deletions internal/prober/icmp/icmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package icmp
import (
"context"
"errors"
"fmt"
"sync"
"time"

Expand All @@ -20,6 +21,7 @@ type Module struct {
Prober string
Timeout time.Duration
PacketCount int64
PacketWaitCount int64
ReqSuccessCount int64
MaxResolveRetries int64
ICMP config.ICMPProbe
Expand Down Expand Up @@ -49,6 +51,7 @@ func (p Prober) Name() string {
}

func (p Prober) Probe(ctx context.Context, target string, registry *prometheus.Registry, l logger.Logger) (bool, float64) {
l.Log("config", fmt.Sprintf("%#v", p.config))
return probeICMP(ctx, target, p.config, registry, l)
}

Expand All @@ -65,16 +68,23 @@ func settingsToModule(settings *sm.PingSettings) Module {

m.ICMP.DontFragment = settings.DontFragment

if settings.PacketCount == 0 {
m.PacketCount = 3
m.ReqSuccessCount = 1
} else {
m.PacketCount = settings.PacketCount
m.ReqSuccessCount = settings.PacketCount // TODO(mem): expose this setting
m.PacketCount = settings.PacketCount
m.PacketWaitCount = settings.WaitCount
m.ReqSuccessCount = settings.SuccessCount

if m.PacketCount == 0 {
m.PacketCount = 3 // Send out 3 by default.
}

if m.PacketWaitCount == 0 {
m.PacketWaitCount = 1 // m.PacketCount // Wait for all of them.
}

if m.ReqSuccessCount == 0 {
m.ReqSuccessCount = 1 // Receiving at least 1 is considered success.
}

// TODO(mem): add a setting for this
m.MaxResolveRetries = 3
m.MaxResolveRetries = 3 // TODO(mem): add a setting for this

return m
}
Expand All @@ -99,10 +109,12 @@ func isPrivilegedRequired() bool {
registry = prometheus.NewRegistry()
logger = log.NewNopLogger()
config = Module{
Prober: "test-unprivileged",
Timeout: 1 * time.Second,
PacketCount: 1,
Privileged: false,
Prober: "test-unprivileged",
Timeout: 1 * time.Second,
PacketCount: 1,
PacketWaitCount: 1,
ReqSuccessCount: 1,
Privileged: false,
ICMP: config.ICMPProbe{
IPProtocol: "ip4",
},
Expand Down
27 changes: 26 additions & 1 deletion internal/prober/icmp/icmp_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package icmp

import (
"context"
"errors"
"fmt"
"strconv"
"sync/atomic"
"time"

"github.com/go-kit/kit/log" //nolint:staticcheck // TODO(mem): replace in BBE
Expand Down Expand Up @@ -50,6 +52,9 @@ func probeICMP(ctx context.Context, target string, module Module, registry *prom
})
)

ctx, cancel := context.WithCancel(ctx)
defer cancel()

for _, lv := range []string{"resolve", "setup", "rtt"} {
durationGaugeVec.WithLabelValues(lv)
}
Expand Down Expand Up @@ -104,13 +109,20 @@ func probeICMP(ctx context.Context, target string, module Module, registry *prom
_ = level.Info(logger).Log("msg", "Waiting for reply packets")
}

var received atomic.Int64

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))

if received.Add(1) >= module.PacketWaitCount {
// Cancel the context to the run.
cancel()
}
}

pinger.OnDuplicateRecv = func(pkt *ping.Packet) {
Expand Down Expand Up @@ -145,12 +157,25 @@ func probeICMP(ctx context.Context, target string, module Module, registry *prom

_ = level.Info(logger).Log("msg", "Creating socket")

if err := pinger.RunWithContext(ctx); err != nil {
err = pinger.RunWithContext(ctx)

switch {
case errors.Is(err, context.DeadlineExceeded):

case errors.Is(err, context.Canceled):

case err == nil:

default:
_ = level.Info(logger).Log("msg", "failed to run ping", "err", err.Error())

return false, 0
}

_ = level.Info(logger).Log("msg", "stopping ping", "err", err.Error())

return pinger.PacketsSent >= int(module.ReqSuccessCount) && pinger.PacketsRecv >= int(module.ReqSuccessCount), duration

}

type icmpLogger struct {
Expand Down
Binary file modified pkg/pb/synthetic_monitoring/checks.binpb
Binary file not shown.
Loading
Loading