monitorcmd.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. package main
  2. import (
  3. "fmt"
  4. "math"
  5. "reflect"
  6. "runtime"
  7. "sort"
  8. "strings"
  9. "time"
  10. "github.com/codegangsta/cli"
  11. "github.com/ethereum/go-ethereum/cmd/utils"
  12. "github.com/ethereum/go-ethereum/common"
  13. "github.com/ethereum/go-ethereum/rpc"
  14. "github.com/ethereum/go-ethereum/rpc/codec"
  15. "github.com/ethereum/go-ethereum/rpc/comms"
  16. "github.com/gizak/termui"
  17. )
  18. var (
  19. monitorCommandAttachFlag = cli.StringFlag{
  20. Name: "attach",
  21. Value: "ipc:" + common.DefaultIpcPath(),
  22. Usage: "API endpoint to attach to",
  23. }
  24. monitorCommandRowsFlag = cli.IntFlag{
  25. Name: "rows",
  26. Value: 5,
  27. Usage: "Maximum rows in the chart grid",
  28. }
  29. monitorCommandRefreshFlag = cli.IntFlag{
  30. Name: "refresh",
  31. Value: 3,
  32. Usage: "Refresh interval in seconds",
  33. }
  34. monitorCommand = cli.Command{
  35. Action: monitor,
  36. Name: "monitor",
  37. Usage: `Geth Monitor: node metrics monitoring and visualization`,
  38. Description: `
  39. The Geth monitor is a tool to collect and visualize various internal metrics
  40. gathered by the node, supporting different chart types as well as the capacity
  41. to display multiple metrics simultaneously.
  42. `,
  43. Flags: []cli.Flag{
  44. monitorCommandAttachFlag,
  45. monitorCommandRowsFlag,
  46. monitorCommandRefreshFlag,
  47. },
  48. }
  49. )
  50. // monitor starts a terminal UI based monitoring tool for the requested metrics.
  51. func monitor(ctx *cli.Context) {
  52. var (
  53. client comms.EthereumClient
  54. err error
  55. )
  56. // Attach to an Ethereum node over IPC or RPC
  57. endpoint := ctx.String(monitorCommandAttachFlag.Name)
  58. if client, err = comms.ClientFromEndpoint(endpoint, codec.JSON); err != nil {
  59. utils.Fatalf("Unable to attach to geth node: %v", err)
  60. }
  61. defer client.Close()
  62. xeth := rpc.NewXeth(client)
  63. // Retrieve all the available metrics and resolve the user pattens
  64. metrics, err := retrieveMetrics(xeth)
  65. if err != nil {
  66. utils.Fatalf("Failed to retrieve system metrics: %v", err)
  67. }
  68. monitored := resolveMetrics(metrics, ctx.Args())
  69. if len(monitored) == 0 {
  70. list := expandMetrics(metrics, "")
  71. sort.Strings(list)
  72. if len(list) > 0 {
  73. utils.Fatalf("No metrics specified.\n\nAvailable:\n - %s", strings.Join(list, "\n - "))
  74. } else {
  75. utils.Fatalf("No metrics collected by geth (--%s).\n", utils.MetricsEnabledFlag.Name)
  76. }
  77. }
  78. sort.Strings(monitored)
  79. if cols := len(monitored) / ctx.Int(monitorCommandRowsFlag.Name); cols > 6 {
  80. utils.Fatalf("Requested metrics (%d) spans more that 6 columns:\n - %s", len(monitored), strings.Join(monitored, "\n - "))
  81. }
  82. // Create and configure the chart UI defaults
  83. if err := termui.Init(); err != nil {
  84. utils.Fatalf("Unable to initialize terminal UI: %v", err)
  85. }
  86. defer termui.Close()
  87. termui.UseTheme("helloworld")
  88. rows := len(monitored)
  89. if max := ctx.Int(monitorCommandRowsFlag.Name); rows > max {
  90. rows = max
  91. }
  92. cols := (len(monitored) + rows - 1) / rows
  93. for i := 0; i < rows; i++ {
  94. termui.Body.AddRows(termui.NewRow())
  95. }
  96. // Create each individual data chart
  97. footer := termui.NewPar("")
  98. footer.HasBorder = true
  99. footer.Height = 3
  100. charts := make([]*termui.LineChart, len(monitored))
  101. units := make([]int, len(monitored))
  102. data := make([][]float64, len(monitored))
  103. for i := 0; i < len(monitored); i++ {
  104. charts[i] = createChart((termui.TermHeight() - footer.Height) / rows)
  105. row := termui.Body.Rows[i%rows]
  106. row.Cols = append(row.Cols, termui.NewCol(12/cols, 0, charts[i]))
  107. }
  108. termui.Body.AddRows(termui.NewRow(termui.NewCol(12, 0, footer)))
  109. refreshCharts(xeth, monitored, data, units, charts, ctx, footer)
  110. termui.Body.Align()
  111. termui.Render(termui.Body)
  112. // Watch for various system events, and periodically refresh the charts
  113. refresh := time.Tick(time.Duration(ctx.Int(monitorCommandRefreshFlag.Name)) * time.Second)
  114. for {
  115. select {
  116. case event := <-termui.EventCh():
  117. if event.Type == termui.EventKey && event.Key == termui.KeyCtrlC {
  118. return
  119. }
  120. if event.Type == termui.EventResize {
  121. termui.Body.Width = termui.TermWidth()
  122. for _, chart := range charts {
  123. chart.Height = (termui.TermHeight() - footer.Height) / rows
  124. }
  125. termui.Body.Align()
  126. termui.Render(termui.Body)
  127. }
  128. case <-refresh:
  129. if refreshCharts(xeth, monitored, data, units, charts, ctx, footer) {
  130. termui.Body.Align()
  131. }
  132. termui.Render(termui.Body)
  133. }
  134. }
  135. }
  136. // retrieveMetrics contacts the attached geth node and retrieves the entire set
  137. // of collected system metrics.
  138. func retrieveMetrics(xeth *rpc.Xeth) (map[string]interface{}, error) {
  139. return xeth.Call("debug_metrics", []interface{}{true})
  140. }
  141. // resolveMetrics takes a list of input metric patterns, and resolves each to one
  142. // or more canonical metric names.
  143. func resolveMetrics(metrics map[string]interface{}, patterns []string) []string {
  144. res := []string{}
  145. for _, pattern := range patterns {
  146. res = append(res, resolveMetric(metrics, pattern, "")...)
  147. }
  148. return res
  149. }
  150. // resolveMetrics takes a single of input metric pattern, and resolves it to one
  151. // or more canonical metric names.
  152. func resolveMetric(metrics map[string]interface{}, pattern string, path string) []string {
  153. results := []string{}
  154. // If a nested metric was requested, recurse optionally branching (via comma)
  155. parts := strings.SplitN(pattern, "/", 2)
  156. if len(parts) > 1 {
  157. for _, variation := range strings.Split(parts[0], ",") {
  158. if submetrics, ok := metrics[variation].(map[string]interface{}); !ok {
  159. utils.Fatalf("Failed to retrieve system metrics: %s", path+variation)
  160. return nil
  161. } else {
  162. results = append(results, resolveMetric(submetrics, parts[1], path+variation+"/")...)
  163. }
  164. }
  165. return results
  166. }
  167. // Depending what the last link is, return or expand
  168. for _, variation := range strings.Split(pattern, ",") {
  169. switch metric := metrics[variation].(type) {
  170. case float64:
  171. // Final metric value found, return as singleton
  172. results = append(results, path+variation)
  173. case map[string]interface{}:
  174. results = append(results, expandMetrics(metric, path+variation+"/")...)
  175. default:
  176. utils.Fatalf("Metric pattern resolved to unexpected type: %v", reflect.TypeOf(metric))
  177. return nil
  178. }
  179. }
  180. return results
  181. }
  182. // expandMetrics expands the entire tree of metrics into a flat list of paths.
  183. func expandMetrics(metrics map[string]interface{}, path string) []string {
  184. // Iterate over all fields and expand individually
  185. list := []string{}
  186. for name, metric := range metrics {
  187. switch metric := metric.(type) {
  188. case float64:
  189. // Final metric value found, append to list
  190. list = append(list, path+name)
  191. case map[string]interface{}:
  192. // Tree of metrics found, expand recursively
  193. list = append(list, expandMetrics(metric, path+name+"/")...)
  194. default:
  195. utils.Fatalf("Metric pattern %s resolved to unexpected type: %v", path+name, reflect.TypeOf(metric))
  196. return nil
  197. }
  198. }
  199. return list
  200. }
  201. // fetchMetric iterates over the metrics map and retrieves a specific one.
  202. func fetchMetric(metrics map[string]interface{}, metric string) float64 {
  203. parts, found := strings.Split(metric, "/"), true
  204. for _, part := range parts[:len(parts)-1] {
  205. metrics, found = metrics[part].(map[string]interface{})
  206. if !found {
  207. return 0
  208. }
  209. }
  210. if v, ok := metrics[parts[len(parts)-1]].(float64); ok {
  211. return v
  212. }
  213. return 0
  214. }
  215. // refreshCharts retrieves a next batch of metrics, and inserts all the new
  216. // values into the active datasets and charts
  217. func refreshCharts(xeth *rpc.Xeth, metrics []string, data [][]float64, units []int, charts []*termui.LineChart, ctx *cli.Context, footer *termui.Par) (realign bool) {
  218. values, err := retrieveMetrics(xeth)
  219. for i, metric := range metrics {
  220. if len(data) < 512 {
  221. data[i] = append([]float64{fetchMetric(values, metric)}, data[i]...)
  222. } else {
  223. data[i] = append([]float64{fetchMetric(values, metric)}, data[i][:len(data[i])-1]...)
  224. }
  225. if updateChart(metric, data[i], &units[i], charts[i], err) {
  226. realign = true
  227. }
  228. }
  229. updateFooter(ctx, err, footer)
  230. return
  231. }
  232. // updateChart inserts a dataset into a line chart, scaling appropriately as to
  233. // not display weird labels, also updating the chart label accordingly.
  234. func updateChart(metric string, data []float64, base *int, chart *termui.LineChart, err error) (realign bool) {
  235. dataUnits := []string{"", "K", "M", "G", "T", "E"}
  236. timeUnits := []string{"ns", "µs", "ms", "s", "ks", "ms"}
  237. colors := []termui.Attribute{termui.ColorBlue, termui.ColorCyan, termui.ColorGreen, termui.ColorYellow, termui.ColorRed, termui.ColorRed}
  238. // Extract only part of the data that's actually visible
  239. if chart.Width*2 < len(data) {
  240. data = data[:chart.Width*2]
  241. }
  242. // Find the maximum value and scale under 1K
  243. high := 0.0
  244. if len(data) > 0 {
  245. high = data[0]
  246. for _, value := range data[1:] {
  247. high = math.Max(high, value)
  248. }
  249. }
  250. unit, scale := 0, 1.0
  251. for high >= 1000 {
  252. high, unit, scale = high/1000, unit+1, scale*1000
  253. }
  254. // If the unit changes, re-create the chart (hack to set max height...)
  255. if unit != *base {
  256. realign, *base, *chart = true, unit, *createChart(chart.Height)
  257. }
  258. // Update the chart's data points with the scaled values
  259. if cap(chart.Data) < len(data) {
  260. chart.Data = make([]float64, len(data))
  261. }
  262. chart.Data = chart.Data[:len(data)]
  263. for i, value := range data {
  264. chart.Data[i] = value / scale
  265. }
  266. // Update the chart's label with the scale units
  267. units := dataUnits
  268. if strings.Contains(metric, "/Percentiles/") || strings.Contains(metric, "/pauses/") || strings.Contains(metric, "/time/") {
  269. units = timeUnits
  270. }
  271. chart.Border.Label = metric
  272. if len(units[unit]) > 0 {
  273. chart.Border.Label += " [" + units[unit] + "]"
  274. }
  275. chart.LineColor = colors[unit] | termui.AttrBold
  276. if err != nil {
  277. chart.LineColor = termui.ColorRed | termui.AttrBold
  278. }
  279. return
  280. }
  281. // createChart creates an empty line chart with the default configs.
  282. func createChart(height int) *termui.LineChart {
  283. chart := termui.NewLineChart()
  284. if runtime.GOOS == "windows" {
  285. chart.Mode = "dot"
  286. }
  287. chart.DataLabels = []string{""}
  288. chart.Height = height
  289. chart.AxesColor = termui.ColorWhite
  290. chart.PaddingBottom = -2
  291. chart.Border.LabelFgColor = chart.Border.FgColor | termui.AttrBold
  292. chart.Border.FgColor = chart.Border.BgColor
  293. return chart
  294. }
  295. // updateFooter updates the footer contents based on any encountered errors.
  296. func updateFooter(ctx *cli.Context, err error, footer *termui.Par) {
  297. // Generate the basic footer
  298. refresh := time.Duration(ctx.Int(monitorCommandRefreshFlag.Name)) * time.Second
  299. footer.Text = fmt.Sprintf("Press Ctrl+C to quit. Refresh interval: %v.", refresh)
  300. footer.TextFgColor = termui.Theme().ParTextFg | termui.AttrBold
  301. // Append any encountered errors
  302. if err != nil {
  303. footer.Text = fmt.Sprintf("Error: %v.", err)
  304. footer.TextFgColor = termui.ColorRed | termui.AttrBold
  305. }
  306. }