Browse Source

[tm-monitor] extract lib to monitor/ dir

because tm-bench needs these structures
pull/1943/head
Anton Kaliaev 8 years ago
parent
commit
31a54b0840
No known key found for this signature in database GPG Key ID: 7B6881D965918214
12 changed files with 111 additions and 84 deletions
  1. +2
    -2
      tm-monitor/Makefile
  2. +12
    -2
      tm-monitor/glide.lock
  3. +5
    -0
      tm-monitor/glide.yaml
  4. +28
    -47
      tm-monitor/main.go
  5. +16
    -2
      tm-monitor/monitor/monitor.go
  6. +2
    -2
      tm-monitor/monitor/monitor_test.go
  7. +1
    -3
      tm-monitor/monitor/network.go
  8. +2
    -2
      tm-monitor/monitor/network_test.go
  9. +22
    -6
      tm-monitor/monitor/node.go
  10. +2
    -2
      tm-monitor/monitor/node_test.go
  11. +15
    -14
      tm-monitor/rpc.go
  12. +4
    -2
      tm-monitor/ton.go

+ 2
- 2
tm-monitor/Makefile View File

@ -3,6 +3,7 @@ VERSION := $(shell perl -ne '/^var version.*"([^"]+)".*$$/ && print "v$$1\n"' ma
GOTOOLS = \ GOTOOLS = \
github.com/Masterminds/glide \ github.com/Masterminds/glide \
github.com/mitchellh/gox github.com/mitchellh/gox
PACKAGES=$(shell go list ./... | grep -v '/vendor/')
tools: tools:
go get -v $(GOTOOLS) go get -v $(GOTOOLS)
@ -17,7 +18,7 @@ install:
go install -ldflags "-X main.version=${VERSION}" go install -ldflags "-X main.version=${VERSION}"
test: test:
go test
@go test $(PACKAGES)
build-all: tools build-all: tools
gox -verbose \ gox -verbose \
@ -41,7 +42,6 @@ build-docker:
docker build -t "tendermint/monitor" . docker build -t "tendermint/monitor" .
clean: clean:
rm -f ./tm-monitor.log
rm -f ./tm-monitor rm -f ./tm-monitor
rm -rf ./dist rm -rf ./dist


+ 12
- 2
tm-monitor/glide.lock View File

@ -1,5 +1,5 @@
hash: 3315dcf12e2554e2927f2a0907f2547cd86d5c8926461d055662a7bee88caa4a
updated: 2017-03-07T08:38:45.613512657Z
hash: d21d1f12681cd4ab5b7f0efd7bf00c1d5f7021b1ae6e8700c11bca6822337079
updated: 2017-03-16T10:01:58.079646405Z
imports: imports:
- name: github.com/btcsuite/btcd - name: github.com/btcsuite/btcd
version: 583684b21bfbde9b5fc4403916fd7c807feb0289 version: 583684b21bfbde9b5fc4403916fd7c807feb0289
@ -7,6 +7,12 @@ imports:
- btcec - btcec
- name: github.com/BurntSushi/toml - name: github.com/BurntSushi/toml
version: 99064174e013895bbd9b025c31100bd1d9b590ca version: 99064174e013895bbd9b025c31100bd1d9b590ca
- name: github.com/go-kit/kit
version: b6f30a2e0632f5722fb26d8765d726335b79d3e6
subpackages:
- log
- name: github.com/go-logfmt/logfmt
version: 390ab7935ee28ec6b286364bba9b4dd6410cb3d5
- name: github.com/go-stack/stack - name: github.com/go-stack/stack
version: 100eb0c0a9c5b306ca2fb4f165df21d80ada4b82 version: 100eb0c0a9c5b306ca2fb4f165df21d80ada4b82
- name: github.com/golang/protobuf - name: github.com/golang/protobuf
@ -19,10 +25,14 @@ imports:
version: 3ab3a8b8831546bd18fd182c20687ca853b2bb13 version: 3ab3a8b8831546bd18fd182c20687ca853b2bb13
- name: github.com/jmhodges/levigo - name: github.com/jmhodges/levigo
version: c42d9e0ca023e2198120196f842701bb4c55d7b9 version: c42d9e0ca023e2198120196f842701bb4c55d7b9
- name: github.com/kr/logfmt
version: b84e30acd515aadc4b783ad4ff83aff3299bdfe0
- name: github.com/mattn/go-colorable - name: github.com/mattn/go-colorable
version: d898aa9fb31c91f35dd28ca75db377eff023c076 version: d898aa9fb31c91f35dd28ca75db377eff023c076
- name: github.com/mattn/go-isatty - name: github.com/mattn/go-isatty
version: dda3de49cbfcec471bd7a70e6cc01fcc3ff90109 version: dda3de49cbfcec471bd7a70e6cc01fcc3ff90109
- name: github.com/pkg/errors
version: bfd5150e4e41705ded2129ec33379de1cb90b513
- name: github.com/rcrowley/go-metrics - name: github.com/rcrowley/go-metrics
version: 1f30fe9094a513ce4c700b9a54458bbb0c96996c version: 1f30fe9094a513ce4c700b9a54458bbb0c96996c
- name: github.com/stretchr/testify - name: github.com/stretchr/testify


+ 5
- 0
tm-monitor/glide.yaml View File

@ -16,3 +16,8 @@ import:
subpackages: subpackages:
- client - client
- package: github.com/tendermint/log15 - package: github.com/tendermint/log15
- package: github.com/go-kit/kit
subpackages:
- log
- term
- package: github.com/pkg/errors

+ 28
- 47
tm-monitor/main.go View File

@ -6,22 +6,21 @@ import (
"os" "os"
"strings" "strings"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/term"
cmn "github.com/tendermint/go-common" cmn "github.com/tendermint/go-common"
logger "github.com/tendermint/go-logger"
log15 "github.com/tendermint/log15"
em "github.com/tendermint/tools/tm-monitor/eventmeter"
monitor "github.com/tendermint/tools/tm-monitor/monitor"
) )
var version = "0.3.0.pre" var version = "0.3.0.pre"
var log = logger.New()
var logger = log.NewNopLogger()
func main() { func main() {
var listenAddr string var listenAddr string
var verbose, noton bool
var noton bool
flag.StringVar(&listenAddr, "listen-addr", "tcp://0.0.0.0:46670", "HTTP and Websocket server listen address") flag.StringVar(&listenAddr, "listen-addr", "tcp://0.0.0.0:46670", "HTTP and Websocket server listen address")
flag.BoolVar(&verbose, "v", false, "verbose logging")
flag.BoolVar(&noton, "no-ton", false, "Do not show ton (table of nodes)") flag.BoolVar(&noton, "no-ton", false, "Do not show ton (table of nodes)")
flag.Usage = func() { flag.Usage = func() {
@ -29,7 +28,7 @@ func main() {
applications, collecting and providing various statistics to the user. applications, collecting and providing various statistics to the user.
Usage: Usage:
tm-monitor [-v] [-no-ton] [-listen-addr="tcp://0.0.0.0:46670"] [endpoints]
tm-monitor [-no-ton] [-listen-addr="tcp://0.0.0.0:46670"] [endpoints]
Examples: Examples:
# monitor single instance # monitor single instance
@ -48,17 +47,28 @@ Examples:
os.Exit(1) os.Exit(1)
} }
if noton {
// Color errors red
colorFn := func(keyvals ...interface{}) term.FgBgColor {
for i := 1; i < len(keyvals); i += 2 {
if _, ok := keyvals[i].(error); ok {
return term.FgBgColor{Fg: term.White, Bg: term.Red}
}
}
return term.FgBgColor{}
}
logger = term.NewLogger(os.Stdout, log.NewLogfmtLogger, colorFn)
}
m := startMonitor(flag.Arg(0)) m := startMonitor(flag.Arg(0))
startRPC(listenAddr, m) startRPC(listenAddr, m)
var ton *Ton var ton *Ton
if !noton { if !noton {
logToFile("tm-monitor.log", verbose)
ton = NewTon(m) ton = NewTon(m)
ton.Start() ton.Start()
} else {
logToStdout(verbose)
} }
cmn.TrapSignal(func() { cmn.TrapSignal(func() {
@ -69,50 +79,21 @@ Examples:
}) })
} }
func startMonitor(endpoints string) *Monitor {
m := NewMonitor()
func startMonitor(endpoints string) *monitor.Monitor {
m := monitor.NewMonitor()
m.SetLogger(log.With(logger, "component", "monitor"))
for _, e := range strings.Split(endpoints, ",") { for _, e := range strings.Split(endpoints, ",") {
if err := m.Monitor(NewNode(e)); err != nil {
log.Crit(err.Error())
os.Exit(1)
n := monitor.NewNode(e)
n.SetLogger(log.With(logger, "node", e))
if err := m.Monitor(n); err != nil {
panic(err)
} }
} }
if err := m.Start(); err != nil { if err := m.Start(); err != nil {
log.Crit(err.Error())
os.Exit(1)
panic(err)
} }
return m return m
} }
func logToStdout(verbose bool) {
if verbose {
log.SetHandler(logger.LvlFilterHandler(
logger.LvlDebug,
logger.BypassHandler(),
))
} else {
log.SetHandler(logger.LvlFilterHandler(
logger.LvlInfo,
logger.BypassHandler(),
))
}
em.Log = log
}
func logToFile(filename string, verbose bool) {
if verbose {
log.SetHandler(logger.LvlFilterHandler(
logger.LvlDebug,
log15.Must.FileHandler(filename, log15.LogfmtFormat()),
))
} else {
log.SetHandler(logger.LvlFilterHandler(
logger.LvlInfo,
log15.Must.FileHandler(filename, log15.LogfmtFormat()),
))
}
em.Log = log
}

tm-monitor/monitor.go → tm-monitor/monitor/monitor.go View File


tm-monitor/monitor_test.go → tm-monitor/monitor/monitor_test.go View File


tm-monitor/network.go → tm-monitor/monitor/network.go View File


tm-monitor/network_test.go → tm-monitor/monitor/network_test.go View File


tm-monitor/node.go → tm-monitor/monitor/node.go View File


tm-monitor/node_test.go → tm-monitor/monitor/node_test.go View File


+ 15
- 14
tm-monitor/rpc.go View File

@ -5,9 +5,10 @@ import (
"net/http" "net/http"
rpc "github.com/tendermint/go-rpc/server" rpc "github.com/tendermint/go-rpc/server"
monitor "github.com/tendermint/tools/tm-monitor/monitor"
) )
func startRPC(listenAddr string, m *Monitor) {
func startRPC(listenAddr string, m *monitor.Monitor) {
routes := routes(m) routes := routes(m)
// serve http and ws // serve http and ws
@ -20,7 +21,7 @@ func startRPC(listenAddr string, m *Monitor) {
} }
} }
func routes(m *Monitor) map[string]*rpc.RPCFunc {
func routes(m *monitor.Monitor) map[string]*rpc.RPCFunc {
return map[string]*rpc.RPCFunc{ return map[string]*rpc.RPCFunc{
"status": rpc.NewRPCFunc(RPCStatus(m), ""), "status": rpc.NewRPCFunc(RPCStatus(m), ""),
"status/network": rpc.NewRPCFunc(RPCNetworkStatus(m), ""), "status/network": rpc.NewRPCFunc(RPCNetworkStatus(m), ""),
@ -35,9 +36,9 @@ func routes(m *Monitor) map[string]*rpc.RPCFunc {
} }
// RPCStatus returns common statistics for the network and statistics per node. // RPCStatus returns common statistics for the network and statistics per node.
func RPCStatus(m *Monitor) interface{} {
func RPCStatus(m *monitor.Monitor) interface{} {
return func() (networkAndNodes, error) { return func() (networkAndNodes, error) {
values := make([]*Node, len(m.Nodes))
values := make([]*monitor.Node, len(m.Nodes))
i := 0 i := 0
for _, v := range m.Nodes { for _, v := range m.Nodes {
values[i] = v values[i] = v
@ -49,15 +50,15 @@ func RPCStatus(m *Monitor) interface{} {
} }
// RPCNetworkStatus returns common statistics for the network. // RPCNetworkStatus returns common statistics for the network.
func RPCNetworkStatus(m *Monitor) interface{} {
return func() (*Network, error) {
func RPCNetworkStatus(m *monitor.Monitor) interface{} {
return func() (*monitor.Network, error) {
return m.Network, nil return m.Network, nil
} }
} }
// RPCNodeStatus returns statistics for the given node. // RPCNodeStatus returns statistics for the given node.
func RPCNodeStatus(m *Monitor) interface{} {
return func(name string) (*Node, error) {
func RPCNodeStatus(m *monitor.Monitor) interface{} {
return func(name string) (*monitor.Node, error) {
if n, ok := m.Nodes[name]; ok { if n, ok := m.Nodes[name]; ok {
return n, nil return n, nil
} }
@ -66,9 +67,9 @@ func RPCNodeStatus(m *Monitor) interface{} {
} }
// RPCMonitor allows to dynamically add a endpoint to under the monitor. // RPCMonitor allows to dynamically add a endpoint to under the monitor.
func RPCMonitor(m *Monitor) interface{} {
return func(endpoint string) (*Node, error) {
n := NewNode(endpoint)
func RPCMonitor(m *monitor.Monitor) interface{} {
return func(endpoint string) (*monitor.Node, error) {
n := monitor.NewNode(endpoint)
if err := m.Monitor(n); err != nil { if err := m.Monitor(n); err != nil {
return nil, err return nil, err
} }
@ -77,7 +78,7 @@ func RPCMonitor(m *Monitor) interface{} {
} }
// RPCUnmonitor removes the given endpoint from under the monitor. // RPCUnmonitor removes the given endpoint from under the monitor.
func RPCUnmonitor(m *Monitor) interface{} {
func RPCUnmonitor(m *monitor.Monitor) interface{} {
return func(endpoint string) (bool, error) { return func(endpoint string) (bool, error) {
if n, ok := m.Nodes[endpoint]; ok { if n, ok := m.Nodes[endpoint]; ok {
m.Unmonitor(n) m.Unmonitor(n)
@ -121,6 +122,6 @@ func RPCUnmonitor(m *Monitor) interface{} {
//--> types //--> types
type networkAndNodes struct { type networkAndNodes struct {
Network *Network `json:"network"`
Nodes []*Node `json:"nodes"`
Network *monitor.Network `json:"network"`
Nodes []*monitor.Node `json:"nodes"`
} }

+ 4
- 2
tm-monitor/ton.go View File

@ -6,6 +6,8 @@ import (
"os" "os"
"text/tabwriter" "text/tabwriter"
"time" "time"
monitor "github.com/tendermint/tools/tm-monitor/monitor"
) )
const ( const (
@ -24,14 +26,14 @@ const (
// Ton was inspired by [Linux top // Ton was inspired by [Linux top
// program](https://en.wikipedia.org/wiki/Top_(software)) as the name suggests. // program](https://en.wikipedia.org/wiki/Top_(software)) as the name suggests.
type Ton struct { type Ton struct {
monitor *Monitor
monitor *monitor.Monitor
RefreshRate time.Duration RefreshRate time.Duration
Output io.Writer Output io.Writer
quit chan struct{} quit chan struct{}
} }
func NewTon(m *Monitor) *Ton {
func NewTon(m *monitor.Monitor) *Ton {
return &Ton{ return &Ton{
RefreshRate: defaultRefreshRate, RefreshRate: defaultRefreshRate,
Output: os.Stdout, Output: os.Stdout,


Loading…
Cancel
Save