forked from resourced/resourced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory_linux.go
46 lines (38 loc) · 1.1 KB
/
memory_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
// +build linux
package docker
import (
"encoding/json"
"github.com/resourced/resourced/libdocker"
gopsutil_docker "github.com/shirou/gopsutil/docker"
)
func NewDockerContainersMemory() *DockerContainersMemory {
m := &DockerContainersMemory{}
m.Data = make(map[string]*gopsutil_docker.CgroupMemStat)
return m
}
// DockerContainersMemory gathers Docker memory data.
// Data source: https://github.com/shirou/gopsutil/blob/master/docker/docker_linux.go
type DockerContainersMemory struct {
Data map[string]*gopsutil_docker.CgroupMemStat
CgroupBasePath string
}
// Run gathers cgroup memory information from cgroup itself.
func (m *DockerContainersMemory) Run() error {
containers, err := libdocker.AllContainers("")
if err != nil {
return nil
}
for _, container := range containers {
if container.ID != "" {
data, err := gopsutil_docker.CgroupMem(container.ID, m.CgroupBasePath)
if err == nil {
m.Data[container.ID] = data
}
}
}
return nil
}
// ToJson serialize Data field to JSON.
func (m *DockerContainersMemory) ToJson() ([]byte, error) {
return json.Marshal(m.Data)
}