Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
spiritLHLS committed Jul 26, 2024
1 parent 1261918 commit 7a91e40
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
47 changes: 24 additions & 23 deletions system/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,32 +172,33 @@ func getCpuInfo(ret *model.SystemInfo, cpuType string) (*model.SystemInfo, error
ret.CpuVAH, _ = checkCPUFeature(hypervFeature, "hypervisor")
}
// 使用 sysctl 获取信息 - 特化适配 freebsd openbsd 系统
if checkSysctlVersion() {
path, exit := utils.GetPATH("sysctl")
if exit && checkSysctlVersion(path) {
if ret.CpuModel == "" {
cname, err := getSysctlValue("hw.model")
cname, err := getSysctlValue(path, "hw.model")
if err == nil && !strings.Contains(cname, "cannot") {
ret.CpuModel = cname
// 获取CPU频率
freq, err := getSysctlValue("dev.cpu.0.freq")
freq, err := getSysctlValue(path, "dev.cpu.0.freq")
if err == nil && !strings.Contains(freq, "cannot") {
ret.CpuModel += " @" + freq + "MHz"
}
}
}
if ret.CpuCores == "" {
cores, err := getSysctlValue("hw.ncpu")
cores, err := getSysctlValue(path, "hw.ncpu")
if err == nil && !strings.Contains(cores, "cannot") {
ret.CpuCores = fmt.Sprintf("%s %s CPU(s)", cores, cpuType)
}
}
if ret.CpuCache == "" {
// 获取CPU缓存配置
ccache, err := getSysctlValue("hw.cacheconfig")
ccache, err := getSysctlValue(path, "hw.cacheconfig")
if err == nil && !strings.Contains(ccache, "cannot") {
ret.CpuCache = strings.TrimSpace(strings.Split(ccache, ":")[1])
}
}
aesOut, err := exec.Command("sysctl", "-a").Output()
aesOut, err := exec.Command(path, "-a").Output()
if ret.CpuAesNi == "Unsupported OS" || ret.CpuAesNi == "" {
// 检查AES指令集支持
var CPU_AES string
Expand Down Expand Up @@ -234,7 +235,7 @@ func getCpuInfo(ret *model.SystemInfo, cpuType string) (*model.SystemInfo, error
if CPU_VIRT != "" {
if runtime.GOOS == "windows" {
ret.CpuVAH = "[Y] Enabled"
} else {
} else {
ret.CpuVAH = "✔️ Enabled"
}
} else {
Expand All @@ -248,7 +249,7 @@ func getCpuInfo(ret *model.SystemInfo, cpuType string) (*model.SystemInfo, error
}
if ret.Uptime == "" {
// 获取系统运行时间
boottimeStr, err := getSysctlValue("kern.boottime")
boottimeStr, err := getSysctlValue(path, "kern.boottime")
if err == nil {
boottimeReg := regexp.MustCompile(`sec = (\d+), usec = (\d+)`)
boottimeMatch := boottimeReg.FindStringSubmatch(boottimeStr)
Expand All @@ -264,24 +265,24 @@ func getCpuInfo(ret *model.SystemInfo, cpuType string) (*model.SystemInfo, error
}
}
}
if ret.Load == "" {
// 获取系统负载
var load string
out, err := exec.Command("w").Output()
}
if ret.Load == "" {
// 获取系统负载
var load string
out, err := exec.Command("w").Output()
if err == nil {
loadFields := strings.Fields(string(out))
load = loadFields[len(loadFields)-3] + " " + loadFields[len(loadFields)-2] + " " + loadFields[len(loadFields)-1]
} else {
out, err = exec.Command("uptime").Output()
if err == nil {
loadFields := strings.Fields(string(out))
load = loadFields[len(loadFields)-3] + " " + loadFields[len(loadFields)-2] + " " + loadFields[len(loadFields)-1]
} else {
out, err = exec.Command("uptime").Output()
if err == nil {
fields := strings.Fields(string(out))
load = fields[len(fields)-3] + " " + fields[len(fields)-2] + " " + fields[len(fields)-1]
}
}
if load != "" {
ret.Load = load
fields := strings.Fields(string(out))
load = fields[len(fields)-3] + " " + fields[len(fields)-2] + " " + fields[len(fields)-1]
}
}
if load != "" {
ret.Load = load
}
}
// MAC需要额外获取信息进行判断
if runtime.GOOS == "darwin" {
Expand Down
8 changes: 4 additions & 4 deletions system/sysctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strings"
)

func checkSysctlVersion() bool {
out, err := exec.Command("sysctl", "-h").Output()
func checkSysctlVersion(path string) bool {
out, err := exec.Command(path, "-h").Output()
if err != nil {
return false
}
Expand All @@ -16,8 +16,8 @@ func checkSysctlVersion() bool {
return true
}

func getSysctlValue(key string) (string, error) {
out, err := exec.Command("sysctl", "-n", key).Output()
func getSysctlValue(path, key string) (string, error) {
out, err := exec.Command(path, "-n", key).Output()
if err != nil {
return "", err
}
Expand Down

0 comments on commit 7a91e40

Please sign in to comment.