Zsolt Haraszti | 9ad4569 | 2016-12-12 10:37:54 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
Zack Williams | 41513bf | 2018-07-07 20:08:35 -0700 | [diff] [blame] | 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
Zsolt Haraszti | 9ad4569 | 2016-12-12 10:37:54 -0800 | [diff] [blame] | 15 | |
| 16 | """ |
| 17 | Setup grafana with proper datasource and some initial dashboards |
| 18 | """ |
| 19 | import os |
| 20 | from os.path import join as pjoin |
| 21 | import requests |
| 22 | from simplejson import dumps |
| 23 | |
| 24 | username = 'admin' |
| 25 | pasword = 'admin' |
| 26 | url = 'http://localhost:8882' |
| 27 | headers = {'content-type': 'application/json'} |
| 28 | |
| 29 | def setup_grafana(): |
| 30 | |
| 31 | # authenticate to grafana |
| 32 | session = requests.session() |
| 33 | login_post = session.post(pjoin(url, 'login'), data=dumps(dict( |
| 34 | user=username, |
| 35 | email='', |
| 36 | password=pasword |
| 37 | )), headers=headers) |
| 38 | assert login_post.ok |
| 39 | |
| 40 | # get current list of data sources |
| 41 | datasources = session.get(pjoin(url, 'api/datasources')).json() |
| 42 | |
| 43 | # check if we have one called 'voltha' |
| 44 | has_voltha = bool([ds for ds in datasources if ds['name'] == 'voltha']) |
| 45 | |
| 46 | # if does not have voltha yet, add it |
| 47 | if not has_voltha: |
| 48 | res = session.put(pjoin(url, 'api/datasources'), data=dumps(dict( |
| 49 | name='voltha', |
| 50 | isDefault=True, |
| 51 | type='graphite', |
| 52 | url='http://localhost:8000', |
| 53 | access='proxy', |
| 54 | withCredentials=False, |
| 55 | user='', |
| 56 | password='', |
| 57 | ))) |
| 58 | assert res.ok |
| 59 | |
| 60 | # get dashboards |
| 61 | dashboards = session.get(pjoin(url, 'api/search?query=')).json() |
| 62 | |
| 63 | # for now, just print all dashboards as json |
| 64 | for dashboard in dashboards: |
| 65 | content = session.get( |
| 66 | pjoin(url, 'api/dashboards', dashboard['uri'])).json() |
| 67 | print '========== Dashboard {} ==========='.format(dashboard['title']) |
| 68 | print dumps(content, indent=4) |
| 69 | |
| 70 | print |
| 71 | |
| 72 | |
| 73 | if __name__ == '__main__': |
| 74 | setup_grafana() |