/
kibana.py
144 lines (133 loc) · 4.95 KB
/
kibana.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import elasticsearch
from kibana_dashboard_api import Visualization, Dashboard
from kibana_dashboard_api import VisualizationsManager, DashboardsManager
def generate_dashboard(es_conn, sensor_names, df_scored, index_name, timefield='time'):
dashboards = DashboardsManager(es_conn)
dashboard = Dashboard()
dashboard.id = "%s-dashboard" % index_name
dashboard.title = "%s dashboard" % index_name
dashboard.panels = []
dashboard.options = {"darkTheme": True}
dashboard.time_from = "now-15m"
dashboard.refresh_interval_value = 5000
dashboard.search_source = {
"filter": [{
"query": {
"query_string": {
"analyze_wildcard": True,
"query": "*"
}
}
}]
}
visualizations = VisualizationsManager(es_conn)
vis_list = visualizations.get_all() # list all visualizations
panels = []
i = 0
for sensor in sensor_names:
viz_id = "%s-%s" % (index_name, sensor)
# Check if visualization exists
viz = next((v for v in vis_list if v.id == viz_id), None)
if not viz: # If not, create it
viz = Visualization()
viz.id = viz_id
viz.title = "%s-%s" % (index_name, sensor)
viz.search_source = {
"index": index_name,
"query":{
"query_string":{
"analyze_wildcard": True,
"query":"*"
}
},
"filter":[]
}
viz.vis_state = {
"title": "%s-%s" % (index_name, sensor),
"type": "line",
"params": {
"addLegend": True,
"addTimeMarker": True,
"addTooltip": True,
"defaultYExtents": True,
"drawLinesBetweenPoints": True,
"interpolate": "linear",
"radiusRatio": 9,
"scale": "linear",
"setYExtents": False,
"shareYAxis": True,
"showCircles": True,
"smoothLines": True,
"times":[],
"yAxis":{}
},
"aggs": [
{
"id": "1",
"type": "avg",
"schema":"metric",
"params": {
"field": sensor,
"customLabel": sensor.replace('_', ' ')
}
}, {
"id": "2",
"type": "max",
"schema":"radius",
"params": {
"field":"SCORE_%s" % sensor
}
}, {
"id": "3",
"type": "date_histogram",
"schema": "segment",
"params":{
"field": timefield,
"interval": "custom",
"customInterval": "5s",
"min_doc_count": 1,
"extended_bounds": {}
}
}
],
"listeners": {}
}
try:
res = visualizations.add(viz)
assert res['_id'] == viz_id
except elasticsearch.exceptions.ConflictError:
res = visualizations.update(viz)
panel = {
"id": viz_id,
"panelIndex": i,
"row": i,
"col": i,
"size_x": 7,
"size_y": 4,
"type": "visualization"
}
panels.append(panel)
ret = dashboard.add_visualization(viz)
i += 1
# Create the index if it does not exist
if not es_conn.indices.exists(index_name):
index_properties = {"time" : {"type": "date"}}
body = {"mappings": {index_name: {"properties": index_properties}}}
es_conn.indices.create(index=index_name, body=body)
try:
ret = dashboards.add(dashboard)
except elasticsearch.exceptions.ConflictError:
ret = dashboards.update(dashboard)
#pass # Dashboard already exists, let's not overwrite it
es_conn.index(index='.kibana', doc_type="index-pattern", id=index_name,
body={"title": index_name, "timeFieldName": "time"})
kibana_config = es_conn.search(index='.kibana',
sort={'_uid': {'order': 'desc'}},
doc_type='config')
try:
kibana_id = kibana_config['hits']['hits'][0]['_id']
except:
raise # TODO
es_conn.update(index='.kibana', doc_type='config', id=kibana_id,
body={"doc": {"defaultIndex" : index_name}})
return ret