Skip to content

Commit 635ab64

Browse files
Refactor Zabbix integration: streamline URL handling, improve exception management, and optimize service dictionary iteration.
1 parent d65a80d commit 635ab64

1 file changed

Lines changed: 13 additions & 23 deletions

File tree

Nagstamon/servers/Zabbix.py

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def __init__(self, **kwds):
6969
# Force authentication refresh by default until verified
7070
self.refresh_authentication = True
7171
self.monitor_path = '/api_jsonrpc.php'
72+
self.monitor_url = self.monitor_url.split(self.monitor_path)[0]
7273

7374
def init_http(self):
7475
"""
@@ -285,37 +286,28 @@ def _get_status(self):
285286
self.new_hosts[host['name']].services[service["triggerid"]].host = host['name']
286287
self.new_hosts[host['name']].services[service["triggerid"]].hostid = host['hostid']
287288
except ZabbixError as e:
288-
print(f"ZabbixError: {e.result.error}")
289289
return Result(result=e.result, error=e.result.error)
290290
except Exception:
291291
self.isChecking = False
292292
result, error = self.error(sys.exc_info())
293-
print(sys.exc_info())
294293
return Result(result=result, error=error)
295294
return ret
296295

297-
def _open_browser(self, url):
298-
webbrowser_open(url)
299-
300-
if conf.debug_mode is True:
301-
self.debug(server=self.get_name(), debug="Open web page " + url)
302-
303-
def open_services(self):
304-
self._open_browser(self.urls['human_services'])
305-
306-
def open_hosts(self):
307-
self._open_browser(self.urls['human_hosts'])
308-
309-
def open_monitor(self, host, service=""):
296+
def open_monitor(self, host, service_str=""):
310297
"""
311298
open monitor from treeview context menu
312299
"""
313-
host_id = self.hosts[host].hostid
314-
url = f"{self.monitor_url}/zabbix.php?action=problem.view&hostids%5B%5D={host_id}&filter_set=1&show_suppressed=1"
300+
monitor_url = self.monitor_url.split(self.monitor_path)[0]
301+
host = self.hosts.get(host, None)
302+
triggerid = None
303+
# host.services ist ein Dict {triggerid: service_obj} – über Werte iterieren
304+
for service_obj in host.services.values():
305+
if service_obj.name == service_str:
306+
triggerid = service_obj.triggerid
307+
break
308+
309+
url = f"{monitor_url}/zabbix.php?action=problem.view&triggerids%5B%5D={triggerid}&filter_set=1&show_suppressed=1"
315310

316-
if conf.debug_mode is True:
317-
self.debug(server=self.get_name(), host=host, service=service,
318-
debug="Open host/service monitor web page " + url)
319311
webbrowser_open(url)
320312

321313
# Disable set_recheck (nosense in Zabbix)
@@ -336,8 +328,7 @@ def _set_acknowledge(self, host, service, author, comment, sticky, notify, persi
336328
# Through all Services
337329
for s in all_services:
338330
# find Trigger ID
339-
for host_service in get_host.services:
340-
host_service = get_host.services[host_service]
331+
for host_service in get_host.services.values():
341332
if host_service.name == s:
342333
eventid = host_service.eventid
343334
# https://github.com/HenriWahl/Nagstamon/issues/826 we may have set eventid = -1 earlier if there was no associated event
@@ -431,7 +422,6 @@ def _set_downtime(self, hostname, service, author, comment, fixed, start_time, e
431422
self.debug(server=self.get_name(),
432423
debug="Downtime for " + hostname + "[" + str(hostids) + "] stime:" + str(
433424
stime) + " etime:" + str(etime))
434-
# print("Downtime for " + hostname + "[" + str(hostids) + "] stime:" + str(stime) + " etime:" + str(etime))
435425
body = {'hostids': hostids, 'name': comment, 'description': author, 'active_since': stime, 'active_till': etime,
436426
'maintenance_type': 0, "timeperiods": [
437427
{"timeperiod_type": 0, "start_date": stime, "period": etime - stime}

0 commit comments

Comments
 (0)