✨ feat(agent): 添加 token 鉴权和流程文档
* 默认监听调整为 0.0.0.0:8080,并要求查询参数 token * 支持首次 run 自动生成含随机 token 的配置文件 * 补充 HTTP 鉴权、配置校验和相关测试 * 完善代码注释,覆盖缓存、上报、服务和平台采集路径 * 新增架构与开发流程文档,补充一键构建脚本
This commit is contained in:
@@ -5,22 +5,32 @@ import "time"
|
||||
// HostStatus 是 /status 返回的完整缓存主机快照,也是上报器 POST 的载荷结构。
|
||||
type HostStatus struct {
|
||||
// Agent 描述 agent 进程及最近一次快照的元数据。
|
||||
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"`
|
||||
Agent AgentInfo `json:"agent"`
|
||||
// Host 描述被监控主机的身份、平台与运行时长。
|
||||
Host HostInfo `json:"host"`
|
||||
// Network 汇总网卡、IP 与 MAC 地址信息。
|
||||
Network NetworkInfo `json:"network"`
|
||||
// CPU 描述主机 CPU 拓扑与最近一次采样利用率。
|
||||
CPU CPUInfo `json:"cpu"`
|
||||
// Memory 描述主机内存容量与使用率。
|
||||
Memory MemoryInfo `json:"memory"`
|
||||
// Disks 列出已挂载且被纳入监控的文件系统容量数据。
|
||||
Disks []DiskInfo `json:"disks"`
|
||||
// Processes 包含进程采集状态与完整进程列表。
|
||||
Processes ProcessResult `json:"processes"`
|
||||
// TCPListeners 包含 TCP 监听端口采集状态与端口到进程的映射。
|
||||
TCPListeners TCPResult `json:"tcp_listeners"`
|
||||
// Errors 记录导致快照变为 partial 的子采集器失败信息。
|
||||
Errors []StatusError `json:"errors"`
|
||||
}
|
||||
|
||||
// AgentInfo 包含 agent 进程元数据及最近一次采集结果。
|
||||
type AgentInfo struct {
|
||||
Version string `json:"version"`
|
||||
StartedAt time.Time `json:"started_at"`
|
||||
// Version 是当前 agent 二进制版本。
|
||||
Version string `json:"version"`
|
||||
// StartedAt 是 agent 进程启动时间(UTC)。
|
||||
StartedAt time.Time `json:"started_at"`
|
||||
// SnapshotAt 是最近一次快照开始构造的时间(UTC)。
|
||||
SnapshotAt time.Time `json:"snapshot_at"`
|
||||
// SnapshotStatus 在所有采集器成功时为 ok,任一失败时为 partial。
|
||||
SnapshotStatus string `json:"snapshot_status"`
|
||||
@@ -28,41 +38,58 @@ type AgentInfo struct {
|
||||
|
||||
// HostInfo 描述被监控主机的标识与操作系统信息。
|
||||
type HostInfo struct {
|
||||
HostID string `json:"host_id"`
|
||||
Hostname string `json:"hostname"`
|
||||
Platform string `json:"platform"`
|
||||
// HostID 是配置的机器标识;为空时平台采集器通常回退为主机名。
|
||||
HostID string `json:"host_id"`
|
||||
// Hostname 是操作系统返回的当前主机名。
|
||||
Hostname string `json:"hostname"`
|
||||
// Platform 是 Go 运行时平台名,例如 linux 或 windows。
|
||||
Platform string `json:"platform"`
|
||||
// PlatformVersion 是尽力采集的平台版本说明。
|
||||
PlatformVersion string `json:"platform_version"`
|
||||
Arch string `json:"arch"`
|
||||
// Arch 是 Go 运行时架构名,例如 amd64。
|
||||
Arch string `json:"arch"`
|
||||
// UptimeSeconds 为主机运行时长(秒),在可用时填充。
|
||||
UptimeSeconds uint64 `json:"uptime_seconds"`
|
||||
}
|
||||
|
||||
// NetworkInfo 包含全部网卡及展平后的 IPv4、IPv6、MAC 列表。
|
||||
type NetworkInfo struct {
|
||||
// Interfaces 列出所有可见网卡及其地址。
|
||||
Interfaces []NetworkInterface `json:"interfaces"`
|
||||
IPv4s []string `json:"ipv4s"`
|
||||
IPv6s []string `json:"ipv6s"`
|
||||
MACs []string `json:"macs"`
|
||||
// IPv4s 是从网卡地址展平并排序后的 IPv4 CIDR/地址列表。
|
||||
IPv4s []string `json:"ipv4s"`
|
||||
// IPv6s 是从网卡地址展平并排序后的 IPv6 CIDR/地址列表。
|
||||
IPv6s []string `json:"ipv6s"`
|
||||
// MACs 是去重并排序后的非空 MAC 地址列表。
|
||||
MACs []string `json:"macs"`
|
||||
}
|
||||
|
||||
// NetworkInterface 描述一块网卡及其配置的地址。
|
||||
type NetworkInterface struct {
|
||||
// Name 是操作系统中的网卡名称。
|
||||
Name string `json:"name"`
|
||||
MAC string `json:"mac"`
|
||||
// MAC 是网卡硬件地址;虚拟或环回接口可能为空。
|
||||
MAC string `json:"mac"`
|
||||
// MTU 为接口最大传输单元(字节)。
|
||||
MTU int `json:"mtu"`
|
||||
Flags []string `json:"flags"`
|
||||
MTU int `json:"mtu"`
|
||||
// Flags 是操作系统接口标志的稳定字符串列表。
|
||||
Flags []string `json:"flags"`
|
||||
// Addresses 是接口配置的原始地址列表,通常包含 CIDR 前缀。
|
||||
Addresses []string `json:"addresses"`
|
||||
}
|
||||
|
||||
// CPUInfo 描述主机 CPU 容量与当前利用率。
|
||||
type CPUInfo struct {
|
||||
LogicalCores int `json:"logical_cores"`
|
||||
// LogicalCores 是运行时可见的逻辑 CPU 数。
|
||||
LogicalCores int `json:"logical_cores"`
|
||||
// PhysicalCores 是尽力采集的物理核心数;平台不可用时可能为 0。
|
||||
PhysicalCores int `json:"physical_cores"`
|
||||
// UsagePercent 为最近一次采集得到的尽力主机 CPU 使用率(百分比)。
|
||||
UsagePercent float64 `json:"usage_percent"`
|
||||
LoadAvg []float64 `json:"load_avg"`
|
||||
ModelName string `json:"model_name"`
|
||||
UsagePercent float64 `json:"usage_percent"`
|
||||
// LoadAvg 是平台支持时的 1/5/15 分钟负载均值。
|
||||
LoadAvg []float64 `json:"load_avg"`
|
||||
// ModelName 是尽力采集的 CPU 型号名称。
|
||||
ModelName string `json:"model_name"`
|
||||
}
|
||||
|
||||
// MemoryInfo 描述主机内存容量与使用情况,数值单位由 Unit 字段声明。
|
||||
@@ -83,9 +110,12 @@ type MemoryInfo struct {
|
||||
|
||||
// DiskInfo 描述一块已挂载文件系统的容量与使用情况。
|
||||
type DiskInfo struct {
|
||||
Device string `json:"device"`
|
||||
// Device 是文件系统来源设备或盘符。
|
||||
Device string `json:"device"`
|
||||
// MountPoint 是文件系统挂载点或 Windows 盘符根路径。
|
||||
MountPoint string `json:"mount_point"`
|
||||
FSType string `json:"fs_type"`
|
||||
// FSType 是文件系统类型或平台驱动器类型标签。
|
||||
FSType string `json:"fs_type"`
|
||||
// Unit 声明 total、used、free 使用的单位。
|
||||
Unit string `json:"unit"`
|
||||
// Total 为文件系统总容量(与 Unit 一致)。
|
||||
@@ -101,19 +131,27 @@ type DiskInfo struct {
|
||||
// ProcessResult 包含进程采集状态及进程列表。
|
||||
type ProcessResult struct {
|
||||
// Status 根据进程采集完整程度为 ok、partial 或 error。
|
||||
Status string `json:"status"`
|
||||
Items []ProcessInfo `json:"items"`
|
||||
Status string `json:"status"`
|
||||
// Items 是最近一次采集到的进程列表;partial 时仍包含成功读取的条目。
|
||||
Items []ProcessInfo `json:"items"`
|
||||
}
|
||||
|
||||
// ProcessInfo 描述一个操作系统进程。
|
||||
type ProcessInfo struct {
|
||||
PID int `json:"pid"`
|
||||
PPID int `json:"ppid"`
|
||||
Name string `json:"name"`
|
||||
Exe string `json:"exe"`
|
||||
Cmdline string `json:"cmdline"`
|
||||
// PID 是操作系统进程 ID。
|
||||
PID int `json:"pid"`
|
||||
// PPID 是父进程 ID。
|
||||
PPID int `json:"ppid"`
|
||||
// Name 是进程短名称。
|
||||
Name string `json:"name"`
|
||||
// Exe 是可执行文件路径;权限不足时可能为空或退化为短名称。
|
||||
Exe string `json:"exe"`
|
||||
// Cmdline 是完整命令行;权限或平台限制可能导致为空。
|
||||
Cmdline string `json:"cmdline"`
|
||||
// Username 是进程所属用户;权限不足时可能为空。
|
||||
Username string `json:"username"`
|
||||
Status string `json:"status"`
|
||||
// Status 是平台提供的进程状态标签。
|
||||
Status string `json:"status"`
|
||||
// StartTime 为平台相关的进程启动时间戳,在可用时填充。
|
||||
StartTime int64 `json:"start_time"`
|
||||
// CPUPercent 为尽力估算的进程 CPU 使用率(百分比)。
|
||||
@@ -127,22 +165,31 @@ type ProcessInfo struct {
|
||||
// TCPResult 包含 TCP 监听采集状态及监听项列表。
|
||||
type TCPResult struct {
|
||||
// Status 根据监听端口到进程映射的完整程度为 ok、partial 或 error。
|
||||
Status string `json:"status"`
|
||||
Items []TCPListenerInfo `json:"items"`
|
||||
Status string `json:"status"`
|
||||
// Items 是监听 TCP 套接字列表;partial 时可能缺少部分进程映射。
|
||||
Items []TCPListenerInfo `json:"items"`
|
||||
}
|
||||
|
||||
// TCPListenerInfo 描述一个处于监听状态的 TCP 套接字及所属进程(若已知)。
|
||||
type TCPListenerInfo struct {
|
||||
IP string `json:"ip"`
|
||||
Port int `json:"port"`
|
||||
PID int `json:"pid"`
|
||||
// IP 是本地监听地址。
|
||||
IP string `json:"ip"`
|
||||
// Port 是本地监听端口。
|
||||
Port int `json:"port"`
|
||||
// PID 是监听套接字所属进程 ID;未知时为 0。
|
||||
PID int `json:"pid"`
|
||||
// ProcessName 是 PID 对应的进程名称;映射失败时为空。
|
||||
ProcessName string `json:"process_name"`
|
||||
ProcessExe string `json:"process_exe"`
|
||||
Family string `json:"family"`
|
||||
// ProcessExe 是 PID 对应的可执行路径;权限不足时可能为空。
|
||||
ProcessExe string `json:"process_exe"`
|
||||
// Family 是地址族标签,通常为 ipv4 或 ipv6。
|
||||
Family string `json:"family"`
|
||||
}
|
||||
|
||||
// StatusError 记录单个子采集器失败,不导致整份快照请求失败。
|
||||
type StatusError struct {
|
||||
// Collector 是失败或降级的子采集器稳定名称。
|
||||
Collector string `json:"collector"`
|
||||
Message string `json:"message"`
|
||||
// Message 是面向排障的错误摘要,不保证适合终端用户展示。
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user