@@ -47,6 +47,7 @@ class ZabbixVSphere(object):
47
47
"""
48
48
def __init__ (self , options ):
49
49
self .options = options
50
+ self .vsphere_datacenters = []
50
51
self .vsphere_clusters = []
51
52
self .vsphere_hosts = []
52
53
self .vsphere_vms = []
@@ -156,6 +157,83 @@ class ZabbixVSphere(object):
156
157
157
158
return hostgroup ['groupid' ]
158
159
160
+ def import_vsphere_datacenters (self ):
161
+ """
162
+ Import vSphere Datacenters into Zabbix
163
+
164
+ """
165
+ logging .info (
166
+ '[Datacenter@%s] Importing objects to Zabbix' ,
167
+ self .options ['vsphere' ]['hostname' ]
168
+ )
169
+
170
+ zabbix_data = self .zapi .host .get (
171
+ output = 'extend'
172
+ )
173
+ zabbix_hosts = [h ['name' ] for h in zabbix_data ]
174
+
175
+ msg = {
176
+ 'method' : 'datacenter.discover' ,
177
+ 'hostname' : self .options ['vsphere' ]['hostname' ]
178
+ }
179
+ vpoller_data = self ._call_vpoller_task (msg = msg )
180
+ self .vsphere_datacenters = [d ['name' ] for d in vpoller_data ]
181
+
182
+ missing_datacenters = set (self .vsphere_datacenters ) - set (zabbix_hosts )
183
+
184
+ if not missing_hosts :
185
+ logging .info (
186
+ '[Datacenter@%s] Objects are in sync with Zabbix' ,
187
+ self .options ['vsphere' ]['hostname' ]
188
+ )
189
+ return
190
+
191
+ logging .info (
192
+ '[Datacenter@%s] Number of objects to be imported: %d' ,
193
+ self .options ['vsphere' ]['hostname' ],
194
+ len (missing_hosts )
195
+ )
196
+
197
+ # Get hosts options (templates, groups, macros) from the config file
198
+ host_options = self ._get_zabbix_host_options (
199
+ name = 'vsphere_object_datacenter'
200
+ )
201
+ # Add a default interface for the host
202
+ host_options ['interfaces' ] = [
203
+ {
204
+ 'type' : 1 ,
205
+ 'main' : 1 ,
206
+ 'useip' : 1 ,
207
+ 'ip' : '127.0.0.1' ,
208
+ 'dns' : '' ,
209
+ 'port' : '10050'
210
+ }
211
+ ]
212
+
213
+ for host in missing_hosts :
214
+ logging .info (
215
+ "[Datacenter@%s] Creating Zabbix host '%s'" ,
216
+ self .options ['vsphere' ]['hostname' ],
217
+ host
218
+ )
219
+
220
+ params = deepcopy (host_options )
221
+ params ['host' ] = host
222
+
223
+ try :
224
+ result = self .zapi .host .create (params )
225
+ except pyzabbix .ZabbixAPIException as e :
226
+ logging .warning (
227
+ '[Datacenter@%s] Cannot create host in Zabbix: %s' ,
228
+ self .options ['vsphere' ]['hostname' ],
229
+ e
230
+ )
231
+
232
+ logging .info (
233
+ '[Datacenter@%s] Import of objects completed' ,
234
+ self .options ['vsphere' ]['hostname' ]
235
+ )
236
+
159
237
def import_vsphere_clusters (self ):
160
238
"""
161
239
Import vSphere Clusters as Zabbix host groups
@@ -512,6 +590,7 @@ class ZabbixVSphere(object):
512
590
513
591
# Get all vSphere objects in one place for easy comparison
514
592
vsphere_objects = []
593
+ vsphere_objects .extend (self .vsphere_datacenters )
515
594
vsphere_objects .extend (self .vsphere_hosts )
516
595
vsphere_objects .extend (self .vsphere_vms )
517
596
vsphere_objects .extend (self .vsphere_datastores )
@@ -665,6 +744,7 @@ Options:
665
744
666
745
zabbix = ZabbixVSphere (options = options )
667
746
zabbix .connect ()
747
+ zabbix .import_vsphere_datacenters ()
668
748
zabbix .import_vsphere_clusters ()
669
749
zabbix .import_vsphere_hosts ()
670
750
zabbix .import_vsphere_vms ()
0 commit comments