From c14bc64319ab02bfb435cd3fc3d46e5dbf043987 Mon Sep 17 00:00:00 2001 From: Ivan Daniluk Date: Fri, 1 May 2015 19:26:28 +0300 Subject: [PATCH] Added host:port parsing --- main.go | 4 ++-- service.go | 39 ++++++++++++++++++++++++--------------- ui_termui.go | 2 +- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index 0777ae4..c03b412 100644 --- a/main.go +++ b/main.go @@ -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") ) diff --git a/service.go b/service.go index d3da6c0..531906a 100644 --- a/service.go +++ b/service.go @@ -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 - cmdline, err := expvar.GetStringArray("cmdline") - if err != nil { - s.Err = err - } else { - s.updateCmdline(cmdline) + // Update Cmdline & Name only once + if len(s.Cmdline) == 0 { + cmdline, err := expvar.GetStringArray("cmdline") + if err != nil { + s.Err = err + } else { + 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 { - return fmt.Sprintf("http://localhost:%s%s", s.Port, ExpvarsURL) + // 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. diff --git a/ui_termui.go b/ui_termui.go index 667e504..b25f79c 100644 --- a/ui_termui.go +++ b/ui_termui.go @@ -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