add statreader

This commit is contained in:
Bradley Cicenas 2016-12-25 22:39:16 +00:00
parent a15d4c4dfe
commit e2231d8835
4 changed files with 85 additions and 60 deletions

View File

@ -2,27 +2,25 @@ package main
import (
"fmt"
"math"
"strconv"
"github.com/fsouza/go-dockerclient"
ui "github.com/gizak/termui"
)
type Container struct {
id string
widgets *Widgets
stats chan *docker.Stats
done chan bool
stats chan *docker.Stats
widgets *Widgets
reader *StatReader
}
func NewContainer(cid string) *Container {
return &Container{
id: cid,
widgets: NewWidgets(cid),
stats: make(chan *docker.Stats),
done: make(chan bool),
cpucalc: &CpuCalc{},
stats: make(chan *docker.Stats),
widgets: NewWidgets(cid),
reader: &StatReader{},
}
}
@ -42,28 +40,10 @@ func (c *Container) Collect(client *docker.Client) {
go func() {
for s := range c.stats {
c.UpdateMem(s.MemoryStats.Usage, s.MemoryStats.Limit)
c.UpdateCPU(s.CPUStats.CPUUsage.TotalUsage, s.CPUStats.SystemCPUUsage, len(s.CPUStats.CPUUsage.PercpuUsage))
c.reader.Read(s)
c.widgets.SetCPU(c.reader.CPUUtil)
c.widgets.SetMem(c.reader.MemUsage, c.reader.MemLimit)
}
}()
}
func (c *Container) UpdateCPU(total uint64, system uint64, ncpus int) {
util := c.widgets.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
}
func (c *Container) UpdateMem(cur uint64, limit uint64) {
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))
}

37
reader.go Normal file
View File

@ -0,0 +1,37 @@
package main
import (
"github.com/fsouza/go-dockerclient"
)
type StatReader struct {
CPUUtil int
MemUsage int64
MemLimit int64
//MemPercent int64
lastCpu float64
lastSysCpu float64
}
func (s *StatReader) Read(stats *docker.Stats) {
s.ReadCPU(stats)
s.ReadMem(stats)
}
func (s *StatReader) ReadCPU(stats *docker.Stats) {
ncpus := float64(len(stats.CPUStats.CPUUsage.PercpuUsage))
total := float64(stats.CPUStats.CPUUsage.TotalUsage)
system := float64(stats.CPUStats.SystemCPUUsage)
cpudiff := total - s.lastCpu
syscpudiff := system - s.lastSysCpu
s.CPUUtil = round((cpudiff / syscpudiff * 100) * ncpus)
s.lastCpu = total
s.lastSysCpu = system
}
func (s *StatReader) ReadMem(stats *docker.Stats) {
s.MemUsage = int64(stats.MemoryStats.Usage)
s.MemLimit = int64(stats.MemoryStats.Limit)
//s.MemPercent = round((float64(cur) / float64(limit)) * 100)
}

30
util.go
View File

@ -1,25 +1,33 @@
package main
import (
"fmt"
"math"
"strconv"
ui "github.com/gizak/termui"
)
func byteFormat(n uint64) string {
if n < 1024 {
return fmt.Sprintf("%sB", strconv.FormatUint(n, 10))
const (
kb = 1024
mb = kb * 1024
gb = mb * 1024
)
func byteFormat(n int64) string {
if n < kb {
return fmt.Sprintf("%sB", strconv.FormatInt(n, 10))
}
if n < 1048576 {
n = n / 1024
return fmt.Sprintf("%sK", strconv.FormatUint(n, 10))
if n < mb {
n = n / kb
return fmt.Sprintf("%sK", strconv.FormatInt(n, 10))
}
if n < 1073741824 {
n = n / 1048576
return fmt.Sprintf("%sM", strconv.FormatUint(n, 10))
if n < gb {
n = n / mb
return fmt.Sprintf("%sM", strconv.FormatInt(n, 10))
}
n = n / 1024000000
return fmt.Sprintf("%sG", strconv.FormatUint(n, 10))
n = n / gb
return fmt.Sprintf("%sG", strconv.FormatInt(n, 10))
}
func round(num float64) int {

View File

@ -2,32 +2,32 @@ package main
import (
"fmt"
"math"
"strconv"
"github.com/fsouza/go-dockerclient"
ui "github.com/gizak/termui"
)
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
}
type Widgets struct {
cid *ui.Par
cpu *ui.Gauge
memory *ui.Gauge
cpucalc *CpuCalc
cid *ui.Par
cpu *ui.Gauge
memory *ui.Gauge
}
func (w *Widgets) SetCPU(val int) {
w.cpu.BarColor = colorScale(val)
w.cpu.Label = fmt.Sprintf("%s%%", strconv.Itoa(val))
if val < 5 && val > 0 {
val = 5
}
w.cpu.Percent = val
}
func (w *Widgets) SetMem(val int64, limit int64) {
if val < 5 {
val = 5
}
w.memory.Percent = round((float64(val) / float64(limit)) * 100)
w.memory.Label = fmt.Sprintf("%s / %s", byteFormat(val), byteFormat(limit))
}
func NewWidgets(id string) *Widgets {