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>
This commit is contained in:
xiaomiku01
2026-04-11 00:26:02 +08:00
parent a42d899e64
commit 865e6db90f
5 changed files with 306 additions and 0 deletions

44
agent/probe_ping.go Normal file
View File

@@ -0,0 +1,44 @@
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
}