expvarmon/service.go

140 lines
2.7 KiB
Go
Raw Normal View History

2015-04-21 17:51:01 +08:00
package main
import (
2015-04-29 05:07:46 +08:00
"errors"
2015-04-25 20:54:17 +08:00
"fmt"
"net/http"
"strings"
2015-04-29 05:07:46 +08:00
2015-05-01 05:47:11 +08:00
"github.com/antonholmquist/jason"
2015-05-01 04:54:54 +08:00
//"github.com/pyk/byten"
2015-04-21 17:51:01 +08:00
)
type Services []*Service
// Service represents constantly updating info about single service.
type Service struct {
2015-05-01 04:54:54 +08:00
Port string
Name string
Cmdline string
2015-04-25 20:54:17 +08:00
2015-05-01 04:54:54 +08:00
values map[string]*Stack
2015-04-21 17:51:01 +08:00
Err error
}
// NewService returns new Service object.
func NewService(port string) *Service {
return &Service{
Name: port, // we have only port on start, so use it as name until resolved
Port: port,
2015-04-25 21:29:19 +08:00
2015-05-01 04:54:54 +08:00
values: make(map[string]*Stack),
2015-04-21 17:51:01 +08:00
}
}
2015-05-01 04:54:54 +08:00
// FetchExpvar fetches expvar by http for the given addr (host:port)
2015-05-01 05:47:11 +08:00
func FetchExpvar(addr string) (*jason.Object, error) {
var e jason.Object
2015-04-29 05:07:46 +08:00
resp, err := http.Get(addr)
2015-04-25 20:54:17 +08:00
if err != nil {
2015-04-29 05:07:46 +08:00
return &e, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
return &e, errors.New("Vars not found. Did you import expvars?")
2015-04-25 20:54:17 +08:00
} else {
2015-05-01 05:47:11 +08:00
expvar, err := jason.NewObjectFromReader(resp.Body)
2015-04-29 05:07:46 +08:00
e = *expvar
2015-04-25 20:54:17 +08:00
if err != nil {
2015-04-29 05:07:46 +08:00
return &e, err
2015-04-21 17:51:01 +08:00
}
}
2015-04-29 05:07:46 +08:00
return &e, nil
}
// Update updates Service info from Expvar variable.
func (s *Service) Update() {
2015-05-01 04:54:54 +08:00
expvar, err := FetchExpvar(s.Addr())
2015-04-29 05:07:46 +08:00
if err != nil {
2015-05-01 05:47:11 +08:00
s.Err = err
2015-04-29 05:07:46 +08:00
}
2015-04-21 17:51:01 +08:00
2015-05-01 05:47:11 +08:00
cmdline, err := expvar.GetStringArray("cmdline")
if err != nil {
s.Err = err
} else {
s.updateCmdline(cmdline)
}
2015-04-25 20:54:17 +08:00
2015-05-01 05:47:11 +08:00
alloc, err := expvar.GetInt64("memstats", "Alloc")
if err != nil {
s.Err = err
} else {
s.updateMem(alloc)
}
2015-05-01 04:54:54 +08:00
}
func (s *Service) updateCmdline(cmdline []string) {
2015-04-25 20:54:17 +08:00
// Update name and cmdline only if empty
if len(s.Cmdline) == 0 {
2015-05-01 04:54:54 +08:00
s.Cmdline = strings.Join(cmdline, " ")
s.Name = BaseCommand(cmdline)
2015-04-25 20:54:17 +08:00
}
2015-05-01 04:54:54 +08:00
}
2015-04-25 21:29:19 +08:00
2015-05-01 05:47:11 +08:00
func (s *Service) updateMem(alloc int64) {
2015-04-25 21:29:19 +08:00
// Put metrics data
2015-05-01 04:54:54 +08:00
mem, ok := s.values["mem.alloc"]
2015-04-25 21:29:19 +08:00
if !ok {
2015-05-01 04:54:54 +08:00
s.values["mem.alloc"] = NewStack(1200)
mem = s.values["mem.alloc"]
2015-04-25 21:29:19 +08:00
}
2015-05-01 05:47:11 +08:00
mem.Push(int(alloc))
2015-04-25 20:54:17 +08:00
}
// 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)
2015-04-21 17:51:01 +08:00
}
2015-04-26 03:46:16 +08:00
// StatusLine returns status line for services with it's name and status.
func (s Service) StatusLine() string {
if s.Err != nil {
return fmt.Sprintf("[ERR] %s failed", s.Name)
}
return fmt.Sprintf("[R] %s", s.Name)
}
2015-05-01 04:54:54 +08:00
func (s Service) Value(key string) string {
if s.Err != nil {
2015-04-26 03:46:16 +08:00
return "N/A"
}
2015-05-01 04:54:54 +08:00
val, ok := s.values[key]
if !ok {
return "N/A"
}
if val.Front() == 0 {
return "N/A"
}
//allocated := byten.Size(int64(val.Front()))
//return fmt.Sprintf("Alloc: %s", allocated)
return fmt.Sprintf("%d", val.Front())
}
func (s Service) Values(key string) []int {
if s.Err != nil {
return nil
}
val, ok := s.values[key]
if !ok {
return nil
}
2015-04-26 03:46:16 +08:00
2015-05-01 04:54:54 +08:00
return val.Values
2015-04-26 03:46:16 +08:00
}