Skip to content

Commit 31bfd18

Browse files
committedFeb 17, 2015
zabbix-vsphere-import: Import vim.Datacenter objects as Zabbix hosts as well
1 parent e20ec89 commit 31bfd18

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
 

‎extra/zabbix/vsphere-import/zabbix-vsphere-import

+80
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class ZabbixVSphere(object):
4747
"""
4848
def __init__(self, options):
4949
self.options = options
50+
self.vsphere_datacenters = []
5051
self.vsphere_clusters = []
5152
self.vsphere_hosts = []
5253
self.vsphere_vms = []
@@ -156,6 +157,83 @@ class ZabbixVSphere(object):
156157

157158
return hostgroup['groupid']
158159

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+
159237
def import_vsphere_clusters(self):
160238
"""
161239
Import vSphere Clusters as Zabbix host groups
@@ -512,6 +590,7 @@ class ZabbixVSphere(object):
512590

513591
# Get all vSphere objects in one place for easy comparison
514592
vsphere_objects = []
593+
vsphere_objects.extend(self.vsphere_datacenters)
515594
vsphere_objects.extend(self.vsphere_hosts)
516595
vsphere_objects.extend(self.vsphere_vms)
517596
vsphere_objects.extend(self.vsphere_datastores)
@@ -665,6 +744,7 @@ Options:
665744

666745
zabbix = ZabbixVSphere(options=options)
667746
zabbix.connect()
747+
zabbix.import_vsphere_datacenters()
668748
zabbix.import_vsphere_clusters()
669749
zabbix.import_vsphere_hosts()
670750
zabbix.import_vsphere_vms()

0 commit comments

Comments
 (0)
Please sign in to comment.