ctop/reader.go

49 lines
1.1 KiB
Go
Raw Normal View History

2016-12-25 22:39:16 +00:00
package main
import (
"github.com/fsouza/go-dockerclient"
)
type StatReader struct {
CPUUtil int
2016-12-26 17:57:55 +00:00
NetTx int64
NetRx int64
2016-12-25 22:39:16 +00:00
MemUsage int64
MemLimit int64
//MemPercent int64
lastCpu float64
lastSysCpu float64
}
func (s *StatReader) Read(stats *docker.Stats) {
s.ReadCPU(stats)
s.ReadMem(stats)
2016-12-26 17:57:55 +00:00
s.ReadNet(stats)
2016-12-25 22:39:16 +00:00
}
func (s *StatReader) ReadCPU(stats *docker.Stats) {
ncpus := float64(len(stats.CPUStats.CPUUsage.PercpuUsage))
total := float64(stats.CPUStats.CPUUsage.TotalUsage)
system := float64(stats.CPUStats.SystemCPUUsage)
cpudiff := total - s.lastCpu
syscpudiff := system - s.lastSysCpu
s.CPUUtil = round((cpudiff / syscpudiff * 100) * ncpus)
s.lastCpu = total
s.lastSysCpu = system
}
func (s *StatReader) ReadMem(stats *docker.Stats) {
s.MemUsage = int64(stats.MemoryStats.Usage)
s.MemLimit = int64(stats.MemoryStats.Limit)
//s.MemPercent = round((float64(cur) / float64(limit)) * 100)
}
2016-12-26 17:57:55 +00:00
func (s *StatReader) ReadNet(stats *docker.Stats) {
s.NetTx, s.NetRx = 0, 0
for _, network := range stats.Networks {
s.NetTx += int64(network.TxBytes)
s.NetRx += int64(network.RxBytes)
}
}