blob: 5fa8f3dc27b31a1edf48c16cbd4127edbcadb4df [file] [log] [blame]
Zsolt Haraszti9ad45692016-12-12 10:37:54 -08001#!/usr/bin/env python
Zack Williams41513bf2018-07-07 20:08:35 -07002# 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 Haraszti9ad45692016-12-12 10:37:54 -080015
16"""
17Setup grafana with proper datasource and some initial dashboards
18"""
19import os
20from os.path import join as pjoin
21import requests
22from simplejson import dumps
23
24username = 'admin'
25pasword = 'admin'
26url = 'http://localhost:8882'
27headers = {'content-type': 'application/json'}
28
29def 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
73if __name__ == '__main__':
74 setup_grafana()