Skip to content

Commit

Permalink
Refactor Zabbix version comparisons (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
pederhan authored Nov 24, 2023
1 parent 217c66b commit 299d8c6
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions zabbix_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ def __init__(self, conf, username='', password='', auth_token=''):

logger.debug('Connected to Zabbix JSON-API')

self.api_version = self.zapi.apiinfo.version()
self.zabbix_version = int(self.api_version.split(".")[0])
self.zabbix_version = Version(self.zapi.apiinfo.version())

#
# The file $HOME/.zabbix-cli_auth_token is created if it does not exists.
Expand All @@ -126,7 +125,7 @@ def __init__(self, conf, username='', password='', auth_token=''):

self.intro = '\n#############################################################\n' + \
'Welcome to the Zabbix command-line interface (v' + self.version + ')\n' + \
'Connected to server ' + self.conf.zabbix_api_url + ' (v' + self.api_version + ')\n' + \
'Connected to server ' + self.conf.zabbix_api_url + ' (v' + str(self.zabbix_version) + ')\n' + \
'#############################################################\n' + \
'Type help or \\? to list commands.\n'

Expand Down Expand Up @@ -865,7 +864,7 @@ def do_show_host(self, args):
}
}

if self.zabbix_version >= 6:
if self.zabbix_version.major >= 6:
query["selectInterfaces"]= ["available"]

if host.isdigit():
Expand Down Expand Up @@ -900,7 +899,7 @@ def do_show_host(self, args):
#

for host in result:
if self.zabbix_version >= 6:
if self.zabbix_version.major >= 6:
available = host['interfaces'][0]['available']
else:
available = host['available']
Expand Down Expand Up @@ -1266,7 +1265,7 @@ def do_show_usergroup(self, args):
#
# Get result from Zabbix API
#
if self.zabbix_version>=6:
if self.zabbix_version.major >= 6:
name_element = "username"
else:
name_element = "alias"
Expand Down Expand Up @@ -1333,7 +1332,7 @@ def do_show_users(self, args):
#
# Get result from Zabbix API
#
if self.zabbix_version>=6:
if self.zabbix_version.major >= 6:
name_element = "username"
type_element = "roleid"
else:
Expand Down Expand Up @@ -2030,7 +2029,7 @@ def do_remove_user_from_usergroup(self, args):
#
# Get list with users to keep in this usergroup
#
if self.zabbix_version>=6:
if self.zabbix_version.major >= 6:
name_element = "username"
else:
name_element = "alias"
Expand Down Expand Up @@ -3206,7 +3205,7 @@ def do_create_maintenance_definition(self, args):
#
# Create maintenance period
#
if self.zabbix_version >=6:
if self.zabbix_version.major >= 6:
since = int(since)
till = int(till)
self.zapi.maintenance.create(name=maintenance_name,
Expand Down Expand Up @@ -3594,7 +3593,7 @@ def do_create_user(self, args):
#

try:
if self.zabbix_version >=6:
if self.zabbix_version.major >= 6:
search = {'username': alias}
else:
search = {'alias': alias}
Expand All @@ -3617,7 +3616,7 @@ def do_create_user(self, args):
self.generate_feedback('Warning', 'This user (' + alias + ') already exists.')
return False
else:
if self.zabbix_version >=6:
if self.zabbix_version.major >= 6:
result = self.zapi.user.create(username=alias,
name=name,
surname=surname,
Expand Down Expand Up @@ -3789,7 +3788,7 @@ def do_create_notification_user(self, args):
#

try:
if self.zabbix_version >=6:
if self.zabbix_version.major >= 6:
search = {'username': alias}
else:
search = {'alias': alias}
Expand All @@ -3806,7 +3805,7 @@ def do_create_notification_user(self, args):
#

try:
if self.zabbix_version >= 6:
if self.zabbix_version.major >= 6:
search = {'name': mediatype}
else:
search = {'description': mediatype}
Expand Down Expand Up @@ -3838,7 +3837,7 @@ def do_create_notification_user(self, args):
usergroup_objects = []
for usergroup in usergroup_list:
usergroup_objects.append({"usrgrpid": usergroup})
if self.zabbix_version >=6 :
if self.zabbix_version.major >= 6 :
result = self.zapi.user.create(username=alias,
passwd=passwd,
roleid=type,
Expand Down Expand Up @@ -4936,8 +4935,7 @@ def do_acknowledge_event(self, args):
return False

# Hotfix for Zabbix 4.0 compability
api_version = Version(self.zapi.api_version())
if api_version >= Version("4.0"):
if self.zabbix_version.major >= 4:
if close == 'false':
action = 6 # "Add message" and "Acknowledge"
elif close == 'true':
Expand Down Expand Up @@ -5051,8 +5049,7 @@ def do_acknowledge_trigger_last_event(self, args):
event_ids.append(data[0]['eventid'])

# Hotfix for Zabbix 4.0 compability
api_version = Version(self.zapi.api_version())
if api_version >= Version("4.0"):
if self.zabbix_version.major >= 4:
if close == 'false':
action = 6 # "Add message" and "Acknowledge"
elif close == 'true':
Expand Down Expand Up @@ -6210,7 +6207,7 @@ def do_export_configuration(self, args):

# Object type
object_type_list = ['groups', 'hosts', 'images', 'maps', 'screens', 'templates']
if self.zabbix_version >=6:
if self.zabbix_version.major >= 6:
object_type_list.remove('screens')
object_type_list.append('mediatypes')
object_type_to_export = []
Expand Down Expand Up @@ -6290,10 +6287,10 @@ def do_export_configuration(self, args):
self.generate_feedback('Error', 'Object type is not a valid value')
return False

if self.zabbix_version <6 and "mediatypes" == object_type:
if self.zabbix_version.major < 6 and "mediatypes" == object_type:
self.generate_feedback('Error', 'You cannot export media types with a version\'s Zabbix less than 6')
return False
if self.zabbix_version >=6 and "screens" == object_type:
if self.zabbix_version.major >= 6 and "screens" == object_type:
self.generate_feedback('Error', 'You cannot export screen with a version\'s Zabbix more than or equal to 6')
return False

Expand Down Expand Up @@ -6646,7 +6643,7 @@ def do_import_configuration(self, args):
'valueMaps': {'createMissing': True, 'updateExisting': True},
'mediaTypes': {'createMissing': True, 'updateExisting': True},
}
if self.zabbix_version < 6:
if self.zabbix_version.major < 6:
rules['applications'] = {'createMissing': True}
rules['screens'] ={'createMissing': True, 'updateExisting': True}
rules['templateScreens'] ={'createMissing': True, 'updateExisting': True}
Expand Down Expand Up @@ -7365,7 +7362,7 @@ def get_user_id(self, user):
DESCRIPTION:
Get the userid for a user
"""
if self.zabbix_version >=6:
if self.zabbix_version.major >= 6:
filter = {'username': user}
else:
filter = {'alias': user}
Expand Down

0 comments on commit 299d8c6

Please sign in to comment.