Skip to content

Instantly share code, notes, and snippets.

@kalinon
Created July 23, 2019 13:52
Show Gist options
  • Save kalinon/bb25571ca6ccfdd09554142baa8760ea to your computer and use it in GitHub Desktop.
Save kalinon/bb25571ca6ccfdd09554142baa8760ea to your computer and use it in GitHub Desktop.
Automating Grafana and adding consistency - test.cr
require "crafana"
data_sources = ["Prometheus AWS", "Prometheus GCP"]
builder = Crafana::Builder.new
dashboard_name = "ElasticSearch Cluster Health Status - DEMO"
builder.add_dashboard(dashboard_name) do |dash|
# Add some tags to the dashboard
dash.tags = ["prometheus", "elasticsearch", "automated"]
# Set the time range
dash.time = Crafana::Time.new("now-12h", "now")
# Loop through each data source,
# making a row and a graph for each
data_sources.each_with_index do |data_source, i|
# add a row for the data source
row_title = "#{data_source} - ES"
dash.add_row(row_title) do |row|
# Add the overall health bar
row.add_single_stat(datasource: data_source) do |panel|
expr = "max(max(elasticsearch_cluster_health_status{color=\"green\"} or up * 0) by (cluster) + max(elasticsearch_cluster_health_status{color=\"yellow\"} * 2 or up * 0) by (cluster) + max(elasticsearch_cluster_health_status{color=\"red\"} * 3 or up * 0) by (cluster) )"
# Set the target
panel.add_target(expr: expr, target_format: "time_series", ref_id: "A") do |target|
target.instant = true
target.interval_factor = 1
end
panel.color_background = true
panel.gauge = Crafana::Gauge.new(100, 0)
panel.max_data_points = 100
panel.thresholds = "2,2.9"
panel.value_name = "current"
# Configure position and size
panel.grid_pos = Crafana::GridPos.new(1, 24, 0, 0)
panel.editable = false
panel.value_font_size = Crafana::Percent.new(50)
panel.value_maps = [
Crafana::ValueMap.new("=", "N/A", "null"),
Crafana::ValueMap.new("=", "GREEN", "1"),
Crafana::ValueMap.new("=", "YELLOW", "2"),
Crafana::ValueMap.new("=", "RED", "3"),
]
end
end
end
end
# Print the dashboard JSON
puts builder.dashboards.first.to_json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment