From 2fb7f190c6a54a454397f8d9a368b8633a4d2580 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Nikolov Date: Thu, 21 Aug 2014 14:44:39 +0300 Subject: [PATCH] zabbix-vsphere-import: Add the VMs to their respective cluster/hostgroup in Zabbix --- src/zabbix/zabbix-vsphere-import | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/zabbix/zabbix-vsphere-import b/src/zabbix/zabbix-vsphere-import index f46ab3c..ad83f93 100755 --- a/src/zabbix/zabbix-vsphere-import +++ b/src/zabbix/zabbix-vsphere-import @@ -30,6 +30,8 @@ into a Zabbix server as regular Zabbix hosts """ +from copy import deepcopy + import yaml import zabbix_api from docopt import docopt @@ -308,9 +310,9 @@ class ZabbixConnector(object): # Create the hosts in Zabbix for host in missing_hosts: logging.info("[vSphere HostSystem] Creating Zabbix host '%s'", host) - params = {} + + params = deepcopy(host_options) params['host'] = host - params.update(host_options) # Add the host to it's respective hostgroup/cluster in Zabbix data = self._get_vsphere_objects( @@ -366,9 +368,25 @@ class ZabbixConnector(object): # Create the hosts in Zabbix for vm in missing_vms: logging.info("[vSphere VirtualMachine] Creating Zabbix host '%s'", vm) - params = {} + + params = deepcopy(host_options) params['host'] = vm - params.update(host_options) + + # Add the virtual machine to it's respective hostgroup/cluster in Zabbix + # We do this by first finding the host this VM runs on and then we get the + # cluster where this host resides on. + data = self._get_vsphere_objects( + method='vm.host.get', + name=vm + ) + vm_host = data['result'][0]['host'] + + data = self._get_vsphere_objects( + method='host.cluster.get', + name=vm_host + ) + host_cluster = data['result'][0]['cluster'] + params['groups'].append({'groupid': self.get_host_group_by_name(name=host_cluster)}) try: result = self.create_host(params)