shirou_gopsutil/docker/docker_linux.go

194 lines
5.3 KiB
Go
Raw Normal View History

2014-06-10 13:38:01 +08:00
// +build linux
2014-12-30 21:09:05 +08:00
package docker
2014-06-10 13:38:01 +08:00
import (
"encoding/json"
"os/exec"
"path"
"strconv"
"strings"
2014-11-27 09:25:14 +08:00
common "github.com/shirou/gopsutil/common"
2014-11-27 09:32:35 +08:00
cpu "github.com/shirou/gopsutil/cpu"
2014-06-10 13:38:01 +08:00
)
type CgroupMemStat struct {
ContainerID string `json:"container_id"`
2014-08-16 02:05:26 +08:00
Cache uint64 `json:"cache"`
RSS uint64 `json:"rss"`
RSSHuge uint64 `json:"rss_huge"`
MappedFile uint64 `json:"mapped_file"`
Pgpgin uint64 `json:"pgpgin"`
Pgpgout uint64 `json:"pgpgout"`
Pgfault uint64 `json:"pgfault"`
Pgmajfault uint64 `json:"pgmajfault"`
InactiveAnon uint64 `json:"inactive_anon"`
ActiveAnon uint64 `json:"active_anon"`
InctiveFile uint64 `json:"inactive_file"`
ActiveFile uint64 `json:"active_file"`
Unevictable uint64 `json:"unevictable"`
HierarchicalMemoryLimit uint64 `json:"hierarchical_memory_limit"`
TotalCache uint64 `json:"total_cache"`
TotalRSS uint64 `json:"total_rss"`
TotalRSSHuge uint64 `json:"total_rss_huge"`
TotalMappedFile uint64 `json:"total_mapped_file"`
TotalPgpgIn uint64 `json:"total_pgpgin"`
TotalPgpgOut uint64 `json:"total_pgpgout"`
TotalPgFault uint64 `json:"total_pgfault"`
TotalPgMajFault uint64 `json:"total_pgmajfault"`
TotalInactiveAnon uint64 `json:"total_inactive_anon"`
TotalActiveAnon uint64 `json:"total_active_anon"`
TotalInactiveFile uint64 `json:"total_inactive_file"`
TotalActiveFile uint64 `json:"total_active_file"`
TotalUnevictable uint64 `json:"total_unevictable"`
2014-06-10 13:38:01 +08:00
}
// GetDockerIDList returnes a list of DockerID.
// This requires certain permission.
func GetDockerIDList() ([]string, error) {
out, err := exec.Command("docker", "ps", "-q", "--no-trunc").Output()
if err != nil {
return []string{}, err
}
lines := strings.Split(string(out), "\n")
ret := make([]string, 0, len(lines))
for _, l := range lines {
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/
2014-11-27 09:32:35 +08:00
func CgroupCPU(containerid string, base string) (*cpu.CPUTimesStat, error) {
2014-06-10 13:38:01 +08:00
if len(base) == 0 {
base = "/sys/fs/cgroup/cpuacct/docker"
}
path := path.Join(base, containerid, "cpuacct.stat")
lines, err := common.ReadLines(path)
if err != nil {
return nil, err
}
2014-06-10 13:38:01 +08:00
// empty containerid means all cgroup
if len(containerid) == 0 {
containerid = "all"
}
2014-11-27 09:32:35 +08:00
ret := &cpu.CPUTimesStat{CPU: containerid}
2014-06-10 13:38:01 +08:00
for _, line := range lines {
fields := strings.Split(line, " ")
if fields[0] == "user" {
user, err := strconv.ParseFloat(fields[1], 64)
2014-06-10 13:38:01 +08:00
if err == nil {
ret.User = float64(user)
2014-06-10 13:38:01 +08:00
}
}
if fields[0] == "system" {
system, err := strconv.ParseFloat(fields[1], 64)
2014-06-10 13:38:01 +08:00
if err == nil {
ret.System = float64(system)
2014-06-10 13:38:01 +08:00
}
}
}
return ret, nil
}
2014-11-27 09:32:35 +08:00
func CgroupCPUDocker(containerid string) (*cpu.CPUTimesStat, error) {
2014-06-10 13:38:01 +08:00
return CgroupCPU(containerid, "/sys/fs/cgroup/cpuacct/docker")
}
func CgroupMem(containerid string, base string) (*CgroupMemStat, error) {
if len(base) == 0 {
base = "/sys/fs/cgroup/memory/docker"
}
path := path.Join(base, containerid, "memory.stat")
// empty containerid means all cgroup
if len(containerid) == 0 {
containerid = "all"
}
lines, err := common.ReadLines(path)
if err != nil {
return nil, err
}
2014-06-10 13:38:01 +08:00
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 "rss_huge":
2014-08-16 02:05:26 +08:00
ret.RSSHuge = v
2014-06-10 13:38:01 +08:00
case "mapped_file":
2014-08-16 02:05:26 +08:00
ret.MappedFile = v
2014-06-10 13:38:01 +08:00
case "pgpgin":
ret.Pgpgin = v
case "pgpgout":
ret.Pgpgout = v
case "pgfault":
ret.Pgfault = v
case "pgmajfault":
ret.Pgmajfault = v
case "inactive_anon":
2014-08-16 02:05:26 +08:00
ret.InactiveAnon = v
2014-06-10 13:38:01 +08:00
case "active_anon":
2014-08-16 02:05:26 +08:00
ret.ActiveAnon = v
2014-06-10 13:38:01 +08:00
case "inactive_file":
2014-08-16 02:05:26 +08:00
ret.InctiveFile = v
2014-06-10 13:38:01 +08:00
case "active_file":
2014-08-16 02:05:26 +08:00
ret.ActiveFile = v
2014-06-10 13:38:01 +08:00
case "unevictable":
ret.Unevictable = v
case "hierarchical_memory_limit":
2014-08-16 02:05:26 +08:00
ret.HierarchicalMemoryLimit = v
2014-06-10 13:38:01 +08:00
case "total_cache":
2014-08-16 02:05:26 +08:00
ret.TotalCache = v
2014-06-10 13:38:01 +08:00
case "total_rss":
2014-08-16 02:05:26 +08:00
ret.TotalRSS = v
2014-06-10 13:38:01 +08:00
case "total_rss_huge":
2014-08-16 02:05:26 +08:00
ret.TotalRSSHuge = v
2014-06-10 13:38:01 +08:00
case "total_mapped_file":
2014-08-16 02:05:26 +08:00
ret.TotalMappedFile = v
2014-06-10 13:38:01 +08:00
case "total_pgpgin":
2014-08-16 02:05:26 +08:00
ret.TotalPgpgIn = v
2014-06-10 13:38:01 +08:00
case "total_pgpgout":
2014-08-16 02:05:26 +08:00
ret.TotalPgpgOut = v
2014-06-10 13:38:01 +08:00
case "total_pgfault":
2014-08-16 02:05:26 +08:00
ret.TotalPgFault = v
2014-06-10 13:38:01 +08:00
case "total_pgmajfault":
2014-08-16 02:05:26 +08:00
ret.TotalPgMajFault = v
2014-06-10 13:38:01 +08:00
case "total_inactive_anon":
2014-08-16 02:05:26 +08:00
ret.TotalInactiveAnon = v
2014-06-10 13:38:01 +08:00
case "total_active_anon":
2014-08-16 02:05:26 +08:00
ret.TotalActiveAnon = v
2014-06-10 13:38:01 +08:00
case "total_inactive_file":
2014-08-16 02:05:26 +08:00
ret.TotalInactiveFile = v
2014-06-10 13:38:01 +08:00
case "total_active_file":
2014-08-16 02:05:26 +08:00
ret.TotalActiveFile = v
2014-06-10 13:38:01 +08:00
case "total_unevictable":
2014-08-16 02:05:26 +08:00
ret.TotalUnevictable = v
2014-06-10 13:38:01 +08:00
}
}
return ret, nil
}
func CgroupMemDocker(containerid string) (*CgroupMemStat, error) {
return CgroupMem(containerid, "/sys/fs/cgroup/memory/docker")
}
func (m CgroupMemStat) String() string {
s, _ := json.Marshal(m)
return string(s)
}