💥 feat(agent)!: 添加主机监控代理首版

* 实现周期采集、快照缓存、本地 HTTP API 和主动上报

* 补齐 Linux 和 Windows 主机、网络、资源、进程和 TCP 采集

* 添加服务生命周期管理、配置加载、构建脚本和测试覆盖

BREAKING CHANGE: network.ips 拆分为 network.ipv4s 和 network.ipv6s。
This commit is contained in:
codex
2026-05-18 15:25:51 +00:00
commit 89d5e27874
33 changed files with 4205 additions and 0 deletions

148
internal/status/types.go Normal file
View File

@@ -0,0 +1,148 @@
package status
import "time"
// HostStatus is the full cached host snapshot returned by /status and posted by the reporter.
type HostStatus struct {
// Agent describes the agent process and most recent snapshot metadata.
Agent AgentInfo `json:"agent"`
Host HostInfo `json:"host"`
Network NetworkInfo `json:"network"`
CPU CPUInfo `json:"cpu"`
Memory MemoryInfo `json:"memory"`
Disks []DiskInfo `json:"disks"`
Processes ProcessResult `json:"processes"`
TCPListeners TCPResult `json:"tcp_listeners"`
// Errors records collector-level failures that caused a partial snapshot.
Errors []StatusError `json:"errors"`
}
// AgentInfo contains agent process metadata and the latest collection result.
type AgentInfo struct {
Version string `json:"version"`
StartedAt time.Time `json:"started_at"`
SnapshotAt time.Time `json:"snapshot_at"`
// SnapshotStatus is ok when all collectors succeed and partial when one or more fail.
SnapshotStatus string `json:"snapshot_status"`
}
// HostInfo describes the monitored host identity and operating system.
type HostInfo struct {
HostID string `json:"host_id"`
Hostname string `json:"hostname"`
Platform string `json:"platform"`
PlatformVersion string `json:"platform_version"`
Arch string `json:"arch"`
// UptimeSeconds is the host uptime in seconds when available.
UptimeSeconds uint64 `json:"uptime_seconds"`
}
// NetworkInfo contains all network interfaces plus flattened IPv4, IPv6, and MAC lists.
type NetworkInfo struct {
Interfaces []NetworkInterface `json:"interfaces"`
IPv4s []string `json:"ipv4s"`
IPv6s []string `json:"ipv6s"`
MACs []string `json:"macs"`
}
// NetworkInterface describes one host network interface and its configured addresses.
type NetworkInterface struct {
Name string `json:"name"`
MAC string `json:"mac"`
// MTU is the interface maximum transmission unit in bytes.
MTU int `json:"mtu"`
Flags []string `json:"flags"`
Addresses []string `json:"addresses"`
}
// CPUInfo describes host CPU capacity and current utilization.
type CPUInfo struct {
LogicalCores int `json:"logical_cores"`
PhysicalCores int `json:"physical_cores"`
// UsagePercent is the best-effort host CPU usage percentage for the latest collection.
UsagePercent float64 `json:"usage_percent"`
LoadAvg []float64 `json:"load_avg"`
ModelName string `json:"model_name"`
}
// MemoryInfo describes host memory usage capacity fields in the declared unit.
type MemoryInfo struct {
// Unit declares the unit used by total, used, free, and available.
Unit string `json:"unit"`
// Total is total physical memory in the declared unit.
Total uint64 `json:"total"`
// Used is memory currently used in the declared unit.
Used uint64 `json:"used"`
// Free is memory currently free in the declared unit.
Free uint64 `json:"free"`
// Available is memory available to new workloads in the declared unit.
Available uint64 `json:"available"`
// UsedPercent is the percentage of total memory currently used.
UsedPercent float64 `json:"used_percent"`
}
// DiskInfo describes one physical or network mounted filesystem and its capacity.
type DiskInfo struct {
Device string `json:"device"`
MountPoint string `json:"mount_point"`
FSType string `json:"fs_type"`
// Unit declares the unit used by total, used, and free.
Unit string `json:"unit"`
// Total is filesystem capacity in the declared unit.
Total uint64 `json:"total"`
// Used is consumed filesystem capacity in the declared unit.
Used uint64 `json:"used"`
// Free is available filesystem capacity in the declared unit.
Free uint64 `json:"free"`
// UsedPercent is the percentage of total filesystem capacity currently used.
UsedPercent float64 `json:"used_percent"`
}
// ProcessResult contains process collection status and the collected process list.
type ProcessResult struct {
// Status is ok, partial, or error depending on process collection completeness.
Status string `json:"status"`
Items []ProcessInfo `json:"items"`
}
// ProcessInfo describes one operating system process.
type ProcessInfo struct {
PID int `json:"pid"`
PPID int `json:"ppid"`
Name string `json:"name"`
Exe string `json:"exe"`
Cmdline string `json:"cmdline"`
Username string `json:"username"`
Status string `json:"status"`
// StartTime is the platform-specific process start timestamp when available.
StartTime int64 `json:"start_time"`
// CPUPercent is the best-effort process CPU usage percentage.
CPUPercent float64 `json:"cpu_percent"`
// MemoryRSS is resident memory size in bytes.
MemoryRSS uint64 `json:"memory_rss"`
// MemoryVMS is virtual memory size in bytes.
MemoryVMS uint64 `json:"memory_vms"`
}
// TCPResult contains TCP listener collection status and listener entries.
type TCPResult struct {
// Status is ok, partial, or error depending on listener mapping completeness.
Status string `json:"status"`
Items []TCPListenerInfo `json:"items"`
}
// TCPListenerInfo describes one listening TCP socket and its owning process when known.
type TCPListenerInfo struct {
IP string `json:"ip"`
Port int `json:"port"`
PID int `json:"pid"`
ProcessName string `json:"process_name"`
ProcessExe string `json:"process_exe"`
Family string `json:"family"`
}
// StatusError records a collector failure without failing the whole snapshot.
type StatusError struct {
Collector string `json:"collector"`
Message string `json:"message"`
}