Skip to content

Commit

Permalink
Add some performance improvements when running in bulk modus. hostgro…
Browse files Browse the repository at this point in the history
…upids are cached when running in bulk modus.
  • Loading branch information
rafaelma committed Jun 27, 2016
1 parent 02fb825 commit 2810193
Showing 1 changed file with 55 additions and 4 deletions.
59 changes: 55 additions & 4 deletions zabbix_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ def __init__(self,logs,conf,username='',password='',auth_token=''):

self.proxyid_cache = self.populate_proxyid_cache()

#
# Populate the dictionary used as a cache with hostgroupname and
# hostgroupid
#

self.hostgroupname_cache = self.populate_hostgroupname_cache()

except Exception as e:
print '\n[ERROR]: ',e
print
Expand Down Expand Up @@ -6364,6 +6371,7 @@ def hostgroup_exists(self, hostgroup):
'''

try:

data = self.zapi.hostgroup.get(filter={'name':hostgroup})

if data != []:
Expand All @@ -6386,12 +6394,23 @@ def get_hostgroup_id(self, hostgroup):
'''

try:
data = self.zapi.hostgroup.get(filter={'name':hostgroup})

if data != []:
hostgroupid = data[0]['groupid']
if self.bulk_execution == True:

if hostgroup in self.hostgroupname_cache:
hostgroupid = self.hostgroupname_cache[hostgroup]

else:
raise Exception('Could not find hostgroupID for: ' + hostgroup)

else:
raise Exception('Could not find hostgroupID for: ' + hostgroup)

data = self.zapi.hostgroup.get(filter={'name':hostgroup})

if data != []:
hostgroupid = data[0]['groupid']
else:
raise Exception('Could not find hostgroupID for: ' + hostgroup)

except Exception as e:
raise e
Expand Down Expand Up @@ -6808,6 +6827,38 @@ def populate_hostid_cache(self):
raise e


# #################################################
# Method populate_hostgroupname_cache
# #################################################

def populate_hostgroupname_cache(self):
'''
DESCRIPTION:
Populate hostgroupname cache
'''

# This method initializes a dictionary with all hostgroups in
# the system.
#
# This will help the performance of creating a host via bulk
# executions because we avoid an extra call to the zabbix-API.
#


try:
temp_dict = {}

data = self.zapi.hostgroup.get(output=['groupid','name'])

for hostgroup in data:
temp_dict[hostgroup['name']] = hostgroup['groupid']

return temp_dict

except Exception as e:
raise e


# #################################################
# Method populate_proxyid_cache
# #################################################
Expand Down

0 comments on commit 2810193

Please sign in to comment.