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.

chowpay
  • 1,515
  • 6
  • 22
  • 44

3 Answers3

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

dux2
  • 1,770
  • 1
  • 21
  • 27
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.

Jan Garaj
  • 25,598
  • 3
  • 38
  • 59
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.

vladimirror
  • 729
  • 12
  • 8