- README.md: React 19, default pwd 123456, Docker Compose deploy, correct module list - api-reference.md: add Agent monitoring section (report/latest/rules/quick-setup/alerts/tasks/kline) - dev-log.md: new entry documenting hostname/IP fix, system collector, offline detection, quick alarm - project-structure.md: add agent/ directory, dev-log.md link, React 19
70 lines
1.8 KiB
Go
70 lines
1.8 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 // 内核版本 (如 "Linux 6.8.0-90-generic")
|
|
}
|
|
|
|
// 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()
|
|
|
|
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 ""
|
|
}
|