monitorcmd.go 11 KB

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