.
This commit is contained in:
145
internal/system/system.go
Normal file
145
internal/system/system.go
Normal file
@@ -0,0 +1,145 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/cpu"
|
||||
"github.com/shirou/gopsutil/v3/disk"
|
||||
"github.com/shirou/gopsutil/v3/host"
|
||||
"github.com/shirou/gopsutil/v3/mem"
|
||||
)
|
||||
|
||||
// GetSystemInfo 获取系统基本信息
|
||||
func GetSystemInfo() (map[string]interface{}, error) {
|
||||
hostInfo, err := host.Info()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("获取主机信息失败: %v", err)
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"os": runtime.GOOS,
|
||||
"arch": runtime.GOARCH,
|
||||
"hostname": hostInfo.Hostname,
|
||||
"platform": hostInfo.Platform,
|
||||
"platform_ver": hostInfo.PlatformVersion,
|
||||
"kernel_ver": hostInfo.KernelVersion,
|
||||
"uptime": hostInfo.Uptime,
|
||||
"boot_time": hostInfo.BootTime,
|
||||
"cpu_count": runtime.NumCPU(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetCPUInfo 获取 CPU 信息
|
||||
func GetCPUInfo() (map[string]interface{}, error) {
|
||||
cpuCount, err := cpu.Counts(true)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("获取 CPU 核心数失败: %v", err)
|
||||
}
|
||||
|
||||
cpuPercent, err := cpu.Percent(0, false)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("获取 CPU 使用率失败: %v", err)
|
||||
}
|
||||
|
||||
cpuInfo, err := cpu.Info()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("获取 CPU 详细信息失败: %v", err)
|
||||
}
|
||||
|
||||
cpuModel := "Unknown"
|
||||
if len(cpuInfo) > 0 {
|
||||
cpuModel = cpuInfo[0].ModelName
|
||||
}
|
||||
|
||||
usage := 0.0
|
||||
if len(cpuPercent) > 0 {
|
||||
usage = cpuPercent[0]
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"cores": cpuCount,
|
||||
"model": cpuModel,
|
||||
"usage": fmt.Sprintf("%.2f%%", usage),
|
||||
"usage_raw": usage,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetMemoryInfo 获取内存信息
|
||||
func GetMemoryInfo() (map[string]interface{}, error) {
|
||||
memInfo, err := mem.VirtualMemory()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("获取内存信息失败: %v", err)
|
||||
}
|
||||
|
||||
formatBytes := func(bytes uint64) string {
|
||||
const unit = 1024
|
||||
if bytes < unit {
|
||||
return fmt.Sprintf("%d B", bytes)
|
||||
}
|
||||
div, exp := int64(unit), 0
|
||||
for n := bytes / unit; n >= unit; n /= unit {
|
||||
div *= unit
|
||||
exp++
|
||||
}
|
||||
return fmt.Sprintf("%.2f %cB", float64(bytes)/float64(div), "KMGTPE"[exp])
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"total": memInfo.Total,
|
||||
"used": memInfo.Used,
|
||||
"free": memInfo.Free,
|
||||
"available": memInfo.Available,
|
||||
"usage": fmt.Sprintf("%.2f%%", memInfo.UsedPercent),
|
||||
"usage_raw": memInfo.UsedPercent,
|
||||
"total_str": formatBytes(memInfo.Total),
|
||||
"used_str": formatBytes(memInfo.Used),
|
||||
"free_str": formatBytes(memInfo.Free),
|
||||
"available_str": formatBytes(memInfo.Available),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetDiskInfo 获取磁盘信息
|
||||
func GetDiskInfo() ([]map[string]interface{}, error) {
|
||||
partitions, err := disk.Partitions(false)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("获取磁盘分区失败: %v", err)
|
||||
}
|
||||
|
||||
var result []map[string]interface{}
|
||||
formatBytes := func(bytes uint64) string {
|
||||
const unit = 1024
|
||||
if bytes < unit {
|
||||
return fmt.Sprintf("%d B", bytes)
|
||||
}
|
||||
div, exp := int64(unit), 0
|
||||
for n := bytes / unit; n >= unit; n /= unit {
|
||||
div *= unit
|
||||
exp++
|
||||
}
|
||||
return fmt.Sprintf("%.2f %cB", float64(bytes)/float64(div), "KMGTPE"[exp])
|
||||
}
|
||||
|
||||
for _, partition := range partitions {
|
||||
usage, err := disk.Usage(partition.Mountpoint)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
result = append(result, map[string]interface{}{
|
||||
"device": partition.Device,
|
||||
"mountpoint": partition.Mountpoint,
|
||||
"fstype": partition.Fstype,
|
||||
"total": usage.Total,
|
||||
"used": usage.Used,
|
||||
"free": usage.Free,
|
||||
"usage": fmt.Sprintf("%.2f%%", usage.UsedPercent),
|
||||
"usage_raw": usage.UsedPercent,
|
||||
"total_str": formatBytes(usage.Total),
|
||||
"used_str": formatBytes(usage.Used),
|
||||
"free_str": formatBytes(usage.Free),
|
||||
})
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
Reference in New Issue
Block a user