mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
update container, add collect methods and main entrypoint
This commit is contained in:
parent
1c285c9e92
commit
83a5b0e1f0
67
container.go
67
container.go
@ -1,22 +1,77 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
|
||||||
|
"github.com/fsouza/go-dockerclient"
|
||||||
ui "github.com/gizak/termui"
|
ui "github.com/gizak/termui"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Container struct {
|
type Widgets struct {
|
||||||
cid *ui.Par
|
cid *ui.Par
|
||||||
cpu *ui.Gauge
|
cpu *ui.Gauge
|
||||||
memory *ui.Gauge
|
memory *ui.Gauge
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Container) UpdateCPU(n int) {
|
func NewWidgets(id string) *Widgets {
|
||||||
c.cpu.BarColor = colorScale(n)
|
cid := ui.NewPar(id)
|
||||||
c.cpu.Percent = n
|
cid.Border = false
|
||||||
|
cid.Height = 2
|
||||||
|
cid.Width = 10
|
||||||
|
cid.TextFgColor = ui.ColorWhite
|
||||||
|
return &Widgets{cid, mkGauge(), mkGauge()}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Container) UpdateMem(n int) {
|
type Container struct {
|
||||||
c.memory.Percent = n
|
id string
|
||||||
|
widgets *Widgets
|
||||||
|
stats chan *docker.Stats
|
||||||
|
done chan bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewContainer(cid string) *Container {
|
||||||
|
return &Container{
|
||||||
|
id: cid,
|
||||||
|
widgets: NewWidgets(cid),
|
||||||
|
stats: make(chan *docker.Stats),
|
||||||
|
done: make(chan bool),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Container) Collect(client *docker.Client) {
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
fmt.Sprintf("starting collector for container: %s\n", c.id)
|
||||||
|
opts := docker.StatsOptions{
|
||||||
|
ID: c.id,
|
||||||
|
Stats: c.stats,
|
||||||
|
Stream: true,
|
||||||
|
Done: c.done,
|
||||||
|
}
|
||||||
|
client.Stats(opts)
|
||||||
|
fmt.Sprintf("stopping collector for container: %s\n", c.id)
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for s := range c.stats {
|
||||||
|
c.UpdateMem(s.MemoryStats.Usage, s.MemoryStats.Limit)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Container) UpdateCPU(n int) {
|
||||||
|
c.widgets.cpu.BarColor = colorScale(n)
|
||||||
|
c.widgets.cpu.Percent = n
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Container) UpdateMem(cur uint64, limit uint64) {
|
||||||
|
c.widgets.memory.Percent = round((float64(cur) / float64(limit)) * 100)
|
||||||
|
}
|
||||||
|
|
||||||
|
func round(num float64) int {
|
||||||
|
return int(num + math.Copysign(0.5, num))
|
||||||
}
|
}
|
||||||
|
|
||||||
func colorScale(n int) ui.Attribute {
|
func colorScale(n int) ui.Attribute {
|
||||||
|
49
grid.go
49
grid.go
@ -11,12 +11,7 @@ type Grid struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g *Grid) AddContainer(id string) {
|
func (g *Grid) AddContainer(id string) {
|
||||||
cid := ui.NewPar(id)
|
g.containers[id] = NewContainer(id)
|
||||||
cid.Border = false
|
|
||||||
cid.Height = 2
|
|
||||||
cid.Width = 10
|
|
||||||
cid.TextFgColor = ui.ColorWhite
|
|
||||||
g.containers[id] = &Container{cid, mkGauge(), mkGauge()}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return sorted list of active container IDs
|
// Return sorted list of active container IDs
|
||||||
@ -33,9 +28,9 @@ func (g *Grid) Rows() (rows []*ui.Row) {
|
|||||||
for _, cid := range g.CIDs() {
|
for _, cid := range g.CIDs() {
|
||||||
c := g.containers[cid]
|
c := g.containers[cid]
|
||||||
rows = append(rows, ui.NewRow(
|
rows = append(rows, ui.NewRow(
|
||||||
ui.NewCol(1, 0, c.cid),
|
ui.NewCol(1, 0, c.widgets.cid),
|
||||||
ui.NewCol(2, 0, c.cpu),
|
ui.NewCol(2, 0, c.widgets.cpu),
|
||||||
ui.NewCol(2, 0, c.memory),
|
ui.NewCol(2, 0, c.widgets.memory),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
return rows
|
return rows
|
||||||
@ -87,25 +82,14 @@ func header() *ui.Row {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGrid() {
|
func Display(g *Grid) {
|
||||||
if err := ui.Init(); err != nil {
|
if err := ui.Init(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
defer ui.Close()
|
defer ui.Close()
|
||||||
|
|
||||||
g := &Grid{make(map[string]*Container)}
|
|
||||||
for _, id := range []string{" 12345", " 56789", " 00001"} {
|
|
||||||
g.AddContainer(id)
|
|
||||||
}
|
|
||||||
|
|
||||||
par := ui.NewPar("<> This row has 3 columns\n<- Widgets can be stacked up like left side\n<- Stacked widgets are treated as a single widget")
|
|
||||||
par.Height = 5
|
|
||||||
par.BorderLabel = "Demonstration"
|
|
||||||
|
|
||||||
// build layout
|
// build layout
|
||||||
ui.Body.AddRows(
|
ui.Body.AddRows(header())
|
||||||
header(),
|
|
||||||
)
|
|
||||||
|
|
||||||
for _, row := range g.Rows() {
|
for _, row := range g.Rows() {
|
||||||
ui.Body.AddRows(row)
|
ui.Body.AddRows(row)
|
||||||
@ -120,17 +104,12 @@ func NewGrid() {
|
|||||||
ui.StopLoop()
|
ui.StopLoop()
|
||||||
})
|
})
|
||||||
ui.Handle("/timer/1s", func(e ui.Event) {
|
ui.Handle("/timer/1s", func(e ui.Event) {
|
||||||
t := e.Data.(ui.EvtTimer)
|
// t := e.Data.(ui.EvtTimer)
|
||||||
i := t.Count
|
// i := t.Count
|
||||||
if i > 103 {
|
// if i > 103 {
|
||||||
ui.StopLoop()
|
// ui.StopLoop()
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
|
|
||||||
for _, c := range g.containers {
|
|
||||||
c.UpdateCPU((c.cpu.Percent + 5) % 100)
|
|
||||||
c.UpdateMem((c.memory.Percent + 12) % 100)
|
|
||||||
}
|
|
||||||
|
|
||||||
ui.Render(ui.Body)
|
ui.Render(ui.Body)
|
||||||
})
|
})
|
||||||
@ -144,7 +123,3 @@ func NewGrid() {
|
|||||||
|
|
||||||
ui.Loop()
|
ui.Loop()
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
|
||||||
NewGrid()
|
|
||||||
}
|
|
||||||
|
30
main.go
Normal file
30
main.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/fsouza/go-dockerclient"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if len(os.Args) < 2 {
|
||||||
|
fmt.Println("no container provided")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := docker.NewClient("tcp://127.0.0.1:4243")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
g := &Grid{make(map[string]*Container)}
|
||||||
|
g.AddContainer(os.Args[1])
|
||||||
|
|
||||||
|
for _, c := range g.containers {
|
||||||
|
c.Collect(client)
|
||||||
|
}
|
||||||
|
|
||||||
|
Display(g)
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user