zhangzengfei
2023-11-28 3a706d3378aa3626501370352963883fd2783558
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package service
 
import (
    "fmt"
    "os"
    "sort"
    "strings"
    "time"
    "vamicro/config"
    "vamicro/sysinfo-service/models/gogpu"
 
    "github.com/shirou/gopsutil/cpu"
    "github.com/shirou/gopsutil/disk"
    "github.com/shirou/gopsutil/mem"
    "github.com/shirou/gopsutil/net"
)
 
type SummaryInfo struct {
    CpuInfo []cpu.InfoStat         `json:"cpu_info"`
    Cpu     float64                `json:"cpu" example:"cpu使用率"`
    Gpu     float64                `json:"gpu" example:"gpu使用率"`
    Mem     mem.VirtualMemoryStat  `json:"mem" example:"内存使用率"`
    Disk    []interface{}          `json:"disk" example:"磁盘空间"`
    Net     map[string]interface{} `json:"net" example:"网络使用情况"`
}
 
func SystemStates() *SummaryInfo {
    var Stat = &SummaryInfo{}
    var configNetIface = config.Server.NetworkAdapter
 
    // Memory
    if meminfo, err := mem.VirtualMemory(); err == nil {
        Stat.Mem = *meminfo
    }
 
    // Gpu
    agxGpuProcIface := "/sys/devices/gpu.0/load"
    if _, err := os.Stat(agxGpuProcIface); !os.IsExist(err) {
        fd, err1 := os.Open(agxGpuProcIface)
        if err1 == nil {
            var load int64
            fmt.Fscanf(fd, "%d", &load)
            Stat.Gpu = float64(load) / 10
            fd.Close()
        }
    } else {
        if gpuInfo, err := gogpu.Info(); err == nil {
            sort.Sort(gogpu.GPURank(gpuInfo.Info))
            var totalMem, usedMem int64
 
            for _, v := range gpuInfo.Info {
                totalMem = totalMem + v.GpuMemoryTotal
                usedMem = usedMem + v.GpuMemoryUsed
            }
 
            Stat.Gpu = (float64(usedMem) / float64(totalMem)) * 100
        }
    }
 
    // Disk 统计两次, 结合下一秒的结果计算
    var disks []string
    var disksInfo = make(map[string]interface{})
 
    if parts, err := disk.Partitions(true); err == nil {
        for _, v := range parts {
            if v.Mountpoint == "/" || strings.Contains(v.Mountpoint, "/data") {
                disks = append(disks, v.Device)
 
                diskInfo, _ := disk.Usage(v.Mountpoint)
 
                pos := strings.LastIndex(v.Device, "/")
                disksInfo[v.Device[pos+1:]] = diskInfo
            }
        }
    }
    //fmt.Println("disksInfo", disksInfo)
 
    // 第一次统计磁盘io
    firstIOCount, _ := disk.IOCounters(disks...)
 
    // 第一次统计网络流量
    firstNetCount, _ := net.IOCounters(true)
 
    // Cpu
    if cpuUsedPercent, err := cpu.Percent(time.Second, true); err == nil {
        var sum float64
        for _, v := range cpuUsedPercent {
            sum = sum + v
        }
 
        Stat.Cpu = sum / float64(len(cpuUsedPercent))
    }
 
    Stat.CpuInfo, _ = cpu.Info()
    //fmt.Println("disksInfo", disksInfo)
 
    secondIOCount, _ := disk.IOCounters(disks...)
    secondNetCount, _ := net.IOCounters(true)
 
    //fmt.Println("secondIOCount", secondIOCount)
    // 计算磁盘io
    for k, v := range secondIOCount {
        Stat.Disk = append(Stat.Disk, map[string]interface{}{
            "name":       k,
            "info":       disksInfo[k],
            "readBytes":  v.ReadBytes - firstIOCount[k].ReadBytes,
            "writeBytes": v.WriteBytes - firstIOCount[k].WriteBytes,
        })
    }
 
    // 网卡信息
    Stat.Net = make(map[string]interface{})
    Stat.Net["name"] = configNetIface
    //fmt.Println("configNetIface", configNetIface)
 
    ifs, _ := net.Interfaces()
    for _, ifi := range ifs {
        if ifi.Name == configNetIface {
            Stat.Net["mac"] = ifi.HardwareAddr
            Stat.Net["addr"] = ifi.Addrs
 
            break
        }
    }
    //fmt.Println("net.Interfaces", ifs)
 
    // 计算网络吞吐量
    for k, v := range secondNetCount {
        if v.Name == configNetIface {
            Stat.Net["bytesSent"] = v.BytesSent - firstNetCount[k].BytesSent
            Stat.Net["bytesRecv"] = v.BytesRecv - firstNetCount[k].BytesRecv
 
            break
        }
    }
 
    return Stat
}