forked from rancher/agent
/
get_stats.go
executable file
·82 lines (79 loc) · 2.37 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package stats
import (
"bufio"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/net"
)
func getRootContainerInfo(count int) (containerInfo, error) {
rootInfo := containerInfo{}
rootStats := []containerStats{}
for i := 0; i < count; i++ {
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(cpuStats[0].User+cpuStats[0].System+cpuStats[0].Idle) * 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
}
func getAllDockerContainers(readers []*bufio.Reader, count int, IDList []string, pids []int) ([]containerInfo, error) {
ret := []containerInfo{}
for i, reader := range readers {
contInfo, err := getContainerStats(reader, count, IDList[i], pids[i])
if err != nil {
return []containerInfo{}, err
}
ret = append(ret, contInfo)
}
return ret, nil
}