forked from rancher/agent
/
get_stats.go
executable file
·67 lines (65 loc) · 1.89 KB
/
get_stats.go
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
package stats
import (
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/net"
)
func getRootContainerInfo() (containerInfo, error) {
rootInfo := containerInfo{}
rootStats := []containerStats{}
stats := containerStats{}
// cpu
cpuPerStats, err := cpu.Times(true)
if err != nil {
return containerInfo{}, err
}
cpuStats, err := cpu.Times(false)
if err != nil {
return containerInfo{}, err
}
stats.CPU.Usage.PerCPU = []uint64{}
for _, perStats := range cpuPerStats {
stats.CPU.Usage.PerCPU = append(stats.CPU.Usage.PerCPU, uint64((perStats.User+perStats.System)*1000000000))
}
if len(cpuStats) > 0 {
stats.CPU.Usage.Total = uint64(cpuPerStats[0].Total() * 1000000000)
stats.CPU.Usage.User = uint64(cpuStats[0].User * 1000000000)
stats.CPU.Usage.System = uint64(cpuStats[0].System * 1000000000)
}
// memory
memStats, err := mem.VirtualMemory()
if err != nil {
return containerInfo{}, err
}
stats.Memory.Usage = memStats.Used
//disk
diskIo, err := disk.IOCounters()
if err != nil {
return containerInfo{}, err
}
readBytes := uint64(0)
writeBytes := uint64(0)
for _, io := range diskIo {
readBytes += io.ReadBytes
writeBytes += io.WriteBytes
}
stats.DiskIo.IoServiceBytes = []PerDiskStats{}
stats.DiskIo.IoServiceBytes = append(stats.DiskIo.IoServiceBytes, PerDiskStats{})
stats.DiskIo.IoServiceBytes[0].Stats = map[string]uint64{}
stats.DiskIo.IoServiceBytes[0].Stats["Read"] = readBytes
stats.DiskIo.IoServiceBytes[0].Stats["Write"] = writeBytes
//network
netStats, err := net.IOCounters(false)
if err != nil {
return containerInfo{}, err
}
if len(netStats) > 0 {
stats.Network.Name = netStats[0].Name
stats.Network.RxBytes = netStats[0].BytesRecv
stats.Network.TxBytes = netStats[0].BytesSent
}
rootStats = append(rootStats, stats)
rootInfo.Stats = rootStats
return rootInfo, nil
}