8

I have a visual in grafana. I can manually go to the menu click export and export the time series data in json. This works great. Is there a way I can script that in python?. Is there some api I can hit that will return the json of a visual?

I was googling around and it looks like I can use the api to create dashboards/visuals and administer them but not sure where how to use the api to export the data.

1
  • Have a look at python's request module to make outbound http call. Commented Feb 1, 2019 at 2:42

3 Answers 3

7

Here's a Python script to export then dashboard json, not the presented data. Tested on Python 2.7:

#!/usr/bin/env python

"""Grafana dashboard exporter"""

import json
import os
import requests

HOST = 'http://localhost:3000'
API_KEY = os.environ["grafana_api_key"]

DIR = 'exported-dashboards/'

def main():
    headers = {'Authorization': 'Bearer %s' % (API_KEY,)}
    response = requests.get('%s/api/search?query=&' % (HOST,), headers=headers)
    response.raise_for_status()
    dashboards = response.json()

    if not os.path.exists(DIR):
        os.makedirs(DIR)

    for d in dashboards:
        print ("Saving: " + d['title'])
        response = requests.get('%s/api/dashboards/%s' % (HOST, d['uri']), headers=headers)
        data = response.json()['dashboard']
        dash = json.dumps(data, sort_keys=True, indent=4, separators=(',', ': '))
        name = data['title'].replace(' ', '_').replace('/', '_').replace(':', '').replace('[', '').replace(']', '')
        tmp = open(DIR + name + '.json', 'w')
        tmp.write(dash)
        tmp.write('\n')
        tmp.close()


if __name__ == '__main__':
    main()

Usage: You should first create an API key in Grafana and then run:

grafana_api_key=my-key python export-dash.py

Credit: This is a simplified version of https://github.com/percona/grafana-dashboards/blob/master/misc/export-dash.py

Sign up to request clarification or add additional context in comments.

Comments

0

http://docs.grafana.org/http_api/data_source/#data-source-proxy-calls. Visit your browser console (network tab) and you will see how it works there.

Comments

0

You could also use this Go client https://github.com/netsage-project/grafana-dashboard-manager

Its purpose is not what you are looking for, but it is possible to reuse that code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.