Added host:port parsing

This commit is contained in:
Ivan Daniluk 2015-05-01 19:26:28 +03:00
parent a09f9dba9f
commit c14bc64319
3 changed files with 27 additions and 18 deletions

View File

@ -9,10 +9,10 @@ import (
)
var (
interval = flag.Duration("i", 1*time.Second, "Polling interval")
interval = flag.Duration("i", 5*time.Second, "Polling interval")
portsArg = flag.String("ports", "40001,40002,40000,40004,1233,1234,1235", "Ports for accessing services expvars")
defaultVars = flag.String("vars", "memstats.Alloc,memstats.Sys", "Default vars to monitor")
extraVars = flag.String("extravars", "Goroutines,Counters.A", "Extra vars exported with expvars package")
extraVars = flag.String("extravars", "", "Comma-separated extra vars exported with expvars")
dummy = flag.Bool("dummy", false, "Use dummy (console) output")
)

View File

@ -2,6 +2,8 @@ package main
import (
"fmt"
"net"
"strconv"
"strings"
//"github.com/pyk/byten"
@ -40,13 +42,18 @@ func (s *Service) Update() {
expvar, err := FetchExpvar(s.Addr())
s.Err = err
// Update Cmdline & Name only once
if len(s.Cmdline) == 0 {
cmdline, err := expvar.GetStringArray("cmdline")
if err != nil {
s.Err = err
} else {
s.updateCmdline(cmdline)
s.Cmdline = strings.Join(cmdline, " ")
s.Name = BaseCommand(cmdline)
}
}
// For all vars, fetch desired value from Json and push to it's own stack.
for name, stack := range s.values {
value, err := expvar.GetInt64(name.ToSlice()...)
if err != nil {
@ -56,20 +63,22 @@ func (s *Service) Update() {
}
}
func (s *Service) updateCmdline(cmdline []string) {
// Update name and cmdline only if empty
// TODO: move it to Update() with sync.Once
if len(s.Cmdline) == 0 {
s.Cmdline = strings.Join(cmdline, " ")
s.Name = BaseCommand(cmdline)
}
}
// Addr returns fully qualified host:port pair for service.
//
// If host is not specified, 'localhost' is used.
func (s Service) Addr() string {
// Try as port only
_, err := strconv.Atoi(s.Port)
if err == nil {
return fmt.Sprintf("http://localhost:%s%s", s.Port, ExpvarsURL)
}
host, port, err := net.SplitHostPort(s.Port)
if err == nil {
return fmt.Sprintf("http://%s:%s%s", host, port, ExpvarsURL)
}
return ""
}
// StatusLine returns status line for services with it's name and status.

View File

@ -32,7 +32,6 @@ 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 {
@ -102,6 +101,7 @@ func (t *TermUI) Init(data UIData) error {
// 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