-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker_client.go
189 lines (171 loc) · 5.98 KB
/
docker_client.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
package main
import (
"context"
"encoding/json"
"io/ioutil"
"time"
"github.com/docker/docker/api/types"
docker "github.com/docker/docker/client"
)
type Stats struct {
Read time.Time `json:"read"`
Preread time.Time `json:"preread"`
PidsStats struct {
Current int `json:"current"`
} `json:"pids_stats"`
BlkioStats struct {
IoServiceBytesRecursive []interface{} `json:"io_service_bytes_recursive"`
IoServicedRecursive []interface{} `json:"io_serviced_recursive"`
IoQueueRecursive []interface{} `json:"io_queue_recursive"`
IoServiceTimeRecursive []interface{} `json:"io_service_time_recursive"`
IoWaitTimeRecursive []interface{} `json:"io_wait_time_recursive"`
IoMergedRecursive []interface{} `json:"io_merged_recursive"`
IoTimeRecursive []interface{} `json:"io_time_recursive"`
SectorsRecursive []interface{} `json:"sectors_recursive"`
} `json:"blkio_stats"`
NumProcs int `json:"num_procs"`
StorageStats struct {
} `json:"storage_stats"`
CPUStats struct {
CPUUsage struct {
TotalUsage int `json:"total_usage"`
PercpuUsage []int `json:"percpu_usage"`
UsageInKernelmode int `json:"usage_in_kernelmode"`
UsageInUsermode int `json:"usage_in_usermode"`
} `json:"cpu_usage"`
SystemCPUUsage int64 `json:"system_cpu_usage"`
OnlineCpus int `json:"online_cpus"`
ThrottlingData struct {
Periods int `json:"periods"`
ThrottledPeriods int `json:"throttled_periods"`
ThrottledTime int `json:"throttled_time"`
} `json:"throttling_data"`
} `json:"cpu_stats"`
PrecpuStats struct {
CPUUsage struct {
TotalUsage int `json:"total_usage"`
PercpuUsage []int `json:"percpu_usage"`
UsageInKernelmode int `json:"usage_in_kernelmode"`
UsageInUsermode int `json:"usage_in_usermode"`
} `json:"cpu_usage"`
SystemCPUUsage int64 `json:"system_cpu_usage"`
OnlineCpus int `json:"online_cpus"`
ThrottlingData struct {
Periods int `json:"periods"`
ThrottledPeriods int `json:"throttled_periods"`
ThrottledTime int `json:"throttled_time"`
} `json:"throttling_data"`
} `json:"precpu_stats"`
MemoryStats struct {
Usage int `json:"usage"`
MaxUsage int `json:"max_usage"`
Stats struct {
ActiveAnon int `json:"active_anon"`
ActiveFile int `json:"active_file"`
Cache int `json:"cache"`
Dirty int `json:"dirty"`
HierarchicalMemoryLimit int64 `json:"hierarchical_memory_limit"`
HierarchicalMemswLimit int64 `json:"hierarchical_memsw_limit"`
InactiveAnon int `json:"inactive_anon"`
InactiveFile int `json:"inactive_file"`
MappedFile int `json:"mapped_file"`
Pgfault int `json:"pgfault"`
Pgmajfault int `json:"pgmajfault"`
Pgpgin int `json:"pgpgin"`
Pgpgout int `json:"pgpgout"`
Rss int `json:"rss"`
RssHuge int `json:"rss_huge"`
Swap int `json:"swap"`
TotalActiveAnon int `json:"total_active_anon"`
TotalActiveFile int `json:"total_active_file"`
TotalCache int `json:"total_cache"`
TotalDirty int `json:"total_dirty"`
TotalInactiveAnon int `json:"total_inactive_anon"`
TotalInactiveFile int `json:"total_inactive_file"`
TotalMappedFile int `json:"total_mapped_file"`
TotalPgfault int `json:"total_pgfault"`
TotalPgmajfault int `json:"total_pgmajfault"`
TotalPgpgin int `json:"total_pgpgin"`
TotalPgpgout int `json:"total_pgpgout"`
TotalRss int `json:"total_rss"`
TotalRssHuge int `json:"total_rss_huge"`
TotalSwap int `json:"total_swap"`
TotalUnevictable int `json:"total_unevictable"`
TotalWriteback int `json:"total_writeback"`
Unevictable int `json:"unevictable"`
Writeback int `json:"writeback"`
} `json:"stats"`
Limit int64 `json:"limit"`
} `json:"memory_stats"`
Name string `json:"name"`
ID string `json:"id"`
Networks struct {
Eth0 struct {
RxBytes int `json:"rx_bytes"`
RxPackets int `json:"rx_packets"`
RxErrors int `json:"rx_errors"`
RxDropped int `json:"rx_dropped"`
TxBytes int `json:"tx_bytes"`
TxPackets int `json:"tx_packets"`
TxErrors int `json:"tx_errors"`
TxDropped int `json:"tx_dropped"`
} `json:"eth0"`
} `json:"networks"`
}
type DockerClient struct {
cli *docker.Client
}
func (client *DockerClient) init() {
cli, err := docker.NewEnvClient()
if err != nil {
panic(err)
}
client.cli = cli
}
//
func (client *DockerClient) GetContainers() []types.Container {
containers, err := client.cli.ContainerList(context.Background(), types.ContainerListOptions{All: true})
if err != nil {
panic(err)
}
return containers
}
//
func (client *DockerClient) ContainerRemove(container types.Container) {
err := client.cli.ContainerRemove(context.Background(), container.ID, types.ContainerRemoveOptions{Force: true})
if err != nil {
// panic(err)
}
}
//
func (client *DockerClient) ContainerStop(container types.Container) {
err := client.cli.ContainerStop(context.Background(), container.ID, nil)
if err != nil {
// panic(err)
}
}
//
func (client *DockerClient) ContainerStart(container types.Container) {
err := client.cli.ContainerStart(context.Background(), container.ID, types.ContainerStartOptions{})
if err != nil {
// panic(err)
}
}
//
func (client *DockerClient) ContainerStats(container types.Container) Stats {
stats, err := client.cli.ContainerStats(context.Background(), container.ID, false)
if err != nil {
// panic(err)
}
body, err := ioutil.ReadAll(stats.Body)
var stat Stats
err = json.Unmarshal(body, &stat)
if err != nil {
}
return stat
}
func CreateClient() DockerClient {
client := DockerClient{}
client.init()
return client
}