2017-01-13 23:07:43 -07:00
|
|
|
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
2016-01-26 18:45:18 -07:00
|
|
|
// Use of this source code is governed by a MIT license that can
|
|
|
|
// be found in the LICENSE file.
|
|
|
|
|
2015-06-16 15:16:37 -06:00
|
|
|
// +build ignore
|
|
|
|
|
2015-06-16 14:39:24 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2015-10-21 09:11:08 -06:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-01-23 21:12:10 -07:00
|
|
|
"image"
|
2015-06-16 14:39:24 -06:00
|
|
|
"io"
|
2019-01-23 21:12:10 -07:00
|
|
|
"log"
|
2015-10-21 09:11:08 -06:00
|
|
|
"os"
|
2015-06-16 14:39:24 -06:00
|
|
|
"regexp"
|
2015-10-21 09:11:08 -06:00
|
|
|
"runtime"
|
2015-06-16 14:39:24 -06:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
2015-10-21 09:11:08 -06:00
|
|
|
"strings"
|
2018-09-06 17:36:14 -06:00
|
|
|
"time"
|
2015-10-21 09:11:08 -06:00
|
|
|
|
2018-09-06 15:48:09 -06:00
|
|
|
ui "github.com/gizak/termui"
|
2019-01-23 21:12:10 -07:00
|
|
|
"github.com/gizak/termui/widgets"
|
2015-06-16 14:39:24 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
const statFilePath = "/proc/stat"
|
|
|
|
const meminfoFilePath = "/proc/meminfo"
|
|
|
|
|
|
|
|
type CpuStat struct {
|
2015-10-21 09:11:08 -06:00
|
|
|
user float32
|
|
|
|
nice float32
|
|
|
|
system float32
|
|
|
|
idle float32
|
2015-06-16 14:39:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type CpusStats struct {
|
2015-10-21 09:11:08 -06:00
|
|
|
stat map[string]CpuStat
|
|
|
|
proc map[string]CpuStat
|
2015-06-16 14:39:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewCpusStats(s map[string]CpuStat) *CpusStats {
|
|
|
|
return &CpusStats{stat: s, proc: make(map[string]CpuStat)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *CpusStats) String() (ret string) {
|
|
|
|
for key, _ := range cs.proc {
|
|
|
|
ret += fmt.Sprintf("%s: %.2f %.2f %.2f %.2f\n", key, cs.proc[key].user, cs.proc[key].nice, cs.proc[key].system, cs.proc[key].idle)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func subCpuStat(m CpuStat, s CpuStat) CpuStat {
|
|
|
|
return CpuStat{user: m.user - s.user,
|
2015-10-21 09:11:08 -06:00
|
|
|
nice: m.nice - s.nice,
|
|
|
|
system: m.system - s.system,
|
|
|
|
idle: m.idle - s.idle}
|
2015-06-16 14:39:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func procCpuStat(c CpuStat) CpuStat {
|
|
|
|
sum := c.user + c.nice + c.system + c.idle
|
2015-10-21 09:11:08 -06:00
|
|
|
return CpuStat{user: c.user / sum * 100,
|
|
|
|
nice: c.nice / sum * 100,
|
|
|
|
system: c.system / sum * 100,
|
|
|
|
idle: c.idle / sum * 100}
|
2015-06-16 14:39:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *CpusStats) tick(ns map[string]CpuStat) {
|
|
|
|
for key, _ := range cs.stat {
|
|
|
|
proc := subCpuStat(ns[key], cs.stat[key])
|
|
|
|
cs.proc[key] = procCpuStat(proc)
|
|
|
|
cs.stat[key] = ns[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type errIntParser struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (eip *errIntParser) parse(s string) (ret int64) {
|
|
|
|
if eip.err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
ret, eip.err = strconv.ParseInt(s, 10, 0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type LineProcessor interface {
|
|
|
|
process(string) error
|
2015-10-21 09:11:08 -06:00
|
|
|
finalize() interface{}
|
2015-06-16 14:39:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type CpuLineProcessor struct {
|
|
|
|
m map[string]CpuStat
|
|
|
|
}
|
|
|
|
|
|
|
|
func (clp *CpuLineProcessor) process(line string) (err error) {
|
|
|
|
r := regexp.MustCompile("^cpu([0-9]*)")
|
|
|
|
|
|
|
|
if r.MatchString(line) {
|
|
|
|
tab := strings.Fields(line)
|
|
|
|
if len(tab) < 5 {
|
|
|
|
err = errors.New("cpu info line has not enough fields")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
parser := errIntParser{}
|
|
|
|
cs := CpuStat{user: float32(parser.parse(tab[1])),
|
2015-10-21 09:11:08 -06:00
|
|
|
nice: float32(parser.parse(tab[2])),
|
|
|
|
system: float32(parser.parse(tab[3])),
|
|
|
|
idle: float32(parser.parse(tab[4]))}
|
2015-06-16 14:39:24 -06:00
|
|
|
clp.m[tab[0]] = cs
|
|
|
|
err = parser.err
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-21 09:11:08 -06:00
|
|
|
func (clp *CpuLineProcessor) finalize() interface{} {
|
2015-06-16 14:39:24 -06:00
|
|
|
return clp.m
|
|
|
|
}
|
|
|
|
|
|
|
|
type MemStat struct {
|
2015-10-21 09:11:08 -06:00
|
|
|
total int64
|
|
|
|
free int64
|
2015-06-16 14:39:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ms MemStat) String() (ret string) {
|
|
|
|
ret = fmt.Sprintf("TotalMem: %d, FreeMem: %d\n", ms.total, ms.free)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *MemStat) process(line string) (err error) {
|
|
|
|
rtotal := regexp.MustCompile("^MemTotal:")
|
|
|
|
rfree := regexp.MustCompile("^MemFree:")
|
|
|
|
var aux int64
|
|
|
|
if rtotal.MatchString(line) || rfree.MatchString(line) {
|
|
|
|
tab := strings.Fields(line)
|
|
|
|
if len(tab) < 3 {
|
|
|
|
err = errors.New("mem info line has not enough fields")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
aux, err = strconv.ParseInt(tab[1], 10, 0)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if rtotal.MatchString(line) {
|
|
|
|
ms.total = aux
|
|
|
|
}
|
|
|
|
if rfree.MatchString(line) {
|
|
|
|
ms.free = aux
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-21 09:11:08 -06:00
|
|
|
func (ms *MemStat) finalize() interface{} {
|
2015-06-16 14:39:24 -06:00
|
|
|
return *ms
|
|
|
|
}
|
|
|
|
|
2015-10-21 09:11:08 -06:00
|
|
|
func processFileLines(filePath string, lp LineProcessor) (ret interface{}, err error) {
|
2015-06-16 14:39:24 -06:00
|
|
|
var statFile *os.File
|
|
|
|
statFile, err = os.Open(filePath)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("open: %v\n", err)
|
|
|
|
}
|
|
|
|
defer statFile.Close()
|
|
|
|
|
|
|
|
statFileReader := bufio.NewReader(statFile)
|
|
|
|
|
|
|
|
for {
|
|
|
|
var line string
|
|
|
|
line, err = statFileReader.ReadString('\n')
|
|
|
|
if err == io.EOF {
|
|
|
|
err = nil
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("open: %v\n", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
|
|
|
|
err = lp.process(line)
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = lp.finalize()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCpusStatsMap() (m map[string]CpuStat, err error) {
|
2015-10-21 09:11:08 -06:00
|
|
|
var aux interface{}
|
2015-06-16 14:39:24 -06:00
|
|
|
aux, err = processFileLines(statFilePath, &CpuLineProcessor{m: make(map[string]CpuStat)})
|
|
|
|
return aux.(map[string]CpuStat), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getMemStats() (ms MemStat, err error) {
|
2015-10-21 09:11:08 -06:00
|
|
|
var aux interface{}
|
2015-06-16 14:39:24 -06:00
|
|
|
aux, err = processFileLines(meminfoFilePath, &MemStat{})
|
|
|
|
return aux.(MemStat), err
|
|
|
|
}
|
|
|
|
|
|
|
|
type CpuTabElems struct {
|
2019-01-23 21:12:10 -07:00
|
|
|
GMap map[string]*widgets.Gauge
|
|
|
|
LChart *widgets.LineChart
|
2015-06-16 14:39:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewCpuTabElems(width int) *CpuTabElems {
|
2019-01-23 21:12:10 -07:00
|
|
|
lc := widgets.NewLineChart()
|
|
|
|
lc.SetRect(0, 0, width, 12)
|
|
|
|
lc.LineType = widgets.DotLine
|
|
|
|
lc.Title = "CPU"
|
|
|
|
return &CpuTabElems{
|
|
|
|
GMap: make(map[string]*widgets.Gauge),
|
|
|
|
LChart: lc,
|
|
|
|
}
|
2015-06-16 14:39:24 -06:00
|
|
|
}
|
|
|
|
|
2019-01-23 21:12:10 -07:00
|
|
|
func (cte *CpuTabElems) AddGauge(key string, Y int, width int) *widgets.Gauge {
|
|
|
|
cte.GMap[key] = widgets.NewGauge()
|
|
|
|
cte.GMap[key].SetRect(0, Y, width, Y+3)
|
|
|
|
cte.GMap[key].Title = key
|
2015-10-21 09:11:08 -06:00
|
|
|
cte.GMap[key].Percent = 0 //int(val.user + val.nice + val.system)
|
2015-06-16 14:39:24 -06:00
|
|
|
return cte.GMap[key]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cte *CpuTabElems) Update(cs CpusStats) {
|
|
|
|
for key, val := range cs.proc {
|
|
|
|
p := int(val.user + val.nice + val.system)
|
|
|
|
cte.GMap[key].Percent = p
|
|
|
|
if key == "cpu" {
|
2019-01-23 21:12:10 -07:00
|
|
|
cte.LChart.Data = append(cte.LChart.Data, []float64{})
|
|
|
|
cte.LChart.Data[0] = append(cte.LChart.Data[0], 0)
|
|
|
|
copy(cte.LChart.Data[0][1:], cte.LChart.Data[0][0:])
|
|
|
|
cte.LChart.Data[0][0] = float64(p)
|
2015-06-16 14:39:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type MemTabElems struct {
|
2019-01-23 21:12:10 -07:00
|
|
|
Gauge *widgets.Gauge
|
|
|
|
SLines *widgets.SparklineGroup
|
2015-06-16 14:39:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewMemTabElems(width int) *MemTabElems {
|
2019-01-23 21:12:10 -07:00
|
|
|
g := widgets.NewGauge()
|
|
|
|
g.SetRect(0, 5, width, 10)
|
2015-06-16 14:39:24 -06:00
|
|
|
|
2019-01-23 21:12:10 -07:00
|
|
|
sline := widgets.NewSparkline()
|
2015-06-16 14:39:24 -06:00
|
|
|
sline.Title = "MEM"
|
|
|
|
|
2019-01-23 21:12:10 -07:00
|
|
|
sls := widgets.NewSparklineGroup(sline)
|
|
|
|
sls.SetRect(0, 10, width, 25)
|
2015-06-16 14:39:24 -06:00
|
|
|
return &MemTabElems{Gauge: g, SLines: sls}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mte *MemTabElems) Update(ms MemStat) {
|
2019-01-23 21:12:10 -07:00
|
|
|
used := (ms.total - ms.free) * 100 / ms.total
|
|
|
|
mte.Gauge.Percent = int(used)
|
|
|
|
mte.SLines.Sparklines[0].Data = append(mte.SLines.Sparklines[0].Data, 0)
|
|
|
|
copy(mte.SLines.Sparklines[0].Data[1:], mte.SLines.Sparklines[0].Data[0:])
|
|
|
|
mte.SLines.Sparklines[0].Data[0] = float64(used)
|
|
|
|
if len(mte.SLines.Sparklines[0].Data) > mte.SLines.Dx()-2 {
|
|
|
|
mte.SLines.Sparklines[0].Data = mte.SLines.Sparklines[0].Data[0 : mte.SLines.Dx()-2]
|
2015-06-16 14:39:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if runtime.GOOS != "linux" {
|
2019-01-23 21:12:10 -07:00
|
|
|
log.Fatalf("Currently only works on Linux")
|
2015-06-16 14:39:24 -06:00
|
|
|
}
|
2019-01-23 21:12:10 -07:00
|
|
|
if err := ui.Init(); err != nil {
|
|
|
|
log.Fatalf("failed to initialize termui: %v", err)
|
2018-09-06 19:22:04 -06:00
|
|
|
}
|
2018-09-06 15:48:09 -06:00
|
|
|
defer ui.Close()
|
2015-06-16 14:39:24 -06:00
|
|
|
|
|
|
|
termWidth := 70
|
|
|
|
|
2019-01-23 21:12:10 -07:00
|
|
|
header := widgets.NewParagraph()
|
|
|
|
header.Text = "Press q to quit, Press h or l to switch tabs"
|
|
|
|
header.SetRect(0, 0, 50, 1)
|
2016-11-02 23:41:47 -06:00
|
|
|
header.Border = false
|
2019-01-23 21:12:10 -07:00
|
|
|
header.TextStyle.Bg = ui.ColorBlue
|
2015-06-16 14:39:24 -06:00
|
|
|
|
|
|
|
cs, errcs := getCpusStatsMap()
|
|
|
|
cpusStats := NewCpusStats(cs)
|
|
|
|
|
|
|
|
if errcs != nil {
|
|
|
|
panic("error")
|
|
|
|
}
|
|
|
|
|
|
|
|
cpuTabElems := NewCpuTabElems(termWidth)
|
|
|
|
|
2019-01-23 21:12:10 -07:00
|
|
|
Y := 5
|
2015-06-16 14:39:24 -06:00
|
|
|
cpuKeys := make([]string, 0, len(cs))
|
|
|
|
for key := range cs {
|
|
|
|
cpuKeys = append(cpuKeys, key)
|
|
|
|
}
|
|
|
|
sort.Strings(cpuKeys)
|
|
|
|
for _, key := range cpuKeys {
|
2019-01-23 21:12:10 -07:00
|
|
|
cpuTabElems.AddGauge(key, Y, termWidth)
|
2015-06-16 14:39:24 -06:00
|
|
|
Y += 3
|
|
|
|
}
|
2019-01-23 21:12:10 -07:00
|
|
|
cpuTabElems.LChart.Rectangle = cpuTabElems.LChart.GetRect().Add(image.Pt(0, Y))
|
2015-06-16 14:39:24 -06:00
|
|
|
|
|
|
|
memTabElems := NewMemTabElems(termWidth)
|
|
|
|
ms, errm := getMemStats()
|
|
|
|
if errm != nil {
|
|
|
|
panic(errm)
|
|
|
|
}
|
|
|
|
memTabElems.Update(ms)
|
|
|
|
|
2019-01-23 21:12:10 -07:00
|
|
|
tabpane := widgets.NewTabPane("CPU", "MEM")
|
|
|
|
tabpane.SetRect(0, 1, 30, 30)
|
|
|
|
tabpane.Border = false
|
|
|
|
renderTab := func() {
|
|
|
|
switch tabpane.ActiveTabIndex {
|
|
|
|
case 0:
|
|
|
|
ui.Render(cpuTabElems.LChart)
|
|
|
|
for _, gauge := range cpuTabElems.GMap {
|
|
|
|
ui.Render(gauge)
|
|
|
|
}
|
|
|
|
case 1:
|
|
|
|
ui.Render(memTabElems.Gauge, memTabElems.SLines)
|
|
|
|
}
|
|
|
|
}
|
2015-06-16 14:39:24 -06:00
|
|
|
|
2018-09-06 15:48:09 -06:00
|
|
|
ui.Render(header, tabpane)
|
2019-01-23 21:12:10 -07:00
|
|
|
renderTab()
|
2015-06-16 14:39:24 -06:00
|
|
|
|
2018-11-28 19:19:34 -07:00
|
|
|
tickerCount := 1
|
2018-11-29 16:32:42 -07:00
|
|
|
uiEvents := ui.PollEvents()
|
|
|
|
ticker := time.NewTicker(time.Second).C
|
2018-11-28 19:19:34 -07:00
|
|
|
for {
|
|
|
|
select {
|
2018-11-29 16:32:42 -07:00
|
|
|
case e := <-uiEvents:
|
2018-11-28 19:19:34 -07:00
|
|
|
switch e.ID {
|
|
|
|
case "q", "<C-c>":
|
|
|
|
return
|
|
|
|
case "h":
|
2019-01-23 21:12:10 -07:00
|
|
|
tabpane.FocusLeft()
|
2018-11-28 19:19:34 -07:00
|
|
|
ui.Render(header, tabpane)
|
2019-01-23 21:12:10 -07:00
|
|
|
renderTab()
|
2018-11-28 19:19:34 -07:00
|
|
|
case "l":
|
2019-01-23 21:12:10 -07:00
|
|
|
tabpane.FocusRight()
|
2018-11-28 19:19:34 -07:00
|
|
|
ui.Render(header, tabpane)
|
2019-01-23 21:12:10 -07:00
|
|
|
renderTab()
|
2018-11-28 19:19:34 -07:00
|
|
|
}
|
2018-11-29 16:32:42 -07:00
|
|
|
case <-ticker:
|
2018-09-06 17:36:14 -06:00
|
|
|
cs, errcs := getCpusStatsMap()
|
|
|
|
if errcs != nil {
|
|
|
|
panic(errcs)
|
|
|
|
}
|
|
|
|
cpusStats.tick(cs)
|
|
|
|
cpuTabElems.Update(*cpusStats)
|
|
|
|
|
|
|
|
ms, errm := getMemStats()
|
|
|
|
if errm != nil {
|
|
|
|
panic(errm)
|
|
|
|
}
|
|
|
|
memTabElems.Update(ms)
|
|
|
|
ui.Render(header, tabpane)
|
2019-01-23 21:12:10 -07:00
|
|
|
renderTab()
|
2018-11-28 19:19:34 -07:00
|
|
|
tickerCount++
|
2015-06-16 14:39:24 -06:00
|
|
|
}
|
2018-11-28 19:19:34 -07:00
|
|
|
}
|
2015-06-16 14:39:24 -06:00
|
|
|
}
|