You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
1.9 KiB

  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "strings"
  7. "github.com/go-kit/kit/log"
  8. "github.com/go-kit/kit/log/term"
  9. cmn "github.com/tendermint/go-common"
  10. monitor "github.com/tendermint/tools/tm-monitor/monitor"
  11. )
  12. var version = "0.2.1"
  13. var logger = log.NewNopLogger()
  14. func main() {
  15. var listenAddr string
  16. var noton bool
  17. flag.StringVar(&listenAddr, "listen-addr", "tcp://0.0.0.0:46670", "HTTP and Websocket server listen address")
  18. flag.BoolVar(&noton, "no-ton", false, "Do not show ton (table of nodes)")
  19. flag.Usage = func() {
  20. fmt.Println(`Tendermint monitor watches over one or more Tendermint core
  21. applications, collecting and providing various statistics to the user.
  22. Usage:
  23. tm-monitor [-no-ton] [-listen-addr="tcp://0.0.0.0:46670"] [endpoints]
  24. Examples:
  25. # monitor single instance
  26. tm-monitor localhost:46657
  27. # monitor a few instances by providing comma-separated list of RPC endpoints
  28. tm-monitor host1:46657,host2:46657`)
  29. fmt.Println("Flags:")
  30. flag.PrintDefaults()
  31. }
  32. flag.Parse()
  33. if flag.NArg() == 0 {
  34. flag.Usage()
  35. os.Exit(1)
  36. }
  37. if noton {
  38. // Color errors red
  39. colorFn := func(keyvals ...interface{}) term.FgBgColor {
  40. for i := 1; i < len(keyvals); i += 2 {
  41. if _, ok := keyvals[i].(error); ok {
  42. return term.FgBgColor{Fg: term.White, Bg: term.Red}
  43. }
  44. }
  45. return term.FgBgColor{}
  46. }
  47. logger = term.NewLogger(os.Stdout, log.NewLogfmtLogger, colorFn)
  48. }
  49. m := startMonitor(flag.Arg(0))
  50. startRPC(listenAddr, m)
  51. var ton *Ton
  52. if !noton {
  53. ton = NewTon(m)
  54. ton.Start()
  55. }
  56. cmn.TrapSignal(func() {
  57. if !noton {
  58. ton.Stop()
  59. }
  60. m.Stop()
  61. })
  62. }
  63. func startMonitor(endpoints string) *monitor.Monitor {
  64. m := monitor.NewMonitor()
  65. m.SetLogger(log.With(logger, "component", "monitor"))
  66. for _, e := range strings.Split(endpoints, ",") {
  67. n := monitor.NewNode(e)
  68. n.SetLogger(log.With(logger, "node", e))
  69. if err := m.Monitor(n); err != nil {
  70. panic(err)
  71. }
  72. }
  73. if err := m.Start(); err != nil {
  74. panic(err)
  75. }
  76. return m
  77. }