2016-12-22 16:15:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-01-06 11:51:11 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/bcicen/ctop/widgets"
|
2016-12-22 22:04:21 +00:00
|
|
|
"github.com/fsouza/go-dockerclient"
|
2016-12-22 16:15:22 +00:00
|
|
|
)
|
|
|
|
|
2016-12-22 22:04:21 +00:00
|
|
|
type Container struct {
|
|
|
|
id string
|
2017-01-01 22:42:13 +00:00
|
|
|
name string
|
2016-12-22 22:04:21 +00:00
|
|
|
done chan bool
|
2017-01-06 12:59:45 +00:00
|
|
|
widgets widgets.ContainerWidgets
|
2017-01-09 14:09:26 +00:00
|
|
|
reader *MetricsReader
|
2016-12-22 22:04:21 +00:00
|
|
|
}
|
|
|
|
|
2017-01-06 11:51:11 +00:00
|
|
|
func NewContainer(c docker.APIContainers) *Container {
|
|
|
|
id := c.ID[:12]
|
|
|
|
name := strings.Replace(c.Names[0], "/", "", 1) // use primary container name
|
|
|
|
return &Container{
|
|
|
|
id: id,
|
|
|
|
name: name,
|
|
|
|
done: make(chan bool),
|
|
|
|
widgets: widgets.NewCompact(id, name),
|
2017-01-09 14:09:26 +00:00
|
|
|
reader: NewMetricsReader(),
|
2017-01-06 11:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-06 19:46:30 +00:00
|
|
|
func (c *Container) Expand() {
|
|
|
|
c.widgets = widgets.NewExpanded(c.id, c.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) Collapse() {
|
|
|
|
c.widgets = widgets.NewCompact(c.id, c.name)
|
|
|
|
}
|
2016-12-22 22:04:21 +00:00
|
|
|
|
2017-01-06 19:46:30 +00:00
|
|
|
func (c *Container) Collect(client *docker.Client) {
|
2017-01-09 14:09:26 +00:00
|
|
|
stats := make(chan *docker.Stats)
|
|
|
|
|
2016-12-22 22:04:21 +00:00
|
|
|
go func() {
|
|
|
|
opts := docker.StatsOptions{
|
|
|
|
ID: c.id,
|
2017-01-09 14:09:26 +00:00
|
|
|
Stats: stats,
|
2016-12-22 22:04:21 +00:00
|
|
|
Stream: true,
|
|
|
|
Done: c.done,
|
|
|
|
}
|
|
|
|
client.Stats(opts)
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2017-01-09 14:09:26 +00:00
|
|
|
for metrics := range c.reader.Read(stats) {
|
|
|
|
c.widgets.SetCPU(metrics.CPUUtil)
|
|
|
|
c.widgets.SetMem(metrics.MemUsage, metrics.MemLimit, metrics.MemPercent)
|
|
|
|
c.widgets.SetNet(metrics.NetRx, metrics.NetTx)
|
2016-12-22 22:04:21 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|