package sys
|
|
import (
|
"sort"
|
"time"
|
"webserver/extend/util"
|
|
"basic.com/valib/gogpu.git"
|
|
"github.com/shirou/gopsutil/cpu"
|
"github.com/shirou/gopsutil/disk"
|
"github.com/shirou/gopsutil/mem"
|
)
|
|
type SummaryInfo struct {
|
Cpu []float64 `json:"cpu" example:"cpu使用率"`
|
Gpu []gogpu.GpuUnitInfo `json:"gpu" example:"gpu使用率"`
|
Mem mem.VirtualMemoryStat `json:"mem" example:"内存使用率"`
|
Disk disk.UsageStat `json:"disk" example:"磁盘空间"`
|
}
|
|
const Interval time.Duration = time.Second * 60
|
|
var Stat = &SummaryInfo{}
|
|
func GatherStat() {
|
for range time.Tick(Interval) {
|
// Cpu
|
if cpuUsedPercent, err := cpu.Percent(time.Second, true); err == nil {
|
Stat.Cpu = Stat.Cpu[0:0]
|
for _, v := range cpuUsedPercent {
|
Stat.Cpu = append(Stat.Cpu, v)
|
}
|
}
|
|
// Memory
|
if meminfo, err := mem.VirtualMemory(); err == nil {
|
Stat.Mem = *meminfo
|
}
|
|
// Disk
|
if disk, err := disk.Usage("/"); err == nil {
|
Stat.Disk = *disk
|
}
|
|
// Gpu
|
if gpuInfo, err := gogpu.Info(); err == nil {
|
sort.Sort(gogpu.GPURank(gpuInfo.Info))
|
Stat.Gpu = Stat.Gpu[0:0]
|
for _, v := range gpuInfo.Info {
|
Stat.Gpu = append(Stat.Gpu, v)
|
}
|
}
|
}
|
}
|
|
func GetSysInfo() map[string]interface{} {
|
return util.Struct2Map(Stat)
|
}
|