[agent-wdd] 基本完成Info部分的整理
This commit is contained in:
82
agent-wdd/config/OS.go
Normal file
82
agent-wdd/config/OS.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"agent-wdd/log"
|
||||
"bufio"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (os *OS) SaveConfig() {
|
||||
log.Info("Saving INFO => OS !")
|
||||
|
||||
ConfigCache.Agent.OS = *os
|
||||
SaveConfig()
|
||||
}
|
||||
|
||||
func (o *OS) Gather() {
|
||||
|
||||
log.Info("Gathering INFO => OS !")
|
||||
|
||||
// 获取主机名
|
||||
if hostname, err := os.Hostname(); err == nil {
|
||||
o.Hostname = hostname
|
||||
}
|
||||
|
||||
o.OsType = "linux" // 固定为linux
|
||||
|
||||
// 解析系统信息
|
||||
file, err := os.Open("/etc/os-release")
|
||||
if err == nil {
|
||||
defer file.Close()
|
||||
scanner := bufio.NewScanner(file)
|
||||
osInfo := make(map[string]string)
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if parts := strings.SplitN(line, "=", 2); len(parts) == 2 {
|
||||
key := parts[0]
|
||||
value := strings.Trim(parts[1], `"`)
|
||||
osInfo[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
//utils.BeautifulPrint(osInfo)
|
||||
|
||||
// 获取操作系统名称
|
||||
if name, ok := osInfo["PRETTY_NAME"]; ok {
|
||||
o.OsName = name
|
||||
}
|
||||
|
||||
// 获取系统家族
|
||||
if id, ok := osInfo["ID"]; ok {
|
||||
o.OsFamily = id
|
||||
}
|
||||
|
||||
// 获取系统版本
|
||||
if version, ok := osInfo["VERSION_ID"]; ok {
|
||||
o.OsVersion = version
|
||||
} else {
|
||||
// 针对RedHat系特殊处理
|
||||
if o.OsFamily == "centos" || o.OsFamily == "rhel" {
|
||||
if data, err := os.ReadFile("/etc/redhat-release"); err == nil {
|
||||
re := regexp.MustCompile(`\d+(\.\d+)+`)
|
||||
if match := re.FindString(string(data)); match != "" {
|
||||
o.OsVersion = match
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取内核版本
|
||||
if out, err := exec.Command("uname", "-r").Output(); err == nil {
|
||||
o.Kernel = strings.TrimSpace(string(out))
|
||||
}
|
||||
|
||||
// 获取系统架构
|
||||
o.Arch = runtime.GOARCH
|
||||
}
|
||||
Reference in New Issue
Block a user