Files
beszel-ipv6/agent/probe_ping.go
xiaomiku01 865e6db90f feat(agent): add ProbeManager with ICMP/TCP/HTTP probes and handlers
Implements the core probe execution engine (ProbeManager) that runs
network probes on configurable intervals, collects latency samples,
and aggregates results over a 60s sliding window. Adds two new
WebSocket handlers (SyncNetworkProbes, GetNetworkProbeResults) for
hub-agent communication and integrates probe lifecycle into the agent.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-11 01:21:38 +08:00

45 lines
986 B
Go

package agent
import (
"os/exec"
"regexp"
"runtime"
"strconv"
"time"
)
var pingTimeRegex = regexp.MustCompile(`time[=<]([\d.]+)\s*ms`)
// probeICMP executes system ping command and parses latency. Returns -1 on failure.
func probeICMP(target string) float64 {
var cmd *exec.Cmd
switch runtime.GOOS {
case "windows":
cmd = exec.Command("ping", "-n", "1", "-w", "3000", target)
default: // linux, darwin, freebsd
cmd = exec.Command("ping", "-c", "1", "-W", "3", target)
}
start := time.Now()
output, err := cmd.Output()
if err != nil {
// If ping fails but we got output, still try to parse
if len(output) == 0 {
return -1
}
}
matches := pingTimeRegex.FindSubmatch(output)
if len(matches) >= 2 {
if ms, err := strconv.ParseFloat(string(matches[1]), 64); err == nil {
return ms
}
}
// Fallback: use wall clock time if ping succeeded but parsing failed
if err == nil {
return float64(time.Since(start).Microseconds()) / 1000.0
}
return -1
}