Chinaunix首页 | 论坛 | 博客
  • 博客访问: 768339
  • 博文数量: 187
  • 博客积分: 4939
  • 博客等级: 上校
  • 技术积分: 2075
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-08 17:15
文章分类

全部博文(187)

文章存档

2018年(1)

2017年(3)

2016年(11)

2015年(42)

2014年(21)

2013年(9)

2012年(18)

2011年(46)

2010年(35)

分类: 系统运维

2017-01-17 21:36:06

  grafana的官方文档写的太简单,要自动化创建一个metrics,经过反复测试,下面一个例子就可以了。

  1. {
  2.   "Dashboard": {
  3.     "title": "gorouter",
  4.     "editable": true,
  5.     "rows": [
  6.       {
  7.         "editable": true,
  8.         "height": "250px",
  9.         "panels": [
  10.           {
  11.             "id": 1,
  12.             "title": "your-metrics-name",
  13.             "datasource": "prod-opentsdb",
  14.             "lines": true,
  15.             "targets": [
  16.               {
  17.                 "aggregator": "sum",
  18.                 "downsampleAggregator": "avg",
  19.                 "downsampleInterval": "5m",
  20.                 "metric": "opentsdb.nozzle.gorouter.latency.uaa",
  21.                 "tags": {
  22.                   "deployment": "cf-cfapps-io2",
  23.                   "job": "router_z1"
  24.                 }
  25.               }
  26.             ],
  27.             "type": "graph",
  28.             "y_formats": [
  29.               "short",
  30.               "short"
  31.             ]
  32.           }
  33.         ]
  34.       }
  35.     ]
  36.   },
  37.   "overwrite": true
  38. }
    现在自动来实现创建metrics,无非是自动创建上述json.
#metrics/test.csv 格式如下
opentsdb.nozzle.gorouter.memoryStats.lastGCPauseTimeNS,ns, router_z1|router_z2

  1. require 'rest-client'
  2. require 'json'
  3. require 'pp'
  4. $deployments = {
  5. gorouter: 'cf-cfapps-io2',
  6. uaa: 'cf-cfapps-io2',
  7. routing_api: 'cf-cfapps-io2-routing',
  8. tcp_emitter: 'cf-cfapps-io2-routing',
  9. syslog_drain_binder: 'cf-cfapps-io2',
  10. loggregatortrafficcontroller: 'cf-cfapps-io2',
  11. cc: 'cf-cfapps-io2',
  12. etcd: 'cf-cfapps-io2',
  13. dopplerserver: 'cf-cfapps-io2'
  14. }
  15. def delete_dashboard(slug)
  16. url = "http://grafanaxxx.com/api/dashboards/db/#{slug}"
  17. begin
  18. r = RestClient.delete(url, {
  19. :content_type => 'application/json',
  20. :Authorization => 'Bearer xxxxxxxx'
  21. })
  22. rescue Exception => e
  23. puts "error: #{e.response}"
  24. end
  25. puts "deleted: #{r}"
  26. end
  27. def create_dashboard(data)
  28. url = 'http://grafanaxxx.com/api/dashboards/db'
  29. begin
  30. r = RestClient.post url, data.to_json, {
  31. :content_type => 'application/json',
  32. :Authorization => 'Bearer xxxxxxxx'
  33. }
  34. rescue Exception => e
  35. puts "error:#{e.response}"
  36. end
  37. puts r
  38. end
  39. def build_json(name, data)
  40. template = {
  41. Dashboard: {
  42. title: '',
  43. editable: true,
  44. rows: []
  45. },
  46. overwrite: true
  47. }
  48. template[:Dashboard][:title] = name
  49. template[:Dashboard][:rows].clear
  50. data.each_with_index do |row, index|
  51. row.chop!
  52. rows_content = {
  53. editable: true,
  54. height: '250px',
  55. panels: []
  56. }
  57. row_data = row.split(',', 3)
  58. targets = []
  59. if row_data[2] == 'none'
  60. targets << {
  61. aggregator: 'sum',
  62. downsampleAggregator: 'avg',
  63. downsampleInterval: '5m',
  64. metric: row_data[0],
  65. tags: {deployment: $deployments.fetch(name.downcase.to_sym, 'cf-cfapps-io2-diego')}
  66. }
  67. else
  68. jobs = row_data[2].split('|')
  69. jobs.each do |job|
  70. targets << {
  71. aggregator: 'sum',
  72. downsampleAggregator: 'avg',
  73. downsampleInterval: '5m',
  74. metric: row_data[0],
  75. tags: {deployment: $deployments.fetch(name.downcase.to_sym, 'cf-cfapps-io2-diego'), job: job}
  76. }
  77. end
  78. end
  79. rows_content[:panels] << {
  80. id: index + 1,
  81. title: row_data[0].downcase,
  82. datasource: 'prod-opentsdb',
  83. lines: true,
  84. targets: targets,
  85. type: 'graph',
  86. y_formats: [row_data[1], 'short'],
  87. timeFrom: '48h',
  88. timeShift: '0h',
  89. }
  90. template[:Dashboard][:rows] << rows_content
  91. end
  92. template
  93. end
  94. def load_metrics
  95. files = Dir.glob('metrics/*.csv')
  96. files.each do |file|
  97. dashboard_name = File.basename(file, '.csv')
  98. delete_dashboard dashboard_name
  99. rows = File.readlines file
  100. data = build_json(dashboard_name, rows)
  101. create_dashboard data
  102. end
  103. end
  104. load_metrics

阅读(5269) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册