mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
51 lines
964 B
Go
51 lines
964 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/fsouza/go-dockerclient"
|
|
)
|
|
|
|
type Container struct {
|
|
id string
|
|
done chan bool
|
|
stats chan *docker.Stats
|
|
widgets *Widgets
|
|
reader *StatReader
|
|
}
|
|
|
|
func NewContainer(cid, names string) *Container {
|
|
return &Container{
|
|
id: cid,
|
|
done: make(chan bool),
|
|
stats: make(chan *docker.Stats),
|
|
widgets: NewWidgets(cid, names),
|
|
reader: &StatReader{},
|
|
}
|
|
}
|
|
|
|
func (c *Container) Collect(client *docker.Client) {
|
|
|
|
go func() {
|
|
fmt.Sprintf("starting collector for container: %s\n", c.id)
|
|
opts := docker.StatsOptions{
|
|
ID: c.id,
|
|
Stats: c.stats,
|
|
Stream: true,
|
|
Done: c.done,
|
|
}
|
|
client.Stats(opts)
|
|
fmt.Sprintf("stopping collector for container: %s\n", c.id)
|
|
}()
|
|
|
|
go func() {
|
|
for s := range c.stats {
|
|
c.reader.Read(s)
|
|
c.widgets.SetCPU(c.reader.CPUUtil)
|
|
c.widgets.SetMem(c.reader.MemUsage, c.reader.MemLimit)
|
|
c.widgets.SetNet(c.reader.NetRx, c.reader.NetTx)
|
|
}
|
|
}()
|
|
|
|
}
|