This commit is contained in:
Ivan Daniluk 2015-05-01 19:13:23 +03:00
parent 06c13f15f9
commit a09f9dba9f
6 changed files with 36 additions and 24 deletions

View File

@ -8,30 +8,35 @@ import (
"github.com/antonholmquist/jason"
)
const ExpvarsUrl = "/debug/vars"
// ExpvarsUrl is the default url for fetching expvar info.
const ExpvarsURL = "/debug/vars"
type Expvar *jason.Object
// Expvar represents fetched expvar variable.
type Expvar struct {
*jason.Object
}
// FetchExpvar fetches expvar by http for the given addr (host:port)
func FetchExpvar(addr string) (*jason.Object, error) {
var e jason.Object
func FetchExpvar(addr string) (*Expvar, error) {
e := &Expvar{&jason.Object{}}
resp, err := http.Get(addr)
if err != nil {
return &e, err
return e, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
return &e, errors.New("Vars not found. Did you import expvars?")
} else {
expvar, err := ParseExpvar(resp.Body)
e = *expvar
if err != nil {
return &e, err
}
return e, errors.New("Vars not found. Did you import expvars?")
}
return &e, nil
e, err = ParseExpvar(resp.Body)
if err != nil {
return e, err
}
return e, nil
}
func ParseExpvar(r io.Reader) (*jason.Object, error) {
return jason.NewObjectFromReader(r)
// ParseExpvar parses expvar data from reader.
func ParseExpvar(r io.Reader) (*Expvar, error) {
object, err := jason.NewObjectFromReader(r)
return &Expvar{object}, err
}

View File

@ -38,7 +38,9 @@ func main() {
if *dummy {
ui = &DummyUI{}
}
ui.Init(*data)
if err := ui.Init(*data); err != nil {
log.Fatal(err)
}
defer ui.Close()
tick := time.NewTicker(*interval)

View File

@ -7,6 +7,7 @@ import (
//"github.com/pyk/byten"
)
// Services is just a slice of Service.
type Services []*Service
// Service represents constantly updating info about single service.
@ -68,7 +69,7 @@ func (s *Service) updateCmdline(cmdline []string) {
//
// If host is not specified, 'localhost' is used.
func (s Service) Addr() string {
return fmt.Sprintf("http://localhost:%s%s", s.Port, ExpvarsUrl)
return fmt.Sprintf("http://localhost:%s%s", s.Port, ExpvarsURL)
}
// StatusLine returns status line for services with it's name and status.
@ -80,6 +81,7 @@ func (s Service) StatusLine() string {
return fmt.Sprintf("[R] %s", s.Name)
}
// Value returns current value for the given var of this service.
func (s Service) Value(name VarName) string {
if s.Err != nil {
return "N/A"
@ -95,6 +97,8 @@ func (s Service) Value(name VarName) string {
return fmt.Sprintf("%d", val.Front())
}
// Values returns slice of ints with recent values of the given var,
// to be used with sparkline.
func (s Service) Values(name VarName) []int {
if s.Err != nil {
return nil

View File

@ -1,5 +0,0 @@
package main
type Source interface {
Update() (interface{}, error)
}

View File

@ -7,8 +7,13 @@ import (
// DummyUI is an simple console UI mockup, for testing purposes.
type DummyUI struct{}
// Init implements UI.
func (*DummyUI) Init(UIData) error { return nil }
func (*DummyUI) Close() {}
// Close implements UI.
func (*DummyUI) Close() {}
// Update implements UI.
func (*DummyUI) Update(data UIData) {
if data.Services == nil {
return

View File

@ -32,6 +32,7 @@ func (t *TermUI) Init(data UIData) error {
p.TextFgColor = termui.ColorWhite
p.Border.Label = "Services Monitor"
p.Border.FgColor = termui.ColorCyan
t.Title.Text = fmt.Sprintf("monitoring %d services, press q to quit", len(data.Services))
return p
}()
t.Status = func() *termui.Par {
@ -99,8 +100,8 @@ func (t *TermUI) Init(data UIData) error {
return nil
}
// Update updates UI widgets from UIData.
func (t *TermUI) Update(data UIData) {
t.Title.Text = fmt.Sprintf("monitoring %d services, press q to quit", len(data.Services))
t.Status.Text = fmt.Sprintf("Last update: %v", data.LastTimestamp.Format("15:04:05 02/Jan/06"))
var services []string