mirror of
https://github.com/fankes/komari-agent.git
synced 2025-10-19 02:59:23 +08:00
feat: 优化CPU名称获取
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
package monitoring
|
package monitoring
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -16,49 +19,90 @@ type CpuInfo struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Cpu() CpuInfo {
|
func Cpu() CpuInfo {
|
||||||
cpuinfo := CpuInfo{}
|
cpuinfo := CpuInfo{
|
||||||
info, err := cpu.Info()
|
CPUName: "Unknown",
|
||||||
if err != nil {
|
CPUArchitecture: runtime.GOARCH,
|
||||||
cpuinfo.CPUName = "Unknown"
|
CPUCores: 1,
|
||||||
|
CPUUsage: 0.0,
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
// multiple CPU
|
// 优先使用 lscpu 获取 CPU 信息
|
||||||
// 多个 CPU
|
name, err := readCPUNameFromLscpu()
|
||||||
if len(info) > 1 {
|
if err == nil && name != "" {
|
||||||
cpuCountMap := make(map[string]int)
|
cpuinfo.CPUName = strings.TrimSpace(name)
|
||||||
for _, cpu := range info {
|
} else {
|
||||||
cpuCountMap[cpu.ModelName]++
|
// 如果 lscpu 无法获取 CPU 名称,尝试使用 gopsutil
|
||||||
}
|
info, err := cpu.Info()
|
||||||
for modelName, count := range cpuCountMap {
|
if err == nil && len(info) > 0 {
|
||||||
if count > 1 {
|
cpuinfo.CPUName = strings.TrimSpace(info[0].ModelName)
|
||||||
cpuinfo.CPUName += modelName + " x " + strconv.Itoa(count) + ", "
|
if cpuinfo.CPUName == "" {
|
||||||
} else {
|
if info[0].VendorID != "" || info[0].Family != "" {
|
||||||
cpuinfo.CPUName += modelName + ", "
|
cpuinfo.CPUName = strings.TrimSpace(info[0].VendorID + " " + info[0].Family)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cpuinfo.CPUName = cpuinfo.CPUName[:len(cpuinfo.CPUName)-2] // Remove trailing comma and space
|
|
||||||
} else if len(info) == 1 {
|
|
||||||
cpuinfo.CPUName = info[0].ModelName
|
|
||||||
}
|
}
|
||||||
*/
|
}
|
||||||
cpuinfo.CPUName = info[0].ModelName
|
|
||||||
|
|
||||||
cpuinfo.CPUName = strings.TrimSpace(cpuinfo.CPUName)
|
if cpuinfo.CPUName == "Unknown" {
|
||||||
|
name, err := readCPUNameFromProc()
|
||||||
cpuinfo.CPUArchitecture = runtime.GOARCH
|
if err == nil && name != "" {
|
||||||
|
cpuinfo.CPUName = strings.TrimSpace(name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cores, err := cpu.Counts(true)
|
cores, err := cpu.Counts(true)
|
||||||
if err != nil {
|
if err == nil {
|
||||||
cpuinfo.CPUCores = 1 // Error case
|
cpuinfo.CPUCores = cores
|
||||||
}
|
}
|
||||||
cpuinfo.CPUCores = cores
|
|
||||||
|
|
||||||
// Get CPU Usage
|
|
||||||
percentages, err := cpu.Percent(1*time.Second, false)
|
percentages, err := cpu.Percent(1*time.Second, false)
|
||||||
if err != nil {
|
if err == nil && len(percentages) > 0 {
|
||||||
cpuinfo.CPUUsage = 0.0 // Error case
|
|
||||||
} else {
|
|
||||||
cpuinfo.CPUUsage = percentages[0]
|
cpuinfo.CPUUsage = percentages[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
return cpuinfo
|
return cpuinfo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// readCPUNameFromLscpu 从 lscpu 命令读取 CPU 名称
|
||||||
|
func readCPUNameFromLscpu() (string, error) {
|
||||||
|
cmd := exec.Command("lscpu")
|
||||||
|
output, err := cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(strings.NewReader(string(output)))
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
if strings.HasPrefix(line, "Model name:") {
|
||||||
|
parts := strings.SplitN(line, ":", 2)
|
||||||
|
if len(parts) == 2 {
|
||||||
|
return strings.TrimSpace(parts[1]), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", scanner.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
// readCPUNameFromProc 从 /proc/cpuinfo 读取 CPU 名称
|
||||||
|
func readCPUNameFromProc() (string, error) {
|
||||||
|
file, err := os.Open("/proc/cpuinfo")
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
if strings.HasPrefix(line, "Model\t") || strings.HasPrefix(line, "Hardware\t") || strings.HasPrefix(line, "Processor\t") {
|
||||||
|
parts := strings.SplitN(line, ":", 2)
|
||||||
|
if len(parts) == 2 {
|
||||||
|
return strings.TrimSpace(parts[1]), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", scanner.Err()
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user