Skip to content

Instantly share code, notes, and snippets.

@kkashyap1707
Forked from al4/setDatasource.py
Created June 3, 2020 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkashyap1707/4b616e3956d45297b1a6c56dd15bc553 to your computer and use it in GitHub Desktop.
Save kkashyap1707/4b616e3956d45297b1a6c56dd15bc553 to your computer and use it in GitHub Desktop.
Set the datasource on grafana dashboards
#!/usr/bin/env python
import sqlite3
import json
"""
Quick quick script to set the datasource for grafana dashboards
"""
OLD_DATASOURCE = 'null'
NEW_DATASOURCE = 'graphite'
SQLITE_DB = '/opt/grafana/data/grafana.db'
conn = None
try:
conn = sqlite3.connect(SQLITE_DB)
cur = conn.cursor()
cur.execute('SELECT id, data FROM dashboard;')
data = cur.fetchall()
except:
raise
for dash_id, dash_data in data:
dash = json.loads(dash_data)
for row in dash['rows']:
for panel in row['panels']:
try:
datasource = panel['datasource']
except KeyError:
print("Skipping dashboard: {}, row: {}, panel: {}".format(
dash['title'], row['title'], panel['title']))
if not datasource or datasource == OLD_DATASOURCE:
print("Updating dashboard: {}, row: {}, panel: {}".format(
dash['title'], row['title'], panel['title']))
panel['datasource'] = NEW_DATASOURCE
cur.execute("UPDATE dashboard SET data=? WHERE id=?", (json.dumps(dash), dash_id))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment