2016-12-22 16:15:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-12-22 22:04:21 +00:00
|
|
|
"fmt"
|
|
|
|
"math"
|
2016-12-23 03:46:31 +00:00
|
|
|
"strconv"
|
2016-12-22 22:04:21 +00:00
|
|
|
|
|
|
|
"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
|
2016-12-24 22:31:24 +00:00
|
|
|
cid.Height = 1
|
2016-12-22 22:04:21 +00:00
|
|
|
cid.Width = 10
|
|
|
|
cid.TextFgColor = ui.ColorWhite
|
|
|
|
return &Widgets{cid, mkGauge(), mkGauge()}
|
|
|
|
}
|
|
|
|
|
2016-12-23 04:16:34 +00:00
|
|
|
type CpuCalc struct {
|
|
|
|
lastCpu uint64
|
|
|
|
lastSysCpu uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CpuCalc) Utilization(cpu uint64, syscpu uint64, ncpus int) int {
|
|
|
|
cpudiff := float64(cpu) - float64(c.lastCpu)
|
|
|
|
syscpudiff := float64(syscpu) - float64(c.lastSysCpu)
|
|
|
|
util := round((cpudiff / syscpudiff * 100) * float64(ncpus))
|
|
|
|
c.lastCpu = cpu
|
|
|
|
c.lastSysCpu = syscpu
|
|
|
|
return util
|
|
|
|
}
|
|
|
|
|
2016-12-22 22:04:21 +00:00
|
|
|
type Container struct {
|
|
|
|
id string
|
|
|
|
widgets *Widgets
|
|
|
|
stats chan *docker.Stats
|
|
|
|
done chan bool
|
2016-12-23 04:16:34 +00:00
|
|
|
cpucalc *CpuCalc
|
2016-12-22 22:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewContainer(cid string) *Container {
|
|
|
|
return &Container{
|
|
|
|
id: cid,
|
|
|
|
widgets: NewWidgets(cid),
|
|
|
|
stats: make(chan *docker.Stats),
|
|
|
|
done: make(chan bool),
|
2016-12-23 04:16:34 +00:00
|
|
|
cpucalc: &CpuCalc{},
|
2016-12-22 22:04:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-23 04:16:34 +00:00
|
|
|
c.UpdateCPU(s.CPUStats.CPUUsage.TotalUsage, s.CPUStats.SystemCPUUsage, len(s.CPUStats.CPUUsage.PercpuUsage))
|
2016-12-22 22:04:21 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-12-23 04:16:34 +00:00
|
|
|
func (c *Container) UpdateCPU(total uint64, system uint64, ncpus int) {
|
|
|
|
util := c.cpucalc.Utilization(total, system, ncpus)
|
|
|
|
c.widgets.cpu.Label = fmt.Sprintf("%s%%", strconv.Itoa(util))
|
|
|
|
c.widgets.cpu.BarColor = colorScale(util)
|
|
|
|
if util < 5 && util > 0 {
|
|
|
|
util = 5
|
|
|
|
}
|
|
|
|
c.widgets.cpu.Percent = util
|
2016-12-22 22:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) UpdateMem(cur uint64, limit uint64) {
|
2016-12-23 03:46:31 +00:00
|
|
|
percent := round((float64(cur) / float64(limit)) * 100)
|
|
|
|
if percent < 5 {
|
|
|
|
percent = 5
|
|
|
|
}
|
|
|
|
c.widgets.memory.Percent = percent
|
|
|
|
c.widgets.memory.Label = fmt.Sprintf("%s / %s", byteFormat(cur), byteFormat(limit))
|
|
|
|
}
|
|
|
|
|
|
|
|
func byteFormat(n uint64) string {
|
|
|
|
if n < 1024 {
|
|
|
|
return fmt.Sprintf("%sB", strconv.FormatUint(n, 10))
|
|
|
|
}
|
|
|
|
if n < 1048576 {
|
|
|
|
n = n / 1024
|
|
|
|
return fmt.Sprintf("%sK", strconv.FormatUint(n, 10))
|
|
|
|
}
|
|
|
|
if n < 1073741824 {
|
|
|
|
n = n / 1048576
|
|
|
|
return fmt.Sprintf("%sM", strconv.FormatUint(n, 10))
|
|
|
|
}
|
|
|
|
n = n / 1024000000
|
|
|
|
return fmt.Sprintf("%sG", strconv.FormatUint(n, 10))
|
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
|
|
|
|
}
|