blob: bb8c8d4e6817660e396d5b34df6552492b9592e5 [file] [log] [blame]
Zsolt Haraszti9ad45692016-12-12 10:37:54 -08001#!/usr/bin/env python
2
3"""
4Setup grafana with proper datasource and some initial dashboards
5"""
6import os
7from os.path import join as pjoin
8import requests
9from simplejson import dumps
10
11username = 'admin'
12pasword = 'admin'
13url = 'http://localhost:8882'
14headers = {'content-type': 'application/json'}
15
16def setup_grafana():
17
18 # authenticate to grafana
19 session = requests.session()
20 login_post = session.post(pjoin(url, 'login'), data=dumps(dict(
21 user=username,
22 email='',
23 password=pasword
24 )), headers=headers)
25 assert login_post.ok
26
27 # get current list of data sources
28 datasources = session.get(pjoin(url, 'api/datasources')).json()
29
30 # check if we have one called 'voltha'
31 has_voltha = bool([ds for ds in datasources if ds['name'] == 'voltha'])
32
33 # if does not have voltha yet, add it
34 if not has_voltha:
35 res = session.put(pjoin(url, 'api/datasources'), data=dumps(dict(
36 name='voltha',
37 isDefault=True,
38 type='graphite',
39 url='http://localhost:8000',
40 access='proxy',
41 withCredentials=False,
42 user='',
43 password='',
44 )))
45 assert res.ok
46
47 # get dashboards
48 dashboards = session.get(pjoin(url, 'api/search?query=')).json()
49
50 # for now, just print all dashboards as json
51 for dashboard in dashboards:
52 content = session.get(
53 pjoin(url, 'api/dashboards', dashboard['uri'])).json()
54 print '========== Dashboard {} ==========='.format(dashboard['title'])
55 print dumps(content, indent=4)
56
57 print
58
59
60if __name__ == '__main__':
61 setup_grafana()