xmtop/cmd/gotop/main.go

511 lines
13 KiB
Go
Raw Normal View History

2018-02-19 15:25:02 +08:00
package main
import (
"bufio"
"flag"
2018-12-11 13:21:40 +08:00
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
2018-02-19 15:25:02 +08:00
"os"
"os/signal"
"path/filepath"
"sort"
"strings"
2018-02-19 15:25:02 +08:00
"syscall"
"time"
//_ "net/http/pprof"
"github.com/VictoriaMetrics/metrics"
jj "github.com/cloudfoundry-attic/jibber_jabber"
2019-03-08 15:15:41 +08:00
ui "github.com/gizak/termui/v3"
"github.com/jdkeke142/lingo-toml"
"github.com/shibukawa/configdir"
"github.com/xxxserxxx/opflag"
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"
2020-06-19 23:01:26 +08:00
"github.com/xxxserxxx/gotop/v4/translations"
2020-04-24 03:07:08 +08:00
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
defaultUI = "2:cpu\ndisk/1 2:mem/2\ntemp\n2:net 2:procs"
minimalUI = "cpu\nmem procs"
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
BuildDate = "Hadean"
2020-02-14 00:15:52 +08:00
conf gotop.Config
help *w.HelpMenu
bar *w.StatusBar
statusbar bool
stderrLogger = log.New(os.Stderr, "", 0)
tr lingo.Translations
2018-02-19 15:25:02 +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
}
help := opflag.BoolP("help", "h", false, tr.Value("args.help"))
color := opflag.StringP("color", "c", conf.Colorscheme.Name, tr.Value("args.color"))
opflag.IntVarP(&conf.GraphHorizontalScale, "graphscale", "S", conf.GraphHorizontalScale, tr.Value("args.scale"))
version := opflag.BoolP("version", "v", false, tr.Value("args.version"))
versioN := opflag.BoolP("", "V", false, tr.Value("args.version"))
opflag.BoolVarP(&conf.PercpuLoad, "percpu", "p", conf.PercpuLoad, tr.Value("args.percpu"))
opflag.BoolVarP(&conf.AverageLoad, "averagecpu", "a", conf.AverageLoad, tr.Value("args.cpuavg"))
fahrenheit := opflag.BoolP("fahrenheit", "f", conf.TempScale == 'F', tr.Value("args.temp"))
opflag.BoolVarP(&conf.Statusbar, "statusbar", "s", conf.Statusbar, tr.Value("args.statusbar"))
opflag.DurationVarP(&conf.UpdateInterval, "rate", "r", conf.UpdateInterval, tr.Value("args.rate"))
opflag.StringVarP(&conf.Layout, "layout", "l", conf.Layout, tr.Value("args.layout"))
opflag.StringVarP(&conf.NetInterface, "interface", "i", "all", tr.Value("args.net"))
opflag.StringVarP(&conf.ExportPort, "export", "x", conf.ExportPort, tr.Value("args.export"))
opflag.BoolVarP(&conf.Mbps, "mbps", "", conf.Mbps, tr.Value("args.mbps"))
opflag.BoolVar(&conf.Test, "test", conf.Test, tr.Value("args.test"))
opflag.StringP("", "C", "", tr.Value("args.conffile"))
list := opflag.String("list", "", tr.Value("args.list"))
wc := opflag.Bool("write-config", false, tr.Value("args.write"))
opflag.SortFlags = false
opflag.Usage = func() {
fmt.Fprintf(os.Stderr, tr.Value("usage", os.Args[0]))
opflag.PrintDefaults()
}
opflag.Parse()
if *version || *versioN {
fmt.Printf("gotop %s (%s)\n", Version, BuildDate)
os.Exit(0)
}
if *help {
opflag.Usage()
os.Exit(0)
}
cs, err := colorschemes.FromName(conf.ConfigDir, *color)
if err != nil {
return err
2020-04-16 22:36:10 +08:00
}
conf.Colorscheme = cs
if *fahrenheit {
conf.TempScale = 'F'
} else {
conf.TempScale = 'C'
}
if *list != "" {
switch *list {
case "layouts":
fmt.Println(tr.Value("help.layouts"))
case "colorschemes":
fmt.Println(tr.Value("help.colorschemes"))
case "paths":
fmt.Println(tr.Value("help.paths"))
paths := make([]string, 0)
for _, d := range conf.ConfigDir.QueryFolders(configdir.All) {
paths = append(paths, d.Path)
}
fmt.Println(strings.Join(paths, "\n"))
fmt.Println()
fmt.Println(tr.Value("help.log", filepath.Join(conf.ConfigDir.QueryCacheFolder().Path, logging.LOGFILE)))
case "devices":
listDevices()
case "keys":
fmt.Println(tr.Value("help.help"))
2020-04-29 01:41:07 +08:00
case "widgets":
fmt.Println(tr.Value("help.widgets"))
default:
2020-05-05 21:13:25 +08:00
fmt.Printf(tr.Value("error.unknownopt", *list))
os.Exit(1)
}
os.Exit(0)
}
if *wc {
path, err := conf.Write()
if err != nil {
2020-05-05 21:13:25 +08:00
fmt.Println(tr.Value("error.writefail", err.Error()))
os.Exit(1)
}
fmt.Println(tr.Value("help.written", path))
2020-03-08 02:14:19 +08:00
os.Exit(0)
}
return nil
2018-02-19 15:25:02 +08:00
}
2020-02-14 00:15:52 +08:00
func setDefaultTermuiColors(c gotop.Config) {
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) {
drawTicker := time.NewTicker(c.UpdateInterval).C
2018-02-19 15:25:02 +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
uiEvents := ui.PollEvents()
2018-02-21 19:41:40 +08:00
previousKey := ""
2018-02-19 15:25:02 +08:00
for {
select {
case <-sigTerm:
return
case <-drawTicker:
if !c.HelpVisible {
ui.Render(grid)
2019-03-17 22:54:56 +08:00
if statusbar {
ui.Render(bar)
}
}
case e := <-uiEvents:
if grid.Proc != nil && grid.Proc.HandleEvent(e) {
ui.Render(grid.Proc)
break
}
switch e.ID {
case "q", "<C-c>":
return
case "?":
c.HelpVisible = !c.HelpVisible
case "<Resize>":
payload := e.Payload.(ui.Resize)
2019-02-07 09:18:44 +08:00
termWidth, termHeight := payload.Width, payload.Height
2019-02-02 15:22:27 +08:00
if 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)
}
help.Resize(payload.Width, payload.Height)
ui.Clear()
}
if c.HelpVisible {
switch e.ID {
case "?":
2018-02-19 15:25:02 +08:00
ui.Clear()
ui.Render(help)
case "<Escape>":
c.HelpVisible = false
ui.Render(grid)
case "<Resize>":
ui.Render(help)
2018-02-19 15:25:02 +08:00
}
} else {
switch e.ID {
case "?":
ui.Render(grid)
case "h":
c.GraphHorizontalScale += graphHorizontalScaleDelta
for _, item := range grid.Lines {
item.Scale(c.GraphHorizontalScale)
}
ui.Render(grid)
case "l":
if c.GraphHorizontalScale > graphHorizontalScaleDelta {
c.GraphHorizontalScale -= graphHorizontalScaleDelta
for _, item := range grid.Lines {
item.Scale(c.GraphHorizontalScale)
ui.Render(item)
}
}
case "b":
if grid.Net != nil {
grid.Net.Mbps = !grid.Net.Mbps
}
case "<Resize>":
ui.Render(grid)
2019-02-06 18:11:41 +08:00
if statusbar {
ui.Render(bar)
2019-02-06 18:11:41 +08:00
}
case "<MouseLeft>":
if grid.Proc != nil {
payload := e.Payload.(ui.Mouse)
grid.Proc.HandleClick(payload.X, payload.Y)
ui.Render(grid.Proc)
}
case "k", "<Up>", "<MouseWheelUp>":
if grid.Proc != nil {
grid.Proc.ScrollUp()
ui.Render(grid.Proc)
}
case "j", "<Down>", "<MouseWheelDown>":
if grid.Proc != nil {
grid.Proc.ScrollDown()
ui.Render(grid.Proc)
}
2019-02-02 15:22:27 +08:00
case "<Home>":
if grid.Proc != nil {
grid.Proc.ScrollTop()
ui.Render(grid.Proc)
}
2019-02-02 15:06:57 +08:00
case "g":
if grid.Proc != nil {
if previousKey == "g" {
grid.Proc.ScrollTop()
ui.Render(grid.Proc)
}
}
case "G", "<End>":
if grid.Proc != nil {
grid.Proc.ScrollBottom()
ui.Render(grid.Proc)
}
case "<C-d>":
if grid.Proc != nil {
grid.Proc.ScrollHalfPageDown()
ui.Render(grid.Proc)
}
case "<C-u>":
if grid.Proc != nil {
grid.Proc.ScrollHalfPageUp()
ui.Render(grid.Proc)
}
case "<C-f>":
if grid.Proc != nil {
grid.Proc.ScrollPageDown()
ui.Render(grid.Proc)
}
case "<C-b>":
if grid.Proc != nil {
grid.Proc.ScrollPageUp()
ui.Render(grid.Proc)
}
case "d":
if grid.Proc != nil {
if previousKey == "d" {
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")
}
}
case "<Tab>":
if grid.Proc != nil {
grid.Proc.ToggleShowingGroupedProcs()
ui.Render(grid.Proc)
}
case "m", "c", "p":
if grid.Proc != nil {
grid.Proc.ChangeProcSortMethod(w.ProcSortMethod(e.ID))
ui.Render(grid.Proc)
}
case "/":
if grid.Proc != nil {
grid.Proc.SetEditingFilter(true)
ui.Render(grid.Proc)
}
}
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-02-19 15:25:02 +08:00
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
//go func() {
// log.Fatal(http.ListenAndServe(":7777", nil))
//}()
// 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 {
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
}
}
os.Exit(ec)
}
func run() int {
ling, err := lingo.New("en_US", "", translations.AssetFile())
if err != nil {
fmt.Printf("failed to load language files: %s\n", err)
return 2
}
lang, err := jj.DetectIETF()
if err != nil {
lang = "en_US"
}
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)
conf = gotop.NewConfig()
2020-06-21 05:47:48 +08:00
conf.Tr = tr
// Find the config file; look in (1) local, (2) user, (3) global
// Check the last argument first
fs := flag.NewFlagSet("config", flag.ContinueOnError)
cfg := fs.String("C", "", tr.Value("configfile"))
fs.SetOutput(bufio.NewWriter(nil))
fs.Parse(os.Args[1:])
if *cfg != "" {
conf.ConfigFile = *cfg
}
err = conf.Load()
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
}
// Override with command line arguments
err = parseArgs()
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 {
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
2020-04-29 01:04:35 +08:00
errs := devices.Startup(conf.ExtensionVars)
if len(errs) > 0 {
for _, err := range errs {
stderrLogger.Print(err)
}
return 1
}
lstream, err := getLayout(conf)
if err != nil {
2020-03-08 21:50:20 +08:00
stderrLogger.Print(err)
return 1
}
ly := layout.ParseLayout(lstream)
if conf.Test {
return runTests(conf)
}
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
}
defer ui.Close()
2018-12-11 13:21:40 +08:00
setDefaultTermuiColors(conf) // done before initializing widgets to allow inheriting colors
2020-05-05 21:13:25 +08:00
help = w.NewHelpMenu(tr)
if statusbar {
bar = w.NewStatusBar()
}
2019-01-13 08:44:12 +08:00
grid, err := layout.Layout(ly, conf)
if err != nil {
2020-03-08 21:50:20 +08:00
stderrLogger.Print(err)
return 1
}
2019-01-20 11:37:31 +08:00
termWidth, termHeight := ui.TerminalDimensions()
2019-02-02 15:22:27 +08:00
if statusbar {
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)
2019-02-02 15:22:27 +08:00
if statusbar {
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
if conf.ExportPort != "" {
go func() {
http.HandleFunc("/metrics", func(w http.ResponseWriter, req *http.Request) {
metrics.WritePrometheus(w, true)
})
http.ListenAndServe(conf.ExportPort, nil)
}()
}
2020-04-16 22:36:10 +08:00
eventLoop(conf, grid)
return 0
2018-02-19 15:25:02 +08:00
}
func getLayout(conf gotop.Config) (io.Reader, error) {
switch conf.Layout {
case "-":
return os.Stdin, nil
case "default":
return strings.NewReader(defaultUI), nil
case "minimal":
return strings.NewReader(minimalUI), nil
case "battery":
return strings.NewReader(batteryUI), nil
2020-02-29 00:03:41 +08:00
case "procs":
return strings.NewReader(procsUI), nil
2020-03-07 19:01:47 +08:00
case "kitchensink":
return strings.NewReader(kitchensink), nil
default:
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-06-21 05:47:48 +08:00
return nil, fmt.Errorf(tr.Value("error.findlayout", conf.Layout, strings.Join(paths, ", ")))
}
lo, err := folder.ReadFile(conf.Layout)
if err != nil {
return nil, err
}
return strings.NewReader(string(lo)), nil
}
}
2020-02-29 00:03:41 +08:00
func runTests(_ gotop.Config) int {
fmt.Printf("PASS")
return 0
}
func listDevices() {
ms := devices.Domains
sort.Strings(ms)
for _, m := range ms {
fmt.Printf("%s:\n", m)
for _, d := range devices.Devices(m, true) {
fmt.Printf("\t%s\n", d)
}
}
}