From b2165b6a297d38c459a7be8940e48c8bfc3b7233 Mon Sep 17 00:00:00 2001 From: Kenan Rhoton Date: Tue, 4 Apr 2017 12:58:32 +0200 Subject: [PATCH 1/3] Added Ports information to the expanded view --- cwidgets/expanded/main.go | 6 +++++ cwidgets/expanded/ports.go | 50 ++++++++++++++++++++++++++++++++++++++ metrics/docker.go | 22 +++++++++++++++++ metrics/main.go | 20 +++++++++------ 4 files changed, 90 insertions(+), 8 deletions(-) create mode 100644 cwidgets/expanded/ports.go diff --git a/cwidgets/expanded/main.go b/cwidgets/expanded/main.go index 9c355fd..2fcd666 100644 --- a/cwidgets/expanded/main.go +++ b/cwidgets/expanded/main.go @@ -18,6 +18,7 @@ type Expanded struct { Cpu *Cpu Mem *Mem IO *IO + Ports *Ports X, Y int Width int } @@ -32,6 +33,7 @@ func NewExpanded(id string) *Expanded { Cpu: NewCpu(), Mem: NewMem(), IO: NewIO(), + Ports: NewPorts(), Width: ui.TermWidth(), } } @@ -60,6 +62,7 @@ func (e *Expanded) SetMetrics(m metrics.Metrics) { e.Net.Update(m.NetRx, m.NetTx) e.Mem.Update(int(m.MemUsage), int(m.MemLimit)) e.IO.Update(m.IOBytesRead, m.IOBytesWrite) + e.Ports.Update(m.PortsExposed, m.PortsOpen) } // Return total column height @@ -69,6 +72,7 @@ func (e *Expanded) GetHeight() (h int) { h += e.Cpu.Height h += e.Mem.Height h += e.IO.Height + h += e.Ports.Height return h } @@ -106,6 +110,7 @@ func (e *Expanded) Buffer() ui.Buffer { buf.Merge(e.Mem.Buffer()) buf.Merge(e.Net.Buffer()) buf.Merge(e.IO.Buffer()) + buf.Merge(e.Ports.Buffer()) return buf } @@ -113,6 +118,7 @@ func (e *Expanded) all() []ui.GridBufferer { return []ui.GridBufferer{ e.Info, e.Cpu, + e.Ports, e.Mem, e.Net, e.IO, diff --git a/cwidgets/expanded/ports.go b/cwidgets/expanded/ports.go new file mode 100644 index 0000000..26e8796 --- /dev/null +++ b/cwidgets/expanded/ports.go @@ -0,0 +1,50 @@ +package expanded + +import ( + "strconv" + "strings" + + ui "github.com/gizak/termui" +) + +type Ports struct { + *ui.Table + Exposed []int + Open [][]int +} + +func NewPorts() *Ports { + t := ui.NewTable() + t.BorderLabel = "Ports" + t.Height = 4 + t.Width = colWidth[0] + t.FgColor = ui.ThemeAttr("par.text.fg") + t.Separator = false + p := &Ports{t, nil, nil} + return p +} + +func (p *Ports) Update(exposed []int64, open [][]int64) { + p.Rows = [][]string{} + + exp_string := "" + for i, exp := range exposed { + if i == 0 { + exp_string = strconv.Itoa(int(exp)) + } else { + exp_string = strings.Join([]string{exp_string, strconv.Itoa(int(exp))}, ", ") + } + } + p.Rows = append(p.Rows, []string{"Exposed: ", exp_string}) + + open_string := "" + for i, op := range open { + ported := strings.Join([]string{strconv.Itoa(int(op[0])), strconv.Itoa(int(op[1]))}, " -> ") + if i == 0 { + open_string = ported + } else { + open_string = strings.Join([]string{open_string, ported}, ", ") + } + } + p.Rows = append(p.Rows, []string{"Open: ", open_string}) +} diff --git a/metrics/docker.go b/metrics/docker.go index 070ddcd..20535e2 100644 --- a/metrics/docker.go +++ b/metrics/docker.go @@ -46,6 +46,7 @@ func (c *Docker) Start() { c.ReadCPU(s) c.ReadMem(s) c.ReadNet(s) + c.ReadPorts() c.ReadIO(s) c.stream <- c.Metrics } @@ -89,6 +90,27 @@ func (c *Docker) ReadMem(stats *api.Stats) { c.MemPercent = round((float64(c.MemUsage) / float64(c.MemLimit)) * 100) } +func (c *Docker) ReadPorts() { + containers, err := c.client.ListContainers(api.ListContainersOptions{All: true}) + if err != nil { + log.Infof("Error gathering containers") + return + } + for _, container := range containers { + if container.ID == c.id { + c.PortsOpen = [][]int64{} + c.PortsExposed = []int64{} + for _, port := range container.Ports { + if port.PublicPort > 0 { + c.PortsOpen = append(c.PortsOpen, []int64{port.PrivatePort, port.PublicPort}) + } else { + c.PortsExposed = append(c.PortsExposed, port.PrivatePort) + } + } + } + } +} + func (c *Docker) ReadNet(stats *api.Stats) { var rx, tx int64 for _, network := range stats.Networks { diff --git a/metrics/main.go b/metrics/main.go index 387b01d..7448268 100644 --- a/metrics/main.go +++ b/metrics/main.go @@ -18,18 +18,22 @@ type Metrics struct { IOBytesRead int64 IOBytesWrite int64 Pids int + PortsExposed []int64 + PortsOpen [][]int64 } func NewMetrics() Metrics { return Metrics{ - CPUUtil: -1, - NetTx: -1, - NetRx: -1, - MemUsage: -1, - MemPercent: -1, - IOBytesRead: -1, - IOBytesWrite: -1, - Pids: -1, + CPUUtil: -1, + NetTx: -1, + NetRx: -1, + MemUsage: -1, + MemPercent: -1, + IOBytesRead: -1, + IOBytesWrite: -1, + Pids: -1, + PortsExposed: nil, + PortsOpen: nil, } } From ccb44c964c0c00b3d3c8d0c3e104cac40265eb00 Mon Sep 17 00:00:00 2001 From: Kenan Rhoton Date: Mon, 15 May 2017 06:52:38 +0200 Subject: [PATCH 2/3] Moved port Info to be fetched from the standard inspect call to avoid superfluous calls. Also moved the information to the info section instead of a whole new section in the expanded view --- cwidgets/expanded/info.go | 2 +- dockersource.go | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/cwidgets/expanded/info.go b/cwidgets/expanded/info.go index aeed911..f798aac 100644 --- a/cwidgets/expanded/info.go +++ b/cwidgets/expanded/info.go @@ -4,7 +4,7 @@ import ( ui "github.com/gizak/termui" ) -var displayInfo = []string{"id", "name", "image", "state"} +var displayInfo = []string{"id", "name", "image", "ports", "state"} type Info struct { *ui.Table diff --git a/dockersource.go b/dockersource.go index 08a140a..1c58b02 100644 --- a/dockersource.go +++ b/dockersource.go @@ -60,6 +60,24 @@ func (cm *DockerContainerSource) watchEvents() { } } +func portsFormat(ports map[docker.Port][]docker.PortBinding) string { + res := "" + for k, v := range ports { + res += string(k) + if len(v) > 0 { + res += " -> [" + for i, p := range v { + res += p.HostPort + if i < len(v)-1 { + res += "," + } + } + res += "] " + } + } + return res +} + func (cm *DockerContainerSource) refresh(c *Container) { insp := cm.inspect(c.Id) // remove container if no longer exists @@ -69,6 +87,7 @@ func (cm *DockerContainerSource) refresh(c *Container) { } c.SetMeta("name", shortName(insp.Name)) c.SetMeta("image", insp.Config.Image) + c.SetMeta("ports", portsFormat(insp.NetworkSettings.Ports)) c.SetMeta("created", insp.Created.Format("Mon Jan 2 15:04:05 2006")) c.SetState(insp.State.Status) } From 02d105013092e92ebf872b40e7d73cfcc780aa03 Mon Sep 17 00:00:00 2001 From: Kenan Rhoton Date: Mon, 15 May 2017 06:53:15 +0200 Subject: [PATCH 3/3] Revert "Added Ports information to the expanded view" This reverts commit b2165b6a297d38c459a7be8940e48c8bfc3b7233. --- cwidgets/expanded/main.go | 6 ----- cwidgets/expanded/ports.go | 50 -------------------------------------- metrics/docker.go | 22 ----------------- metrics/main.go | 20 ++++++--------- 4 files changed, 8 insertions(+), 90 deletions(-) delete mode 100644 cwidgets/expanded/ports.go diff --git a/cwidgets/expanded/main.go b/cwidgets/expanded/main.go index 2fcd666..9c355fd 100644 --- a/cwidgets/expanded/main.go +++ b/cwidgets/expanded/main.go @@ -18,7 +18,6 @@ type Expanded struct { Cpu *Cpu Mem *Mem IO *IO - Ports *Ports X, Y int Width int } @@ -33,7 +32,6 @@ func NewExpanded(id string) *Expanded { Cpu: NewCpu(), Mem: NewMem(), IO: NewIO(), - Ports: NewPorts(), Width: ui.TermWidth(), } } @@ -62,7 +60,6 @@ func (e *Expanded) SetMetrics(m metrics.Metrics) { e.Net.Update(m.NetRx, m.NetTx) e.Mem.Update(int(m.MemUsage), int(m.MemLimit)) e.IO.Update(m.IOBytesRead, m.IOBytesWrite) - e.Ports.Update(m.PortsExposed, m.PortsOpen) } // Return total column height @@ -72,7 +69,6 @@ func (e *Expanded) GetHeight() (h int) { h += e.Cpu.Height h += e.Mem.Height h += e.IO.Height - h += e.Ports.Height return h } @@ -110,7 +106,6 @@ func (e *Expanded) Buffer() ui.Buffer { buf.Merge(e.Mem.Buffer()) buf.Merge(e.Net.Buffer()) buf.Merge(e.IO.Buffer()) - buf.Merge(e.Ports.Buffer()) return buf } @@ -118,7 +113,6 @@ func (e *Expanded) all() []ui.GridBufferer { return []ui.GridBufferer{ e.Info, e.Cpu, - e.Ports, e.Mem, e.Net, e.IO, diff --git a/cwidgets/expanded/ports.go b/cwidgets/expanded/ports.go deleted file mode 100644 index 26e8796..0000000 --- a/cwidgets/expanded/ports.go +++ /dev/null @@ -1,50 +0,0 @@ -package expanded - -import ( - "strconv" - "strings" - - ui "github.com/gizak/termui" -) - -type Ports struct { - *ui.Table - Exposed []int - Open [][]int -} - -func NewPorts() *Ports { - t := ui.NewTable() - t.BorderLabel = "Ports" - t.Height = 4 - t.Width = colWidth[0] - t.FgColor = ui.ThemeAttr("par.text.fg") - t.Separator = false - p := &Ports{t, nil, nil} - return p -} - -func (p *Ports) Update(exposed []int64, open [][]int64) { - p.Rows = [][]string{} - - exp_string := "" - for i, exp := range exposed { - if i == 0 { - exp_string = strconv.Itoa(int(exp)) - } else { - exp_string = strings.Join([]string{exp_string, strconv.Itoa(int(exp))}, ", ") - } - } - p.Rows = append(p.Rows, []string{"Exposed: ", exp_string}) - - open_string := "" - for i, op := range open { - ported := strings.Join([]string{strconv.Itoa(int(op[0])), strconv.Itoa(int(op[1]))}, " -> ") - if i == 0 { - open_string = ported - } else { - open_string = strings.Join([]string{open_string, ported}, ", ") - } - } - p.Rows = append(p.Rows, []string{"Open: ", open_string}) -} diff --git a/metrics/docker.go b/metrics/docker.go index 20535e2..070ddcd 100644 --- a/metrics/docker.go +++ b/metrics/docker.go @@ -46,7 +46,6 @@ func (c *Docker) Start() { c.ReadCPU(s) c.ReadMem(s) c.ReadNet(s) - c.ReadPorts() c.ReadIO(s) c.stream <- c.Metrics } @@ -90,27 +89,6 @@ func (c *Docker) ReadMem(stats *api.Stats) { c.MemPercent = round((float64(c.MemUsage) / float64(c.MemLimit)) * 100) } -func (c *Docker) ReadPorts() { - containers, err := c.client.ListContainers(api.ListContainersOptions{All: true}) - if err != nil { - log.Infof("Error gathering containers") - return - } - for _, container := range containers { - if container.ID == c.id { - c.PortsOpen = [][]int64{} - c.PortsExposed = []int64{} - for _, port := range container.Ports { - if port.PublicPort > 0 { - c.PortsOpen = append(c.PortsOpen, []int64{port.PrivatePort, port.PublicPort}) - } else { - c.PortsExposed = append(c.PortsExposed, port.PrivatePort) - } - } - } - } -} - func (c *Docker) ReadNet(stats *api.Stats) { var rx, tx int64 for _, network := range stats.Networks { diff --git a/metrics/main.go b/metrics/main.go index 7448268..387b01d 100644 --- a/metrics/main.go +++ b/metrics/main.go @@ -18,22 +18,18 @@ type Metrics struct { IOBytesRead int64 IOBytesWrite int64 Pids int - PortsExposed []int64 - PortsOpen [][]int64 } func NewMetrics() Metrics { return Metrics{ - CPUUtil: -1, - NetTx: -1, - NetRx: -1, - MemUsage: -1, - MemPercent: -1, - IOBytesRead: -1, - IOBytesWrite: -1, - Pids: -1, - PortsExposed: nil, - PortsOpen: nil, + CPUUtil: -1, + NetTx: -1, + NetRx: -1, + MemUsage: -1, + MemPercent: -1, + IOBytesRead: -1, + IOBytesWrite: -1, + Pids: -1, } }