expvarmon/main.go

79 lines
1.5 KiB
Go
Raw Normal View History

2015-04-21 17:51:01 +08:00
package main
import (
"flag"
"log"
2015-05-02 01:12:23 +08:00
"sync"
2015-04-21 17:51:01 +08:00
"time"
2015-04-26 03:46:16 +08:00
"github.com/divan/termui"
2015-04-21 17:51:01 +08:00
)
var (
2015-05-02 01:12:23 +08:00
interval = flag.Duration("i", 5*time.Second, "Polling interval")
portsArg = flag.String("ports", "1234", "Ports for accessing services expvars")
varsArg = flag.String("vars", "memstats.Alloc,memstats.Sys", "Default vars to monitor")
dummy = flag.Bool("dummy", false, "Use dummy (console) output")
2015-04-21 17:51:01 +08:00
)
func main() {
flag.Parse()
ports, err := ParsePorts(*portsArg)
if err != nil {
log.Fatal("cannot parse ports:", err)
}
2015-05-02 01:12:23 +08:00
vars, err := ParseVars(*varsArg)
2015-05-01 21:49:19 +08:00
if err != nil {
log.Fatal(err)
}
data := NewUIData(vars)
2015-04-21 17:51:01 +08:00
for _, port := range ports {
2015-05-01 21:49:19 +08:00
service := NewService(port, vars)
2015-04-21 17:51:01 +08:00
data.Services = append(data.Services, service)
}
var ui UI = &TermUI{}
if *dummy {
ui = &DummyUI{}
}
2015-05-02 00:13:23 +08:00
if err := ui.Init(*data); err != nil {
log.Fatal(err)
}
2015-04-21 17:51:01 +08:00
defer ui.Close()
tick := time.NewTicker(*interval)
evtCh := termui.EventCh()
2015-05-02 01:12:23 +08:00
UpdateAll(ui, data)
2015-04-21 17:51:01 +08:00
for {
select {
case <-tick.C:
2015-05-02 01:12:23 +08:00
UpdateAll(ui, data)
2015-04-21 17:51:01 +08:00
case e := <-evtCh:
if e.Type == termui.EventKey && e.Ch == 'q' {
return
}
2015-04-26 03:46:16 +08:00
if e.Type == termui.EventResize {
termui.Body.Width = termui.TermWidth()
termui.Body.Align()
}
2015-04-21 17:51:01 +08:00
}
}
}
2015-05-02 01:12:23 +08:00
// UpdateAll collects data from expvars and refreshes UI.
func UpdateAll(ui UI, data *UIData) {
var wg sync.WaitGroup
for _, service := range data.Services {
wg.Add(1)
go service.Update(&wg)
}
wg.Wait()
data.LastTimestamp = time.Now()
ui.Update(*data)
}