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.

115 lines
2.2 KiB

  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "strings"
  7. cmn "github.com/tendermint/go-common"
  8. logger "github.com/tendermint/go-logger"
  9. log15 "github.com/tendermint/log15"
  10. )
  11. var version = "0.2.0"
  12. var log = logger.New()
  13. func main() {
  14. var listenAddr string
  15. var verbose, noton bool
  16. flag.StringVar(&listenAddr, "listen-addr", "tcp://0.0.0.0:46670", "HTTP and Websocket server listen address")
  17. flag.BoolVar(&verbose, "v", false, "verbose logging")
  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 [-v] [-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. m := startMonitor(flag.Arg(0))
  38. startRPC(listenAddr, m)
  39. var ton *Ton
  40. if !noton {
  41. logToFile("tm-monitor.log", verbose)
  42. ton = NewTon(m)
  43. ton.Start()
  44. } else {
  45. logToStdout(verbose)
  46. }
  47. cmn.TrapSignal(func() {
  48. if !noton {
  49. ton.Stop()
  50. }
  51. m.Stop()
  52. })
  53. }
  54. func startMonitor(endpoints string) *Monitor {
  55. m := NewMonitor()
  56. for _, e := range strings.Split(endpoints, ",") {
  57. if err := m.Monitor(NewNode(e)); err != nil {
  58. log.Crit(err.Error())
  59. os.Exit(1)
  60. }
  61. }
  62. if err := m.Start(); err != nil {
  63. log.Crit(err.Error())
  64. os.Exit(1)
  65. }
  66. return m
  67. }
  68. func logToStdout(verbose bool) {
  69. if verbose {
  70. log.SetHandler(logger.LvlFilterHandler(
  71. logger.LvlDebug,
  72. logger.BypassHandler(),
  73. ))
  74. } else {
  75. log.SetHandler(logger.LvlFilterHandler(
  76. logger.LvlInfo,
  77. logger.BypassHandler(),
  78. ))
  79. }
  80. }
  81. func logToFile(filename string, verbose bool) {
  82. if verbose {
  83. log.SetHandler(logger.LvlFilterHandler(
  84. logger.LvlDebug,
  85. log15.Must.FileHandler(filename, log15.LogfmtFormat()),
  86. ))
  87. } else {
  88. log.SetHandler(logger.LvlFilterHandler(
  89. logger.LvlInfo,
  90. log15.Must.FileHandler(filename, log15.LogfmtFormat()),
  91. ))
  92. }
  93. }