1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-28 13:48:49 +08:00
shirou_gopsutil/docker/docker_linux.go

214 lines
5.2 KiB
Go
Raw Normal View History

2014-06-10 14:38:01 +09:00
// +build linux
2014-12-30 22:09:05 +09:00
package docker
2014-06-10 14:38:01 +09:00
import (
"encoding/json"
"fmt"
"os"
2014-06-10 14:38:01 +09:00
"os/exec"
"path"
"strconv"
"strings"
2014-11-27 10:25:14 +09:00
2014-11-27 10:32:35 +09:00
cpu "github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/internal/common"
2014-06-10 14:38:01 +09:00
)
// GetDockerIDList returnes a list of DockerID.
// This requires certain permission.
func GetDockerIDList() ([]string, error) {
2015-07-23 11:24:45 +09:00
path, err := exec.LookPath("docker")
if err != nil {
return nil, ErrDockerNotAvailable
}
out, err := exec.Command(path, "ps", "-q", "--no-trunc").Output()
2014-06-10 14:38:01 +09:00
if err != nil {
return []string{}, err
}
lines := strings.Split(string(out), "\n")
ret := make([]string, 0, len(lines))
for _, l := range lines {
2015-07-23 11:24:45 +09:00
if l == "" {
continue
}
2014-06-10 14:38:01 +09:00
ret = append(ret, l)
}
return ret, nil
}
// CgroupCPU returnes specified cgroup id CPU status.
// containerId is same as docker id if you use docker.
2014-06-10 14:38:01 +09:00
// 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) {
statfile := getCgroupFilePath(containerId, base, "cpuacct", "cpuacct.stat")
2015-09-21 23:29:17 +03:00
lines, err := common.ReadLines(statfile)
if err != nil {
return nil, err
}
// empty containerId means all cgroup
if len(containerId) == 0 {
containerId = "all"
2014-06-10 14:38:01 +09:00
}
ret := &cpu.TimesStat{CPU: containerId}
2014-06-10 14:38:01 +09:00
for _, line := range lines {
fields := strings.Split(line, " ")
if fields[0] == "user" {
user, err := strconv.ParseFloat(fields[1], 64)
2014-06-10 14:38:01 +09:00
if err == nil {
ret.User = float64(user)
2014-06-10 14:38:01 +09:00
}
}
if fields[0] == "system" {
system, err := strconv.ParseFloat(fields[1], 64)
2014-06-10 14:38:01 +09:00
if err == nil {
ret.System = float64(system)
2014-06-10 14:38:01 +09:00
}
}
}
return ret, nil
}
func CgroupCPUDocker(containerid string) (*cpu.TimesStat, error) {
return CgroupCPU(containerid, common.HostSys("fs/cgroup/cpuacct/docker"))
2014-06-10 14:38:01 +09:00
}
func CgroupMem(containerId string, base string) (*CgroupMemStat, error) {
statfile := getCgroupFilePath(containerId, base, "memory", "memory.stat")
2015-09-21 23:29:17 +03:00
// empty containerId means all cgroup
if len(containerId) == 0 {
containerId = "all"
2014-06-10 14:38:01 +09:00
}
2015-09-21 23:29:17 +03:00
lines, err := common.ReadLines(statfile)
if err != nil {
return nil, err
}
ret := &CgroupMemStat{ContainerID: containerId}
2014-06-10 14:38:01 +09:00
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-15 20:05:26 +02:00
ret.RSSHuge = v
2014-06-10 14:38:01 +09:00
case "mapped_file":
2014-08-15 20:05:26 +02:00
ret.MappedFile = v
2014-06-10 14:38:01 +09: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-15 20:05:26 +02:00
ret.InactiveAnon = v
2014-06-10 14:38:01 +09:00
case "active_anon":
2014-08-15 20:05:26 +02:00
ret.ActiveAnon = v
2014-06-10 14:38:01 +09:00
case "inactive_file":
2015-07-23 11:24:45 +09:00
ret.InactiveFile = v
2014-06-10 14:38:01 +09:00
case "active_file":
2014-08-15 20:05:26 +02:00
ret.ActiveFile = v
2014-06-10 14:38:01 +09:00
case "unevictable":
ret.Unevictable = v
case "hierarchical_memory_limit":
2014-08-15 20:05:26 +02:00
ret.HierarchicalMemoryLimit = v
2014-06-10 14:38:01 +09:00
case "total_cache":
2014-08-15 20:05:26 +02:00
ret.TotalCache = v
2014-06-10 14:38:01 +09:00
case "total_rss":
2014-08-15 20:05:26 +02:00
ret.TotalRSS = v
2014-06-10 14:38:01 +09:00
case "total_rss_huge":
2014-08-15 20:05:26 +02:00
ret.TotalRSSHuge = v
2014-06-10 14:38:01 +09:00
case "total_mapped_file":
2014-08-15 20:05:26 +02:00
ret.TotalMappedFile = v
2014-06-10 14:38:01 +09:00
case "total_pgpgin":
2014-08-15 20:05:26 +02:00
ret.TotalPgpgIn = v
2014-06-10 14:38:01 +09:00
case "total_pgpgout":
2014-08-15 20:05:26 +02:00
ret.TotalPgpgOut = v
2014-06-10 14:38:01 +09:00
case "total_pgfault":
2014-08-15 20:05:26 +02:00
ret.TotalPgFault = v
2014-06-10 14:38:01 +09:00
case "total_pgmajfault":
2014-08-15 20:05:26 +02:00
ret.TotalPgMajFault = v
2014-06-10 14:38:01 +09:00
case "total_inactive_anon":
2014-08-15 20:05:26 +02:00
ret.TotalInactiveAnon = v
2014-06-10 14:38:01 +09:00
case "total_active_anon":
2014-08-15 20:05:26 +02:00
ret.TotalActiveAnon = v
2014-06-10 14:38:01 +09:00
case "total_inactive_file":
2014-08-15 20:05:26 +02:00
ret.TotalInactiveFile = v
2014-06-10 14:38:01 +09:00
case "total_active_file":
2014-08-15 20:05:26 +02:00
ret.TotalActiveFile = v
2014-06-10 14:38:01 +09:00
case "total_unevictable":
2014-08-15 20:05:26 +02:00
ret.TotalUnevictable = v
2014-06-10 14:38:01 +09:00
}
}
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, "memory.limit_in_bytes")
if err == nil {
ret.MemLimitInBytes = r
}
r, err = getCgroupMemFile(containerId, base, "memory.failcnt")
if err == nil {
ret.MemFailCnt = r
}
2014-06-10 14:38:01 +09:00
return ret, nil
}
func CgroupMemDocker(containerId string) (*CgroupMemStat, error) {
return CgroupMem(containerId, common.HostSys("fs/cgroup/memory/docker"))
2014-06-10 14:38:01 +09:00
}
func (m CgroupMemStat) String() string {
s, _ := json.Marshal(m)
return string(s)
}
// getCgroupFilePath constructs file path to get targetted 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)
}