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