2018-02-19 15:25:02 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-04-29 23:25:21 +08:00
|
|
|
"bufio"
|
2022-02-09 06:50:30 +08:00
|
|
|
_ "embed"
|
2020-04-29 23:25:21 +08:00
|
|
|
"flag"
|
2018-12-11 13:21:40 +08:00
|
|
|
"fmt"
|
2020-02-18 01:40:16 +08:00
|
|
|
"io"
|
2021-02-04 04:27:46 +08:00
|
|
|
"io/fs"
|
2020-05-02 09:25:29 +08:00
|
|
|
"io/ioutil"
|
2018-12-05 13:44:25 +08:00
|
|
|
"log"
|
2020-02-18 23:44:29 +08:00
|
|
|
"net/http"
|
2018-02-19 15:25:02 +08:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2018-12-05 13:44:25 +08:00
|
|
|
"path/filepath"
|
2020-04-23 23:41:20 +08:00
|
|
|
"sort"
|
2020-02-13 23:40:20 +08:00
|
|
|
"strings"
|
2018-02-19 15:25:02 +08:00
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2020-04-28 02:53:45 +08:00
|
|
|
//_ "net/http/pprof"
|
|
|
|
|
2020-05-13 07:42:45 +08:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
2020-05-02 09:25:29 +08:00
|
|
|
jj "github.com/cloudfoundry-attic/jibber_jabber"
|
2022-02-09 06:50:30 +08:00
|
|
|
"github.com/droundy/goopt"
|
2019-03-08 15:15:41 +08:00
|
|
|
ui "github.com/gizak/termui/v3"
|
2020-03-07 23:03:25 +08:00
|
|
|
"github.com/shibukawa/configdir"
|
2021-02-04 04:27:46 +08:00
|
|
|
"github.com/xxxserxxx/lingo/v2"
|
2019-02-07 09:18:44 +08:00
|
|
|
|
2020-04-24 03:07:08 +08:00
|
|
|
"github.com/xxxserxxx/gotop/v4"
|
|
|
|
"github.com/xxxserxxx/gotop/v4/colorschemes"
|
|
|
|
"github.com/xxxserxxx/gotop/v4/devices"
|
|
|
|
"github.com/xxxserxxx/gotop/v4/layout"
|
|
|
|
"github.com/xxxserxxx/gotop/v4/logging"
|
|
|
|
w "github.com/xxxserxxx/gotop/v4/widgets"
|
2018-02-19 15:25:02 +08:00
|
|
|
)
|
|
|
|
|
2019-01-13 08:31:37 +08:00
|
|
|
const (
|
|
|
|
graphHorizontalScaleDelta = 3
|
2020-03-04 04:16:28 +08:00
|
|
|
defaultUI = "2:cpu\ndisk/1 2:mem/2\ntemp\n2:net 2:procs"
|
2020-02-14 03:24:41 +08:00
|
|
|
minimalUI = "cpu\nmem procs"
|
2020-02-16 06:10:45 +08:00
|
|
|
batteryUI = "cpu/2 batt/1\ndisk/1 2:mem/2\ntemp\nnet procs"
|
2020-02-29 00:03:41 +08:00
|
|
|
procsUI = "cpu 4:procs\ndisk\nmem\nnet"
|
2020-03-07 19:01:47 +08:00
|
|
|
kitchensink = "3:cpu/2 3:mem/1\n4:temp/1 3:disk/2\npower\n3:net 3:procs"
|
2019-01-13 08:31:37 +08:00
|
|
|
)
|
2018-02-19 15:25:02 +08:00
|
|
|
|
|
|
|
var (
|
2020-04-28 09:33:41 +08:00
|
|
|
// Version of the program; set during build from git tags
|
|
|
|
Version = "0.0.0"
|
|
|
|
// BuildDate when the program was compiled; set during build
|
2020-04-23 23:41:20 +08:00
|
|
|
BuildDate = "Hadean"
|
2020-02-14 00:15:52 +08:00
|
|
|
conf gotop.Config
|
2020-02-13 10:15:49 +08:00
|
|
|
help *w.HelpMenu
|
|
|
|
bar *w.StatusBar
|
2018-12-05 13:44:25 +08:00
|
|
|
stderrLogger = log.New(os.Stderr, "", 0)
|
2020-05-02 09:25:29 +08:00
|
|
|
tr lingo.Translations
|
2018-02-19 15:25:02 +08:00
|
|
|
)
|
|
|
|
|
2022-02-09 06:50:30 +08:00
|
|
|
//go:embed "description.txt"
|
|
|
|
var description string
|
|
|
|
|
2020-04-29 23:25:21 +08:00
|
|
|
func parseArgs() error {
|
2020-04-07 00:04:08 +08:00
|
|
|
cds := conf.ConfigDir.QueryFolders(configdir.All)
|
|
|
|
cpaths := make([]string, len(cds))
|
|
|
|
for i, p := range cds {
|
|
|
|
cpaths[i] = p.Path
|
|
|
|
}
|
2022-02-09 06:50:30 +08:00
|
|
|
goopt.Summary = "A terminal based graphical activity monitor, inspired by gtop and vtop"
|
|
|
|
goopt.Version = Version
|
|
|
|
goopt.Description = func() string {
|
|
|
|
return description
|
2020-04-28 02:53:45 +08:00
|
|
|
}
|
2022-02-09 06:50:30 +08:00
|
|
|
color := goopt.String([]string{"--color", "-c"}, conf.Colorscheme.Name, tr.Value("args.color"))
|
|
|
|
graphhorizontalscale := goopt.Int([]string{"--graphscale", "-S"}, conf.GraphHorizontalScale, tr.Value("args.scale"))
|
|
|
|
version := goopt.Flag([]string{"-v", "-V", "--version"}, []string{}, tr.Value("args.version"), "")
|
|
|
|
percpuload := goopt.Flag([]string{"--percpu", "-p"}, []string{"--no-percpu"}, tr.Value("args.percpu"), tr.Value("args.no-percpu"))
|
|
|
|
averageload := goopt.Flag([]string{"--averagecpu", "-a"}, []string{"--no-averagecpu"}, tr.Value("args.cpuavg"), tr.Value("args.no-cpuavg"))
|
|
|
|
tempScale := goopt.Flag([]string{"--fahrenheit"}, []string{"--celsius"}, tr.Value("args.temp"), tr.Value("args.tempc"))
|
|
|
|
statusbar := goopt.Flag([]string{"--statusbar", "-s"}, []string{"--no-statusbar"}, tr.Value("args.statusbar"), tr.Value("args.no-statusbar"))
|
|
|
|
updateinterval := goopt.String([]string{"--rate", "-r"}, conf.UpdateInterval.String(), tr.Value("args.rate"))
|
|
|
|
layout := goopt.String([]string{"--layout", "-l"}, conf.Layout, tr.Value("args.layout"))
|
|
|
|
netinterface := goopt.String([]string{"--interface", "-i"}, "all", tr.Value("args.net"))
|
|
|
|
exportport := goopt.String([]string{"--export", "-x"}, conf.ExportPort, tr.Value("args.export"))
|
|
|
|
mbps := goopt.Flag([]string{"--mbps"}, []string{"--bytes"}, tr.Value("args.mbps"), tr.Value("args.no-mbps"))
|
|
|
|
test := goopt.Flag([]string{"--test"}, []string{"--no-test"}, tr.Value("args.test"), tr.Value("args.no-test"))
|
|
|
|
// This is so the flag package doesn't barf on an unrecognized flag; it's processed earlier
|
|
|
|
goopt.String([]string{"-C"}, "", tr.Value("args.conffile"))
|
|
|
|
nvidia := goopt.Flag([]string{"--nvidia"}, []string{"--no-nvidia"}, tr.Value("args.nvidia"), tr.Value("args.no-nvidia"))
|
|
|
|
list := goopt.String([]string{"--list"}, "", tr.Value("args.list"))
|
|
|
|
wc := goopt.Flag([]string{"--write-config"}, []string{}, tr.Value("args.write"), "")
|
|
|
|
goopt.Parse(nil)
|
|
|
|
|
|
|
|
conf.PercpuLoad = *percpuload
|
|
|
|
conf.GraphHorizontalScale = *graphhorizontalscale
|
|
|
|
conf.PercpuLoad = *percpuload
|
|
|
|
conf.AverageLoad = *averageload
|
|
|
|
conf.Statusbar = *statusbar
|
|
|
|
conf.Layout = *layout
|
|
|
|
conf.NetInterface = *netinterface
|
|
|
|
conf.ExportPort = *exportport
|
|
|
|
conf.Mbps = *mbps
|
|
|
|
conf.Nvidia = *nvidia
|
|
|
|
conf.AverageLoad = *averageload
|
|
|
|
conf.Test = *test
|
|
|
|
conf.Statusbar = *statusbar
|
|
|
|
conf.Mbps = *mbps
|
|
|
|
conf.Nvidia = *nvidia
|
|
|
|
if upInt, err := time.ParseDuration(*updateinterval); err == nil {
|
|
|
|
conf.UpdateInterval = upInt
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Update interval must be a time interval such as '10s' or '1m'")
|
|
|
|
os.Exit(1)
|
2018-09-19 04:42:49 +08:00
|
|
|
}
|
2022-02-09 06:50:30 +08:00
|
|
|
if *tempScale {
|
|
|
|
conf.TempScale = 'F'
|
|
|
|
} else {
|
|
|
|
conf.TempScale = 'C'
|
|
|
|
}
|
|
|
|
|
|
|
|
if *version {
|
|
|
|
fmt.Printf("gotop %s (%s)\n", Version, BuildDate)
|
2020-04-28 02:53:45 +08:00
|
|
|
os.Exit(0)
|
2020-03-02 23:09:17 +08:00
|
|
|
}
|
2022-02-09 01:50:52 +08:00
|
|
|
if *color != "" {
|
|
|
|
cs, err := colorschemes.FromName(conf.ConfigDir, *color)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
conf.Colorscheme = cs
|
2020-04-16 22:36:10 +08:00
|
|
|
}
|
2020-04-28 02:53:45 +08:00
|
|
|
if *list != "" {
|
|
|
|
switch *list {
|
2020-04-23 23:41:20 +08:00
|
|
|
case "layouts":
|
2020-06-20 21:33:01 +08:00
|
|
|
fmt.Println(tr.Value("help.layouts"))
|
2020-04-23 23:41:20 +08:00
|
|
|
case "colorschemes":
|
2020-06-20 21:33:01 +08:00
|
|
|
fmt.Println(tr.Value("help.colorschemes"))
|
2020-04-23 23:41:20 +08:00
|
|
|
case "paths":
|
2020-05-02 09:25:29 +08:00
|
|
|
fmt.Println(tr.Value("help.paths"))
|
2020-04-23 23:41:20 +08:00
|
|
|
paths := make([]string, 0)
|
|
|
|
for _, d := range conf.ConfigDir.QueryFolders(configdir.All) {
|
|
|
|
paths = append(paths, d.Path)
|
|
|
|
}
|
|
|
|
fmt.Println(strings.Join(paths, "\n"))
|
2020-05-02 09:25:29 +08:00
|
|
|
fmt.Println()
|
|
|
|
fmt.Println(tr.Value("help.log", filepath.Join(conf.ConfigDir.QueryCacheFolder().Path, logging.LOGFILE)))
|
2020-04-23 23:41:20 +08:00
|
|
|
case "devices":
|
|
|
|
listDevices()
|
|
|
|
case "keys":
|
2020-06-20 21:33:01 +08:00
|
|
|
fmt.Println(tr.Value("help.help"))
|
2020-04-29 01:41:07 +08:00
|
|
|
case "widgets":
|
2020-06-20 21:33:01 +08:00
|
|
|
fmt.Println(tr.Value("help.widgets"))
|
2021-01-27 01:56:01 +08:00
|
|
|
case "langs":
|
2021-02-04 04:27:46 +08:00
|
|
|
err := fs.WalkDir(gotop.Dicts, ".", func(pth string, info fs.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if info.IsDir() { // We skip these
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
fileName := info.Name()
|
2021-07-18 02:30:40 +08:00
|
|
|
if strings.HasSuffix(fileName, ".toml") {
|
2021-02-04 04:27:46 +08:00
|
|
|
fmt.Println(strings.TrimSuffix(fileName, ".toml"))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2021-01-27 01:56:01 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-23 23:41:20 +08:00
|
|
|
default:
|
2020-05-05 21:13:25 +08:00
|
|
|
fmt.Printf(tr.Value("error.unknownopt", *list))
|
2020-04-23 23:41:20 +08:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
2021-03-05 06:23:04 +08:00
|
|
|
if conf.Nvidia {
|
|
|
|
conf.ExtensionVars["nvidia"] = "true"
|
|
|
|
}
|
2020-04-28 02:53:45 +08:00
|
|
|
if *wc {
|
2020-04-23 23:41:20 +08:00
|
|
|
path, err := conf.Write()
|
|
|
|
if err != nil {
|
2020-05-05 21:13:25 +08:00
|
|
|
fmt.Println(tr.Value("error.writefail", err.Error()))
|
2020-04-23 23:41:20 +08:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-05-02 09:25:29 +08:00
|
|
|
fmt.Println(tr.Value("help.written", path))
|
2020-03-08 02:14:19 +08:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
2022-02-04 22:46:58 +08:00
|
|
|
|
2020-02-18 01:40:16 +08:00
|
|
|
return nil
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
|
2020-02-14 00:15:52 +08:00
|
|
|
func setDefaultTermuiColors(c gotop.Config) {
|
2020-02-13 10:15:49 +08:00
|
|
|
ui.Theme.Default = ui.NewStyle(ui.Color(c.Colorscheme.Fg), ui.Color(c.Colorscheme.Bg))
|
|
|
|
ui.Theme.Block.Title = ui.NewStyle(ui.Color(c.Colorscheme.BorderLabel), ui.Color(c.Colorscheme.Bg))
|
|
|
|
ui.Theme.Block.Border = ui.NewStyle(ui.Color(c.Colorscheme.BorderLine), ui.Color(c.Colorscheme.Bg))
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
|
2020-02-14 00:15:52 +08:00
|
|
|
func eventLoop(c gotop.Config, grid *layout.MyGrid) {
|
2020-02-13 10:15:49 +08:00
|
|
|
drawTicker := time.NewTicker(c.UpdateInterval).C
|
2018-02-19 15:25:02 +08:00
|
|
|
|
2018-11-30 10:17:13 +08:00
|
|
|
// handles kill signal sent to gotop
|
|
|
|
sigTerm := make(chan os.Signal, 2)
|
|
|
|
signal.Notify(sigTerm, os.Interrupt, syscall.SIGTERM)
|
2018-02-19 15:25:02 +08:00
|
|
|
|
2018-11-30 10:17:13 +08:00
|
|
|
uiEvents := ui.PollEvents()
|
2018-02-21 19:41:40 +08:00
|
|
|
|
2018-11-30 10:17:13 +08:00
|
|
|
previousKey := ""
|
2018-02-19 15:25:02 +08:00
|
|
|
|
2018-11-30 10:17:13 +08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-sigTerm:
|
|
|
|
return
|
|
|
|
case <-drawTicker:
|
2020-02-13 10:15:49 +08:00
|
|
|
if !c.HelpVisible {
|
2019-03-08 15:43:10 +08:00
|
|
|
ui.Render(grid)
|
2021-01-27 03:09:10 +08:00
|
|
|
if c.Statusbar {
|
2019-03-17 22:54:56 +08:00
|
|
|
ui.Render(bar)
|
|
|
|
}
|
2018-11-30 10:17:13 +08:00
|
|
|
}
|
|
|
|
case e := <-uiEvents:
|
2020-02-14 23:35:58 +08:00
|
|
|
if grid.Proc != nil && grid.Proc.HandleEvent(e) {
|
|
|
|
ui.Render(grid.Proc)
|
|
|
|
break
|
|
|
|
}
|
2018-11-30 10:17:13 +08:00
|
|
|
switch e.ID {
|
|
|
|
case "q", "<C-c>":
|
|
|
|
return
|
|
|
|
case "?":
|
2020-02-13 10:15:49 +08:00
|
|
|
c.HelpVisible = !c.HelpVisible
|
2019-01-08 12:10:09 +08:00
|
|
|
case "<Resize>":
|
|
|
|
payload := e.Payload.(ui.Resize)
|
2019-02-07 09:18:44 +08:00
|
|
|
termWidth, termHeight := payload.Width, payload.Height
|
2021-01-27 03:09:10 +08:00
|
|
|
if c.Statusbar {
|
2019-02-07 09:18:44 +08:00
|
|
|
grid.SetRect(0, 0, termWidth, termHeight-1)
|
|
|
|
bar.SetRect(0, termHeight-1, termWidth, termHeight)
|
2019-02-02 15:22:27 +08:00
|
|
|
} else {
|
|
|
|
grid.SetRect(0, 0, payload.Width, payload.Height)
|
|
|
|
}
|
2019-01-08 12:10:09 +08:00
|
|
|
help.Resize(payload.Width, payload.Height)
|
|
|
|
ui.Clear()
|
|
|
|
}
|
|
|
|
|
2020-02-13 10:15:49 +08:00
|
|
|
if c.HelpVisible {
|
2019-01-08 12:10:09 +08:00
|
|
|
switch e.ID {
|
|
|
|
case "?":
|
2018-02-19 15:25:02 +08:00
|
|
|
ui.Clear()
|
|
|
|
ui.Render(help)
|
2019-01-08 12:10:09 +08:00
|
|
|
case "<Escape>":
|
2020-02-13 10:15:49 +08:00
|
|
|
c.HelpVisible = false
|
2019-03-08 15:43:10 +08:00
|
|
|
ui.Render(grid)
|
2019-01-08 12:10:09 +08:00
|
|
|
case "<Resize>":
|
|
|
|
ui.Render(help)
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
2019-01-08 12:10:09 +08:00
|
|
|
} else {
|
|
|
|
switch e.ID {
|
|
|
|
case "?":
|
2019-03-08 15:43:10 +08:00
|
|
|
ui.Render(grid)
|
2019-01-08 12:10:09 +08:00
|
|
|
case "h":
|
2020-02-13 10:15:49 +08:00
|
|
|
c.GraphHorizontalScale += graphHorizontalScaleDelta
|
|
|
|
for _, item := range grid.Lines {
|
|
|
|
item.Scale(c.GraphHorizontalScale)
|
|
|
|
}
|
|
|
|
ui.Render(grid)
|
2019-01-08 12:10:09 +08:00
|
|
|
case "l":
|
2020-02-13 10:15:49 +08:00
|
|
|
if c.GraphHorizontalScale > graphHorizontalScaleDelta {
|
|
|
|
c.GraphHorizontalScale -= graphHorizontalScaleDelta
|
|
|
|
for _, item := range grid.Lines {
|
|
|
|
item.Scale(c.GraphHorizontalScale)
|
|
|
|
ui.Render(item)
|
|
|
|
}
|
2018-11-30 10:17:13 +08:00
|
|
|
}
|
2020-04-17 02:28:18 +08:00
|
|
|
case "b":
|
|
|
|
if grid.Net != nil {
|
|
|
|
grid.Net.Mbps = !grid.Net.Mbps
|
|
|
|
}
|
2019-01-08 12:10:09 +08:00
|
|
|
case "<Resize>":
|
2019-03-08 15:43:10 +08:00
|
|
|
ui.Render(grid)
|
2021-01-27 03:09:10 +08:00
|
|
|
if c.Statusbar {
|
2019-03-08 15:43:10 +08:00
|
|
|
ui.Render(bar)
|
2019-02-06 18:11:41 +08:00
|
|
|
}
|
2019-01-08 12:10:09 +08:00
|
|
|
case "<MouseLeft>":
|
2020-02-13 10:15:49 +08:00
|
|
|
if grid.Proc != nil {
|
|
|
|
payload := e.Payload.(ui.Mouse)
|
|
|
|
grid.Proc.HandleClick(payload.X, payload.Y)
|
|
|
|
ui.Render(grid.Proc)
|
|
|
|
}
|
2019-01-08 12:10:09 +08:00
|
|
|
case "k", "<Up>", "<MouseWheelUp>":
|
2020-02-13 10:15:49 +08:00
|
|
|
if grid.Proc != nil {
|
|
|
|
grid.Proc.ScrollUp()
|
|
|
|
ui.Render(grid.Proc)
|
|
|
|
}
|
2019-01-08 12:10:09 +08:00
|
|
|
case "j", "<Down>", "<MouseWheelDown>":
|
2020-02-13 10:15:49 +08:00
|
|
|
if grid.Proc != nil {
|
|
|
|
grid.Proc.ScrollDown()
|
|
|
|
ui.Render(grid.Proc)
|
|
|
|
}
|
2019-02-02 15:22:27 +08:00
|
|
|
case "<Home>":
|
2020-02-13 10:15:49 +08:00
|
|
|
if grid.Proc != nil {
|
|
|
|
grid.Proc.ScrollTop()
|
|
|
|
ui.Render(grid.Proc)
|
|
|
|
}
|
2019-02-02 15:06:57 +08:00
|
|
|
case "g":
|
2020-02-13 10:15:49 +08:00
|
|
|
if grid.Proc != nil {
|
|
|
|
if previousKey == "g" {
|
|
|
|
grid.Proc.ScrollTop()
|
|
|
|
ui.Render(grid.Proc)
|
|
|
|
}
|
2019-01-08 12:10:09 +08:00
|
|
|
}
|
|
|
|
case "G", "<End>":
|
2020-02-13 10:15:49 +08:00
|
|
|
if grid.Proc != nil {
|
|
|
|
grid.Proc.ScrollBottom()
|
|
|
|
ui.Render(grid.Proc)
|
|
|
|
}
|
2019-01-08 12:10:09 +08:00
|
|
|
case "<C-d>":
|
2020-02-13 10:15:49 +08:00
|
|
|
if grid.Proc != nil {
|
|
|
|
grid.Proc.ScrollHalfPageDown()
|
|
|
|
ui.Render(grid.Proc)
|
|
|
|
}
|
2019-01-08 12:10:09 +08:00
|
|
|
case "<C-u>":
|
2020-02-13 10:15:49 +08:00
|
|
|
if grid.Proc != nil {
|
|
|
|
grid.Proc.ScrollHalfPageUp()
|
|
|
|
ui.Render(grid.Proc)
|
|
|
|
}
|
2022-07-23 03:33:36 +08:00
|
|
|
case "<C-f>", "<PageDown>":
|
2020-02-13 10:15:49 +08:00
|
|
|
if grid.Proc != nil {
|
|
|
|
grid.Proc.ScrollPageDown()
|
|
|
|
ui.Render(grid.Proc)
|
|
|
|
}
|
2022-07-23 03:33:36 +08:00
|
|
|
case "<C-b>", "<PageUp>":
|
2020-02-13 10:15:49 +08:00
|
|
|
if grid.Proc != nil {
|
|
|
|
grid.Proc.ScrollPageUp()
|
|
|
|
ui.Render(grid.Proc)
|
|
|
|
}
|
2019-01-08 12:10:09 +08:00
|
|
|
case "d":
|
2020-02-13 10:15:49 +08:00
|
|
|
if grid.Proc != nil {
|
|
|
|
if previousKey == "d" {
|
2020-02-14 03:38:45 +08:00
|
|
|
grid.Proc.KillProc("SIGTERM")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "3":
|
|
|
|
if grid.Proc != nil {
|
|
|
|
if previousKey == "d" {
|
|
|
|
grid.Proc.KillProc("SIGQUIT")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "9":
|
|
|
|
if grid.Proc != nil {
|
|
|
|
if previousKey == "d" {
|
|
|
|
grid.Proc.KillProc("SIGKILL")
|
2020-02-13 10:15:49 +08:00
|
|
|
}
|
2019-01-08 12:10:09 +08:00
|
|
|
}
|
|
|
|
case "<Tab>":
|
2020-02-13 10:15:49 +08:00
|
|
|
if grid.Proc != nil {
|
|
|
|
grid.Proc.ToggleShowingGroupedProcs()
|
|
|
|
ui.Render(grid.Proc)
|
|
|
|
}
|
2022-02-20 11:02:36 +08:00
|
|
|
case "m", "c", "n", "p":
|
2020-02-13 10:15:49 +08:00
|
|
|
if grid.Proc != nil {
|
|
|
|
grid.Proc.ChangeProcSortMethod(w.ProcSortMethod(e.ID))
|
|
|
|
ui.Render(grid.Proc)
|
|
|
|
}
|
2020-02-14 23:35:58 +08:00
|
|
|
case "/":
|
|
|
|
if grid.Proc != nil {
|
|
|
|
grid.Proc.SetEditingFilter(true)
|
|
|
|
ui.Render(grid.Proc)
|
|
|
|
}
|
2018-11-30 10:17:13 +08:00
|
|
|
}
|
2019-01-08 12:10:09 +08:00
|
|
|
|
|
|
|
if previousKey == e.ID {
|
|
|
|
previousKey = ""
|
|
|
|
} else {
|
|
|
|
previousKey = e.ID
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
}
|
2018-12-05 13:07:14 +08:00
|
|
|
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
2018-11-30 10:17:13 +08:00
|
|
|
}
|
|
|
|
}
|
2018-02-19 15:25:02 +08:00
|
|
|
|
2021-07-18 03:34:56 +08:00
|
|
|
// FIXME CPU use regression
|
2021-09-07 23:50:55 +08:00
|
|
|
// TODO add CPU freq
|
2018-12-10 13:19:09 +08:00
|
|
|
func main() {
|
2020-05-02 01:41:18 +08:00
|
|
|
// TODO: Make this an option, for performance testing
|
2020-04-28 02:53:45 +08:00
|
|
|
//go func() {
|
|
|
|
// log.Fatal(http.ListenAndServe(":7777", nil))
|
|
|
|
//}()
|
|
|
|
|
2020-03-08 21:30:06 +08:00
|
|
|
// This is just to make sure gotop returns a useful exit code, but also
|
|
|
|
// executes all defer statements and so cleans up before exit. Sort of
|
|
|
|
// annoying work-around for a lack of a clean way to exit Go programs
|
|
|
|
// with exit codes.
|
|
|
|
ec := run()
|
2020-03-08 21:50:20 +08:00
|
|
|
if ec > 0 {
|
|
|
|
if ec < 2 {
|
2020-05-02 09:25:29 +08:00
|
|
|
logpath := filepath.Join(conf.ConfigDir.QueryCacheFolder().Path, logging.LOGFILE)
|
2020-05-05 21:13:25 +08:00
|
|
|
fmt.Println(tr.Value("error.checklog", logpath))
|
2020-06-19 08:51:01 +08:00
|
|
|
bs, _ := ioutil.ReadFile(logpath)
|
|
|
|
fmt.Println(string(bs))
|
2020-03-08 21:50:20 +08:00
|
|
|
}
|
|
|
|
}
|
2020-03-08 21:30:06 +08:00
|
|
|
os.Exit(ec)
|
|
|
|
}
|
|
|
|
|
|
|
|
func run() int {
|
2021-02-04 04:27:46 +08:00
|
|
|
ling, err := lingo.New("en_US", ".", gotop.Dicts)
|
2020-05-02 09:25:29 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("failed to load language files: %s\n", err)
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
lang, err := jj.DetectIETF()
|
|
|
|
if err != nil {
|
2020-06-21 23:14:18 +08:00
|
|
|
lang = "en_US"
|
2020-05-02 09:25:29 +08:00
|
|
|
}
|
|
|
|
lang = strings.Replace(lang, "-", "_", -1)
|
|
|
|
// Get the locale from the os
|
|
|
|
tr = ling.TranslationsForLocale(lang)
|
2020-06-21 05:47:48 +08:00
|
|
|
colorschemes.SetTr(tr)
|
2020-04-29 23:25:21 +08:00
|
|
|
conf = gotop.NewConfig()
|
2020-06-21 05:47:48 +08:00
|
|
|
conf.Tr = tr
|
2020-03-07 23:03:25 +08:00
|
|
|
// Find the config file; look in (1) local, (2) user, (3) global
|
2020-04-29 23:25:21 +08:00
|
|
|
// Check the last argument first
|
|
|
|
fs := flag.NewFlagSet("config", flag.ContinueOnError)
|
2020-05-02 09:25:29 +08:00
|
|
|
cfg := fs.String("C", "", tr.Value("configfile"))
|
2020-04-29 23:25:21 +08:00
|
|
|
fs.SetOutput(bufio.NewWriter(nil))
|
|
|
|
fs.Parse(os.Args[1:])
|
|
|
|
if *cfg != "" {
|
|
|
|
conf.ConfigFile = *cfg
|
|
|
|
}
|
2020-05-02 09:25:29 +08:00
|
|
|
err = conf.Load()
|
2020-03-07 23:03:25 +08:00
|
|
|
if err != nil {
|
2020-05-05 21:13:25 +08:00
|
|
|
fmt.Println(tr.Value("error.configparse", err.Error()))
|
2020-03-08 21:50:20 +08:00
|
|
|
return 2
|
2020-02-18 01:40:16 +08:00
|
|
|
}
|
|
|
|
// Override with command line arguments
|
2020-04-29 23:25:21 +08:00
|
|
|
err = parseArgs()
|
2020-02-13 10:15:49 +08:00
|
|
|
if err != nil {
|
2020-05-05 21:13:25 +08:00
|
|
|
fmt.Println(tr.Value("error.cliparse", err.Error()))
|
2020-03-08 21:50:20 +08:00
|
|
|
return 2
|
2018-12-11 13:21:40 +08:00
|
|
|
}
|
|
|
|
|
2020-02-16 04:27:31 +08:00
|
|
|
logfile, err := logging.New(conf)
|
2019-01-20 11:37:31 +08:00
|
|
|
if err != nil {
|
2020-05-02 09:25:29 +08:00
|
|
|
fmt.Println(tr.Value("logsetup", err.Error()))
|
2020-03-08 21:50:20 +08:00
|
|
|
return 2
|
2019-01-20 11:37:31 +08:00
|
|
|
}
|
2019-02-07 09:18:44 +08:00
|
|
|
defer logfile.Close()
|
2019-01-20 11:37:31 +08:00
|
|
|
|
2021-03-02 17:06:39 +08:00
|
|
|
// device initialization errors do not stop execution
|
|
|
|
for _, err := range devices.Startup(conf.ExtensionVars) {
|
|
|
|
stderrLogger.Print(err)
|
2020-04-29 01:04:35 +08:00
|
|
|
}
|
|
|
|
|
2020-03-07 23:03:25 +08:00
|
|
|
lstream, err := getLayout(conf)
|
|
|
|
if err != nil {
|
2020-03-08 21:50:20 +08:00
|
|
|
stderrLogger.Print(err)
|
2020-03-08 21:30:06 +08:00
|
|
|
return 1
|
2020-03-07 23:03:25 +08:00
|
|
|
}
|
2020-03-02 23:09:17 +08:00
|
|
|
ly := layout.ParseLayout(lstream)
|
|
|
|
|
|
|
|
if conf.Test {
|
2020-03-08 21:30:06 +08:00
|
|
|
return runTests(conf)
|
2020-03-02 23:09:17 +08:00
|
|
|
}
|
|
|
|
|
2020-04-28 09:33:41 +08:00
|
|
|
if err = ui.Init(); err != nil {
|
2020-03-08 21:50:20 +08:00
|
|
|
stderrLogger.Print(err)
|
|
|
|
return 1
|
2018-11-30 10:17:13 +08:00
|
|
|
}
|
|
|
|
defer ui.Close()
|
2018-12-11 13:21:40 +08:00
|
|
|
|
2020-02-13 10:15:49 +08:00
|
|
|
setDefaultTermuiColors(conf) // done before initializing widgets to allow inheriting colors
|
2020-05-05 21:13:25 +08:00
|
|
|
help = w.NewHelpMenu(tr)
|
2021-01-27 03:09:10 +08:00
|
|
|
if conf.Statusbar {
|
2020-02-13 10:15:49 +08:00
|
|
|
bar = w.NewStatusBar()
|
|
|
|
}
|
2019-01-13 08:44:12 +08:00
|
|
|
|
2020-02-13 10:15:49 +08:00
|
|
|
grid, err := layout.Layout(ly, conf)
|
|
|
|
if err != nil {
|
2020-03-08 21:50:20 +08:00
|
|
|
stderrLogger.Print(err)
|
|
|
|
return 1
|
2020-02-13 10:15:49 +08:00
|
|
|
}
|
2019-01-20 11:37:31 +08:00
|
|
|
|
2019-01-24 13:23:35 +08:00
|
|
|
termWidth, termHeight := ui.TerminalDimensions()
|
2021-01-27 03:09:10 +08:00
|
|
|
if conf.Statusbar {
|
2019-02-02 15:22:27 +08:00
|
|
|
grid.SetRect(0, 0, termWidth, termHeight-1)
|
|
|
|
} else {
|
|
|
|
grid.SetRect(0, 0, termWidth, termHeight)
|
|
|
|
}
|
2019-01-01 08:55:50 +08:00
|
|
|
help.Resize(termWidth, termHeight)
|
|
|
|
|
|
|
|
ui.Render(grid)
|
2021-01-27 03:09:10 +08:00
|
|
|
if conf.Statusbar {
|
2019-02-02 15:22:27 +08:00
|
|
|
bar.SetRect(0, termHeight-1, termWidth, termHeight)
|
|
|
|
ui.Render(bar)
|
|
|
|
}
|
2018-12-11 13:21:40 +08:00
|
|
|
|
2020-06-03 21:34:31 +08:00
|
|
|
// TODO https://godoc.org/github.com/VictoriaMetrics/metrics#Set
|
2020-02-18 23:44:29 +08:00
|
|
|
if conf.ExportPort != "" {
|
|
|
|
go func() {
|
2020-05-13 07:42:45 +08:00
|
|
|
http.HandleFunc("/metrics", func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
metrics.WritePrometheus(w, true)
|
|
|
|
})
|
2020-02-18 23:44:29 +08:00
|
|
|
http.ListenAndServe(conf.ExportPort, nil)
|
|
|
|
}()
|
|
|
|
}
|
2020-04-16 22:36:10 +08:00
|
|
|
|
2020-02-13 10:15:49 +08:00
|
|
|
eventLoop(conf, grid)
|
2020-03-08 21:30:06 +08:00
|
|
|
return 0
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
2020-02-18 01:40:16 +08:00
|
|
|
|
2020-03-07 23:03:25 +08:00
|
|
|
func getLayout(conf gotop.Config) (io.Reader, error) {
|
2020-02-18 01:40:16 +08:00
|
|
|
switch conf.Layout {
|
|
|
|
case "-":
|
2020-03-07 23:03:25 +08:00
|
|
|
return os.Stdin, nil
|
2020-02-18 01:40:16 +08:00
|
|
|
case "default":
|
2020-03-07 23:03:25 +08:00
|
|
|
return strings.NewReader(defaultUI), nil
|
2020-02-18 01:40:16 +08:00
|
|
|
case "minimal":
|
2020-03-07 23:03:25 +08:00
|
|
|
return strings.NewReader(minimalUI), nil
|
2020-02-18 01:40:16 +08:00
|
|
|
case "battery":
|
2020-03-07 23:03:25 +08:00
|
|
|
return strings.NewReader(batteryUI), nil
|
2020-02-29 00:03:41 +08:00
|
|
|
case "procs":
|
2020-03-07 23:03:25 +08:00
|
|
|
return strings.NewReader(procsUI), nil
|
2020-03-07 19:01:47 +08:00
|
|
|
case "kitchensink":
|
2020-03-07 23:03:25 +08:00
|
|
|
return strings.NewReader(kitchensink), nil
|
2020-02-18 01:40:16 +08:00
|
|
|
default:
|
2020-03-07 23:03:25 +08:00
|
|
|
folder := conf.ConfigDir.QueryFolderContainsFile(conf.Layout)
|
|
|
|
if folder == nil {
|
|
|
|
paths := make([]string, 0)
|
|
|
|
for _, d := range conf.ConfigDir.QueryFolders(configdir.Existing) {
|
|
|
|
paths = append(paths, d.Path)
|
2020-02-18 01:40:16 +08:00
|
|
|
}
|
2020-06-21 05:47:48 +08:00
|
|
|
return nil, fmt.Errorf(tr.Value("error.findlayout", conf.Layout, strings.Join(paths, ", ")))
|
2020-03-07 23:03:25 +08:00
|
|
|
}
|
|
|
|
lo, err := folder.ReadFile(conf.Layout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-02-18 01:40:16 +08:00
|
|
|
}
|
2020-03-07 23:03:25 +08:00
|
|
|
return strings.NewReader(string(lo)), nil
|
2020-02-18 01:40:16 +08:00
|
|
|
}
|
|
|
|
}
|
2020-02-29 00:03:41 +08:00
|
|
|
|
2020-06-08 05:40:03 +08:00
|
|
|
func runTests(_ gotop.Config) int {
|
2020-03-02 23:09:17 +08:00
|
|
|
fmt.Printf("PASS")
|
|
|
|
return 0
|
|
|
|
}
|
2020-04-23 23:41:20 +08:00
|
|
|
|
|
|
|
func listDevices() {
|
|
|
|
ms := devices.Domains
|
|
|
|
sort.Strings(ms)
|
|
|
|
for _, m := range ms {
|
|
|
|
fmt.Printf("%s:\n", m)
|
2020-04-24 02:01:13 +08:00
|
|
|
for _, d := range devices.Devices(m, true) {
|
2020-04-23 23:41:20 +08:00
|
|
|
fmt.Printf("\t%s\n", d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|