2 # -*- coding: utf-8 -*-
4 # @License EPL-1.0 <http://spdx.org/licenses/EPL-1.0>
5 ##############################################################################
6 # Copyright (c) 2017 Raghuram Vadapalli, Jaspreet Singh and others.
8 # All rights reserved. This program and the accompanying materials
9 # are made available under the terms of the Eclipse Public License v1.0
10 # which accompanies this distribution, and is available at
11 # http://www.eclipse.org/legal/epl-v10.html
12 ##############################################################################
15 This script is used to parse dashboard config files, construct
16 JSON BODY and push it to ELK DB.
18 Usage: python push_dashboard.py host:port
29 from elasticsearch import Elasticsearch, RequestsHttpConnection, exceptions
33 import generate_visState as vis_gen
34 import generate_uiStateJSON as uiStateJSON_gen
35 import generate_dashVis as dash_gen
36 import generate_searchSourceJSON as searchSourceJSON_gen
37 import data_generate as data_gen
41 print(json.dumps(x, indent=6, sort_keys=True))
44 # ELK DB host and port to be passed as ':' separated argument
46 if ':' in sys.argv[1]:
47 ELK_DB_HOST = sys.argv[1].split(':')[0]
48 ELK_DB_PORT = sys.argv[1].split(':')[1]
50 print('Usage: python push_to_elk.py host:port')
51 print('Unable to publish data to ELK. Exiting.')
56 hosts=[{'host': ELK_DB_HOST, 'port': int(ELK_DB_PORT)}],
58 connection_class=RequestsHttpConnection
60 except Exception as e:
61 print('Unexpected Error Occurred. Exiting')
67 # Function to convert JSON object to string.
68 # Python puts 'true' as 'True' etc. which need handling.
71 def JSONToString(jobj):
73 retval = retval.replace('\'', '"')
74 retval = retval.replace(': ', ':')
75 retval = retval.replace(', ', ',')
76 retval = retval.replace('True', 'true')
77 retval = retval.replace('False', 'false')
78 retval = retval.replace('None', 'null')
82 # Clear .kibana index before pushing visualizations
85 res = es.indices.delete(index=index)
86 except Exception as e:
89 print('Unable to push data to ElasticSearch')
92 # Create and push index-pattern to be used by visualizations
94 TEST_DATA_INDEX = 'opendaylight-test'
96 INDEX_PATTERN_BODY = {
97 "type": "index-pattern",
99 "timeFieldName": "@timestamp",
100 "title": TEST_DATA_INDEX
105 KIBANA_CONFIG = {'config': {
106 'defaultIndex': 'pattern-for-{}'.format(TEST_DATA_INDEX),
107 'timepicker:timeDefaults': '{\n "from": "now-5y",\n \
108 "to": "now",\n "mode": "quick"\n}',
109 'xPackMonitoring:showBanner': False},
113 res = es.index(index='.kibana', doc_type='doc',
114 id='config:6.2.4', body=KIBANA_CONFIG)
119 ES_ID = 'index-pattern:pattern-for-{}'.format(
121 res = es.index(index=index, doc_type='doc',
122 id=ES_ID, body=INDEX_PATTERN_BODY)
123 p(json.dumps(INDEX_PATTERN_BODY, indent=4))
124 print(json.dumps(res, indent=4))
125 except Exception as e:
128 print('Unable to push data to ElasticSearch')
131 viz_config_path = glob.glob('**/dashboard/viz_config.yaml')[0]
133 print('Visualization template file not found!')
137 dash_config_path = glob.glob('**/dashboard/dash_config.yaml')[0]
139 print('Dashboard configuration file not found!')
142 with open(dash_config_path, 'r') as f:
143 dash_config = yaml.safe_load(f)
145 with open(viz_config_path, 'r') as f:
146 viz_config = yaml.safe_load(f)
149 # Create and push visualizations
150 for dashboard_id, dashboard_content in dash_config.items():
152 for _, i in dash_config[dashboard_id]['viz'].items():
153 intermediate_format, visState = vis_gen.generate(
154 i, viz_config[i['viz-template']])
156 searchSourceJSON = searchSourceJSON_gen.generate(
157 i, viz_config[i['viz-template']],
158 intermediate_format['index_pattern'])
160 uiStateJSON = uiStateJSON_gen.generate(
161 i, viz_config[i['viz-template']])
163 # p(intermediate_format)
166 # Template for visualization template
168 'type': 'visualization',
175 "kibanaSavedObjectMeta": {
176 "searchSourceJSON": None
181 VIZ_BODY['visualization']['title'] = intermediate_format['title']
182 VIZ_BODY['visualization']['visState'] = JSONToString(visState)
183 VIZ_BODY['visualization']['uiStateJSON'] = JSONToString(uiStateJSON)
184 VIZ_BODY['visualization']['description'] = intermediate_format['desc']
185 VIZ_BODY['visualization']['kibanaSavedObjectMeta']['searchSourceJSON']\
190 # Pushing visualization to Kibana
192 ES_ID = 'visualization:{}'.format(i['id'])
193 res = es.index(index=index, doc_type='doc', id=ES_ID, body=VIZ_BODY)
194 print(json.dumps(res, indent=4))
196 # Create and push dashboards
198 # Template for dashboard body in Kibana
205 'optionsJSON': '{\"darkTheme\":false,\
206 \"hidePanelTitles\":false,\"useMargins\":true}',
208 'kibanaSavedObjectMeta': {
209 'searchSourceJSON': '{\"query\":{\"language\":\"lucene\", \
211 \"filter\":[],\"highlightAll\" \
212 :true,\"version\":true}'
217 DASH_BODY['dashboard']['title'] = dashboard_content['title']
218 DASH_BODY['dashboard']['description'] = dashboard_content['desc']
219 DASH_BODY['dashboard']['panelsJSON'] = JSONToString(
220 dash_gen.generate(dashboard_content['viz']))
223 # Pushing dashboard to kibana
225 ES_ID = 'dashboard:{}'.format(dashboard_content['id'])
226 res = es.index(index=index, doc_type='doc', id=ES_ID, body=DASH_BODY)
227 print(json.dumps(res, indent=4))