41 lines
1.4 KiB
Go
41 lines
1.4 KiB
Go
package service
|
|
|
|
import (
|
|
"runtime"
|
|
"time"
|
|
|
|
"github.com/shirou/gopsutil/v4/cpu"
|
|
"github.com/shirou/gopsutil/v4/disk"
|
|
"github.com/shirou/gopsutil/v4/mem"
|
|
)
|
|
|
|
func (s *SystemConfigService) ServerInfo() (map[string]any, error) {
|
|
physicalCores, err := cpu.Counts(false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
cpuPercent, err := cpu.Percent(200*time.Millisecond, true)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
memory, err := mem.VirtualMemory()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
diskInfo := make([]map[string]any, 0)
|
|
for _, mountPoint := range s.DiskMountPoints() {
|
|
usage, usageErr := disk.Usage(mountPoint)
|
|
if usageErr != nil {
|
|
return nil, usageErr
|
|
}
|
|
diskInfo = append(diskInfo, map[string]any{"mountPoint": mountPoint, "usedMb": usage.Used / 1024 / 1024, "usedGb": usage.Used / 1024 / 1024 / 1024, "totalMb": usage.Total / 1024 / 1024, "totalGb": usage.Total / 1024 / 1024 / 1024, "usedPercent": int(usage.UsedPercent)})
|
|
}
|
|
usedMB, totalMB := memory.Used/1024/1024, memory.Total/1024/1024
|
|
return map[string]any{
|
|
"os": map[string]any{"goos": runtime.GOOS, "numCpu": runtime.NumCPU(), "compiler": runtime.Compiler, "goVersion": runtime.Version(), "numGoroutine": runtime.NumGoroutine()},
|
|
"cpu": map[string]any{"cpus": cpuPercent, "cores": physicalCores},
|
|
"ram": map[string]any{"usedMb": usedMB, "totalMb": totalMB, "usedPercent": int(memory.UsedPercent)},
|
|
"disk": diskInfo,
|
|
}, nil
|
|
}
|