- {
- "Dashboard": {
- "title": "gorouter",
- "editable": true,
- "rows": [
- {
- "editable": true,
- "height": "250px",
- "panels": [
- {
- "id": 1,
- "title": "your-metrics-name",
- "datasource": "prod-opentsdb",
- "lines": true,
- "targets": [
- {
- "aggregator": "sum",
- "downsampleAggregator": "avg",
- "downsampleInterval": "5m",
- "metric": "opentsdb.nozzle.gorouter.latency.uaa",
- "tags": {
- "deployment": "cf-cfapps-io2",
- "job": "router_z1"
- }
- }
- ],
- "type": "graph",
- "y_formats": [
- "short",
- "short"
- ]
- }
- ]
- }
- ]
- },
- "overwrite": true
- }
#metrics/test.csv 格式如下
opentsdb.nozzle.gorouter.memoryStats.lastGCPauseTimeNS,ns, router_z1|router_z2
- require 'rest-client'
- require 'json'
- require 'pp'
- $deployments = {
- gorouter: 'cf-cfapps-io2',
- uaa: 'cf-cfapps-io2',
- routing_api: 'cf-cfapps-io2-routing',
- tcp_emitter: 'cf-cfapps-io2-routing',
- syslog_drain_binder: 'cf-cfapps-io2',
- loggregatortrafficcontroller: 'cf-cfapps-io2',
- cc: 'cf-cfapps-io2',
- etcd: 'cf-cfapps-io2',
- dopplerserver: 'cf-cfapps-io2'
- }
- def delete_dashboard(slug)
- url = "http://grafanaxxx.com/api/dashboards/db/#{slug}"
- begin
- r = RestClient.delete(url, {
- :content_type => 'application/json',
- :Authorization => 'Bearer xxxxxxxx'
- })
- rescue Exception => e
- puts "error: #{e.response}"
- end
- puts "deleted: #{r}"
- end
- def create_dashboard(data)
- url = 'http://grafanaxxx.com/api/dashboards/db'
- begin
- r = RestClient.post url, data.to_json, {
- :content_type => 'application/json',
- :Authorization => 'Bearer xxxxxxxx'
- }
- rescue Exception => e
- puts "error:#{e.response}"
- end
- puts r
- end
- def build_json(name, data)
- template = {
- Dashboard: {
- title: '',
- editable: true,
- rows: []
- },
- overwrite: true
- }
- template[:Dashboard][:title] = name
- template[:Dashboard][:rows].clear
- data.each_with_index do |row, index|
- row.chop!
- rows_content = {
- editable: true,
- height: '250px',
- panels: []
- }
- row_data = row.split(',', 3)
- targets = []
- if row_data[2] == 'none'
- targets << {
- aggregator: 'sum',
- downsampleAggregator: 'avg',
- downsampleInterval: '5m',
- metric: row_data[0],
- tags: {deployment: $deployments.fetch(name.downcase.to_sym, 'cf-cfapps-io2-diego')}
- }
- else
- jobs = row_data[2].split('|')
- jobs.each do |job|
- targets << {
- aggregator: 'sum',
- downsampleAggregator: 'avg',
- downsampleInterval: '5m',
- metric: row_data[0],
- tags: {deployment: $deployments.fetch(name.downcase.to_sym, 'cf-cfapps-io2-diego'), job: job}
- }
- end
- end
- rows_content[:panels] << {
- id: index + 1,
- title: row_data[0].downcase,
- datasource: 'prod-opentsdb',
- lines: true,
- targets: targets,
- type: 'graph',
- y_formats: [row_data[1], 'short'],
- timeFrom: '48h',
- timeShift: '0h',
- }
- template[:Dashboard][:rows] << rows_content
- end
- template
- end
- def load_metrics
- files = Dir.glob('metrics/*.csv')
- files.each do |file|
- dashboard_name = File.basename(file, '.csv')
- delete_dashboard dashboard_name
- rows = File.readlines file
- data = build_json(dashboard_name, rows)
- create_dashboard data
- end
- end
- load_metrics