mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
start moving collector init into containermap
This commit is contained in:
parent
b529d41091
commit
d06f07044f
13
container.go
13
container.go
@ -10,7 +10,6 @@ type Container struct {
|
|||||||
name string
|
name string
|
||||||
state string
|
state string
|
||||||
metrics collector.Metrics
|
metrics collector.Metrics
|
||||||
collect collector.Collector
|
|
||||||
widgets widgets.ContainerWidgets
|
widgets widgets.ContainerWidgets
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,17 +24,13 @@ func (c *Container) Collapse() {
|
|||||||
func (c *Container) SetState(s string) {
|
func (c *Container) SetState(s string) {
|
||||||
c.state = s
|
c.state = s
|
||||||
c.widgets.SetStatus(s)
|
c.widgets.SetStatus(s)
|
||||||
// start collector if necessary
|
|
||||||
if s == "running" && !c.collect.Running() {
|
|
||||||
c.Collect()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Container) Collect() {
|
// Read metric stream, updating widgets
|
||||||
log.Infof("starting collector for container: %s", c.id)
|
func (c *Container) Read(stream chan collector.Metrics) {
|
||||||
c.collect.Start()
|
log.Infof("starting reader for container: %s", c.id)
|
||||||
go func() {
|
go func() {
|
||||||
for metrics := range c.collect.Stream() {
|
for metrics := range stream {
|
||||||
c.metrics = metrics
|
c.metrics = metrics
|
||||||
c.widgets.SetCPU(metrics.CPUUtil)
|
c.widgets.SetCPU(metrics.CPUUtil)
|
||||||
c.widgets.SetMem(metrics.MemUsage, metrics.MemLimit, metrics.MemPercent)
|
c.widgets.SetMem(metrics.MemUsage, metrics.MemLimit, metrics.MemPercent)
|
||||||
|
@ -10,6 +10,12 @@ import (
|
|||||||
"github.com/fsouza/go-dockerclient"
|
"github.com/fsouza/go-dockerclient"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ContainerMap struct {
|
||||||
|
client *docker.Client
|
||||||
|
containers map[string]*Container
|
||||||
|
collectors map[string]collector.Collector
|
||||||
|
}
|
||||||
|
|
||||||
func NewContainerMap() *ContainerMap {
|
func NewContainerMap() *ContainerMap {
|
||||||
// init docker client
|
// init docker client
|
||||||
client, err := docker.NewClient(config.GetVal("dockerHost"))
|
client, err := docker.NewClient(config.GetVal("dockerHost"))
|
||||||
@ -19,16 +25,12 @@ func NewContainerMap() *ContainerMap {
|
|||||||
cm := &ContainerMap{
|
cm := &ContainerMap{
|
||||||
client: client,
|
client: client,
|
||||||
containers: make(map[string]*Container),
|
containers: make(map[string]*Container),
|
||||||
|
collectors: make(map[string]collector.Collector),
|
||||||
}
|
}
|
||||||
cm.Refresh()
|
cm.Refresh()
|
||||||
return cm
|
return cm
|
||||||
}
|
}
|
||||||
|
|
||||||
type ContainerMap struct {
|
|
||||||
client *docker.Client
|
|
||||||
containers map[string]*Container
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cm *ContainerMap) Refresh() {
|
func (cm *ContainerMap) Refresh() {
|
||||||
var id, name string
|
var id, name string
|
||||||
|
|
||||||
@ -43,15 +45,16 @@ func (cm *ContainerMap) Refresh() {
|
|||||||
for _, c := range containers {
|
for _, c := range containers {
|
||||||
id = c.ID[:12]
|
id = c.ID[:12]
|
||||||
states[id] = c.State
|
states[id] = c.State
|
||||||
|
|
||||||
if _, ok := cm.containers[id]; ok == false {
|
if _, ok := cm.containers[id]; ok == false {
|
||||||
name = strings.Replace(c.Names[0], "/", "", 1) // use primary container name
|
name = strings.Replace(c.Names[0], "/", "", 1) // use primary container name
|
||||||
cm.containers[id] = &Container{
|
cm.containers[id] = &Container{
|
||||||
id: id,
|
id: id,
|
||||||
name: name,
|
name: name,
|
||||||
collect: collector.NewDocker(cm.client, id),
|
|
||||||
widgets: widgets.NewCompact(id, name),
|
widgets: widgets.NewCompact(id, name),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var removeIDs []string
|
var removeIDs []string
|
||||||
@ -62,6 +65,15 @@ func (cm *ContainerMap) Refresh() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
c.SetState(states[id])
|
c.SetState(states[id])
|
||||||
|
// start collector if needed
|
||||||
|
if c.state == "running" {
|
||||||
|
if _, ok := cm.collectors[id]; ok == false {
|
||||||
|
log.Infof("starting collector for container: %s", id)
|
||||||
|
cm.collectors[id] = collector.NewDocker(cm.client, id)
|
||||||
|
cm.collectors[id].Start()
|
||||||
|
c.Read(cm.collectors[id].Stream())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove dead containers
|
// remove dead containers
|
||||||
|
2
sort.go
2
sort.go
@ -9,8 +9,6 @@ import (
|
|||||||
|
|
||||||
type sortMethod func(c1, c2 *Container) bool
|
type sortMethod func(c1, c2 *Container) bool
|
||||||
|
|
||||||
func lessStr(s1, s2 string) bool { return s1 < s2 }
|
|
||||||
|
|
||||||
var idSorter = func(c1, c2 *Container) bool { return c1.id < c2.id }
|
var idSorter = func(c1, c2 *Container) bool { return c1.id < c2.id }
|
||||||
var nameSorter = func(c1, c2 *Container) bool { return c1.name < c2.name }
|
var nameSorter = func(c1, c2 *Container) bool { return c1.name < c2.name }
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user