programmatically export grafana dashboard data
Asked Answered
A

3

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.

Averroism answered 1/2, 2019 at 0:31 Comment(1)
Have a look at python's request module to make outbound http call.Lely
T
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

Thorazine answered 23/10, 2019 at 13:19 Comment(0)
R
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.

Raindrop answered 1/2, 2019 at 7:32 Comment(0)
C
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.

Chrono answered 4/10, 2021 at 5:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.