Skip to content

Commit

Permalink
Fix Zabbix 6.4 auth Parameter (#1040)
Browse files Browse the repository at this point in the history
* Handle Zabbix Version while init

* Add Bearer header instead of auth parameter, which is deprecated since 6.4.

* Remove deprecated Zabbix Code

* Undo Removal

---------

Co-authored-by: Kimmig, Simon - D0242573 <[email protected]>
  • Loading branch information
Simkimdm and simonkimmigdmde authored Jun 11, 2024
1 parent 4be7833 commit 6111bff
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
14 changes: 1 addition & 13 deletions Nagstamon/Servers/Zabbix.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,6 @@ def _get_status(self):
services = []
services_in_maintenance = set()

try:
api_version = int(''.join(self.zapi.api_version().split('.')[:-1])) # Make API Version smaller
except ZabbixAPIException:
# FIXME Is there a cleaner way to handle this? I just borrowed
# this code from 80 lines ahead. -- AGV
# set checking flag back to False
self.isChecking = False
result, error = self.Error(sys.exc_info())
print(sys.exc_info())
return Result(result=result, error=error)

try:
now_ts = int(datetime.datetime.utcnow().timestamp())
# only the maintenance object knows about services "in downtime"
Expand All @@ -165,7 +154,6 @@ def _get_status(self):
try:
try:
# Get a list of all issues (AKA tripped triggers)
# Zabbix 3+ returns array of objects
services = self.zapi.trigger.get({'only_true': True,
'skipDependent': True,
'monitored': True,
Expand Down Expand Up @@ -217,7 +205,7 @@ def _get_status(self):
# Create Hostids for shorten Query
try:
hosts = []
if api_version >= 54: # For Version 5.4 and higher
if self.zapi.api_version > '5.4': # For Version 5.4 and higher
# Some performance improvement for 5.4
hostids = []
# get just involved Hosts.
Expand Down
13 changes: 9 additions & 4 deletions Nagstamon/thirdparty/zabbix_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class ZabbixAPI(object):
httppasswd = None
timeout = 10
validate_certs = None
api_version = '0.0'
# sub-class instances.
# Constructor Params:
# server: Server to connect to
Expand Down Expand Up @@ -149,6 +150,8 @@ def __init__(self, server='http://localhost/zabbix', user=httpuser, passwd=httpp
self.r_query = deque([], maxlen=r_query_len)
self.validate_certs = validate_certs
self.debug(logging.INFO, "url: " + self.url)
self.api_version = self.get_api_version()
self.debug(logging.INFO, "Zabbix API version: " + self.api_version)

def _setuplogging(self):
self.logger = logging.getLogger("zabbix_api.%s" % self.__class__.__name__)
Expand Down Expand Up @@ -176,10 +179,10 @@ def json_obj(self, method, params={}, auth=True):
obj = {'jsonrpc': '2.0',
'method': method,
'params': params,
'auth': self.auth,
'auth': self.auth, # deprecated in 6.4
'id': self.id
}
if not auth:
if not auth or self.api_version > '6.4':
del obj['auth']

self.debug(logging.DEBUG, "json_obj: " + str(obj))
Expand All @@ -201,7 +204,7 @@ def login(self, user='', password='', save=True):
raise ZabbixAPIException("No authentication information available.")

# check version to use the correct keyword for username which changed since 6.4
if self.api_version() < '6.4':
if self.api_version < '6.4':
username_keyword = 'user'
else:
username_keyword = 'username'
Expand Down Expand Up @@ -230,6 +233,8 @@ def do_request(self, json_obj):
headers = {'Content-Type': 'application/json-rpc',
'User-Agent': 'python/zabbix_api'}

if self.api_version > '6.4':
headers['Authorization'] = 'Bearer ' + self.auth
if self.httpuser:
self.debug(logging.INFO, "HTTP Auth enabled")
auth = 'Basic ' + string.strip(base64.encodestring(self.httpuser + ':' + self.httppasswd))
Expand Down Expand Up @@ -301,7 +306,7 @@ def logged_in(self):
return True
return False

def api_version(self, **options):
def get_api_version(self, **options):
# kicked out check auth to be able to check version before being logged in to use the correct username keyword
obj = self.do_request(self.json_obj('apiinfo.version', options, auth=False))
return obj['result']
Expand Down

0 comments on commit 6111bff

Please sign in to comment.