2018-02-19 15:25:02 +08:00
|
|
|
package widgets
|
|
|
|
|
|
|
|
import (
|
2018-04-11 11:08:02 +08:00
|
|
|
"fmt"
|
2018-12-05 13:44:25 +08:00
|
|
|
"log"
|
2019-01-20 12:34:19 +08:00
|
|
|
"sync"
|
2018-02-19 15:25:02 +08:00
|
|
|
"time"
|
|
|
|
|
2020-02-18 23:44:29 +08:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2020-04-24 03:07:08 +08:00
|
|
|
"github.com/xxxserxxx/gotop/v4/devices"
|
2019-01-31 07:20:09 +08:00
|
|
|
|
2020-04-24 03:07:08 +08:00
|
|
|
ui "github.com/xxxserxxx/gotop/v4/termui"
|
2018-02-19 15:25:02 +08:00
|
|
|
)
|
|
|
|
|
2020-04-28 09:33:41 +08:00
|
|
|
type CPUWidget struct {
|
2018-02-19 15:25:02 +08:00
|
|
|
*ui.LineGraph
|
2020-04-28 09:33:41 +08:00
|
|
|
CPUCount int
|
2019-03-01 08:29:52 +08:00
|
|
|
ShowAverageLoad bool
|
2020-04-28 09:33:41 +08:00
|
|
|
ShowPerCPULoad bool
|
2019-03-01 08:29:52 +08:00
|
|
|
updateInterval time.Duration
|
|
|
|
updateLock sync.Mutex
|
2020-02-28 08:51:28 +08:00
|
|
|
metric map[string]prometheus.Gauge
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 08:51:28 +08:00
|
|
|
var cpuLabels []string
|
|
|
|
|
2020-04-28 09:33:41 +08:00
|
|
|
func NewCPUWidget(updateInterval time.Duration, horizontalScale int, showAverageLoad bool, showPerCPULoad bool) *CPUWidget {
|
|
|
|
self := &CPUWidget{
|
2019-03-01 08:29:52 +08:00
|
|
|
LineGraph: ui.NewLineGraph(),
|
2020-04-28 09:33:41 +08:00
|
|
|
CPUCount: len(cpuLabels),
|
2019-03-01 08:29:52 +08:00
|
|
|
updateInterval: updateInterval,
|
|
|
|
ShowAverageLoad: showAverageLoad,
|
2020-04-28 09:33:41 +08:00
|
|
|
ShowPerCPULoad: showPerCPULoad,
|
2018-03-04 09:05:52 +08:00
|
|
|
}
|
2019-01-01 08:55:50 +08:00
|
|
|
self.Title = " CPU Usage "
|
2019-01-13 08:31:37 +08:00
|
|
|
self.HorizontalScale = horizontalScale
|
2018-08-01 05:24:44 +08:00
|
|
|
|
2020-04-28 09:33:41 +08:00
|
|
|
if !(self.ShowAverageLoad || self.ShowPerCPULoad) {
|
|
|
|
if self.CPUCount <= 8 {
|
|
|
|
self.ShowPerCPULoad = true
|
2018-08-01 05:40:51 +08:00
|
|
|
} else {
|
2019-03-01 08:29:52 +08:00
|
|
|
self.ShowAverageLoad = true
|
2018-08-01 05:40:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-01 08:29:52 +08:00
|
|
|
if self.ShowAverageLoad {
|
2018-08-17 07:44:44 +08:00
|
|
|
self.Data["AVRG"] = []float64{0}
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
|
2020-04-28 09:33:41 +08:00
|
|
|
if self.ShowPerCPULoad {
|
2020-02-29 00:03:41 +08:00
|
|
|
cpus := make(map[string]int)
|
2020-04-28 09:33:41 +08:00
|
|
|
devices.UpdateCPU(cpus, self.updateInterval, self.ShowPerCPULoad)
|
2020-02-28 08:51:28 +08:00
|
|
|
for k, v := range cpus {
|
2020-02-29 00:03:41 +08:00
|
|
|
self.Data[k] = []float64{float64(v)}
|
2018-08-01 05:24:44 +08:00
|
|
|
}
|
|
|
|
}
|
2018-04-13 11:00:34 +08:00
|
|
|
|
2018-12-19 10:12:22 +08:00
|
|
|
self.update()
|
2018-11-30 10:17:13 +08:00
|
|
|
|
2018-02-19 15:25:02 +08:00
|
|
|
go func() {
|
2019-03-01 08:29:52 +08:00
|
|
|
for range time.NewTicker(self.updateInterval).C {
|
2018-03-28 05:27:23 +08:00
|
|
|
self.update()
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-03-28 05:27:23 +08:00
|
|
|
return self
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
|
2020-04-28 09:33:41 +08:00
|
|
|
func (cpu *CPUWidget) EnableMetric() {
|
|
|
|
if cpu.ShowAverageLoad {
|
|
|
|
cpu.metric = make(map[string]prometheus.Gauge)
|
|
|
|
cpu.metric["AVRG"] = prometheus.NewGauge(prometheus.GaugeOpts{
|
2020-02-18 23:44:29 +08:00
|
|
|
Subsystem: "cpu",
|
|
|
|
Name: "avg",
|
|
|
|
})
|
|
|
|
} else {
|
2020-02-29 00:03:41 +08:00
|
|
|
cpus := make(map[string]int)
|
2020-04-28 09:33:41 +08:00
|
|
|
devices.UpdateCPU(cpus, cpu.updateInterval, cpu.ShowPerCPULoad)
|
|
|
|
cpu.metric = make(map[string]prometheus.Gauge)
|
2020-02-28 08:51:28 +08:00
|
|
|
for key, perc := range cpus {
|
2020-02-18 23:44:29 +08:00
|
|
|
gauge := prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
Namespace: "gotop",
|
|
|
|
Subsystem: "cpu",
|
2020-02-28 08:51:28 +08:00
|
|
|
Name: key,
|
2020-02-18 23:44:29 +08:00
|
|
|
})
|
2020-02-29 00:03:41 +08:00
|
|
|
gauge.Set(float64(perc))
|
2020-02-18 23:44:29 +08:00
|
|
|
prometheus.MustRegister(gauge)
|
2020-04-28 09:33:41 +08:00
|
|
|
cpu.metric[key] = gauge
|
2020-02-18 23:44:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 09:33:41 +08:00
|
|
|
func (cpu *CPUWidget) Scale(i int) {
|
|
|
|
cpu.LineGraph.HorizontalScale = i
|
2020-02-13 10:15:49 +08:00
|
|
|
}
|
|
|
|
|
2020-04-28 09:33:41 +08:00
|
|
|
func (cpu *CPUWidget) update() {
|
|
|
|
if cpu.ShowAverageLoad {
|
2018-08-17 07:44:44 +08:00
|
|
|
go func() {
|
2020-02-29 00:03:41 +08:00
|
|
|
cpus := make(map[string]int)
|
2020-04-28 09:33:41 +08:00
|
|
|
devices.UpdateCPU(cpus, cpu.updateInterval, false)
|
|
|
|
cpu.Lock()
|
|
|
|
defer cpu.Unlock()
|
|
|
|
cpu.updateLock.Lock()
|
|
|
|
defer cpu.updateLock.Unlock()
|
2020-02-28 08:51:28 +08:00
|
|
|
var val float64
|
|
|
|
for _, v := range cpus {
|
2020-02-29 00:03:41 +08:00
|
|
|
val = float64(v)
|
2020-02-28 08:51:28 +08:00
|
|
|
break
|
|
|
|
}
|
2020-04-28 09:33:41 +08:00
|
|
|
cpu.Data["AVRG"] = append(cpu.Data["AVRG"], val)
|
|
|
|
cpu.Labels["AVRG"] = fmt.Sprintf("%3.0f%%", val)
|
|
|
|
if cpu.metric != nil {
|
|
|
|
cpu.metric["AVRG"].Set(val)
|
2018-12-05 13:44:25 +08:00
|
|
|
}
|
2018-08-17 07:44:44 +08:00
|
|
|
}()
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
2018-08-01 05:24:44 +08:00
|
|
|
|
2020-04-28 09:33:41 +08:00
|
|
|
if cpu.ShowPerCPULoad {
|
2018-08-17 07:44:44 +08:00
|
|
|
go func() {
|
2020-02-29 00:03:41 +08:00
|
|
|
cpus := make(map[string]int)
|
2020-04-28 09:33:41 +08:00
|
|
|
devices.UpdateCPU(cpus, cpu.updateInterval, true)
|
|
|
|
cpu.Lock()
|
|
|
|
defer cpu.Unlock()
|
|
|
|
cpu.updateLock.Lock()
|
|
|
|
defer cpu.updateLock.Unlock()
|
2020-02-28 08:51:28 +08:00
|
|
|
for key, percent := range cpus {
|
2020-04-28 09:33:41 +08:00
|
|
|
cpu.Data[key] = append(cpu.Data[key], float64(percent))
|
|
|
|
cpu.Labels[key] = fmt.Sprintf("%d%%", percent)
|
|
|
|
if cpu.metric != nil {
|
|
|
|
if cpu.metric[key] == nil {
|
2020-02-28 08:51:28 +08:00
|
|
|
log.Printf("no metrics for %s", key)
|
|
|
|
} else {
|
2020-04-28 09:33:41 +08:00
|
|
|
cpu.metric[key].Set(float64(percent))
|
2018-12-14 13:59:45 +08:00
|
|
|
}
|
|
|
|
}
|
2018-08-17 07:44:44 +08:00
|
|
|
}
|
|
|
|
}()
|
2018-08-01 05:24:44 +08:00
|
|
|
}
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|