-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzabbix_db_migration.py
208 lines (170 loc) · 8.07 KB
/
zabbix_db_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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
from pyzabbix import ZabbixAPI, ZabbixAPIException
import json
srczaburl = "http://localhost:8080"
srczabuser = 'Admin'
srczabpass = 'zabbix'
dstzaburl = "http://localhost:9080"
dstzabuser = 'Admin'
dstzabpass = 'zabbix'
"""srczapi = ZabbixAPI(srczaburl)
srczapi.login(srczabuser, srczabpass)
dstzapi = ZabbixAPI(dstzaburl)
dstzapi.login(dstzabuser, dstzabpass)"""
def usergroups_import():
with open('usergroups.json', 'r') as f:
usergroups = json.loads(f.read())
for usergroup in usergroups:
usergroup.pop('usrgrpid')
try:
res = dstzapi.usergroup.create(usergroup)
except ZabbixAPIException as e:
print(str(e))
else:
print('Group {0} created with group id {1}'.format(usergroup['name'], res['usrgrpids'][0]))
def usergroups_export():
usergroups = srczapi.usergroup.get()
with open('usergroups.json', 'w') as f:
json.dump(usergroups, f, indent=4)
def hostgroups_export():
with open('hostgroups.json', 'w') as f:
hostgroups = srczapi.hostgroup.get()
json.dump(hostgroups, f, indent=4)
def hostgroups_import():
with open('hostgroups.json', 'r') as f:
hostgroups = json.loads(f.read())
for hostgroup in hostgroups:
if hostgroup['flags'] == '0':
groupname = hostgroup['name']
try:
res = dstzapi.hostgroup.create(name=groupname)
except ZabbixAPIException as e:
print(str(e))
else:
print('Host Group {0} created with group id {1}'.format(hostgroup['name'], res['groupids'][0]))
def proxies_export_import():
proxies = srczapi.proxy.get(output='extend', selectInterface='extend')
for proxy in proxies:
if proxy['status'] == '6':
del proxy['interface']['interfaceid']
del proxy['interface']['hostid']
try:
res = dstzapi.proxy.create(host=proxy['host'],
status=proxy['status'],
tls_connect=proxy['tls_connect'],
description=proxy['description'],
tls_accept= proxy['tls_accept'],
tls_issuer= proxy['tls_issuer'],
tls_subject=proxy['tls_subject'],
tls_psk_identity=proxy['tls_psk_identity'],
tls_psk=proxy['tls_psk'],
proxy_address=proxy['proxy_address'],
interface=proxy['interface'])
except ZabbixAPIException as e:
print(str(e))
else:
print('Proxy {0} created with id {1}'.format(proxy['host'], res['proxyids'][0]))
def clean_destination():
'''
Drop all hosts and templates
on target zabbix installation
'''
templates = dstzapi.template.get(output='templateid')
if templates:
print(f"[Templates] - {templates}")
templatesids = [templ['templateid'] for templ in templates]
for templateid in templatesids:
res = dstzapi.template.delete(templateid)
print(f"[Deleted] - {res}")
else:
print("[Templates] - No templates to delete!")
hosts = dstzapi.host.get(output='hostid')
if hosts:
print(f"[Hosts] - {hosts}")
hostsids = [h['hostid'] for h in hosts]
for hostid in hostsids:
res = dstzapi.host.delete(hostid)
print(f"[Deleted] - {res}")
else:
print("[Hosts] - No hosts to delete!")
hostgroups = dstzapi.hostgroup.get(output='groupid')
if hostgroups:
print(f"[Hostgroup] - {hostgroups}")
hostgroupids = [hg['groupid'] for hg in hostgroups]
for hostgroupid in hostgroupids:
try:
res = dstzapi.hostgroup.delete(hostgroupid)
print(f"[Deleted] - {res}")
except ZabbixAPIException as e:
print(f"[WARNING] - {e.error['data']}")
pass
else:
print("[Hostgroups] - No hostgroups to delete!")
def configuration_export():
hostgroups = srczapi.hostgroup.get(output='groupid')
groupsids = [hg['groupid'] for hg in hostgroups]
templates = srczapi.template.get(output='templateid')
templatesids = [templ['templateid'] for templ in templates]
mediatypes = srczapi.mediatype.get(output='mediatypeid')
mediatypesids = [media['mediatypeid'] for media in mediatypes]
hosts = srczapi.host.get(output='hostid')
hostsids = [h['hostid'] for h in hosts]
images = srczapi.image.get(output='imageid')
imagesids = [i['imageid'] for i in images]
maps = srczapi.map.get(output='selementid')
mapsids = [m['sysmapid'] for m in maps]
screens = srczapi.screen.get(output='screenid')
screensids = [screen['screenid'] for screen in screens]
valuemaps = srczapi.valuemap.get()
valuemapsids = [val['valuemapid'] for val in valuemaps]
## TODO : Add a condition on API versions (option mediatypes not available before 4.4)
config = srczapi.configuration.export(format='json',
options={'groups': groupsids,
'hosts': hostsids,
'images': imagesids,
'maps': mapsids,
'mediaTypes': mediatypesids,
'screens': screensids,
'templates': templatesids,
'valueMaps': valuemapsids
}
)
with open('config-export.json', 'w') as f:
json.dump(config, f, indent=4)
return config
def configuration_import(config):
## TODO : Proxies must be imported first
res = dstzapi.confimport(confformat='json', source=config,
rules={'hosts':{'createMissing': True, 'updateExisting': True},
'templates':{'createMissing': True, 'updateExisting': True},
'groups':{'createMissing': True},
'images':{'createMissing': True, 'updateExisting': True},
'maps':{'createMissing': True, 'updateExisting': True},
'mediaTypes':{'createMissing': True, 'updateExisting': True},
'screens':{'createMissing': True, 'updateExisting': True},
'valueMaps':{'createMissing': True, 'updateExisting': True},
'applications': {'createMissing': True},
'discoveryRules':{'createMissing': True, 'updateExisting': True},
'graphs': {'createMissing': True, 'updateExisting': True},
'httptests': {'createMissing': True, 'updateExisting': True},
'items': {'createMissing': True, 'updateExisting': True},
'templateLinkage': {'createMissing': True},
'templateScreens': {'createMissing': True, 'updateExisting': True},
'triggers': {'createMissing': True, 'updateExisting': True}})
print(res)
def users_export():
pass
def users_import():
pass
def maintenances_export():
pass
def maintenances_import():
pass
if __name__ == '__main__':
clean_destination()
hostgroups_export()
hostgroups_import()
proxies_export_import()
# configuration_export()
configuration_import(configuration_export())
usergroups_export()
usergroups_import()