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" )
2015-05-03 03:54:00 +08:00
portsArg = flag . String ( "ports" , "1234" , "Ports for accessing services expvars (comma-separated)" )
2015-05-03 21:52:38 +08:00
varsArg = flag . String ( "vars" , "mem:memstats.Alloc,mem:memstats.Sys,mem:memstats.HeapAlloc,mem:memstats.HeapInuse,memstats.EnableGC,memstats.NumGC,duration:memstats.PauseTotalNs" , "Vars to monitor (comma-separated)" )
2015-05-02 01:12:23 +08:00
dummy = flag . Bool ( "dummy" , false , "Use dummy (console) output" )
2015-05-03 21:52:38 +08:00
bind = flag . String ( "expvar" , "1234" , "Port to listen to be able monitor itself" )
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 )
}
2015-05-03 21:52:38 +08:00
go StartHttp ( * bind )
2015-05-01 21:49:19 +08:00
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 )
}
2015-05-03 02:17:51 +08:00
var ui UI
if len ( data . Services ) > 1 {
ui = & TermUI { }
} else {
ui = & TermUISingle { }
}
2015-04-21 17:51:01 +08:00
if * dummy {
ui = & DummyUI { }
}
2015-05-03 02:17:51 +08:00
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 )
}