- Agent: system.go 添加 readUptime(), SystemStat 携带 Uptime - Backend: Server 模型新增 Kernel 字段, ReceiveReport 持久化 kernel+uptime - Backend: /agent/latest 返回 kernel/uptime/port_info/disk_io_info/disk_total - Frontend: 磁盘监控改为 BPS+IOPS (4张卡片), 系统信息修复内核/运行时长/监听端口/磁盘容量 - 图表单位从 MB/s 改为 KB/s, 添加 formatBPS 工具函数
100 lines
2.5 KiB
Go
100 lines
2.5 KiB
Go
// Package collector 实现 AIOps Agent 系统静态信息采集。
|
|
// 仅在 Agent 启动时执行一次,采集 OS 版本、内核版本等信息。
|
|
//
|
|
// 署名 Logan.Li
|
|
package collector
|
|
|
|
import (
|
|
"bufio"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// SystemInfo 包含系统静态信息(Agent 版本通过编译时注入,非运行时采集)
|
|
type SystemInfo struct {
|
|
OSVersion string // OS 发行版版本 (如 "Ubuntu 22.04.4 LTS")
|
|
Kernel string // 内核版本 (如 "6.8.0-90-generic")
|
|
UptimeSeconds int64 // 系统运行时长(秒)
|
|
}
|
|
|
|
// CollectSystem 采集系统静态信息。
|
|
// 从 /etc/os-release 和 /proc/version 读取(Linux 特定)。
|
|
// 非 Linux 系统返回空值,不报错。
|
|
func CollectSystem() SystemInfo {
|
|
info := SystemInfo{}
|
|
|
|
// 读取 /etc/os-release 获取 PRETTY_NAME
|
|
info.OSVersion = readOSRelease()
|
|
|
|
// 读取 /proc/version 获取内核版本
|
|
info.Kernel = readProcVersion()
|
|
|
|
// 读取 /proc/uptime 获取运行时长
|
|
info.UptimeSeconds = readUptime()
|
|
|
|
return info
|
|
}
|
|
|
|
// readOSRelease 从 /etc/os-release 提取 PRETTY_NAME
|
|
func readOSRelease() string {
|
|
f, err := os.Open("/etc/os-release")
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
defer f.Close()
|
|
|
|
scanner := bufio.NewScanner(f)
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
if strings.HasPrefix(line, "PRETTY_NAME=") {
|
|
// 去除前缀和引号: PRETTY_NAME="Ubuntu 22.04.4 LTS"
|
|
val := strings.TrimPrefix(line, "PRETTY_NAME=")
|
|
val = strings.Trim(val, `"`)
|
|
return val
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// readProcVersion 从 /proc/version 提取内核版本
|
|
func readProcVersion() string {
|
|
data, err := os.ReadFile("/proc/version")
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
// 格式: Linux version 6.8.0-90-generic (...) ...
|
|
line := strings.TrimSpace(string(data))
|
|
// 提取 "Linux version X.Y.Z-..."
|
|
parts := strings.Fields(line)
|
|
if len(parts) >= 3 && parts[0] == "Linux" && parts[1] == "version" {
|
|
return parts[2]
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// readUptime 从 /proc/uptime 读取系统运行秒数
|
|
func readUptime() int64 {
|
|
data, err := os.ReadFile("/proc/uptime")
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
// 格式: "12345.67 98765.43"
|
|
// 第一个值是系统总运行秒数(含小数)
|
|
fields := strings.Fields(strings.TrimSpace(string(data)))
|
|
if len(fields) < 1 {
|
|
return 0
|
|
}
|
|
// 截断小数部分,只取整数
|
|
parts := strings.SplitN(fields[0], ".", 2)
|
|
if len(parts) > 0 {
|
|
var sec int64
|
|
for _, c := range parts[0] {
|
|
if c >= '0' && c <= '9' {
|
|
sec = sec*10 + int64(c-'0')
|
|
}
|
|
}
|
|
return sec
|
|
}
|
|
return 0
|
|
}
|