Use dockerclient to get containers and info

This commit is contained in:
Evan Phoenix 2015-04-06 17:24:04 -07:00
parent a574261b3a
commit 7b0c09d5e9
1 changed files with 38 additions and 14 deletions

View File

@ -2,7 +2,9 @@ package system
import ( import (
"fmt" "fmt"
"strings"
dc "github.com/fsouza/go-dockerclient"
"github.com/influxdb/tivan/plugins" "github.com/influxdb/tivan/plugins"
"github.com/influxdb/tivan/plugins/system/ps/common" "github.com/influxdb/tivan/plugins/system/ps/common"
"github.com/influxdb/tivan/plugins/system/ps/cpu" "github.com/influxdb/tivan/plugins/system/ps/cpu"
@ -14,7 +16,9 @@ import (
) )
type DockerContainerStat struct { type DockerContainerStat struct {
Id string
Name string Name string
Command string
CPU *cpu.CPUTimesStat CPU *cpu.CPUTimesStat
Mem *docker.CgroupMemStat Mem *docker.CgroupMemStat
} }
@ -171,12 +175,14 @@ func (s *SystemStats) Gather(acc plugins.Accumulator) error {
containers, err := s.ps.DockerStat() containers, err := s.ps.DockerStat()
if err != nil { if err != nil {
return err return fmt.Errorf("error getting docker info: %s", err)
} }
for _, cont := range containers { for _, cont := range containers {
tags := map[string]string{ tags := map[string]string{
"docker": cont.Name, "docker": cont.Id,
"name": cont.Name,
"command": cont.Command,
} }
cts := cont.CPU cts := cont.CPU
@ -225,7 +231,9 @@ func (s *SystemStats) Gather(acc plugins.Accumulator) error {
return nil return nil
} }
type systemPS struct{} type systemPS struct {
dockerClient *dc.Client
}
func (s *systemPS) LoadAvg() (*load.LoadAvgStat, error) { func (s *systemPS) LoadAvg() (*load.LoadAvgStat, error) {
return load.LoadAvg() return load.LoadAvg()
@ -277,29 +285,45 @@ func (s *systemPS) SwapStat() (*mem.SwapMemoryStat, error) {
} }
func (s *systemPS) DockerStat() ([]*DockerContainerStat, error) { func (s *systemPS) DockerStat() ([]*DockerContainerStat, error) {
list, err := docker.GetDockerIDList() if s.dockerClient == nil {
c, err := dc.NewClient("unix:///var/run/docker.sock")
if err != nil { if err != nil {
if err == common.NotImplementedError { return nil, err
return nil, nil
} }
return nil, err s.dockerClient = c
}
opts := dc.ListContainersOptions{}
list, err := s.dockerClient.ListContainers(opts)
if err != nil {
fmt.Printf("list err: %s\n", err)
return nil, nil
} }
var stats []*DockerContainerStat var stats []*DockerContainerStat
for _, cont := range list { for _, cont := range list {
ctu, err := docker.CgroupCPUDocker(cont) ctu, err := docker.CgroupCPUDocker(cont.ID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
mem, err := docker.CgroupMemDocker(cont) mem, err := docker.CgroupMemDocker(cont.ID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
stats = append(stats, &DockerContainerStat{cont, ctu, mem}) name := strings.Join(cont.Names, " ")
stats = append(stats, &DockerContainerStat{
Id: cont.ID,
Name: name,
Command: cont.Command,
CPU: ctu,
Mem: mem,
})
} }
return stats, nil return stats, nil