cho*_*pay 5 grafana grafana-api
我在grafana中有一个视觉效果。我可以手动转到菜单单击导出并导出 json 中的时间序列数据。这很好用。有没有办法可以在 python 中编写脚本?是否有一些我可以点击的 api 会返回视觉对象的 json?
我在谷歌上搜索,看起来我可以使用 api 创建仪表板/视觉效果并管理它们,但不确定如何使用 api 导出数据。
这是一个 Python 脚本,用于导出仪表板 json,而不是显示的数据。在 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()
Run Code Online (Sandbox Code Playgroud)
用法:您应该首先在 Grafana 中创建一个 API 密钥,然后运行:
grafana_api_key=my-key python export-dash.py
Run Code Online (Sandbox Code Playgroud)
信用:这是https://github.com/percona/grafana-dashboards/blob/master/misc/export-dash.py的简化版本
| 归档时间: |
|
| 查看次数: |
5508 次 |
| 最近记录: |