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
|
}
|