fix
liuxiaolong
2019-11-13 263c7b18ce7f18f1222318f6e0e499e907895a52
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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)
}