Skip to content

Commit dbe08f8

Browse files
SimkimdmHenriWahlstearzsimonkimmigdmde
authored
Fix for #1144 (#1189)
* readd nagstamon.1.gz * readd nagstamon.1.gz * fixes issues with Alertmanager and Prometheus (#1184) * fix(Alertmanager/Prometheus): Ensure session is not None before updating headers for JSON * fix(Alertmanager/Prometheus): service key issue with display_name from UI and multiple little fixes * fix CREDITS * fix cookies.py for python3.9 * build -> dist * fixed windows signing * add test for python 3.9 * updae codeql.yml * fix fedora/rhel repo * --depth 1 * ${{ env.cr_image }}-${{ env.family }}-${{ env.version_latest }}:${{ steps.dockerfile_hash.outputs.HASH }} * ${{ env.cr_image }}-${{ env.family }}-${{ env.version_latest }}:${{ steps.dockerfile_hash.outputs.HASH }} part II * rhel version * rhel version part II * fixed fedora repo * prevent duplicate host creation in Zabbix.py * refactor Zabbix.py for improved readability and error handling, fix #1186 --------- Co-authored-by: HenriWahl <2835065+HenriWahl@users.noreply.github.com> Co-authored-by: Stephan Schwarz <22387558+stearz@users.noreply.github.com> Co-authored-by: Kimmig, Simon - D0242573 <simon.kimmig@dm.de>
1 parent e95f5d3 commit dbe08f8

1 file changed

Lines changed: 23 additions & 25 deletions

File tree

Nagstamon/servers/Zabbix.py

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def __init__(self, **kwds):
5252
'5': 'DISASTER'}
5353

5454
# Entries for monitor default actions in context menu
55-
self.MENU_ACTIONS = ["Monitor", "Acknowledge", "Downtime"]
55+
self.MENU_ACTIONS = ['Monitor', 'Acknowledge', 'Downtime']
5656
# URLs for browser shortlinks/buttons on popup window
5757
self.BROWSER_URLS = {'monitor': '$MONITOR$',
5858
'hosts': '$MONITOR$/zabbix.php?action=host.view',
@@ -96,6 +96,8 @@ def init_http(self):
9696
'Content-Type': 'application/json-rpc'})
9797
try:
9898
self.set_zabbix_version()
99+
if not self.api_version:
100+
raise RuntimeError('Failed to determine Zabbix API version. Please verify the server URL and API endpoint are correct. Also check your network connectivity and authentication credentials.')
99101
self.check_authentication()
100102
if self.refresh_authentication:
101103
self.login()
@@ -276,19 +278,22 @@ def _get_status(self):
276278
service_obj.triggerid = service['triggerid']
277279
service_obj.eventid = service['lastEvent']['eventid']
278280
service_obj.allow_manual_close = False if str(service['manual_close']) == '0' else True
279-
281+
scheduled_downtime = False
280282
if service['hosts']:
281-
# Get the first host only, because we only support one host per service
282283
for host in service['hosts']:
283-
self.new_hosts[host['name']] = GenericHost()
284-
self.new_hosts[host['name']].name = host['name']
285-
self.new_hosts[host['name']].server = self.name
286-
self.new_hosts[host['name']].status = 'UP'
287-
self.new_hosts[host['name']].scheduled_downtime = True if host["maintenance_status"] == '1' else False
284+
if host['maintenance_status'] == '1':
285+
scheduled_downtime = True
286+
if host['name'] not in self.new_hosts:
287+
self.new_hosts[host['name']] = GenericHost()
288+
self.new_hosts[host['name']].name = host['name']
289+
self.new_hosts[host['name']].server = self.name
290+
self.new_hosts[host['name']].status = 'UP'
291+
self.new_hosts[host['name']].scheduled_downtime = host['maintenance_status'] == '1'
288292
# Map Stuff from Service to Host
289-
self.new_hosts[host['name']].services[service["triggerid"]] = service_obj
290-
self.new_hosts[host['name']].services[service["triggerid"]].host = host['name']
291-
self.new_hosts[host['name']].services[service["triggerid"]].hostid = host['hostid']
293+
self.new_hosts[host['name']].services[service['triggerid']] = service_obj
294+
self.new_hosts[host['name']].services[service['triggerid']].host = host['name']
295+
self.new_hosts[host['name']].services[service['triggerid']].hostid = host['hostid']
296+
service_obj.scheduled_downtime = scheduled_downtime
292297
except ZabbixError as e:
293298
return Result(result=e.result, error=e.result.error)
294299
except Exception:
@@ -304,14 +309,11 @@ def open_monitor(self, host, service_str=""):
304309
try:
305310
host = self.hosts.get(host, None)
306311
triggerid = None
307-
# host.services ist ein Dict {triggerid: service_obj} – über Werte iterieren
308312
for service_obj in host.services.values():
309313
if service_obj.name == service_str:
310314
triggerid = service_obj.triggerid
311315
break
312-
313-
url = f"{self.monitor_url}/zabbix.php?action=problem.view&triggerids%5B%5D={triggerid}&filter_set=1&show_suppressed=1"
314-
316+
url = f'{self.monitor_url}/zabbix.php?action=problem.view&triggerids%5B%5D={triggerid}&filter_set=1&show_suppressed=1'
315317
webbrowser_open(url)
316318
except Exception as e:
317319
if conf.debug_mode is True:
@@ -325,9 +327,7 @@ def set_recheck(self, info_dict):
325327

326328
def _set_acknowledge(self, host, service, author, comment, sticky, notify, persistent, all_services=None):
327329
if conf.debug_mode is True:
328-
self.debug(server=self.get_name(),
329-
debug="Set Acknowledge Host: " + host + " Service: " + service + " Sticky: " + str(
330-
sticky) + " persistent:" + str(persistent) + " All services: " + str(all_services))
330+
self.debug(server=self.get_name(), debug=f'Set Acknowledge Host: {host} Service: {service} Sticky: {sticky} persistent: {persistent} All services: {all_services}')
331331
eventids = set()
332332
unclosable_events = set()
333333
if all_services is None:
@@ -428,17 +428,15 @@ def _set_downtime(self, hostname, service, author, comment, fixed, start_time, e
428428
etime = int(time.mktime(end_date.timetuple()))
429429

430430
if conf.debug_mode is True:
431-
self.debug(server=self.get_name(),
432-
debug="Downtime for " + hostname + "[" + str(hostids) + "] stime:" + str(
433-
stime) + " etime:" + str(etime))
431+
self.debug(server=self.get_name(), debug=f'Downtime for {hostname}[{hostids}] stime: {stime} etime: {etime}')
434432
body = {'hostids': hostids, 'name': comment, 'description': author, 'active_since': stime, 'active_till': etime,
435433
'maintenance_type': 0, "timeperiods": [
436434
{"timeperiod_type": 0, "start_date": stime, "period": etime - stime}
437435
]
438436
}
439437
if triggerid:
440438
body['tags'] = [{'tag': 'triggerid', 'operator': 0, 'value': triggerid}]
441-
body['description'] = body['description'] + '(Nagstamon): ' + comment
439+
body['description'] = f"{body['description']}(Nagstamon): {comment}"
442440
body['name'] = f'{hostname}: {service}'
443441
try:
444442
self.api_request(
@@ -470,7 +468,7 @@ def get_host(self, host):
470468
if host in self.hosts:
471469
ip = self.hosts[host].address
472470
if conf.debug_mode is True:
473-
self.debug(server=self.get_name(), host=host, debug="IP of %s:" % host + " " + ip)
471+
self.debug(server=self.get_name(), host=host, debug=f'IP of {host}: {ip}')
474472

475473
if conf.connect_by_dns is True:
476474
try:
@@ -489,7 +487,7 @@ def nagiosify_service(self, service):
489487
"""
490488
next dirty workaround to get Zabbix events to look Nagios-esque
491489
"""
492-
if (" on " or " is ") in service:
493-
for separator in [" on ", " is "]:
490+
if ' on ' in service or ' is ' in service:
491+
for separator in [' on ', ' is ']:
494492
service = service.split(separator)[0]
495493
return service

0 commit comments

Comments
 (0)