mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
add names column to multiview
This commit is contained in:
parent
055257528a
commit
407e8fca0e
@ -2,24 +2,28 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/fsouza/go-dockerclient"
|
"github.com/fsouza/go-dockerclient"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Container struct {
|
type Container struct {
|
||||||
id string
|
id string
|
||||||
|
name string
|
||||||
done chan bool
|
done chan bool
|
||||||
stats chan *docker.Stats
|
stats chan *docker.Stats
|
||||||
widgets *Widgets
|
widgets *Widgets
|
||||||
reader *StatReader
|
reader *StatReader
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewContainer(cid string) *Container {
|
func NewContainer(cid string, names []string) *Container {
|
||||||
|
name := strings.Join(names, ",")
|
||||||
return &Container{
|
return &Container{
|
||||||
id: cid,
|
id: cid,
|
||||||
|
name: name,
|
||||||
done: make(chan bool),
|
done: make(chan bool),
|
||||||
stats: make(chan *docker.Stats),
|
stats: make(chan *docker.Stats),
|
||||||
widgets: NewWidgets(cid),
|
widgets: NewWidgets(cid, name),
|
||||||
reader: &StatReader{},
|
reader: &StatReader{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
57
grid.go
57
grid.go
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
ui "github.com/gizak/termui"
|
ui "github.com/gizak/termui"
|
||||||
@ -11,8 +12,8 @@ type Grid struct {
|
|||||||
containers map[string]*Container
|
containers map[string]*Container
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Grid) AddContainer(id string) {
|
func (g *Grid) AddContainer(id string, names []string) {
|
||||||
g.containers[id] = NewContainer(id)
|
g.containers[id] = NewContainer(id, names)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return number of containers/rows
|
// Return number of containers/rows
|
||||||
@ -48,52 +49,30 @@ func (g *Grid) Cursor() {
|
|||||||
func (g *Grid) Rows() (rows []*ui.Row) {
|
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, c.widgets.MakeRow())
|
||||||
ui.NewCol(1, 0, c.widgets.cid),
|
|
||||||
ui.NewCol(2, 0, c.widgets.cpu),
|
|
||||||
ui.NewCol(2, 0, c.widgets.memory),
|
|
||||||
ui.NewCol(2, 0, c.widgets.net),
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
return rows
|
return rows
|
||||||
}
|
}
|
||||||
|
|
||||||
func header() *ui.Row {
|
func header() *ui.Row {
|
||||||
// cid
|
|
||||||
c1 := ui.NewPar(" CID")
|
|
||||||
c1.Border = false
|
|
||||||
c1.Height = 2
|
|
||||||
c1.Width = 20
|
|
||||||
c1.TextFgColor = ui.ColorWhite
|
|
||||||
|
|
||||||
// cpu
|
|
||||||
c2 := ui.NewPar(" CPU")
|
|
||||||
c2.Border = false
|
|
||||||
c2.Height = 2
|
|
||||||
c2.Width = 10
|
|
||||||
c2.TextFgColor = ui.ColorWhite
|
|
||||||
|
|
||||||
// mem
|
|
||||||
c3 := ui.NewPar(" MEM")
|
|
||||||
c3.Border = false
|
|
||||||
c3.Height = 2
|
|
||||||
c3.Width = 10
|
|
||||||
c3.TextFgColor = ui.ColorWhite
|
|
||||||
|
|
||||||
// net
|
|
||||||
c4 := ui.NewPar(" NET RX/TX")
|
|
||||||
c4.Border = false
|
|
||||||
c4.Height = 2
|
|
||||||
c4.Width = 10
|
|
||||||
c4.TextFgColor = ui.ColorWhite
|
|
||||||
return ui.NewRow(
|
return ui.NewRow(
|
||||||
ui.NewCol(1, 0, c1),
|
ui.NewCol(1, 0, headerPar("CID")),
|
||||||
ui.NewCol(2, 0, c2),
|
ui.NewCol(2, 0, headerPar("CPU")),
|
||||||
ui.NewCol(2, 0, c3),
|
ui.NewCol(2, 0, headerPar("MEM")),
|
||||||
ui.NewCol(2, 0, c4),
|
ui.NewCol(2, 0, headerPar("NET RX/TX")),
|
||||||
|
ui.NewCol(2, 0, headerPar("NAMES")),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func headerPar(s string) *ui.Par {
|
||||||
|
p := ui.NewPar(fmt.Sprintf(" %s", s))
|
||||||
|
p.Border = false
|
||||||
|
p.Height = 2
|
||||||
|
p.Width = 20
|
||||||
|
p.TextFgColor = ui.ColorWhite
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
func Display(g *Grid) {
|
func Display(g *Grid) {
|
||||||
if err := ui.Init(); err != nil {
|
if err := ui.Init(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
20
main.go
20
main.go
@ -6,7 +6,7 @@ import (
|
|||||||
"github.com/fsouza/go-dockerclient"
|
"github.com/fsouza/go-dockerclient"
|
||||||
)
|
)
|
||||||
|
|
||||||
func runningCIDs(client *docker.Client) (running []string) {
|
func getContainers(client *docker.Client) []docker.APIContainers {
|
||||||
filters := make(map[string][]string)
|
filters := make(map[string][]string)
|
||||||
filters["status"] = []string{"running"}
|
filters["status"] = []string{"running"}
|
||||||
opts := docker.ListContainersOptions{
|
opts := docker.ListContainersOptions{
|
||||||
@ -16,15 +16,10 @@ func runningCIDs(client *docker.Client) (running []string) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
for _, c := range containers {
|
return containers
|
||||||
running = append(running, c.ID[:12])
|
|
||||||
}
|
|
||||||
return running
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var containers []string
|
|
||||||
|
|
||||||
dockerhost := os.Getenv("DOCKER_HOST")
|
dockerhost := os.Getenv("DOCKER_HOST")
|
||||||
if dockerhost == "" {
|
if dockerhost == "" {
|
||||||
dockerhost = "unix:///var/run/docker.sock"
|
dockerhost = "unix:///var/run/docker.sock"
|
||||||
@ -35,16 +30,9 @@ func main() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default to all running containers
|
|
||||||
if len(os.Args) < 2 {
|
|
||||||
containers = runningCIDs(client)
|
|
||||||
} else {
|
|
||||||
containers = os.Args[1:]
|
|
||||||
}
|
|
||||||
|
|
||||||
g := &Grid{0, make(map[string]*Container)}
|
g := &Grid{0, make(map[string]*Container)}
|
||||||
for _, c := range containers {
|
for _, c := range getContainers(client) {
|
||||||
g.AddContainer(c)
|
g.AddContainer(c.ID[:12], c.Names)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range g.containers {
|
for _, c := range g.containers {
|
||||||
|
19
widgets.go
19
widgets.go
@ -15,6 +15,16 @@ type Widgets struct {
|
|||||||
memory *ui.Gauge
|
memory *ui.Gauge
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *Widgets) MakeRow() *ui.Row {
|
||||||
|
return ui.NewRow(
|
||||||
|
ui.NewCol(1, 0, w.cid),
|
||||||
|
ui.NewCol(2, 0, w.cpu),
|
||||||
|
ui.NewCol(2, 0, w.memory),
|
||||||
|
ui.NewCol(2, 0, w.net),
|
||||||
|
ui.NewCol(2, 0, w.names),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
func (w *Widgets) SetCPU(val int) {
|
func (w *Widgets) SetCPU(val int) {
|
||||||
w.cpu.BarColor = colorScale(val)
|
w.cpu.BarColor = colorScale(val)
|
||||||
w.cpu.Label = fmt.Sprintf("%s%%", strconv.Itoa(val))
|
w.cpu.Label = fmt.Sprintf("%s%%", strconv.Itoa(val))
|
||||||
@ -36,13 +46,18 @@ func (w *Widgets) SetMem(val int64, limit int64) {
|
|||||||
w.memory.Label = fmt.Sprintf("%s / %s", byteFormat(val), byteFormat(limit))
|
w.memory.Label = fmt.Sprintf("%s / %s", byteFormat(val), byteFormat(limit))
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWidgets(id string) *Widgets {
|
func NewWidgets(id string, names string) *Widgets {
|
||||||
cid := ui.NewPar(id)
|
cid := ui.NewPar(id)
|
||||||
cid.Border = false
|
cid.Border = false
|
||||||
cid.Height = 1
|
cid.Height = 1
|
||||||
cid.Width = 20
|
cid.Width = 20
|
||||||
cid.TextFgColor = ui.ColorWhite
|
cid.TextFgColor = ui.ColorWhite
|
||||||
return &Widgets{cid, mkGauge(), mkGauge(), mkGauge()}
|
name := ui.NewPar(names)
|
||||||
|
name.Border = false
|
||||||
|
name.Height = 1
|
||||||
|
name.Width = 20
|
||||||
|
name.TextFgColor = ui.ColorWhite
|
||||||
|
return &Widgets{cid, name, mkGauge(), mkGauge(), mkGauge()}
|
||||||
}
|
}
|
||||||
|
|
||||||
func mkGauge() *ui.Gauge {
|
func mkGauge() *ui.Gauge {
|
||||||
|
Loading…
Reference in New Issue
Block a user