2016-12-22 16:15:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2016-12-22 22:04:21 +00:00
|
|
|
"fmt"
|
|
|
|
|
"math"
|
|
|
|
|
|
|
|
|
|
"github.com/fsouza/go-dockerclient"
|
2016-12-22 16:15:22 +00:00
|
|
|
ui "github.com/gizak/termui"
|
|
|
|
|
)
|
|
|
|
|
|
2016-12-22 22:04:21 +00:00
|
|
|
type Widgets struct {
|
2016-12-22 16:15:22 +00:00
|
|
|
cid *ui.Par
|
|
|
|
|
cpu *ui.Gauge
|
|
|
|
|
memory *ui.Gauge
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-22 22:04:21 +00:00
|
|
|
func NewWidgets(id string) *Widgets {
|
|
|
|
|
cid := ui.NewPar(id)
|
|
|
|
|
cid.Border = false
|
|
|
|
|
cid.Height = 2
|
|
|
|
|
cid.Width = 10
|
|
|
|
|
cid.TextFgColor = ui.ColorWhite
|
|
|
|
|
return &Widgets{cid, mkGauge(), mkGauge()}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Container struct {
|
|
|
|
|
id string
|
|
|
|
|
widgets *Widgets
|
|
|
|
|
stats chan *docker.Stats
|
|
|
|
|
done chan bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewContainer(cid string) *Container {
|
|
|
|
|
return &Container{
|
|
|
|
|
id: cid,
|
|
|
|
|
widgets: NewWidgets(cid),
|
|
|
|
|
stats: make(chan *docker.Stats),
|
|
|
|
|
done: make(chan bool),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.UpdateMem(s.MemoryStats.Usage, s.MemoryStats.Limit)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-22 16:15:22 +00:00
|
|
|
func (c *Container) UpdateCPU(n int) {
|
2016-12-22 22:04:21 +00:00
|
|
|
c.widgets.cpu.BarColor = colorScale(n)
|
|
|
|
|
c.widgets.cpu.Percent = n
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Container) UpdateMem(cur uint64, limit uint64) {
|
|
|
|
|
c.widgets.memory.Percent = round((float64(cur) / float64(limit)) * 100)
|
2016-12-22 16:15:22 +00:00
|
|
|
}
|
|
|
|
|
|
2016-12-22 22:04:21 +00:00
|
|
|
func round(num float64) int {
|
|
|
|
|
return int(num + math.Copysign(0.5, num))
|
2016-12-22 16:15:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func colorScale(n int) ui.Attribute {
|
|
|
|
|
if n > 70 {
|
|
|
|
|
return ui.ColorRed
|
|
|
|
|
}
|
|
|
|
|
if n > 30 {
|
|
|
|
|
return ui.ColorYellow
|
|
|
|
|
}
|
|
|
|
|
return ui.ColorGreen
|
|
|
|
|
}
|