/
probe.go
47 lines (40 loc) · 1.28 KB
/
probe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package swarm
import "github.com/docker/engine-api/types"
// AverageCPU probe of all containers
type AverageCPU struct {
Tag string `json:"tag"`
}
// Name of probe
func (info AverageCPU) Name() string {
return "AverageCPU"
}
// Value return average CPU consuption of all service containers
// TODO better error handling
func (info AverageCPU) Value() (float64, error) {
sp := getAPI()
containers := sp.getTag(info.Tag)
var cpusum float64
var reportCPU = make(chan float64, len(containers))
for _, c := range containers {
go func(c types.Container) {
reportCPU <- calculateCPUPercent(sp.getStats(c.ID))
}(c)
}
for range containers {
cpusum = +<-reportCPU
}
return cpusum / float64(len(containers)), nil
}
func calculateCPUPercent(v *types.StatsJSON) float64 {
var (
cpuPercent = 0.0
// calculate the change for the cpu usage of the container in between readings
cpuDelta = float64(v.CPUStats.CPUUsage.TotalUsage) - float64(v.PreCPUStats.CPUUsage.TotalUsage)
// calculate the change for the entire system between readings
systemDelta = float64(v.CPUStats.SystemUsage) - float64(v.PreCPUStats.SystemUsage)
)
if systemDelta > 0.0 && cpuDelta > 0.0 {
cpuPercent = (cpuDelta / systemDelta) * float64(len(v.CPUStats.CPUUsage.PercpuUsage)) * 100.0
}
return cpuPercent
}