2017-02-23 02:03:55 +00:00
|
|
|
package metrics
|
2017-01-20 12:41:26 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
api "github.com/fsouza/go-dockerclient"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Docker collector
|
|
|
|
type Docker struct {
|
|
|
|
Metrics
|
2017-02-02 07:09:43 +00:00
|
|
|
id string
|
|
|
|
client *api.Client
|
|
|
|
running bool
|
2017-01-20 12:41:26 +00:00
|
|
|
stream chan Metrics
|
|
|
|
done chan bool
|
|
|
|
lastCpu float64
|
|
|
|
lastSysCpu float64
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDocker(client *api.Client, id string) *Docker {
|
2017-02-02 07:09:43 +00:00
|
|
|
return &Docker{
|
2017-01-20 15:56:49 +00:00
|
|
|
Metrics: Metrics{},
|
2017-02-02 07:09:43 +00:00
|
|
|
id: id,
|
|
|
|
client: client,
|
2017-01-20 15:56:49 +00:00
|
|
|
stream: make(chan Metrics),
|
|
|
|
done: make(chan bool),
|
2017-01-20 12:41:26 +00:00
|
|
|
}
|
2017-02-02 07:09:43 +00:00
|
|
|
}
|
2017-01-20 12:41:26 +00:00
|
|
|
|
2017-02-02 07:09:43 +00:00
|
|
|
func (c *Docker) Start() {
|
2017-01-20 12:41:26 +00:00
|
|
|
stats := make(chan *api.Stats)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
opts := api.StatsOptions{
|
2017-02-02 07:09:43 +00:00
|
|
|
ID: c.id,
|
2017-01-20 12:41:26 +00:00
|
|
|
Stats: stats,
|
|
|
|
Stream: true,
|
|
|
|
Done: c.done,
|
|
|
|
}
|
2017-02-02 07:09:43 +00:00
|
|
|
c.client.Stats(opts)
|
|
|
|
c.running = false
|
2017-01-20 12:41:26 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2017-01-26 00:53:03 +00:00
|
|
|
defer close(c.stream)
|
2017-01-20 12:41:26 +00:00
|
|
|
for s := range stats {
|
|
|
|
c.ReadCPU(s)
|
|
|
|
c.ReadMem(s)
|
|
|
|
c.ReadNet(s)
|
|
|
|
c.stream <- c.Metrics
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-02-02 07:09:43 +00:00
|
|
|
c.running = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Docker) Running() bool {
|
|
|
|
return c.running
|
2017-01-20 12:41:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Docker) Stream() chan Metrics {
|
|
|
|
return c.stream
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop collector
|
|
|
|
func (c *Docker) Stop() {
|
|
|
|
c.done <- true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Docker) ReadCPU(stats *api.Stats) {
|
|
|
|
ncpus := float64(len(stats.CPUStats.CPUUsage.PercpuUsage))
|
|
|
|
total := float64(stats.CPUStats.CPUUsage.TotalUsage)
|
|
|
|
system := float64(stats.CPUStats.SystemCPUUsage)
|
|
|
|
|
|
|
|
cpudiff := total - c.lastCpu
|
|
|
|
syscpudiff := system - c.lastSysCpu
|
|
|
|
|
|
|
|
c.CPUUtil = round((cpudiff / syscpudiff * 100) * ncpus)
|
|
|
|
c.lastCpu = total
|
|
|
|
c.lastSysCpu = system
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Docker) ReadMem(stats *api.Stats) {
|
|
|
|
c.MemUsage = int64(stats.MemoryStats.Usage)
|
|
|
|
c.MemLimit = int64(stats.MemoryStats.Limit)
|
|
|
|
c.MemPercent = round((float64(c.MemUsage) / float64(c.MemLimit)) * 100)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Docker) ReadNet(stats *api.Stats) {
|
|
|
|
var rx, tx int64
|
|
|
|
for _, network := range stats.Networks {
|
|
|
|
rx += int64(network.RxBytes)
|
|
|
|
tx += int64(network.TxBytes)
|
|
|
|
}
|
|
|
|
c.NetRx, c.NetTx = rx, tx
|
|
|
|
}
|