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.

131 lines
4.0 KiB

  1. package main
  2. import (
  3. "errors"
  4. "net"
  5. "net/http"
  6. rpctypes "github.com/tendermint/tendermint/rpc/lib/types"
  7. "github.com/tendermint/tendermint/libs/log"
  8. rpc "github.com/tendermint/tendermint/rpc/lib/server"
  9. "github.com/tendermint/tendermint/tools/tm-monitor/monitor"
  10. )
  11. func startRPC(listenAddr string, m *monitor.Monitor, logger log.Logger) net.Listener {
  12. routes := routes(m)
  13. mux := http.NewServeMux()
  14. wm := rpc.NewWebsocketManager(routes, nil)
  15. mux.HandleFunc("/websocket", wm.WebsocketHandler)
  16. rpc.RegisterRPCFuncs(mux, routes, cdc, logger)
  17. config := rpc.DefaultConfig()
  18. listener, err := rpc.Listen(listenAddr, config)
  19. if err != nil {
  20. panic(err)
  21. }
  22. go rpc.StartHTTPServer(listener, mux, logger, config)
  23. return listener
  24. }
  25. func routes(m *monitor.Monitor) map[string]*rpc.RPCFunc {
  26. return map[string]*rpc.RPCFunc{
  27. "status": rpc.NewRPCFunc(RPCStatus(m), ""),
  28. "status/network": rpc.NewRPCFunc(RPCNetworkStatus(m), ""),
  29. "status/node": rpc.NewRPCFunc(RPCNodeStatus(m), "name"),
  30. "monitor": rpc.NewRPCFunc(RPCMonitor(m), "endpoint"),
  31. "unmonitor": rpc.NewRPCFunc(RPCUnmonitor(m), "endpoint"),
  32. // "start_meter": rpc.NewRPCFunc(network.StartMeter, "chainID,valID,event"),
  33. // "stop_meter": rpc.NewRPCFunc(network.StopMeter, "chainID,valID,event"),
  34. // "meter": rpc.NewRPCFunc(GetMeterResult(network), "chainID,valID,event"),
  35. }
  36. }
  37. // RPCStatus returns common statistics for the network and statistics per node.
  38. func RPCStatus(m *monitor.Monitor) func(*rpctypes.Context) (networkAndNodes, error) {
  39. return func(_ *rpctypes.Context) (networkAndNodes, error) {
  40. return networkAndNodes{m.Network, m.Nodes}, nil
  41. }
  42. }
  43. // RPCNetworkStatus returns common statistics for the network.
  44. func RPCNetworkStatus(m *monitor.Monitor) func(*rpctypes.Context) (*monitor.Network, error) {
  45. return func(_ *rpctypes.Context) (*monitor.Network, error) {
  46. return m.Network, nil
  47. }
  48. }
  49. // RPCNodeStatus returns statistics for the given node.
  50. func RPCNodeStatus(m *monitor.Monitor) func(*rpctypes.Context, string) (*monitor.Node, error) {
  51. return func(_ *rpctypes.Context, name string) (*monitor.Node, error) {
  52. if i, n := m.NodeByName(name); i != -1 {
  53. return n, nil
  54. }
  55. return nil, errors.New("cannot find node with that name")
  56. }
  57. }
  58. // RPCMonitor allows to dynamically add a endpoint to under the monitor. Safe
  59. // to call multiple times.
  60. func RPCMonitor(m *monitor.Monitor) func(*rpctypes.Context, string) (*monitor.Node, error) {
  61. return func(_ *rpctypes.Context, endpoint string) (*monitor.Node, error) {
  62. i, n := m.NodeByName(endpoint)
  63. if i == -1 {
  64. n = monitor.NewNode(endpoint)
  65. if err := m.Monitor(n); err != nil {
  66. return nil, err
  67. }
  68. }
  69. return n, nil
  70. }
  71. }
  72. // RPCUnmonitor removes the given endpoint from under the monitor.
  73. func RPCUnmonitor(m *monitor.Monitor) func(*rpctypes.Context, string) (bool, error) {
  74. return func(_ *rpctypes.Context, endpoint string) (bool, error) {
  75. if i, n := m.NodeByName(endpoint); i != -1 {
  76. m.Unmonitor(n)
  77. return true, nil
  78. }
  79. return false, errors.New("cannot find node with that name")
  80. }
  81. }
  82. // func (tn *TendermintNetwork) StartMeter(chainID, valID, eventID string) error {
  83. // tn.mtx.Lock()
  84. // defer tn.mtx.Unlock()
  85. // val, err := tn.getChainVal(chainID, valID)
  86. // if err != nil {
  87. // return err
  88. // }
  89. // return val.EventMeter().Subscribe(eventID, nil)
  90. // }
  91. // func (tn *TendermintNetwork) StopMeter(chainID, valID, eventID string) error {
  92. // tn.mtx.Lock()
  93. // defer tn.mtx.Unlock()
  94. // val, err := tn.getChainVal(chainID, valID)
  95. // if err != nil {
  96. // return err
  97. // }
  98. // return val.EventMeter().Unsubscribe(eventID)
  99. // }
  100. // func (tn *TendermintNetwork) GetMeter(chainID, valID, eventID string) (*eventmeter.EventMetric, error) {
  101. // tn.mtx.Lock()
  102. // defer tn.mtx.Unlock()
  103. // val, err := tn.getChainVal(chainID, valID)
  104. // if err != nil {
  105. // return nil, err
  106. // }
  107. // return val.EventMeter().GetMetric(eventID)
  108. // }
  109. //--> types
  110. type networkAndNodes struct {
  111. Network *monitor.Network `json:"network"`
  112. Nodes []*monitor.Node `json:"nodes"`
  113. }