-
Notifications
You must be signed in to change notification settings - Fork 6
/
stats.go
84 lines (74 loc) · 2.65 KB
/
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
package adoc
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
type ThrottlingData struct {
Periods uint64 `json:"periods"`
ThrottledPeriods uint64 `json:"throttled_periods"`
ThrottledTime uint64 `json:"throttled_time"`
}
type CpuUsage struct {
TotalUsage uint64 `json:"total_usage"`
PercpuUsage []uint64 `json:"percpu_usage"`
UsageInKernelmode uint64 `json:"usage_in_kernelmode"`
UsageInUsermode uint64 `json:"usage_in_usermode"`
}
type CpuStats struct {
CpuUsage CpuUsage `json:"cpu_usage"`
SystemUsage uint64 `json:"system_cpu_usage"`
ThrottlingData ThrottlingData `json:"throttling_data"`
}
type NetworkStats struct {
RxBytes uint64 `json:"rx_bytes"`
RxPackets uint64 `json:"rx_packets"`
RxErrors uint64 `json:"rx_errors"`
RxDropped uint64 `json:"rx_dropped"`
TxBytes uint64 `json:"tx_bytes"`
TxPackets uint64 `json:"tx_packets"`
TxErrors uint64 `json:"tx_errors"`
TxDropped uint64 `json:"tx_dropped"`
}
type MemoryStats struct {
Usage uint64 `json:"usage"`
MaxUsage uint64 `json:"max_usage"`
Stats map[string]uint64 `json:"stats"`
Failcnt uint64 `json:"failcnt"`
Limit uint64 `json:"limit"`
}
type BlkioStatEntry struct {
Major uint64 `json:"major"`
Minor uint64 `json:"minor"`
Op string `json:"op"`
Value uint64 `json:"value"`
}
type BlkioStats struct {
IoServiceBytesRecursive []BlkioStatEntry `json:"io_service_bytes_recursive"`
IoServicedRecursive []BlkioStatEntry `json:"io_serviced_recursive"`
IoQueuedRecursive []BlkioStatEntry `json:"io_queue_recursive"`
IoServiceTimeRecursive []BlkioStatEntry `json:"io_service_time_recursive"`
IoWaitTimeRecursive []BlkioStatEntry `json:"io_wait_time_recursive"`
IoMergedRecursive []BlkioStatEntry `json:"io_merged_recursive"`
IoTimeRecursive []BlkioStatEntry `json:"io_time_recursive"`
SectorsRecursive []BlkioStatEntry `json:"sectors_recursive"`
}
type Stats struct {
Read time.Time `json:"read"`
NetworkStats NetworkStats `json:"network"`
CpuStats CpuStats `json:"cpu_stats"`
MemoryStats MemoryStats `json:"memory_stats"`
BlkioStats BlkioStats `json:"blkio_stats"`
}
func (client *DockerClient) ContainerStats(id string) (Stats, error) {
// this call only return recent one shot of stats, so it maybe blocks the call routine if the container is not alive
uri := fmt.Sprintf("containers/%s/stats", id)
var stats Stats
err := client.sendRequestCallback("GET", uri, nil, nil, func(resp *http.Response) error {
decoder := json.NewDecoder(resp.Body)
cbErr := decoder.Decode(&stats)
return cbErr
}, nil)
return stats, err
}