forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kibana-migration.py
136 lines (104 loc) · 4.29 KB
/
kibana-migration.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
import yaml
import glob
import argparse
def migration(append_ecs):
print("Start Kibana files migration")
print("Migrate all fields to the ECS fields")
migration_fields = read_migration_fields()
rename_entries(migration_fields)
if append_ecs:
print("Postfix all ids with -ecs")
ids = get_replaceable_ids()
rename_entries(ids)
print("Postfix all titles with ` ECS`")
titles = get_replacable_titles()
rename_entries(titles)
def get_replaceable_ids():
files = get_files()
ids = {}
for file in files:
with open(file, 'r') as f:
objects = yaml.safe_load(f)
for v in objects["objects"]:
# Checks if an id was already migrated, if not adds it to the list
if "-ecs" not in v["id"]:
# Add "{}" around fields to make them more unique and not have false positives
ids['"' + v["id"] + '"'] = '"' + v["id"] + "-ecs" + '"'
# Prefix with / to also modify links
ids['/' + v["id"]] = '/' + v["id"] + "-ecs"
return ids
def read_migration_fields():
migration_fields = {}
migration_yml = "ecs-migration-8x.yml"
with open(migration_yml, 'r') as f:
migration = yaml.safe_load(f)
for k in migration:
if "to" in k and "from" in k:
if "rename" in k and k["rename"] is False:
continue
if k["alias"] == False:
continue
if not isinstance(k["to"], str):
continue
# Add "{}" around fields to make them more unique and not have false positives
migration_fields['"' + k["from"] + '"'] = '"' + k["to"] + '"'
# Some fields exist inside a query / filter where they are followed by :
migration_fields[k["from"] + ':'] = k["to"] + ':'
return migration_fields
def get_replacable_titles():
files = get_files()
titles = {}
for file in files:
with open(file, 'r') as f:
objects = yaml.safe_load(f)
for v in objects["objects"]:
# Add "{}" around titles to make them more unique and not have false positives
if "title" in v["attributes"]:
if "ECS" not in v["attributes"]["title"]:
titles['"' + v["attributes"]["title"] + '"'] = '"' + v["attributes"]["title"] + " ECS" + '"'
if "visState" in v["attributes"] and "title" in v["attributes"]["visState"]:
if "ECS" not in v["attributes"]["visState"]["title"]:
titles['"' + v["attributes"]["visState"]["title"] + '"'] = '"' + \
v["attributes"]["visState"]["title"] + " ECS" + '"'
return titles
def rename_entries(renames):
files = get_files()
for file in files:
print(file)
s = open(file).read()
for k in renames:
s = s.replace(k, renames[k])
f = open(file, 'w')
f.write(s)
f.close()
def get_files():
all_beats = '../*/_meta/kibana/7/dashboard/*.json'
module_beats = '../*/module/*/_meta/kibana/7/dashboard/*.json'
heartbeat = '../heartbeat/monitors/active/*/_meta/kibana/7/dashboard/*.json'
xpack_module_beats = '../x-pack/*/module/*/_meta/kibana/7/dashboard/*.json'
return glob.glob(all_beats) + glob.glob(module_beats) + glob.glob(heartbeat) + glob.glob(xpack_module_beats)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Migrate field names in dashboards')
parser.add_argument('--append-ecs', action='store_true', help='append "-ecs" to the end of all viz identifiers')
args = parser.parse_args()
migration(args.append_ecs)
# There are more id's, do they matter?
# Example:
#
# "series": [
# {
# "axis_position": "right",
# "chart_type": "line",
# "color": "#68BC00",
# "fill": 0.5,
# "formatter": "number",
# "id": "6984af11-4d5d-11e7-aa29-87a97a796de6",
# "label": "In Packetloss",
# "line_width": 1,
# "metrics": [
# {
# "field": "system.network.in.dropped",
# "id": "6984af12-4d5d-11e7-aa29-87a97a796de6",
# "type": "max"
# }
# ],