Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added supprot for network rx/tx #159

Open
wants to merge 8 commits into
base: k8s
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions connector/collector/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (k *Kubernetes) Start() {
k.ReadMem()
k.ReadNetRx()
k.ReadNetTx()
k.ReadUptime()
k.stream <- k.Metrics
time.Sleep(k.interval)
}
Expand Down Expand Up @@ -151,6 +152,16 @@ func (k *Kubernetes) ReadNetTx() {
k.NetTx = tx
}

func (k *Kubernetes) ReadUptime() {
uptime, err := k.read("/uptime")
if err != nil {
log.Errorf("collecte network uptime metric has error %s here %s", k.name, err.Error())
time.Sleep(1 * time.Second)
return
}
k.Uptime = uptime
}

func (k *Kubernetes) read(name string) (int64, error) {
m := &Response{}
url := buildURL(config.GetVal("namespace"), k.name) + "/metrics" + name
Expand Down
2 changes: 1 addition & 1 deletion cwidgets/compact/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type CompactHeader struct {
}

func NewCompactHeader() *CompactHeader {
fields := []string{"", "NAME", "CID", "CPU", "MEM", "NET RX/TX", "IO R/W", "PIDS"}
fields := []string{"", "NAME", "CID", "CPU", "MEM", "NET RX/TX", "IO R/W", "PIDS", "UPTIME"}
ch := &CompactHeader{}
ch.Height = 2
for _, f := range fields {
Expand Down
8 changes: 8 additions & 0 deletions cwidgets/compact/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Compact struct {
Net *TextCol
IO *TextCol
Pids *TextCol
Uptime *TextCol
Bg *RowBg
X, Y int
Width int
Expand All @@ -38,6 +39,7 @@ func NewCompact(id string) *Compact {
Net: NewTextCol("-"),
IO: NewTextCol("-"),
Pids: NewTextCol("-"),
Uptime: NewTextCol("-"),
Bg: NewRowBg(),
X: 1,
Height: 1,
Expand Down Expand Up @@ -70,6 +72,7 @@ func (row *Compact) SetMetrics(m models.Metrics) {
row.SetMem(m.MemUsage, m.MemLimit, m.MemPercent)
row.SetIO(m.IOBytesRead, m.IOBytesWrite)
row.SetPids(m.Pids)
row.SetUptime(m.Uptime)
}

// Set gauges, counters to default unread values
Expand All @@ -79,6 +82,7 @@ func (row *Compact) Reset() {
row.Net.Reset()
row.IO.Reset()
row.Pids.Reset()
row.Uptime.Reset()
}

func (row *Compact) GetHeight() int {
Expand Down Expand Up @@ -137,6 +141,7 @@ func (row *Compact) Buffer() ui.Buffer {
buf.Merge(row.Net.Buffer())
buf.Merge(row.IO.Buffer())
buf.Merge(row.Pids.Buffer())
buf.Merge(row.Uptime.Buffer())
return buf
}

Expand All @@ -150,6 +155,7 @@ func (row *Compact) all() []ui.GridBufferer {
row.Net,
row.IO,
row.Pids,
row.Uptime,
}
}

Expand All @@ -163,6 +169,7 @@ func (row *Compact) Highlight() {
row.Net.Highlight()
row.IO.Highlight()
row.Pids.Highlight()
row.Uptime.Highlight()
}
}

Expand All @@ -176,6 +183,7 @@ func (row *Compact) UnHighlight() {
row.Net.UnHighlight()
row.IO.UnHighlight()
row.Pids.UnHighlight()
row.Uptime.UnHighlight()
}
}

Expand Down
14 changes: 14 additions & 0 deletions cwidgets/compact/setters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package compact
import (
"fmt"
"strconv"
"time"

"github.com/bcicen/ctop/cwidgets"
ui "github.com/gizak/termui"
Expand All @@ -23,6 +24,19 @@ func (row *Compact) SetPids(val int) {
row.Pids.Set(label)
}

func (row *Compact) SetUptime(val int64) {
d := time.Duration(val) * time.Millisecond
label := "- h"
sah4ez marked this conversation as resolved.
Show resolved Hide resolved
if d.Hours() < 1.0 {
label = fmt.Sprintf("%.0fm", d.Minutes())
} else if d.Hours() < 24.0 {
label = fmt.Sprintf("%.0fh", d.Hours())
} else {
label = fmt.Sprintf("%dd", int(d.Hours())%24)
}
row.Uptime.Set(label)
}

func (row *Compact) SetCPU(val int) {
row.Cpu.BarColor = colorScale(val)
row.Cpu.Label = fmt.Sprintf("%s%%", strconv.Itoa(val))
Expand Down
3 changes: 2 additions & 1 deletion cwidgets/compact/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ var colWidths = []int{
0, // memory
0, // net
0, // io
4, // pids
0, // pids
6, // uptime
}

// Calculate per-column width, given total width
Expand Down
2 changes: 2 additions & 0 deletions models/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Metrics struct {
IOBytesRead int64
IOBytesWrite int64
Pids int
Uptime int64
}

func NewMetrics() Metrics {
Expand All @@ -29,5 +30,6 @@ func NewMetrics() Metrics {
IOBytesRead: -1,
IOBytesWrite: -1,
Pids: -1,
Uptime: -1,
}
}