- 支持 Windows Agent 构建、安装脚本和统一上报协议 - 增强 RDP 剪贴板、虚拟盘和文件传输能力 - 新增 AIOps 上下文工具、告警 RCA trace 落库和回放接口 - 重构 docs 模块文档并补充终端、堡垒机、监控、AI Agent 设计
102 lines
2.5 KiB
Go
102 lines
2.5 KiB
Go
//go:build !windows
|
|
|
|
// 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
|
|
}
|