2020-02-14 00:15:52 +08:00
|
|
|
package gotop
|
2020-02-13 10:15:49 +08:00
|
|
|
|
|
|
|
import (
|
2020-02-18 01:40:16 +08:00
|
|
|
"bufio"
|
2020-04-23 23:41:20 +08:00
|
|
|
"bytes"
|
2020-02-18 01:40:16 +08:00
|
|
|
"fmt"
|
2020-04-24 02:38:22 +08:00
|
|
|
"io"
|
2020-04-29 23:25:21 +08:00
|
|
|
"io/ioutil"
|
2020-03-07 23:03:25 +08:00
|
|
|
"log"
|
2020-04-29 23:25:21 +08:00
|
|
|
"os"
|
2020-04-23 23:41:20 +08:00
|
|
|
"path/filepath"
|
2020-02-18 01:40:16 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-02-13 10:15:49 +08:00
|
|
|
"time"
|
|
|
|
|
2020-03-07 23:03:25 +08:00
|
|
|
"github.com/shibukawa/configdir"
|
2020-04-24 03:07:08 +08:00
|
|
|
"github.com/xxxserxxx/gotop/v4/colorschemes"
|
|
|
|
"github.com/xxxserxxx/gotop/v4/widgets"
|
2020-02-13 10:15:49 +08:00
|
|
|
)
|
|
|
|
|
2020-04-29 23:25:21 +08:00
|
|
|
// CONFFILE is the name of the default config file
|
|
|
|
const CONFFILE = "gotop.conf"
|
|
|
|
|
2020-02-13 10:15:49 +08:00
|
|
|
type Config struct {
|
2020-04-28 08:38:58 +08:00
|
|
|
ConfigDir configdir.ConfigDir
|
2020-02-13 10:15:49 +08:00
|
|
|
GraphHorizontalScale int
|
|
|
|
HelpVisible bool
|
|
|
|
Colorscheme colorschemes.Colorscheme
|
2020-04-28 08:38:58 +08:00
|
|
|
UpdateInterval time.Duration
|
|
|
|
AverageLoad bool
|
|
|
|
PercpuLoad bool
|
|
|
|
Statusbar bool
|
|
|
|
TempScale widgets.TempScale
|
|
|
|
NetInterface string
|
|
|
|
Layout string
|
|
|
|
MaxLogSize int64
|
|
|
|
ExportPort string
|
|
|
|
Mbps bool
|
|
|
|
Temps []string
|
|
|
|
Test bool
|
|
|
|
ExtensionVars map[string]string
|
2020-04-29 23:25:21 +08:00
|
|
|
ConfigFile string
|
2020-04-28 08:38:58 +08:00
|
|
|
}
|
2020-02-13 10:15:49 +08:00
|
|
|
|
2020-04-28 08:38:58 +08:00
|
|
|
func NewConfig() Config {
|
|
|
|
cd := configdir.New("", "gotop")
|
|
|
|
cd.LocalPath, _ = filepath.Abs(".")
|
|
|
|
conf := Config{
|
|
|
|
ConfigDir: cd,
|
|
|
|
GraphHorizontalScale: 7,
|
|
|
|
HelpVisible: false,
|
|
|
|
UpdateInterval: time.Second,
|
|
|
|
AverageLoad: false,
|
|
|
|
PercpuLoad: true,
|
|
|
|
TempScale: widgets.Celsius,
|
|
|
|
Statusbar: false,
|
2020-04-28 09:33:41 +08:00
|
|
|
NetInterface: widgets.NetInterfaceAll,
|
2020-04-28 08:38:58 +08:00
|
|
|
MaxLogSize: 5000000,
|
|
|
|
Layout: "default",
|
|
|
|
ExtensionVars: make(map[string]string),
|
|
|
|
}
|
|
|
|
conf.Colorscheme, _ = colorschemes.FromName(conf.ConfigDir, "default")
|
2020-04-29 23:25:21 +08:00
|
|
|
folder := conf.ConfigDir.QueryFolderContainsFile(CONFFILE)
|
|
|
|
if folder != nil {
|
|
|
|
conf.ConfigFile = filepath.Join(folder.Path, CONFFILE)
|
|
|
|
}
|
2020-04-28 08:38:58 +08:00
|
|
|
return conf
|
2020-02-13 10:15:49 +08:00
|
|
|
}
|
2020-02-18 01:40:16 +08:00
|
|
|
|
2020-03-07 23:03:25 +08:00
|
|
|
func (conf *Config) Load() error {
|
2020-04-23 23:41:20 +08:00
|
|
|
var in []byte
|
2020-04-29 23:25:21 +08:00
|
|
|
if conf.ConfigFile == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-23 23:41:20 +08:00
|
|
|
var err error
|
2020-04-29 23:25:21 +08:00
|
|
|
if _, err = os.Stat(conf.ConfigFile); os.IsNotExist(err) {
|
2020-04-30 01:07:37 +08:00
|
|
|
// Check for the file in the usual suspects
|
|
|
|
folder := conf.ConfigDir.QueryFolderContainsFile(conf.ConfigFile)
|
|
|
|
if folder == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
conf.ConfigFile = filepath.Join(folder.Path, conf.ConfigFile)
|
2020-03-07 23:03:25 +08:00
|
|
|
}
|
2020-04-29 23:25:21 +08:00
|
|
|
if in, err = ioutil.ReadFile(conf.ConfigFile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-24 02:38:22 +08:00
|
|
|
return load(bytes.NewReader(in), conf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func load(in io.Reader, conf *Config) error {
|
|
|
|
r := bufio.NewScanner(in)
|
2020-02-18 01:40:16 +08:00
|
|
|
var lineNo int
|
|
|
|
for r.Scan() {
|
|
|
|
l := strings.TrimSpace(r.Text())
|
2020-04-23 23:41:20 +08:00
|
|
|
if l[0] == '#' {
|
|
|
|
continue
|
|
|
|
}
|
2020-02-18 01:40:16 +08:00
|
|
|
kv := strings.Split(l, "=")
|
|
|
|
if len(kv) != 2 {
|
|
|
|
return fmt.Errorf("bad config file syntax; should be KEY=VALUE, was %s", l)
|
|
|
|
}
|
|
|
|
key := strings.ToLower(kv[0])
|
|
|
|
switch key {
|
|
|
|
default:
|
2020-04-28 08:38:58 +08:00
|
|
|
conf.ExtensionVars[key] = kv[1]
|
2020-02-18 01:40:16 +08:00
|
|
|
case "configdir":
|
2020-03-07 23:03:25 +08:00
|
|
|
log.Printf("configdir is deprecated. Ignored configdir=%s", kv[1])
|
2020-02-18 01:40:16 +08:00
|
|
|
case "logdir":
|
2020-03-07 23:03:25 +08:00
|
|
|
log.Printf("logdir is deprecated. Ignored logdir=%s", kv[1])
|
2020-02-18 01:40:16 +08:00
|
|
|
case "logfile":
|
2020-03-07 23:03:25 +08:00
|
|
|
log.Printf("logfile is deprecated. Ignored logfile=%s", kv[1])
|
2020-04-23 23:41:20 +08:00
|
|
|
case graphhorizontalscale:
|
2020-02-18 01:40:16 +08:00
|
|
|
iv, err := strconv.Atoi(kv[1])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
conf.GraphHorizontalScale = iv
|
2020-04-23 23:41:20 +08:00
|
|
|
case helpvisible:
|
2020-02-18 01:40:16 +08:00
|
|
|
bv, err := strconv.ParseBool(kv[1])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("line %d: %v", lineNo, err)
|
|
|
|
}
|
|
|
|
conf.HelpVisible = bv
|
2020-04-23 23:41:20 +08:00
|
|
|
case colorscheme:
|
2020-02-18 01:40:16 +08:00
|
|
|
cs, err := colorschemes.FromName(conf.ConfigDir, kv[1])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("line %d: %v", lineNo, err)
|
|
|
|
}
|
|
|
|
conf.Colorscheme = cs
|
2020-04-23 23:41:20 +08:00
|
|
|
case updateinterval:
|
2020-02-18 01:40:16 +08:00
|
|
|
iv, err := strconv.Atoi(kv[1])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
conf.UpdateInterval = time.Duration(iv)
|
2020-04-23 23:41:20 +08:00
|
|
|
case averagecpu:
|
2020-02-18 01:40:16 +08:00
|
|
|
bv, err := strconv.ParseBool(kv[1])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("line %d: %v", lineNo, err)
|
|
|
|
}
|
|
|
|
conf.AverageLoad = bv
|
2020-04-23 23:41:20 +08:00
|
|
|
case percpuload:
|
2020-02-18 01:40:16 +08:00
|
|
|
bv, err := strconv.ParseBool(kv[1])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("line %d: %v", lineNo, err)
|
|
|
|
}
|
|
|
|
conf.PercpuLoad = bv
|
2020-04-23 23:41:20 +08:00
|
|
|
case tempscale:
|
2020-04-24 02:01:13 +08:00
|
|
|
switch kv[1] {
|
|
|
|
case "C":
|
|
|
|
conf.TempScale = 'C'
|
|
|
|
case "F":
|
|
|
|
conf.TempScale = 'F'
|
|
|
|
default:
|
|
|
|
conf.TempScale = 'C'
|
|
|
|
return fmt.Errorf("invalid TempScale value %s", kv[1])
|
2020-02-18 01:40:16 +08:00
|
|
|
}
|
2020-04-23 23:41:20 +08:00
|
|
|
case statusbar:
|
2020-02-18 01:40:16 +08:00
|
|
|
bv, err := strconv.ParseBool(kv[1])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("line %d: %v", lineNo, err)
|
|
|
|
}
|
|
|
|
conf.Statusbar = bv
|
2020-04-23 23:41:20 +08:00
|
|
|
case netinterface:
|
2020-02-18 01:40:16 +08:00
|
|
|
conf.NetInterface = kv[1]
|
2020-04-23 23:41:20 +08:00
|
|
|
case layout:
|
2020-02-18 01:40:16 +08:00
|
|
|
conf.Layout = kv[1]
|
2020-04-23 23:41:20 +08:00
|
|
|
case maxlogsize:
|
2020-02-18 01:40:16 +08:00
|
|
|
iv, err := strconv.Atoi(kv[1])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
conf.MaxLogSize = int64(iv)
|
2020-04-23 23:41:20 +08:00
|
|
|
case export:
|
2020-02-18 23:44:29 +08:00
|
|
|
conf.ExportPort = kv[1]
|
2020-04-23 23:41:20 +08:00
|
|
|
case mbps:
|
2020-04-17 02:28:18 +08:00
|
|
|
conf.Mbps = true
|
2020-04-23 23:41:20 +08:00
|
|
|
case temperatures:
|
|
|
|
conf.Temps = strings.Split(kv[1], ",")
|
2020-02-18 01:40:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-23 23:41:20 +08:00
|
|
|
|
2020-04-29 23:25:21 +08:00
|
|
|
// Write serializes the configuration to a file.
|
|
|
|
// The configuration written is based on the loaded configuration, plus any
|
|
|
|
// command-line changes, so it can be used to update an existing configuration
|
|
|
|
// file. The file will be written to the specificed `--config` argument file,
|
|
|
|
// if one is set; otherwise, it'll create one in the user's config directory.
|
2020-04-28 09:33:41 +08:00
|
|
|
func (conf *Config) Write() (string, error) {
|
2020-04-29 23:25:21 +08:00
|
|
|
var dir *configdir.Config
|
|
|
|
var file string = CONFFILE
|
|
|
|
if conf.ConfigFile == "" {
|
|
|
|
ds := conf.ConfigDir.QueryFolders(configdir.Global)
|
2020-04-23 23:41:20 +08:00
|
|
|
if len(ds) == 0 {
|
2020-04-29 23:25:21 +08:00
|
|
|
ds = conf.ConfigDir.QueryFolders(configdir.Local)
|
|
|
|
if len(ds) == 0 {
|
|
|
|
return "", fmt.Errorf("error locating config folders")
|
|
|
|
}
|
2020-04-23 23:41:20 +08:00
|
|
|
}
|
2020-04-29 23:25:21 +08:00
|
|
|
ds[0].CreateParentDir(CONFFILE)
|
|
|
|
dir = ds[0]
|
|
|
|
} else {
|
|
|
|
dir = &configdir.Config{}
|
|
|
|
dir.Path = filepath.Dir(conf.ConfigFile)
|
|
|
|
file = filepath.Base(conf.ConfigFile)
|
2020-04-23 23:41:20 +08:00
|
|
|
}
|
2020-04-28 09:33:41 +08:00
|
|
|
marshalled := marshal(conf)
|
2020-04-29 23:25:21 +08:00
|
|
|
err := dir.WriteFile(file, marshalled)
|
2020-04-23 23:41:20 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-04-29 23:25:21 +08:00
|
|
|
return filepath.Join(dir.Path, file), nil
|
2020-04-23 23:41:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func marshal(c *Config) []byte {
|
|
|
|
buff := bytes.NewBuffer(nil)
|
2020-04-24 02:01:13 +08:00
|
|
|
fmt.Fprintln(buff, "# Scale graphs to this level; 7 is the default, 2 is zoomed out.")
|
2020-04-23 23:41:20 +08:00
|
|
|
fmt.Fprintf(buff, "%s=%d\n", graphhorizontalscale, c.GraphHorizontalScale)
|
2020-04-24 02:01:13 +08:00
|
|
|
fmt.Fprintln(buff, "# If true, start the UI with the help visible")
|
2020-04-23 23:41:20 +08:00
|
|
|
fmt.Fprintf(buff, "%s=%t\n", helpvisible, c.HelpVisible)
|
2020-04-24 02:01:13 +08:00
|
|
|
fmt.Fprintln(buff, "# The color scheme to use. See `--list colorschemes`")
|
2020-04-23 23:41:20 +08:00
|
|
|
fmt.Fprintf(buff, "%s=%s\n", colorscheme, c.Colorscheme.Name)
|
2020-04-24 02:01:13 +08:00
|
|
|
fmt.Fprintln(buff, "# How frequently to update the UI, in nanoseconds")
|
2020-04-23 23:41:20 +08:00
|
|
|
fmt.Fprintf(buff, "%s=%d\n", updateinterval, c.UpdateInterval)
|
2020-04-24 02:01:13 +08:00
|
|
|
fmt.Fprintln(buff, "# If true, show the average CPU load")
|
2020-04-23 23:41:20 +08:00
|
|
|
fmt.Fprintf(buff, "%s=%t\n", averagecpu, c.AverageLoad)
|
2020-04-24 02:01:13 +08:00
|
|
|
fmt.Fprintln(buff, "# If true, show load per CPU")
|
2020-04-23 23:41:20 +08:00
|
|
|
fmt.Fprintf(buff, "%s=%t\n", percpuload, c.PercpuLoad)
|
2020-04-24 02:01:13 +08:00
|
|
|
fmt.Fprintln(buff, "# Temperature units. C for Celcius, F for Fahrenheit")
|
|
|
|
fmt.Fprintf(buff, "%s=%c\n", tempscale, c.TempScale)
|
|
|
|
fmt.Fprintln(buff, "# If true, display a status bar")
|
2020-04-23 23:41:20 +08:00
|
|
|
fmt.Fprintf(buff, "%s=%t\n", statusbar, c.Statusbar)
|
2020-04-24 02:01:13 +08:00
|
|
|
fmt.Fprintln(buff, "# The network interface to monitor")
|
2020-04-23 23:41:20 +08:00
|
|
|
fmt.Fprintf(buff, "%s=%s\n", netinterface, c.NetInterface)
|
2020-04-24 02:01:13 +08:00
|
|
|
fmt.Fprintln(buff, "# A layout name. See `--list layouts`")
|
2020-04-23 23:41:20 +08:00
|
|
|
fmt.Fprintf(buff, "%s=%s\n", layout, c.Layout)
|
2020-04-24 02:01:13 +08:00
|
|
|
fmt.Fprintln(buff, "# The maximum log file size, in bytes")
|
2020-04-23 23:41:20 +08:00
|
|
|
fmt.Fprintf(buff, "%s=%d\n", maxlogsize, c.MaxLogSize)
|
2020-04-24 02:01:13 +08:00
|
|
|
fmt.Fprintln(buff, "# If set, export data as Promethius metrics on the interface:port.\n# E.g., `:8080` (colon is required, interface is not)")
|
|
|
|
if c.ExportPort == "" {
|
|
|
|
fmt.Fprint(buff, "#")
|
|
|
|
}
|
2020-04-23 23:41:20 +08:00
|
|
|
fmt.Fprintf(buff, "%s=%s\n", export, c.ExportPort)
|
2020-04-24 02:01:13 +08:00
|
|
|
fmt.Fprintln(buff, "# Display network IO in mpbs if true")
|
2020-04-23 23:41:20 +08:00
|
|
|
fmt.Fprintf(buff, "%s=%t\n", mbps, c.Mbps)
|
2020-04-24 02:01:13 +08:00
|
|
|
fmt.Fprintln(buff, "# A list of enabled temp sensors. See `--list devices`")
|
|
|
|
if len(c.Temps) == 0 {
|
|
|
|
fmt.Fprint(buff, "#")
|
|
|
|
}
|
2020-04-23 23:41:20 +08:00
|
|
|
fmt.Fprintf(buff, "%s=%s\n", temperatures, strings.Join(c.Temps, ","))
|
|
|
|
return buff.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
graphhorizontalscale = "graphhorizontalscale"
|
|
|
|
helpvisible = "helpvisible"
|
|
|
|
colorscheme = "colorscheme"
|
|
|
|
updateinterval = "updateinterval"
|
|
|
|
averagecpu = "averagecpu"
|
|
|
|
percpuload = "percpuload"
|
|
|
|
tempscale = "tempscale"
|
|
|
|
statusbar = "statusbar"
|
|
|
|
netinterface = "netinterface"
|
|
|
|
layout = "layout"
|
|
|
|
maxlogsize = "maxlogsize"
|
|
|
|
export = "metricsexportport"
|
|
|
|
mbps = "mbps"
|
|
|
|
temperatures = "temperatures"
|
|
|
|
)
|