forked from shirou/gopsutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_linux.go
298 lines (264 loc) · 8.34 KB
/
docker_linux.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
// +build linux
package docker
import (
"context"
"fmt"
"os"
"os/exec"
"path"
"strconv"
"strings"
cpu "github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/internal/common"
)
// GetDockerStat returns a list of Docker basic stats.
// This requires certain permission.
func GetDockerStat() ([]CgroupDockerStat, error) {
return GetDockerStatWithContext(context.Background())
}
func GetDockerStatWithContext(ctx context.Context) ([]CgroupDockerStat, error) {
path, err := exec.LookPath("docker")
if err != nil {
return nil, ErrDockerNotAvailable
}
out, err := invoke.CommandWithContext(ctx, path, "ps", "-a", "--no-trunc", "--format", "{{.ID}}|{{.Image}}|{{.Names}}|{{.Status}}")
if err != nil {
return []CgroupDockerStat{}, err
}
lines := strings.Split(string(out), "\n")
ret := make([]CgroupDockerStat, 0, len(lines))
for _, l := range lines {
if l == "" {
continue
}
cols := strings.Split(l, "|")
if len(cols) != 4 {
continue
}
names := strings.Split(cols[2], ",")
stat := CgroupDockerStat{
ContainerID: cols[0],
Name: names[0],
Image: cols[1],
Status: cols[3],
Running: strings.Contains(cols[3], "Up"),
}
ret = append(ret, stat)
}
return ret, nil
}
// GetDockerIDList returnes a list of DockerID.
// This requires certain permission.
func GetDockerIDList() ([]string, error) {
return GetDockerIDListWithContext(context.Background())
}
func GetDockerIDListWithContext(ctx context.Context) ([]string, error) {
path, err := exec.LookPath("docker")
if err != nil {
return nil, ErrDockerNotAvailable
}
out, err := invoke.CommandWithContext(ctx, path, "ps", "-q", "--no-trunc")
if err != nil {
return []string{}, err
}
lines := strings.Split(string(out), "\n")
ret := make([]string, 0, len(lines))
for _, l := range lines {
if l == "" {
continue
}
ret = append(ret, l)
}
return ret, nil
}
// CgroupCPU returnes specified cgroup id CPU status.
// containerID is same as docker id if you use docker.
// If you use container via systemd.slice, you could use
// containerID = docker-<container id>.scope and base=/sys/fs/cgroup/cpuacct/system.slice/
func CgroupCPU(containerID string, base string) (*cpu.TimesStat, error) {
return CgroupCPUWithContext(context.Background(), containerID, base)
}
// CgroupCPUUsage returnes specified cgroup id CPU usage.
// containerID is same as docker id if you use docker.
// If you use container via systemd.slice, you could use
// containerID = docker-<container id>.scope and base=/sys/fs/cgroup/cpuacct/system.slice/
func CgroupCPUUsage(containerID string, base string) (float64, error) {
return CgroupCPUUsageWithContext(context.Background(), containerID, base)
}
func CgroupCPUWithContext(ctx context.Context, containerID string, base string) (*cpu.TimesStat, error) {
statfile := getCgroupFilePath(containerID, base, "cpuacct", "cpuacct.stat")
lines, err := common.ReadLines(statfile)
if err != nil {
return nil, err
}
// empty containerID means all cgroup
if len(containerID) == 0 {
containerID = "all"
}
ret := &cpu.TimesStat{CPU: containerID}
for _, line := range lines {
fields := strings.Split(line, " ")
if fields[0] == "user" {
user, err := strconv.ParseFloat(fields[1], 64)
if err == nil {
ret.User = user / cpu.CPUTick
}
}
if fields[0] == "system" {
system, err := strconv.ParseFloat(fields[1], 64)
if err == nil {
ret.System = system / cpu.CPUTick
}
}
}
return ret, nil
}
func CgroupCPUUsageWithContext(ctx context.Context, containerID, base string) (float64, error) {
usagefile := getCgroupFilePath(containerID, base, "cpuacct", "cpuacct.usage")
lines, err := common.ReadLinesOffsetN(usagefile, 0, 1)
if err != nil {
return 0.0, err
}
ns, err := strconv.ParseFloat(lines[0], 64)
if err != nil {
return 0.0, err
}
return ns / nanoseconds, nil
}
func CgroupCPUDocker(containerid string) (*cpu.TimesStat, error) {
return CgroupCPUDockerWithContext(context.Background(), containerid)
}
func CgroupCPUUsageDocker(containerid string) (float64, error) {
return CgroupCPUDockerUsageWithContext(context.Background(), containerid)
}
func CgroupCPUDockerWithContext(ctx context.Context, containerid string) (*cpu.TimesStat, error) {
return CgroupCPU(containerid, common.HostSys("fs/cgroup/cpuacct/docker"))
}
func CgroupCPUDockerUsageWithContext(ctx context.Context, containerid string) (float64, error) {
return CgroupCPUUsage(containerid, common.HostSys("fs/cgroup/cpuacct/docker"))
}
func CgroupMem(containerID string, base string) (*CgroupMemStat, error) {
return CgroupMemWithContext(context.Background(), containerID, base)
}
func CgroupMemWithContext(ctx context.Context, containerID string, base string) (*CgroupMemStat, error) {
statfile := getCgroupFilePath(containerID, base, "memory", "memory.stat")
// empty containerID means all cgroup
if len(containerID) == 0 {
containerID = "all"
}
lines, err := common.ReadLines(statfile)
if err != nil {
return nil, err
}
ret := &CgroupMemStat{ContainerID: containerID}
for _, line := range lines {
fields := strings.Split(line, " ")
v, err := strconv.ParseUint(fields[1], 10, 64)
if err != nil {
continue
}
switch fields[0] {
case "cache":
ret.Cache = v
case "rss":
ret.RSS = v
case "rssHuge":
ret.RSSHuge = v
case "mappedFile":
ret.MappedFile = v
case "pgpgin":
ret.Pgpgin = v
case "pgpgout":
ret.Pgpgout = v
case "pgfault":
ret.Pgfault = v
case "pgmajfault":
ret.Pgmajfault = v
case "inactiveAnon", "inactive_anon":
ret.InactiveAnon = v
case "activeAnon", "active_anon":
ret.ActiveAnon = v
case "inactiveFile", "inactive_file":
ret.InactiveFile = v
case "activeFile", "active_file":
ret.ActiveFile = v
case "unevictable":
ret.Unevictable = v
case "hierarchicalMemoryLimit", "hierarchical_memory_limit":
ret.HierarchicalMemoryLimit = v
case "totalCache", "total_cache":
ret.TotalCache = v
case "totalRss", "total_rss":
ret.TotalRSS = v
case "totalRssHuge", "total_rss_huge":
ret.TotalRSSHuge = v
case "totalMappedFile", "total_mapped_file":
ret.TotalMappedFile = v
case "totalPgpgin", "total_pgpgin":
ret.TotalPgpgIn = v
case "totalPgpgout", "total_pgpgout":
ret.TotalPgpgOut = v
case "totalPgfault", "total_pgfault":
ret.TotalPgFault = v
case "totalPgmajfault", "total_pgmajfault":
ret.TotalPgMajFault = v
case "totalInactiveAnon", "total_inactive_anon":
ret.TotalInactiveAnon = v
case "totalActiveAnon", "total_active_anon":
ret.TotalActiveAnon = v
case "totalInactiveFile", "total_inactive_file":
ret.TotalInactiveFile = v
case "totalActiveFile", "total_active_file":
ret.TotalActiveFile = v
case "totalUnevictable", "total_unevictable":
ret.TotalUnevictable = v
}
}
r, err := getCgroupMemFile(containerID, base, "memory.usage_in_bytes")
if err == nil {
ret.MemUsageInBytes = r
}
r, err = getCgroupMemFile(containerID, base, "memory.max_usage_in_bytes")
if err == nil {
ret.MemMaxUsageInBytes = r
}
r, err = getCgroupMemFile(containerID, base, "memoryLimitInBbytes")
if err == nil {
ret.MemLimitInBytes = r
}
r, err = getCgroupMemFile(containerID, base, "memoryFailcnt")
if err == nil {
ret.MemFailCnt = r
}
return ret, nil
}
func CgroupMemDocker(containerID string) (*CgroupMemStat, error) {
return CgroupMemDockerWithContext(context.Background(), containerID)
}
func CgroupMemDockerWithContext(ctx context.Context, containerID string) (*CgroupMemStat, error) {
return CgroupMem(containerID, common.HostSys("fs/cgroup/memory/docker"))
}
// getCgroupFilePath constructs file path to get targeted stats file.
func getCgroupFilePath(containerID, base, target, file string) string {
if len(base) == 0 {
base = common.HostSys(fmt.Sprintf("fs/cgroup/%s/docker", target))
}
statfile := path.Join(base, containerID, file)
if _, err := os.Stat(statfile); os.IsNotExist(err) {
statfile = path.Join(
common.HostSys(fmt.Sprintf("fs/cgroup/%s/system.slice", target)), "docker-"+containerID+".scope", file)
}
return statfile
}
// getCgroupMemFile reads a cgroup file and return the contents as uint64.
func getCgroupMemFile(containerID, base, file string) (uint64, error) {
statfile := getCgroupFilePath(containerID, base, "memory", file)
lines, err := common.ReadLines(statfile)
if err != nil {
return 0, err
}
if len(lines) != 1 {
return 0, fmt.Errorf("wrong format file: %s", statfile)
}
return strconv.ParseUint(lines[0], 10, 64)
}