Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

programmatically export grafana dashboard data

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.

like image 321
chowpay Avatar asked Feb 01 '19 00:02

chowpay


People also ask

How do I export data from Grafana dashboard?

Export your Grafana dashboard To export Grafana dashboards: Create a dashboard in a Grafana instance and save it. In the dashboard menu, click Share dashboard to export the dashboard to your computer. On the Export tab, enable to share externally and click Save to file.

Can we export Grafana dashboard?

Dashboards are exported in Amazon Managed Grafana JSON format, and contain everything you need, including layout, variables, styles, data sources, and queries, to import the dashboard at a later time. The export feature is accessed in the share window, which you open by choosing the share button in the dashboard menu.

Can Grafana get data from REST API?

You can use Grafana Infinity datasource for connecting to REST API endpoints. Note: I am the author of the plugin.


1 Answers

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

like image 110
dux2 Avatar answered Sep 21 '22 03:09

dux2