mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
refactor statreader into metricreader, add processed metrics stream
This commit is contained in:
parent
409d44e8a3
commit
ee2b63d21d
19
container.go
19
container.go
@ -11,9 +11,8 @@ type Container struct {
|
|||||||
id string
|
id string
|
||||||
name string
|
name string
|
||||||
done chan bool
|
done chan bool
|
||||||
stats chan *docker.Stats
|
|
||||||
widgets widgets.ContainerWidgets
|
widgets widgets.ContainerWidgets
|
||||||
reader *StatReader
|
reader *MetricsReader
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewContainer(c docker.APIContainers) *Container {
|
func NewContainer(c docker.APIContainers) *Container {
|
||||||
@ -23,9 +22,8 @@ func NewContainer(c docker.APIContainers) *Container {
|
|||||||
id: id,
|
id: id,
|
||||||
name: name,
|
name: name,
|
||||||
done: make(chan bool),
|
done: make(chan bool),
|
||||||
stats: make(chan *docker.Stats),
|
|
||||||
widgets: widgets.NewCompact(id, name),
|
widgets: widgets.NewCompact(id, name),
|
||||||
reader: &StatReader{},
|
reader: NewMetricsReader(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,10 +36,12 @@ func (c *Container) Collapse() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Container) Collect(client *docker.Client) {
|
func (c *Container) Collect(client *docker.Client) {
|
||||||
|
stats := make(chan *docker.Stats)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
opts := docker.StatsOptions{
|
opts := docker.StatsOptions{
|
||||||
ID: c.id,
|
ID: c.id,
|
||||||
Stats: c.stats,
|
Stats: stats,
|
||||||
Stream: true,
|
Stream: true,
|
||||||
Done: c.done,
|
Done: c.done,
|
||||||
}
|
}
|
||||||
@ -49,11 +49,10 @@ func (c *Container) Collect(client *docker.Client) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for s := range c.stats {
|
for metrics := range c.reader.Read(stats) {
|
||||||
c.reader.Read(s)
|
c.widgets.SetCPU(metrics.CPUUtil)
|
||||||
c.widgets.SetCPU(c.reader.CPUUtil)
|
c.widgets.SetMem(metrics.MemUsage, metrics.MemLimit, metrics.MemPercent)
|
||||||
c.widgets.SetMem(c.reader.MemUsage, c.reader.MemLimit, c.reader.MemPercent)
|
c.widgets.SetNet(metrics.NetRx, metrics.NetTx)
|
||||||
c.widgets.SetNet(c.reader.NetRx, c.reader.NetTx)
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
2
grid.go
2
grid.go
@ -9,7 +9,7 @@ import (
|
|||||||
|
|
||||||
type Grid struct {
|
type Grid struct {
|
||||||
cursorID string // id of currently selected container
|
cursorID string // id of currently selected container
|
||||||
containers []*Container
|
containers []*Container // sorted slice of containers
|
||||||
containerMap *ContainerMap
|
containerMap *ContainerMap
|
||||||
header *widgets.CTopHeader
|
header *widgets.CTopHeader
|
||||||
}
|
}
|
||||||
|
56
reader.go
56
reader.go
@ -6,47 +6,65 @@ import (
|
|||||||
"github.com/fsouza/go-dockerclient"
|
"github.com/fsouza/go-dockerclient"
|
||||||
)
|
)
|
||||||
|
|
||||||
type StatReader struct {
|
type Metrics struct {
|
||||||
CPUUtil int
|
CPUUtil int
|
||||||
NetTx int64
|
NetTx int64
|
||||||
NetRx int64
|
NetRx int64
|
||||||
MemLimit int64
|
MemLimit int64
|
||||||
MemPercent int
|
MemPercent int
|
||||||
MemUsage int64
|
MemUsage int64
|
||||||
|
}
|
||||||
|
|
||||||
|
type MetricsReader struct {
|
||||||
|
Metrics
|
||||||
lastCpu float64
|
lastCpu float64
|
||||||
lastSysCpu float64
|
lastSysCpu float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StatReader) Read(stats *docker.Stats) {
|
func NewMetricsReader() *MetricsReader {
|
||||||
s.ReadCPU(stats)
|
return &MetricsReader{}
|
||||||
s.ReadMem(stats)
|
|
||||||
s.ReadNet(stats)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StatReader) ReadCPU(stats *docker.Stats) {
|
func (m *MetricsReader) Read(statsCh chan *docker.Stats) chan Metrics {
|
||||||
|
stream := make(chan Metrics)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for s := range statsCh {
|
||||||
|
m.ReadCPU(s)
|
||||||
|
m.ReadMem(s)
|
||||||
|
m.ReadNet(s)
|
||||||
|
stream <- m.Metrics
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return stream
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MetricsReader) ReadCPU(stats *docker.Stats) {
|
||||||
ncpus := float64(len(stats.CPUStats.CPUUsage.PercpuUsage))
|
ncpus := float64(len(stats.CPUStats.CPUUsage.PercpuUsage))
|
||||||
total := float64(stats.CPUStats.CPUUsage.TotalUsage)
|
total := float64(stats.CPUStats.CPUUsage.TotalUsage)
|
||||||
system := float64(stats.CPUStats.SystemCPUUsage)
|
system := float64(stats.CPUStats.SystemCPUUsage)
|
||||||
|
|
||||||
cpudiff := total - s.lastCpu
|
cpudiff := total - m.lastCpu
|
||||||
syscpudiff := system - s.lastSysCpu
|
syscpudiff := system - m.lastSysCpu
|
||||||
s.CPUUtil = round((cpudiff / syscpudiff * 100) * ncpus)
|
m.CPUUtil = round((cpudiff / syscpudiff * 100) * ncpus)
|
||||||
s.lastCpu = total
|
m.lastCpu = total
|
||||||
s.lastSysCpu = system
|
m.lastSysCpu = system
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StatReader) ReadMem(stats *docker.Stats) {
|
func (m *MetricsReader) ReadMem(stats *docker.Stats) {
|
||||||
s.MemUsage = int64(stats.MemoryStats.Usage)
|
m.MemUsage = int64(stats.MemoryStats.Usage)
|
||||||
s.MemLimit = int64(stats.MemoryStats.Limit)
|
m.MemLimit = int64(stats.MemoryStats.Limit)
|
||||||
s.MemPercent = round((float64(s.MemUsage) / float64(s.MemLimit)) * 100)
|
m.MemPercent = round((float64(m.MemUsage) / float64(m.MemLimit)) * 100)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StatReader) ReadNet(stats *docker.Stats) {
|
func (m *MetricsReader) ReadNet(stats *docker.Stats) {
|
||||||
s.NetTx, s.NetRx = 0, 0
|
var rx, tx int64
|
||||||
for _, network := range stats.Networks {
|
for _, network := range stats.Networks {
|
||||||
s.NetTx += int64(network.TxBytes)
|
rx += int64(network.RxBytes)
|
||||||
s.NetRx += int64(network.RxBytes)
|
tx += int64(network.TxBytes)
|
||||||
}
|
}
|
||||||
|
m.NetRx, m.NetTx = rx, tx
|
||||||
}
|
}
|
||||||
|
|
||||||
func round(num float64) int {
|
func round(num float64) int {
|
||||||
|
Loading…
Reference in New Issue
Block a user