2017-06-08 18:33:34 +00:00
|
|
|
// +build ignore
|
2017-06-08 14:51:02 +00:00
|
|
|
package connector
|
2016-12-30 22:17:46 +00:00
|
|
|
|
|
|
|
import (
|
2017-05-15 10:54:35 +00:00
|
|
|
"fmt"
|
2017-01-12 19:24:12 +00:00
|
|
|
"sort"
|
2017-03-03 07:57:26 +00:00
|
|
|
"strings"
|
2017-03-10 09:10:39 +00:00
|
|
|
"sync"
|
2017-01-12 19:24:12 +00:00
|
|
|
|
2017-06-08 14:51:02 +00:00
|
|
|
"github.com/bcicen/ctop/container"
|
2017-02-23 02:03:55 +00:00
|
|
|
"github.com/bcicen/ctop/metrics"
|
2017-06-08 15:01:08 +00:00
|
|
|
api "github.com/fsouza/go-dockerclient"
|
2016-12-30 22:17:46 +00:00
|
|
|
)
|
|
|
|
|
2017-06-08 15:01:08 +00:00
|
|
|
type Docker struct {
|
|
|
|
client *api.Client
|
2017-06-08 14:51:02 +00:00
|
|
|
containers map[string]*container.Container
|
2017-03-05 06:46:41 +00:00
|
|
|
needsRefresh chan string // container IDs requiring refresh
|
2017-03-10 09:10:39 +00:00
|
|
|
lock sync.RWMutex
|
2017-02-23 02:01:56 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 17:35:29 +00:00
|
|
|
func NewDocker() Connector {
|
2017-01-01 22:42:13 +00:00
|
|
|
// init docker client
|
2017-06-08 15:01:08 +00:00
|
|
|
client, err := api.NewClientFromEnv()
|
2017-01-01 22:42:13 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-06-08 15:01:08 +00:00
|
|
|
cm := &Docker{
|
2017-02-24 01:18:59 +00:00
|
|
|
client: client,
|
2017-06-08 14:51:02 +00:00
|
|
|
containers: make(map[string]*container.Container),
|
2017-03-05 06:46:41 +00:00
|
|
|
needsRefresh: make(chan string, 60),
|
2017-03-10 09:10:39 +00:00
|
|
|
lock: sync.RWMutex{},
|
2016-12-30 22:17:46 +00:00
|
|
|
}
|
2017-02-26 21:12:28 +00:00
|
|
|
go cm.Loop()
|
2017-03-09 07:39:11 +00:00
|
|
|
cm.refreshAll()
|
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-06-08 15:01:08 +00:00
|
|
|
func (cm *Docker) watchEvents() {
|
2017-02-24 01:18:59 +00:00
|
|
|
log.Info("docker event listener starting")
|
2017-06-08 15:01:08 +00:00
|
|
|
events := make(chan *api.APIEvents)
|
2017-02-24 01:18:59 +00:00
|
|
|
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-06-08 15:01:08 +00:00
|
|
|
func portsFormat(ports map[api.Port][]api.PortBinding) string {
|
2017-05-15 10:54:35 +00:00
|
|
|
var exposed []string
|
|
|
|
var published []string
|
|
|
|
|
2017-05-15 04:52:38 +00:00
|
|
|
for k, v := range ports {
|
2017-05-15 10:54:35 +00:00
|
|
|
if len(v) == 0 {
|
|
|
|
exposed = append(exposed, string(k))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, binding := range v {
|
|
|
|
s := fmt.Sprintf("%s -> %s:%s", k, binding.HostIP, binding.HostPort)
|
|
|
|
published = append(published, s)
|
2017-05-15 04:52:38 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-15 10:54:35 +00:00
|
|
|
|
|
|
|
return strings.Join(append(exposed, published...), "\n")
|
2017-05-15 04:52:38 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 15:01:08 +00:00
|
|
|
func (cm *Docker) refresh(c *container.Container) {
|
2017-03-05 06:46:41 +00:00
|
|
|
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)
|
2017-05-15 04:52:38 +00:00
|
|
|
c.SetMeta("ports", portsFormat(insp.NetworkSettings.Ports))
|
2017-03-06 22:05:04 +00:00
|
|
|
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-06-08 15:01:08 +00:00
|
|
|
func (cm *Docker) inspect(id string) *api.Container {
|
2017-02-24 01:18:59 +00:00
|
|
|
c, err := cm.client.InspectContainer(id)
|
|
|
|
if err != nil {
|
2017-06-08 15:01:08 +00:00
|
|
|
if _, ok := err.(*api.NoSuchContainer); ok == false {
|
2017-02-24 01:18:59 +00:00
|
|
|
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
|
2017-06-08 15:01:08 +00:00
|
|
|
func (cm *Docker) refreshAll() {
|
|
|
|
opts := api.ListContainersOptions{All: true}
|
2017-02-24 01:18:59 +00:00
|
|
|
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-06-08 15:01:08 +00:00
|
|
|
func (cm *Docker) 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
|
2017-06-08 15:01:08 +00:00
|
|
|
func (cm *Docker) MustGet(id string) *container.Container {
|
2017-03-05 06:46:41 +00:00
|
|
|
c, ok := cm.Get(id)
|
|
|
|
// append container struct for new containers
|
|
|
|
if !ok {
|
|
|
|
// create collector
|
|
|
|
collector := metrics.NewDocker(cm.client, id)
|
|
|
|
// create container
|
2017-06-08 14:51:02 +00:00
|
|
|
c = container.New(id, collector)
|
2017-03-10 09:10:39 +00:00
|
|
|
cm.lock.Lock()
|
2017-03-05 06:46:41 +00:00
|
|
|
cm.containers[id] = c
|
2017-03-10 09:10:39 +00:00
|
|
|
cm.lock.Unlock()
|
2017-03-05 06:46:41 +00:00
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2016-12-30 22:17:46 +00:00
|
|
|
// Get a single container, by ID
|
2017-06-08 15:01:08 +00:00
|
|
|
func (cm *Docker) Get(id string) (*container.Container, bool) {
|
2017-03-10 09:10:39 +00:00
|
|
|
cm.lock.Lock()
|
2017-03-05 06:46:41 +00:00
|
|
|
c, ok := cm.containers[id]
|
2017-03-10 09:10:39 +00:00
|
|
|
cm.lock.Unlock()
|
2017-03-05 06:46:41 +00:00
|
|
|
return c, ok
|
2016-12-30 22:17:46 +00:00
|
|
|
}
|
|
|
|
|
2017-02-24 01:18:59 +00:00
|
|
|
// Remove containers by ID
|
2017-06-08 15:01:08 +00:00
|
|
|
func (cm *Docker) delByID(id string) {
|
2017-03-10 09:10:39 +00:00
|
|
|
cm.lock.Lock()
|
2017-03-05 06:46:41 +00:00
|
|
|
delete(cm.containers, id)
|
2017-03-10 09:10:39 +00:00
|
|
|
cm.lock.Unlock()
|
2017-03-05 06:46:41 +00:00
|
|
|
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-06-08 15:01:08 +00:00
|
|
|
func (cm *Docker) All() (containers container.Containers) {
|
2017-03-10 09:10:39 +00:00
|
|
|
cm.lock.Lock()
|
2017-03-05 06:46:41 +00:00
|
|
|
for _, c := range cm.containers {
|
|
|
|
containers = append(containers, c)
|
|
|
|
}
|
2017-03-10 09:10:39 +00:00
|
|
|
cm.lock.Unlock()
|
2017-03-05 06:46:41 +00:00
|
|
|
sort.Sort(containers)
|
2017-03-08 00:26:22 +00:00
|
|
|
containers.Filter()
|
2017-03-05 06:46:41 +00:00
|
|
|
return containers
|
2017-03-03 07:57:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// use primary container name
|
|
|
|
func shortName(name string) string {
|
|
|
|
return strings.Replace(name, "/", "", 1)
|
|
|
|
}
|