ctop/container.go

51 lines
964 B
Go
Raw Normal View History

2016-12-22 16:15:22 +00:00
package main
import (
"fmt"
"github.com/fsouza/go-dockerclient"
2016-12-22 16:15:22 +00:00
)
type Container struct {
id string
done chan bool
2016-12-25 22:39:16 +00:00
stats chan *docker.Stats
widgets *Widgets
reader *StatReader
}
func NewContainer(cid, names string) *Container {
return &Container{
id: cid,
done: make(chan bool),
2016-12-25 22:39:16 +00:00
stats: make(chan *docker.Stats),
widgets: NewWidgets(cid, names),
2016-12-25 22:39:16 +00:00
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 {
2016-12-25 22:39:16 +00:00
c.reader.Read(s)
c.widgets.SetCPU(c.reader.CPUUtil)
c.widgets.SetMem(c.reader.MemUsage, c.reader.MemLimit)
2016-12-26 17:57:55 +00:00
c.widgets.SetNet(c.reader.NetRx, c.reader.NetTx)
}
}()
}