Last active
April 7, 2024 08:35
-
-
Save tennix/2c422f82e349aed57182f899bb484259 to your computer and use it in GitHub Desktop.
Convert Grafana dashboard json to jsonnet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import json | |
from jinja2 import Template | |
# git clone https://github.com/pingcap/tidb-docker-compose | |
# cd tidb-docker-compose | |
# git clone https://github.com/tennix/grafonnet-lib -b table | |
# python dashboard-to-jsonnet.py > pd.jsonnet | |
# jsonnet -J grafonnet-lib pd.jsonnet > config/dashboards/generated-pd.json | |
with open('config/dashboards/pd.json', 'r') as f: | |
data = json.load(f) | |
# Ref https://github.com/grafana/grafonnet-lib for jsonnet template. | |
tmpl = """local grafana = import 'grafonnet/grafana.libsonnet'; | |
local dashboard = grafana.dashboard; | |
local row = grafana.row; | |
local graphPanel = grafana.graphPanel; | |
local tablePanel = grafana.tablePanel; | |
local singlestat = grafana.singlestat; | |
local prometheus = grafana.prometheus; | |
local template = grafana.template; | |
dashboard.new( | |
'PD', | |
editable=true, | |
schemaVersion=14, | |
refresh='1m', | |
time_from='now-30m', | |
tags=['tidb'], | |
) | |
.addTemplate( | |
template.datasource( | |
'datasource', | |
'prometheus', | |
'Prometheus', | |
) | |
) | |
.addTemplate( | |
template.new( | |
'instance', | |
'$datasource', | |
'label_values(pd_cluster_status, instance)', | |
label='instance', | |
refresh='time', | |
) | |
) | |
.addTemplate( | |
template.new( | |
'namespace', | |
'$datasource', | |
'label_values(pd_cluster_status{instance="$instance"}, namespace)', | |
label='namespace', | |
refresh='time', | |
) | |
) | |
{% for row in rows %} | |
.addRow( | |
row.new( | |
"{{ row.title }}", | |
collapse='true', | |
) | |
{% for panel in row.panels %} | |
{% if panel.type == 'singlestat' %} | |
.addPanel( | |
singlestat.new( | |
"{{ panel.title }}", | |
datasource='$datasource', | |
mappingType={{ panel.mappingType }}, | |
rangeMaps={{ panel.rangeMaps | tojson }}, | |
format='{{ panel.format }}', | |
sparklineShow={{ panel.sparkline.show | tojson }}, | |
) | |
{% for target in panel.targets %} | |
.addTarget( | |
prometheus.target( | |
'{{ target.expr }}', | |
format='{{ target.format }}', | |
legendFormat='{{ target.legendFormat }}', | |
) | |
) | |
{% endfor %} | |
) | |
{% elif panel.type == 'graph' %} | |
.addPanel( | |
graphPanel.new( | |
"{{ panel.title }}", | |
datasource='$datasource', | |
{% if panel.legend.rightSide %}legend_rightSide={{ panel.legend.rightSide | tojson }},{% endif %} | |
) | |
{% for target in panel.targets %} | |
.addTarget( | |
prometheus.target( | |
'{{ target.expr }}', | |
format='{{ target.format }}', | |
legendFormat='{{ target.legendFormat }}', | |
) | |
) | |
{% endfor %} | |
) | |
{% elif panel.type == 'table' %} | |
.addPanel( | |
tablePanel.new( | |
"{{ panel.title }}", | |
datasource='$datasource', | |
columns={{ panel.columns | tojson }}, | |
transform='{{ panel.transform }}', | |
styles={{ panel.styles | tojson }}, | |
) | |
{% for target in panel.targets %} | |
.addTarget( | |
prometheus.target( | |
'{{ target.expr }}', | |
format='{{ target.format }}', | |
legendFormat='{{ target.legendFormat }}', | |
) | |
) | |
{% endfor %} | |
) | |
{% endif %} | |
{% endfor %} | |
) | |
{% endfor %} | |
.addRequired('grafana', 'grafana', 'Grafana', '5.0.0') | |
.addRequired('panel', 'graph', 'Graph', '5.0.0') | |
.addRequired('datasource', 'prometheus', 'Prometheus', '5.0.0') | |
.addRequired('panel', 'singlestat', 'Singlestat', '5.0.0') | |
""" | |
template = Template(tmpl) | |
print template.render(rows=data['rows']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/tennix/2c422f82e349aed57182f899bb484259#file-dashbord-to-jsonnet-py-L23-L28 lines could be also read from json file