Zsolt Haraszti | 9ad4569 | 2016-12-12 10:37:54 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | """ |
| 4 | Setup grafana with proper datasource and some initial dashboards |
| 5 | """ |
| 6 | import os |
| 7 | from os.path import join as pjoin |
| 8 | import requests |
| 9 | from simplejson import dumps |
| 10 | |
| 11 | username = 'admin' |
| 12 | pasword = 'admin' |
| 13 | url = 'http://localhost:8882' |
| 14 | headers = {'content-type': 'application/json'} |
| 15 | |
| 16 | def 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 | |
| 60 | if __name__ == '__main__': |
| 61 | setup_grafana() |