Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize ICMP echo ID and sequence to random values #412

Merged
merged 1 commit into from
Jan 30, 2019
Merged
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
20 changes: 19 additions & 1 deletion prober/icmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package prober
import (
"bytes"
"context"
"math/rand"
"net"
"os"
"sync"
Expand All @@ -32,10 +33,27 @@ import (
)

var (
icmpID int
icmpSequence uint16
icmpSequenceMutex sync.Mutex
)

func init() {
// PID is typically 1 when running in a container; in that case, set
// the ICMP echo ID to a random value to avoid potential clashes with
// other blackbox_exporter instances. See #411.
if pid := os.Getpid(); pid == 1 {
icmpID = rand.Intn(1 << 16)
} else {
icmpID = pid & 0xffff
}

// Start the ICMP echo sequence at a random offset to prevent them from
// being in sync when several blackbox_exporter instances are restarted
// at the same time. See #411.
icmpSequence = uint16(rand.Intn(1 << 16))
}

func getICMPSequence() uint16 {
icmpSequenceMutex.Lock()
defer icmpSequenceMutex.Unlock()
Expand Down Expand Up @@ -129,7 +147,7 @@ func ProbeICMP(ctx context.Context, target string, module config.Module, registr
}

body := &icmp.Echo{
ID: os.Getpid() & 0xffff,
ID: icmpID,
Seq: int(getICMPSequence()),
Data: data,
}
Expand Down