2016-12-30 22:17:46 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-01-12 19:24:12 +00:00
|
|
|
"sort"
|
2017-03-03 07:57:26 +00:00
|
|
|
"strings"
|
2017-01-12 19:24:12 +00:00
|
|
|
|
2017-02-07 03:33:09 +00:00
|
|
|
"github.com/bcicen/ctop/config"
|
2017-02-23 02:03:55 +00:00
|
|
|
"github.com/bcicen/ctop/metrics"
|
2016-12-30 22:17:46 +00:00
|
|
|
"github.com/fsouza/go-dockerclient"
|
|
|
|
)
|
|
|
|
|
2017-02-26 21:12:28 +00:00
|
|
|
type ContainerSource interface {
|
2017-03-06 02:00:30 +00:00
|
|
|
All() Containers
|
2017-02-26 21:12:28 +00:00
|
|
|
Get(string) (*Container, bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
type DockerContainerSource struct {
|
2017-02-24 01:18:59 +00:00
|
|
|
client *docker.Client
|
2017-03-05 06:46:41 +00:00
|
|
|
containers map[string]*Container
|
|
|
|
needsRefresh chan string // container IDs requiring refresh
|
2017-02-23 02:01:56 +00:00
|
|
|
}
|
|
|
|
|
2017-02-26 21:12:28 +00:00
|
|
|
func NewDockerContainerSource() *DockerContainerSource {
|
2017-01-01 22:42:13 +00:00
|
|
|
// init docker client
|
2017-02-16 04:06:05 +00:00
|
|
|
client, err := docker.NewClient(config.GetVal("dockerHost"))
|
2017-01-01 22:42:13 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-02-26 21:12:28 +00:00
|
|
|
cm := &DockerContainerSource{
|
2017-02-24 01:18:59 +00:00
|
|
|
client: client,
|
2017-03-05 06:46:41 +00:00
|
|
|
containers: make(map[string]*Container),
|
|
|
|
needsRefresh: make(chan string, 60),
|
2016-12-30 22:17:46 +00:00
|
|
|
}
|
2017-02-24 01:18:59 +00:00
|
|
|
cm.refreshAll()
|
2017-02-26 21:12:28 +00:00
|
|
|
go cm.Loop()
|
2017-02-24 09:10:14 +00:00
|
|
|
go cm.watchEvents()
|
2017-01-01 22:42:13 +00:00
|
|
|
return cm
|
2016-12-30 22:17:46 +00:00
|
|
|
}
|
|
|
|
|
2017-02-24 01:18:59 +00:00
|
|
|
// Docker events watcher
|
2017-02-26 21:12:28 +00:00
|
|
|
func (cm *DockerContainerSource) watchEvents() {
|
2017-02-24 01:18:59 +00:00
|
|
|
log.Info("docker event listener starting")
|
|
|
|
events := make(chan *docker.APIEvents)
|
|
|
|
cm.client.AddEventListener(events)
|
2017-01-26 00:53:03 +00:00
|
|
|
|
2017-02-24 01:18:59 +00:00
|
|
|
for e := range events {
|
2017-02-24 09:10:14 +00:00
|
|
|
if e.Type != "container" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch e.Action {
|
|
|
|
case "start", "die", "pause", "unpause":
|
|
|
|
log.Debugf("handling docker event: action=%s id=%s", e.Action, e.ID)
|
2017-03-05 06:46:41 +00:00
|
|
|
cm.needsRefresh <- e.ID
|
2017-02-24 09:10:14 +00:00
|
|
|
case "destroy":
|
|
|
|
log.Debugf("handling docker event: action=%s id=%s", e.Action, e.ID)
|
2017-02-26 21:12:28 +00:00
|
|
|
cm.delByID(e.ID)
|
2017-02-24 09:10:14 +00:00
|
|
|
}
|
2017-02-24 01:18:59 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-23 02:01:56 +00:00
|
|
|
|
2017-03-05 06:46:41 +00:00
|
|
|
func (cm *DockerContainerSource) refresh(c *Container) {
|
|
|
|
insp := cm.inspect(c.Id)
|
2017-02-24 01:18:59 +00:00
|
|
|
// remove container if no longer exists
|
|
|
|
if insp == nil {
|
2017-03-05 06:46:41 +00:00
|
|
|
cm.delByID(c.Id)
|
2017-02-24 01:18:59 +00:00
|
|
|
return
|
|
|
|
}
|
2017-03-06 08:25:59 +00:00
|
|
|
c.SetMeta("name", shortName(insp.Name))
|
2017-03-06 22:05:04 +00:00
|
|
|
c.SetMeta("image", insp.Config.Image)
|
|
|
|
c.SetMeta("created", insp.Created.Format("Mon Jan 2 15:04:05 2006"))
|
2017-02-24 01:18:59 +00:00
|
|
|
c.SetState(insp.State.Status)
|
|
|
|
}
|
2017-02-23 02:24:26 +00:00
|
|
|
|
2017-02-26 21:12:28 +00:00
|
|
|
func (cm *DockerContainerSource) inspect(id string) *docker.Container {
|
2017-02-24 01:18:59 +00:00
|
|
|
c, err := cm.client.InspectContainer(id)
|
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(*docker.NoSuchContainer); ok == false {
|
|
|
|
log.Errorf(err.Error())
|
2017-02-23 02:01:56 +00:00
|
|
|
}
|
2017-01-26 00:53:03 +00:00
|
|
|
}
|
2017-02-24 01:18:59 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2017-02-26 21:12:28 +00:00
|
|
|
// Mark all container IDs for refresh
|
|
|
|
func (cm *DockerContainerSource) refreshAll() {
|
2017-02-24 01:18:59 +00:00
|
|
|
opts := docker.ListContainersOptions{All: true}
|
|
|
|
allContainers, err := cm.client.ListContainers(opts)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-01-26 00:53:03 +00:00
|
|
|
|
2017-03-05 06:46:41 +00:00
|
|
|
for _, i := range allContainers {
|
|
|
|
c := cm.MustGet(i.ID)
|
2017-03-06 08:25:59 +00:00
|
|
|
c.SetMeta("name", shortName(i.Names[0]))
|
2017-03-05 06:46:41 +00:00
|
|
|
c.SetState(i.State)
|
|
|
|
cm.needsRefresh <- c.Id
|
2017-02-24 01:18:59 +00:00
|
|
|
}
|
2017-01-01 22:42:13 +00:00
|
|
|
}
|
|
|
|
|
2017-02-26 21:12:28 +00:00
|
|
|
func (cm *DockerContainerSource) Loop() {
|
2017-03-05 06:46:41 +00:00
|
|
|
for id := range cm.needsRefresh {
|
|
|
|
c := cm.MustGet(id)
|
|
|
|
cm.refresh(c)
|
2017-01-09 15:02:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 06:46:41 +00:00
|
|
|
// Get a single container, creating one anew if not existing
|
|
|
|
func (cm *DockerContainerSource) MustGet(id string) *Container {
|
|
|
|
c, ok := cm.Get(id)
|
|
|
|
// append container struct for new containers
|
|
|
|
if !ok {
|
|
|
|
// create collector
|
|
|
|
collector := metrics.NewDocker(cm.client, id)
|
|
|
|
// create container
|
|
|
|
c = NewContainer(id, collector)
|
|
|
|
cm.containers[id] = c
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2016-12-30 22:17:46 +00:00
|
|
|
// Get a single container, by ID
|
2017-02-26 21:12:28 +00:00
|
|
|
func (cm *DockerContainerSource) Get(id string) (*Container, bool) {
|
2017-03-05 06:46:41 +00:00
|
|
|
c, ok := cm.containers[id]
|
|
|
|
return c, ok
|
2016-12-30 22:17:46 +00:00
|
|
|
}
|
|
|
|
|
2017-02-24 01:18:59 +00:00
|
|
|
// Remove containers by ID
|
2017-02-26 21:12:28 +00:00
|
|
|
func (cm *DockerContainerSource) delByID(id string) {
|
2017-03-05 06:46:41 +00:00
|
|
|
delete(cm.containers, id)
|
|
|
|
log.Infof("removed dead container: %s", id)
|
2017-01-26 00:53:03 +00:00
|
|
|
}
|
|
|
|
|
2017-01-03 17:37:09 +00:00
|
|
|
// Return array of all containers, sorted by field
|
2017-03-06 02:00:30 +00:00
|
|
|
func (cm *DockerContainerSource) All() (containers Containers) {
|
2017-03-05 06:46:41 +00:00
|
|
|
for _, c := range cm.containers {
|
|
|
|
containers = append(containers, c)
|
|
|
|
}
|
|
|
|
sort.Sort(containers)
|
|
|
|
return containers
|
2017-03-03 07:57:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// use primary container name
|
|
|
|
func shortName(name string) string {
|
|
|
|
return strings.Replace(name, "/", "", 1)
|
|
|
|
}
|