-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_stats.go
327 lines (287 loc) · 9.45 KB
/
docker_stats.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"io"
"math"
"strings"
"sync"
"time"
)
type ContainerInfo struct {
mutex sync.RWMutex
Data ContainerData
OnUpdated func()
OnStopped func()
Stop func()
Restart func()
}
type ContainerData struct {
ID string
State ContainerState
Created int64
Name string
AlternativeName string
Image string
DockerComposeProject string
DockerComposeProjectDir string
DockerComposeService string
DockerComposeContainerNumber int
EnvVars map[string]string
LastUpdated int64
CpuPercent float64
CpuPercentHistory History
Memory uint64
MemoryLimit uint64
MemoryPercent float64
MemoryHistory History
NetworkTx uint64
NetworkTxHistory History
NetworkRx uint64
NetworkRxHistory History
BlockRead uint64
BlockWrite uint64
PIDs uint64
HealthUpdated int64
HealthStatus HealthState
}
type ContainerState int
const (
ContainerUnknownState ContainerState = iota
ContainerRunning ContainerState = iota
ContainerRestarting ContainerState = iota
ContainerStopping ContainerState = iota
ContainerStopped ContainerState = iota
)
type HealthState int
const (
UnknownHealth HealthState = iota
Healthy HealthState = iota
Unhealthy HealthState = iota
)
func NewContainerData(id string) ContainerData {
return ContainerData{
ID: id,
State: ContainerUnknownState,
CpuPercentHistory: NewHistory(),
MemoryHistory: NewHistory(),
EnvVars: make(map[string]string, 0),
}
}
func (d *ContainerData) SetAlternativeName() {
if len(d.DockerComposeService) > 0 && d.DockerComposeContainerNumber > 0 {
d.AlternativeName = fmt.Sprintf("%s-%d", d.DockerComposeService, d.DockerComposeContainerNumber)
} else if len(d.DockerComposeService) > 0 {
d.AlternativeName = d.DockerComposeService
} else if len(d.Name) > 0 {
d.AlternativeName = d.Name
} else {
d.AlternativeName = fmt.Sprintf("%8.8s", d.ID)
}
}
func NewContainerInfo(id string) *ContainerInfo {
return &ContainerInfo{Data: NewContainerData(id)}
}
func (c *ContainerInfo) Updated() {
if c.OnUpdated != nil {
c.OnUpdated()
}
}
// functionality copied from
// https://github.com/moby/moby/blob/eb131c5383db8cac633919f82abad86c99bffbe5/cli/command/container/stats_helpers.go
func updateContainerStats(ctx context.Context, cli *client.Client, container *ContainerInfo) {
ctx_ := ctx
response, err := cli.ContainerStats(ctx, container.Data.ID, true)
if err != nil {
panic(err)
}
var (
previousCPU uint64
previousSystem uint64
errors = make(chan error, 1)
)
dec := json.NewDecoder(response.Body)
go func() {
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
fmt.Printf("Failed to close body reader: %v", err)
}
}(response.Body)
for {
var (
v *types.StatsJSON
memPercent = 0.0
cpuPercent float64
blkRead, blkWrite uint64 // Only used on Linux
mem float64
memLimit = 0.0
pidsStatsCurrent uint64
)
select {
case <-ctx.Done():
return
default:
//
}
if err := dec.Decode(&v); err != nil {
dec = json.NewDecoder(io.MultiReader(dec.Buffered(), response.Body))
errors <- err
if err == io.EOF {
break
}
time.Sleep(100 * time.Millisecond)
continue
}
daemonOSType := response.OSType
if daemonOSType != "windows" {
// MemoryStats.Limit will never be 0 unless the container is not running and we haven't
// got any Samples from cgroup
if v.MemoryStats.Limit != 0 {
memPercent = float64(v.MemoryStats.Usage) / float64(v.MemoryStats.Limit) * 100.0
}
previousCPU = v.PreCPUStats.CPUUsage.TotalUsage
previousSystem = v.PreCPUStats.SystemUsage
cpuPercent = calculateCPUPercentUnix(previousCPU, previousSystem, v)
blkRead, blkWrite = calculateBlockIO(v.BlkioStats)
mem = float64(v.MemoryStats.Usage)
memLimit = float64(v.MemoryStats.Limit)
pidsStatsCurrent = v.PidsStats.Current
} else {
cpuPercent = calculateCPUPercentWindows(v)
blkRead = v.StorageStats.ReadSizeBytes
blkWrite = v.StorageStats.WriteSizeBytes
mem = float64(v.MemoryStats.PrivateWorkingSet)
}
container.mutex.Lock()
firstSeen := container.Data.LastUpdated == 0
healthStatusTooOld := time.Since(time.Unix(container.Data.HealthUpdated, 0)) > time.Duration(5)
container.Data.LastUpdated = v.Stats.Read.Unix()
container.Data.CpuPercent = cpuPercent
container.Data.CpuPercentHistory.Add(Sample{float64(container.Data.LastUpdated), cpuPercent})
container.Data.Memory = uint64(mem)
container.Data.MemoryPercent = memPercent
container.Data.MemoryLimit = uint64(memLimit)
container.Data.MemoryHistory.Add(Sample{float64(container.Data.LastUpdated), mem})
prevNetworkRx, prevNetworkTx := container.Data.NetworkRx, container.Data.NetworkTx
container.Data.NetworkRx, container.Data.NetworkTx = calculateNetwork(v.Networks)
container.Data.NetworkRxHistory.Add(Sample{float64(container.Data.LastUpdated), float64(container.Data.NetworkRx - prevNetworkRx)})
container.Data.NetworkTxHistory.Add(Sample{float64(container.Data.LastUpdated), float64(container.Data.NetworkTx - prevNetworkTx)})
container.Data.BlockRead = blkRead
container.Data.BlockWrite = blkWrite
container.Data.PIDs = pidsStatsCurrent
if firstSeen || healthStatusTooOld {
if inspect, err := cli.ContainerInspect(ctx, container.Data.ID); err == nil {
if firstSeen {
for _, env := range inspect.Config.Env {
if s := strings.SplitN(env, "=", 2); len(s) == 2 {
container.Data.EnvVars[s[0]] = s[1]
}
}
}
container.Data.HealthUpdated = container.Data.LastUpdated
if inspect.State != nil && inspect.State.Health != nil {
switch inspect.State.Health.Status {
case "healthy":
container.Data.HealthStatus = Healthy
default:
container.Data.HealthStatus = Unhealthy
}
} else {
container.Data.HealthStatus = UnknownHealth
}
} else {
container.Data.HealthStatus = UnknownHealth
fmt.Printf("Failed to inspect container %s: %v", container.Data.ID, err)
}
}
stopped := false
if container.Data.State == ContainerRunning && container.Data.PIDs == 0 {
// double check that container is still running
if containers, err := cli.ContainerList(ctx_, types.ContainerListOptions{}); err == nil {
found := false
for _, c := range containers {
if c.ID == container.Data.ID {
found = true
break
}
}
if !found {
stopped = true
container.Data.State = ContainerStopped
}
}
}
container.mutex.Unlock()
if stopped {
container.OnStopped()
}
container.Updated()
errors <- nil // we just handled a valid update
}
fmt.Printf("Done following stats of container %s (%s)\n", container.Data.AlternativeName, container.Data.ID)
}()
for {
select {
case <-time.After(2 * time.Second):
fmt.Printf("Timeout while following stats of container %s (%s)\n", container.Data.AlternativeName, container.Data.ID)
case <-ctx.Done():
fmt.Printf("Done following stats of container %s (%s)\n", container.Data.AlternativeName, container.Data.ID)
return
case err := <-errors:
if err != nil {
fmt.Printf("Error while following stats of container %s (%s): %v\n", container.Data.AlternativeName, container.Data.ID, err)
continue
}
}
}
}
func calculateCPUPercentUnix(previousCPU, previousSystem uint64, 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(previousCPU)
// calculate the change for the entire system between readings
systemDelta = float64(v.CPUStats.SystemUsage) - float64(previousSystem)
)
if systemDelta > 0.0 && cpuDelta > 0.0 {
nofCpu := math.Max(float64(len(v.CPUStats.CPUUsage.PercpuUsage)), float64(v.CPUStats.OnlineCPUs))
cpuPercent = (cpuDelta / systemDelta) * nofCpu * 100.0
}
return cpuPercent
}
func calculateCPUPercentWindows(v *types.StatsJSON) float64 {
// Max number of 100ns intervals between the previous time read and now
possIntervals := uint64(v.Read.Sub(v.PreRead).Nanoseconds()) // Start with number of ns intervals
possIntervals /= 100 // Convert to number of 100ns intervals
possIntervals *= uint64(v.NumProcs) // Multiply by the number of processors
// Intervals used
intervalsUsed := v.CPUStats.CPUUsage.TotalUsage - v.PreCPUStats.CPUUsage.TotalUsage
// Percentage avoiding divide-by-zero
if possIntervals > 0 {
return float64(intervalsUsed) / float64(possIntervals) * 100.0
}
return 0.00
}
func calculateBlockIO(blkio types.BlkioStats) (blkRead uint64, blkWrite uint64) {
for _, bioEntry := range blkio.IoServiceBytesRecursive {
switch strings.ToLower(bioEntry.Op) {
case "read":
blkRead = blkRead + bioEntry.Value
case "write":
blkWrite = blkWrite + bioEntry.Value
}
}
return
}
func calculateNetwork(network map[string]types.NetworkStats) (uint64, uint64) {
var rx, tx uint64
for _, v := range network {
rx += v.RxBytes
tx += v.TxBytes
}
return rx, tx
}