💥 feat(agent)!: 添加主机监控代理首版
* 实现周期采集、快照缓存、本地 HTTP API 和主动上报 * 补齐 Linux 和 Windows 主机、网络、资源、进程和 TCP 采集 * 添加服务生命周期管理、配置加载、构建脚本和测试覆盖 BREAKING CHANGE: network.ips 拆分为 network.ipv4s 和 network.ipv6s。
This commit is contained in:
75
internal/collector/collector_other.go
Normal file
75
internal/collector/collector_other.go
Normal file
@@ -0,0 +1,75 @@
|
||||
//go:build !linux && !windows
|
||||
|
||||
package collector
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"mfd-monitor-agent/internal/status"
|
||||
)
|
||||
|
||||
// collectSystem fills portable host identity fields for non-Linux platforms.
|
||||
func collectSystem(_ context.Context, hostID string, out *status.HostStatus) error {
|
||||
hostname, err := os.Hostname()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
out.Host.HostID = hostID
|
||||
if out.Host.HostID == "" {
|
||||
out.Host.HostID = hostname
|
||||
}
|
||||
out.Host.Hostname = hostname
|
||||
out.Host.Platform = runtime.GOOS
|
||||
out.Host.Arch = runtime.GOARCH
|
||||
return nil
|
||||
}
|
||||
|
||||
// collectNetwork fills interface, address, and MAC data using the portable net package.
|
||||
func collectNetwork(_ context.Context, out *status.HostStatus) error {
|
||||
ifaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
seenIPs := map[string]struct{}{}
|
||||
seenMACs := map[string]struct{}{}
|
||||
for _, iface := range ifaces {
|
||||
item := status.NetworkInterface{Name: iface.Name, MAC: iface.HardwareAddr.String(), MTU: iface.MTU, Flags: strings.Fields(iface.Flags.String())}
|
||||
addrs, _ := iface.Addrs()
|
||||
for _, addr := range addrs {
|
||||
item.Addresses = append(item.Addresses, addr.String())
|
||||
seenIPs[addr.String()] = struct{}{}
|
||||
}
|
||||
if item.MAC != "" {
|
||||
seenMACs[item.MAC] = struct{}{}
|
||||
}
|
||||
out.Network.Interfaces = append(out.Network.Interfaces, item)
|
||||
}
|
||||
out.Network.IPv4s, out.Network.IPv6s = networkAddressSets(seenIPs)
|
||||
for mac := range seenMACs {
|
||||
out.Network.MACs = append(out.Network.MACs, mac)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// collectResource fills portable resource fields available without platform-specific APIs.
|
||||
func collectResource(_ context.Context, out *status.HostStatus) error {
|
||||
out.CPU.LogicalCores = runtime.NumCPU()
|
||||
return nil
|
||||
}
|
||||
|
||||
// collectProcesses reports partial process collection until platform-specific support is added.
|
||||
func collectProcesses(_ context.Context, out *status.HostStatus) error {
|
||||
out.Processes.Status = "partial"
|
||||
return fmt.Errorf("process collection is not implemented for %s without platform-specific support", runtime.GOOS)
|
||||
}
|
||||
|
||||
// collectSockets reports partial TCP listener collection until platform-specific support is added.
|
||||
func collectSockets(_ context.Context, out *status.HostStatus) error {
|
||||
out.TCPListeners.Status = "partial"
|
||||
return fmt.Errorf("tcp listener process mapping is not implemented for %s without platform-specific support", runtime.GOOS)
|
||||
}
|
||||
Reference in New Issue
Block a user