Files
komari-agent/monitoring/unit/cpu.go
Akizon77 e34367de69 Refactor monitoring package: remove platform-specific files and consolidate OS detection and process counting logic
- Deleted os_windows.go and process_windows.go, replacing them with platform-agnostic implementations in unit directory.
- Removed Linux-specific process counting logic from process_linux.go and integrated it into unit.
- Consolidated uptime and OS name retrieval into unit files for better organization.
- Updated update mechanism to use global variables for current version and repository.
- Introduced command-line flags for configuration, including disabling auto-update and web SSH.
- Implemented WebSocket connection handling and terminal interaction for both Unix and Windows systems.
- Added basic info upload functionality to server package, enhancing monitoring capabilities.
2025-05-16 19:56:22 +08:00

63 lines
1.3 KiB
Go

package monitoring
import (
"runtime"
"strconv"
"strings"
"time"
"github.com/shirou/gopsutil/cpu"
)
type CpuInfo struct {
CPUName string `json:"cpu_name"`
CPUArchitecture string `json:"cpu_architecture"`
CPUCores int `json:"cpu_cores"`
CPUUsage float64 `json:"cpu_usage"`
}
func Cpu() CpuInfo {
cpuinfo := CpuInfo{}
info, err := cpu.Info()
if err != nil {
cpuinfo.CPUName = "Unknown"
}
// multiple CPU
// 多个 CPU
if len(info) > 1 {
cpuCountMap := make(map[string]int)
for _, cpu := range info {
cpuCountMap[cpu.ModelName]++
}
for modelName, count := range cpuCountMap {
if count > 1 {
cpuinfo.CPUName += modelName + " x " + strconv.Itoa(count) + ", "
} else {
cpuinfo.CPUName += modelName + ", "
}
}
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 = strings.TrimSpace(cpuinfo.CPUName)
cpuinfo.CPUArchitecture = runtime.GOARCH
cores, err := cpu.Counts(true)
if err != nil {
cpuinfo.CPUCores = 1 // Error case
}
cpuinfo.CPUCores = cores
// Get CPU Usage
percentages, err := cpu.Percent(1*time.Second, false)
if err != nil {
cpuinfo.CPUUsage = 0.0 // Error case
} else {
cpuinfo.CPUUsage = percentages[0]
}
return cpuinfo
}