expvarmon/main.go

67 lines
1.1 KiB
Go
Raw Normal View History

2015-04-21 17:51:01 +08:00
package main
import (
"flag"
"log"
"time"
"github.com/gizak/termui"
)
var (
2015-04-21 21:33:04 +08:00
interval = flag.Duration("i", 1*time.Second, "Polling interval")
2015-04-21 22:59:28 +08:00
portsArg = flag.String("ports", "40001,40002,40000,40004,1233,1234,1235", "Ports for accessing services expvars")
2015-04-21 17:51:01 +08:00
dummy = flag.Bool("dummy", false, "Use dummy (console) output")
)
func main() {
flag.Parse()
ports, err := ParsePorts(*portsArg)
if err != nil {
log.Fatal("cannot parse ports:", err)
}
2015-04-21 21:33:04 +08:00
data := *NewData()
2015-04-25 20:54:17 +08:00
var source = NewExpvarsSource(ports)
2015-04-21 17:51:01 +08:00
for _, port := range ports {
service := NewService(port)
data.Services = append(data.Services, service)
}
var ui UI = &TermUI{}
if *dummy {
ui = &DummyUI{}
}
ui.Init()
defer ui.Close()
tick := time.NewTicker(*interval)
evtCh := termui.EventCh()
update := func() {
2015-04-25 20:54:17 +08:00
for _, port := range source.Ports {
service := data.FindService(port)
if service == nil {
continue
2015-04-21 17:51:01 +08:00
}
2015-04-25 20:54:17 +08:00
service.Update()
2015-04-21 17:51:01 +08:00
}
2015-04-25 20:54:17 +08:00
2015-04-21 17:51:01 +08:00
data.LastTimestamp = time.Now()
2015-04-25 20:54:17 +08:00
2015-04-21 17:51:01 +08:00
ui.Update(data)
}
update()
for {
select {
case <-tick.C:
update()
case e := <-evtCh:
if e.Type == termui.EventKey && e.Ch == 'q' {
return
}
}
}
}