docs(agent): 统一中文注释并补充部署文档
将代码与测试注释改为中文以符合仓库规范;新增中文部署指南、 示例配置,并完善构建脚本对 VERSION 注入的说明。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -26,7 +26,7 @@ import (
|
||||
var socketInodesFunc = socketInodes
|
||||
var readTCPListenersFunc = readTCPListeners
|
||||
|
||||
// collectSystem fills Linux host identity, OS, architecture, and uptime fields.
|
||||
// collectSystem 从 Linux 填充主机标识、操作系统、架构与运行时长字段。
|
||||
func collectSystem(_ context.Context, hostID string, out *status.HostStatus) error {
|
||||
hostname, err := os.Hostname()
|
||||
if err != nil {
|
||||
@@ -44,7 +44,7 @@ func collectSystem(_ context.Context, hostID string, out *status.HostStatus) err
|
||||
return nil
|
||||
}
|
||||
|
||||
// collectNetwork fills Linux network interface, address, and MAC data.
|
||||
// collectNetwork 填充 Linux 网卡、地址与 MAC 数据。
|
||||
func collectNetwork(_ context.Context, out *status.HostStatus) error {
|
||||
ifaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
@@ -75,7 +75,7 @@ func collectNetwork(_ context.Context, out *status.HostStatus) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// collectResource fills CPU, memory, and disk resource data from Linux proc files.
|
||||
// collectResource 从 Linux /proc 文件填充 CPU、内存与磁盘资源数据。
|
||||
func collectResource(ctx context.Context, out *status.HostStatus) error {
|
||||
out.CPU.LogicalCores = runtime.NumCPU()
|
||||
out.CPU.PhysicalCores, out.CPU.ModelName = readCPUInfo()
|
||||
@@ -90,7 +90,7 @@ func collectResource(ctx context.Context, out *status.HostStatus) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// collectProcesses reads /proc process directories and degrades to partial when some entries fail.
|
||||
// collectProcesses 读取 /proc 进程目录;部分条目失败时降级为 partial。
|
||||
func collectProcesses(_ context.Context, out *status.HostStatus) error {
|
||||
entries, err := os.ReadDir("/proc")
|
||||
if err != nil {
|
||||
@@ -124,7 +124,7 @@ func collectProcesses(_ context.Context, out *status.HostStatus) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// collectSockets reads Linux TCP listener tables and maps socket inodes to owning processes.
|
||||
// collectSockets 读取 Linux TCP 监听表,并将 socket inode 映射到所属进程。
|
||||
func collectSockets(_ context.Context, out *status.HostStatus) error {
|
||||
inodes, inodeErr := socketInodesFunc()
|
||||
var listeners []status.TCPListenerInfo
|
||||
@@ -161,7 +161,7 @@ func collectSockets(_ context.Context, out *status.HostStatus) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// readOSRelease returns the Linux PRETTY_NAME value when /etc/os-release is readable.
|
||||
// readOSRelease 在可读时从 /etc/os-release 返回 PRETTY_NAME。
|
||||
func readOSRelease() string {
|
||||
file, err := os.Open("/etc/os-release")
|
||||
if err != nil {
|
||||
@@ -178,7 +178,7 @@ func readOSRelease() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// readUptime returns host uptime in seconds from /proc/uptime.
|
||||
// readUptime 从 /proc/uptime 返回主机运行时长(秒)。
|
||||
func readUptime() uint64 {
|
||||
data, err := os.ReadFile("/proc/uptime")
|
||||
if err != nil {
|
||||
@@ -192,7 +192,7 @@ func readUptime() uint64 {
|
||||
return uint64(value)
|
||||
}
|
||||
|
||||
// readCPUInfo returns physical CPU package count and the first model name from /proc/cpuinfo.
|
||||
// readCPUInfo 从 /proc/cpuinfo 返回物理 CPU 封装数与首个型号名称。
|
||||
func readCPUInfo() (int, string) {
|
||||
file, err := os.Open("/proc/cpuinfo")
|
||||
if err != nil {
|
||||
@@ -222,7 +222,7 @@ func readCPUInfo() (int, string) {
|
||||
return len(physical), model
|
||||
}
|
||||
|
||||
// readLoadAvg returns the 1, 5, and 15 minute Linux load averages when available.
|
||||
// readLoadAvg 在可用时返回 Linux 1/5/15 分钟负载均值。
|
||||
func readLoadAvg() []float64 {
|
||||
data, err := os.ReadFile("/proc/loadavg")
|
||||
if err != nil {
|
||||
@@ -239,7 +239,7 @@ func readLoadAvg() []float64 {
|
||||
return values
|
||||
}
|
||||
|
||||
// readMemory returns Linux memory counters in bytes from /proc/meminfo.
|
||||
// readMemory 从 /proc/meminfo 返回以字节为单位的 Linux 内存计数。
|
||||
func readMemory() status.MemoryInfo {
|
||||
values := map[string]uint64{}
|
||||
file, err := os.Open("/proc/meminfo")
|
||||
@@ -254,8 +254,8 @@ func readMemory() status.MemoryInfo {
|
||||
continue
|
||||
}
|
||||
value, _ := strconv.ParseUint(fields[1], 10, 64)
|
||||
// /proc/meminfo reports memory in KiB; the API exposes bytes and
|
||||
// includes memory.unit so clients can render the values explicitly.
|
||||
// /proc/meminfo 以 KiB 报告内存;API 对外暴露字节,
|
||||
// 并通过 memory.unit 让客户端可明确展示单位。
|
||||
values[strings.TrimSuffix(fields[0], ":")] = value * 1024
|
||||
}
|
||||
total := values["MemTotal"]
|
||||
@@ -269,7 +269,7 @@ func readMemory() status.MemoryInfo {
|
||||
return status.MemoryInfo{Unit: "byte", Total: total, Used: used, Free: free, Available: available, UsedPercent: percent}
|
||||
}
|
||||
|
||||
// readDisks returns physical and network mounted filesystem usage from /proc/mounts and statfs.
|
||||
// readDisks 从 /proc/mounts 与 statfs 返回物理盘与网络挂载文件系统用量。
|
||||
func readDisks() ([]status.DiskInfo, error) {
|
||||
file, err := os.Open("/proc/mounts")
|
||||
if err != nil {
|
||||
@@ -305,7 +305,7 @@ func readDisks() ([]status.DiskInfo, error) {
|
||||
return disks, scanner.Err()
|
||||
}
|
||||
|
||||
// shouldIncludeDiskMount reports whether a mount represents a user-relevant disk.
|
||||
// shouldIncludeDiskMount 判断挂载点是否代表用户关心的磁盘。
|
||||
func shouldIncludeDiskMount(device string, fsType string, total uint64) bool {
|
||||
if total == 0 {
|
||||
return false
|
||||
@@ -319,7 +319,7 @@ func shouldIncludeDiskMount(device string, fsType string, total uint64) bool {
|
||||
return strings.HasPrefix(device, "/dev/")
|
||||
}
|
||||
|
||||
// readProcess returns one Linux process entry from /proc/<pid>.
|
||||
// readProcess 从 /proc/<pid> 返回一条 Linux 进程记录。
|
||||
func readProcess(pid int) (status.ProcessInfo, error) {
|
||||
base := filepath.Join("/proc", strconv.Itoa(pid))
|
||||
statusValues, err := readProcessStatus(filepath.Join(base, "status"))
|
||||
@@ -348,12 +348,12 @@ func readProcess(pid int) (status.ProcessInfo, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
// processStat contains process counters parsed from /proc/<pid>/stat.
|
||||
// processStat 包含从 /proc/<pid>/stat 解析的进程计数。
|
||||
type processStat struct {
|
||||
StartTime int64
|
||||
}
|
||||
|
||||
// readProcessStat parses Linux process stat counters from path.
|
||||
// readProcessStat 从路径解析 Linux 进程 stat 计数。
|
||||
func readProcessStat(path string) (processStat, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
@@ -366,7 +366,7 @@ func readProcessStat(path string) (processStat, error) {
|
||||
return processStat{StartTime: startTime}, nil
|
||||
}
|
||||
|
||||
// readProcessStatus parses key-value fields from /proc/<pid>/status.
|
||||
// readProcessStatus 从 /proc/<pid>/status 解析键值字段。
|
||||
func readProcessStatus(path string) (map[string]string, error) {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
@@ -384,7 +384,7 @@ func readProcessStatus(path string) (map[string]string, error) {
|
||||
return values, scanner.Err()
|
||||
}
|
||||
|
||||
// socketInodes maps Linux socket inode numbers to best-effort owning process data.
|
||||
// socketInodes 将 Linux socket inode 映射到尽力获取的所属进程数据。
|
||||
func socketInodes() (map[string]status.ProcessInfo, error) {
|
||||
out := map[string]status.ProcessInfo{}
|
||||
var readErrors int
|
||||
@@ -419,7 +419,7 @@ func socketInodes() (map[string]status.ProcessInfo, error) {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// readTCPListeners parses a Linux TCP table and returns sockets in LISTEN state.
|
||||
// readTCPListeners 解析 Linux TCP 表并返回处于 LISTEN 状态的套接字。
|
||||
func readTCPListeners(path string, family string, inodes map[string]status.ProcessInfo) ([]status.TCPListenerInfo, error) {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
@@ -429,7 +429,7 @@ func readTCPListeners(path string, family string, inodes map[string]status.Proce
|
||||
var items []status.TCPListenerInfo
|
||||
scanner := bufio.NewScanner(file)
|
||||
if scanner.Scan() {
|
||||
// Skip header.
|
||||
// 跳过表头行。
|
||||
}
|
||||
for scanner.Scan() {
|
||||
fields := strings.Fields(scanner.Text())
|
||||
@@ -449,7 +449,7 @@ func readTCPListeners(path string, family string, inodes map[string]status.Proce
|
||||
return items, scanner.Err()
|
||||
}
|
||||
|
||||
// readCPUUsagePercent samples /proc/stat over a short interval and returns best-effort usage.
|
||||
// readCPUUsagePercent 在短间隔内采样 /proc/stat 并返回尽力估算的 CPU 使用率。
|
||||
func readCPUUsagePercent(ctx context.Context) float64 {
|
||||
prevData, err := os.ReadFile("/proc/stat")
|
||||
if err != nil {
|
||||
@@ -475,7 +475,7 @@ func readCPUUsagePercent(ctx context.Context) float64 {
|
||||
return cpuUsagePercent(prevIdle, prevTotal, nextIdle, nextTotal)
|
||||
}
|
||||
|
||||
// parseAddress decodes a Linux /proc/net/tcp address into IP and port values.
|
||||
// parseAddress 将 Linux /proc/net/tcp 地址解码为 IP 与端口。
|
||||
func parseAddress(raw string, family string) (string, int, error) {
|
||||
parts := strings.Split(raw, ":")
|
||||
if len(parts) != 2 {
|
||||
@@ -502,7 +502,7 @@ func parseAddress(raw string, family string) (string, int, error) {
|
||||
return "", 0, fmt.Errorf("invalid %s address", family)
|
||||
}
|
||||
|
||||
// parseProcessStatStartTime extracts field 22 from Linux /proc/<pid>/stat content.
|
||||
// parseProcessStatStartTime 从 Linux /proc/<pid>/stat 内容提取第 22 字段。
|
||||
func parseProcessStatStartTime(raw string) (int64, error) {
|
||||
end := strings.LastIndex(raw, ")")
|
||||
if end < 0 || end+2 >= len(raw) {
|
||||
@@ -515,7 +515,7 @@ func parseProcessStatStartTime(raw string) (int64, error) {
|
||||
return strconv.ParseInt(fields[19], 10, 64)
|
||||
}
|
||||
|
||||
// parseCPUTimes extracts idle and total jiffy counters from the first cpu line in /proc/stat.
|
||||
// parseCPUTimes 从 /proc/stat 首行 cpu 提取空闲与总 jiffies 计数。
|
||||
func parseCPUTimes(raw string) (idle uint64, total uint64, err error) {
|
||||
scanner := bufio.NewScanner(strings.NewReader(raw))
|
||||
for scanner.Scan() {
|
||||
@@ -544,7 +544,7 @@ func parseCPUTimes(raw string) (idle uint64, total uint64, err error) {
|
||||
return 0, 0, fmt.Errorf("cpu line not found")
|
||||
}
|
||||
|
||||
// cpuUsagePercent calculates non-idle CPU percentage between two /proc/stat samples.
|
||||
// cpuUsagePercent 根据两次 /proc/stat 采样计算非空闲 CPU 百分比。
|
||||
func cpuUsagePercent(prevIdle, prevTotal, nextIdle, nextTotal uint64) float64 {
|
||||
totalDelta := nextTotal - prevTotal
|
||||
if totalDelta == 0 {
|
||||
@@ -557,13 +557,13 @@ func cpuUsagePercent(prevIdle, prevTotal, nextIdle, nextTotal uint64) float64 {
|
||||
return float64(totalDelta-idleDelta) / float64(totalDelta) * 100
|
||||
}
|
||||
|
||||
// parseKB converts the first numeric kilobyte value in raw to bytes.
|
||||
// parseKB 将 raw 中首个以 KiB 表示的数值转换为字节。
|
||||
func parseKB(raw string) uint64 {
|
||||
value, _ := strconv.ParseUint(firstField(raw), 10, 64)
|
||||
return value * 1024
|
||||
}
|
||||
|
||||
// firstField returns the first whitespace-separated token in raw.
|
||||
// firstField 返回 raw 中首个以空白分隔的字段。
|
||||
func firstField(raw string) string {
|
||||
fields := strings.Fields(raw)
|
||||
if len(fields) == 0 {
|
||||
@@ -572,7 +572,7 @@ func firstField(raw string) string {
|
||||
return fields[0]
|
||||
}
|
||||
|
||||
// sortedKeys returns deterministic sorted keys from a string set.
|
||||
// sortedKeys 从字符串集合返回确定顺序的排序键列表。
|
||||
func sortedKeys(values map[string]struct{}) []string {
|
||||
keys := make([]string, 0, len(values))
|
||||
for key := range values {
|
||||
|
||||
Reference in New Issue
Block a user