xmtop/widgets/cpu.go

141 lines
3.1 KiB
Go
Raw Normal View History

2018-02-19 15:25:02 +08:00
package widgets
import (
2018-04-11 11:08:02 +08:00
"fmt"
"log"
"sync"
2018-02-19 15:25:02 +08:00
"time"
"github.com/prometheus/client_golang/prometheus"
2020-02-29 04:48:35 +08:00
"github.com/xxxserxxx/gotop/v3/devices"
2019-01-31 07:20:09 +08:00
2020-02-29 04:48:35 +08:00
ui "github.com/xxxserxxx/gotop/v3/termui"
2018-02-19 15:25:02 +08:00
)
2019-03-01 08:29:52 +08:00
type CpuWidget struct {
2018-02-19 15:25:02 +08:00
*ui.LineGraph
2019-03-01 08:29:52 +08:00
CpuCount int
ShowAverageLoad bool
ShowPerCpuLoad bool
updateInterval time.Duration
updateLock sync.Mutex
metric map[string]prometheus.Gauge
2018-02-19 15:25:02 +08:00
}
var cpuLabels []string
func NewCpuWidget(updateInterval time.Duration, horizontalScale int, showAverageLoad bool, showPerCpuLoad bool) *CpuWidget {
2019-03-01 08:29:52 +08:00
self := &CpuWidget{
LineGraph: ui.NewLineGraph(),
CpuCount: len(cpuLabels),
2019-03-01 08:29:52 +08:00
updateInterval: updateInterval,
ShowAverageLoad: showAverageLoad,
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
2019-03-01 08:29:52 +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 {
self.Data["AVRG"] = []float64{0}
2018-02-19 15:25:02 +08:00
}
2019-03-01 08:29:52 +08:00
if self.ShowPerCpuLoad {
2020-02-29 00:03:41 +08:00
cpus := make(map[string]int)
devices.UpdateCPU(cpus, self.updateInterval, self.ShowPerCpuLoad)
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-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
}
func (self *CpuWidget) EnableMetric() {
if self.ShowAverageLoad {
self.metric = make(map[string]prometheus.Gauge)
self.metric["AVRG"] = prometheus.NewGauge(prometheus.GaugeOpts{
Subsystem: "cpu",
Name: "avg",
})
} else {
2020-02-29 00:03:41 +08:00
cpus := make(map[string]int)
devices.UpdateCPU(cpus, self.updateInterval, self.ShowPerCpuLoad)
self.metric = make(map[string]prometheus.Gauge)
for key, perc := range cpus {
gauge := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "gotop",
Subsystem: "cpu",
Name: key,
})
2020-02-29 00:03:41 +08:00
gauge.Set(float64(perc))
prometheus.MustRegister(gauge)
self.metric[key] = gauge
}
}
}
func (b *CpuWidget) Scale(i int) {
b.LineGraph.HorizontalScale = i
}
2019-03-01 08:29:52 +08:00
func (self *CpuWidget) update() {
if self.ShowAverageLoad {
go func() {
2020-02-29 00:03:41 +08:00
cpus := make(map[string]int)
devices.UpdateCPU(cpus, self.updateInterval, false)
self.Lock()
defer self.Unlock()
self.updateLock.Lock()
defer self.updateLock.Unlock()
var val float64
for _, v := range cpus {
2020-02-29 00:03:41 +08:00
val = float64(v)
break
}
self.Data["AVRG"] = append(self.Data["AVRG"], val)
self.Labels["AVRG"] = fmt.Sprintf("%3.0f%%", val)
if self.metric != nil {
self.metric["AVRG"].Set(val)
}
}()
2018-02-19 15:25:02 +08:00
}
2018-08-01 05:24:44 +08:00
2019-03-01 08:29:52 +08:00
if self.ShowPerCpuLoad {
go func() {
2020-02-29 00:03:41 +08:00
cpus := make(map[string]int)
devices.UpdateCPU(cpus, self.updateInterval, true)
self.Lock()
defer self.Unlock()
self.updateLock.Lock()
defer self.updateLock.Unlock()
for key, percent := range cpus {
2020-02-29 00:03:41 +08:00
self.Data[key] = append(self.Data[key], float64(percent))
self.Labels[key] = fmt.Sprintf("%d%%", percent)
if self.metric != nil {
if self.metric[key] == nil {
log.Printf("no metrics for %s", key)
} else {
2020-02-29 00:03:41 +08:00
self.metric[key].Set(float64(percent))
2018-12-14 13:59:45 +08:00
}
}
}
}()
2018-08-01 05:24:44 +08:00
}
2018-02-19 15:25:02 +08:00
}