Skip to content

Commit

Permalink
[docker]linux: add CgroupDockerStat (shirou#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
shirou committed Apr 24, 2016
1 parent f8c7af5 commit 97e1d05
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,11 @@ type CgroupMemStat struct {
MemLimitInBytes uint64 `json:"memoryLimitInBbytes"`
MemFailCnt uint64 `json:"memoryFailcnt"`
}

type CgroupDockerStat struct {
ContainerID string `json:"containerID"`
Name string `json:"name"`
Image string `json:"image"`
Status string `json:"status"`
Running bool `json:"running"`
}
42 changes: 42 additions & 0 deletions docker/docker_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,48 @@ import (
"github.com/shirou/gopsutil/internal/common"
)

// GetDockerStat returns a list of Docker basic stats.
// This requires certain permission.
func GetDockerStat() ([]CgroupDockerStat, error) {
path, err := exec.LookPath("docker")
if err != nil {
return nil, ErrDockerNotAvailable
}

out, err := exec.Command(path, "ps", "-a", "--no-trunc", "--format", "{{.ID}}|{{.Image}}|{{.Names}}|{{.Status}}").Output()
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
}

func (c CgroupDockerStat) String() string {
s, _ := json.Marshal(c)
return string(s)
}

// GetDockerIDList returnes a list of DockerID.
// This requires certain permission.
func GetDockerIDList() ([]string, error) {
Expand Down
24 changes: 24 additions & 0 deletions docker/docker_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,30 @@ func TestGetDockerIDList(t *testing.T) {
*/
}

func TestGetDockerStat(t *testing.T) {
// If there is not docker environment, this test always fail.
// not tested here

/*
ret, err := GetDockerStat()
if err != nil {
t.Errorf("error %v", err)
}
if len(ret) == 0 {
t.Errorf("ret is empty")
}
empty := CgroupDockerStat{}
for _, v := range ret {
if empty == v {
t.Errorf("empty CgroupDockerStat")
}
if v.ContainerID == "" {
t.Errorf("Could not get container id")
}
}
*/
}

func TestCgroupCPU(t *testing.T) {
v, _ := GetDockerIDList()
for _, id := range v {
Expand Down
6 changes: 6 additions & 0 deletions docker/docker_notlinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import (
"github.com/shirou/gopsutil/internal/common"
)

// GetDockerStat returns a list of Docker basic stats.
// This requires certain permission.
func GetDockerStat() ([]CgroupDockerStat, error) {
return nil, ErrDockerNotAvailable
}

// GetDockerIDList returnes a list of DockerID.
// This requires certain permission.
func GetDockerIDList() ([]string, error) {
Expand Down

0 comments on commit 97e1d05

Please sign in to comment.