From 1f03651fba844bca5523aa17be804cdbb4a3119c Mon Sep 17 00:00:00 2001 From: pederhan Date: Thu, 15 Aug 2024 10:58:53 +0200 Subject: [PATCH] Fix setting proxies on Zabbix 7 --- zabbix_auto_config/pyzabbix/client.py | 36 +++++---------------------- zabbix_auto_config/pyzabbix/enums.py | 8 ++++++ 2 files changed, 14 insertions(+), 30 deletions(-) diff --git a/zabbix_auto_config/pyzabbix/client.py b/zabbix_auto_config/pyzabbix/client.py index eaec8a6..ea1643e 100644 --- a/zabbix_auto_config/pyzabbix/client.py +++ b/zabbix_auto_config/pyzabbix/client.py @@ -41,6 +41,7 @@ from zabbix_auto_config.pyzabbix.enums import InterfaceType from zabbix_auto_config.pyzabbix.enums import InventoryMode from zabbix_auto_config.pyzabbix.enums import MaintenanceStatus +from zabbix_auto_config.pyzabbix.enums import MonitoredBy from zabbix_auto_config.pyzabbix.enums import MonitoringStatus from zabbix_auto_config.pyzabbix.enums import TriggerPriority from zabbix_auto_config.pyzabbix.enums import UsergroupPermission @@ -1389,6 +1390,8 @@ def update_host_proxy(self, host: Host, proxy: Proxy) -> str: "hostid": host.hostid, compat.host_proxyid(self.version): proxy.proxyid, } + if self.version.release >= (7, 0, 0): + params["monitored_by"] = MonitoredBy.PROXY.value try: resp = self.host.update(**params) except ZabbixAPIException as e: @@ -1405,8 +1408,10 @@ def clear_host_proxy(self, host: Host) -> str: """Clear a host's proxy.""" params: ParamsType = { "hostid": host.hostid, - compat.host_proxyid(self.version): None, + compat.host_proxyid(self.version): "0", } + if self.version.release >= (7, 0, 0): + params["monitored_by"] = MonitoredBy.SERVER.value try: resp = self.host.massupdate(**params) except ZabbixAPIException as e: @@ -1417,35 +1422,6 @@ def clear_host_proxy(self, host: Host) -> str: ) return resp["hostids"][0] - def update_host_status(self, host: Host, status: MonitoringStatus) -> str: - """Update a host status given a host ID and status.""" - try: - resp = self.host.update(hostid=host.hostid, status=status) - except ZabbixAPIException as e: - raise ZabbixAPICallError( - f"Failed to update host status for host {host.host!r} (ID {host.hostid})" - ) from e - if not resp or not resp.get("hostids"): - raise ZabbixNotFoundError( - f"No host ID returned when updating status for host {host.host!r} (ID {host.hostid})" - ) - return resp["hostids"][0] - - # NOTE: maybe passing in a list of hosts to this is overkill? - # Just pass in a list of host IDs instead? - def move_hosts_to_proxy(self, hosts: List[Host], proxy: Proxy) -> None: - """Move a list of hosts to a proxy.""" - params: ParamsType = { - "hosts": [{"hostid": host.hostid} for host in hosts], - compat.host_proxyid(self.version): proxy.proxyid, - } - try: - self.host.massupdate(**params) - except ZabbixAPIException as e: - raise ZabbixAPICallError( - f"Failed to move hosts {[str(host) for host in hosts]} to proxy {proxy.name!r}" - ) from e - def get_template( self, template_name_or_id: str, diff --git a/zabbix_auto_config/pyzabbix/enums.py b/zabbix_auto_config/pyzabbix/enums.py index 0d95d67..dd3acf0 100644 --- a/zabbix_auto_config/pyzabbix/enums.py +++ b/zabbix_auto_config/pyzabbix/enums.py @@ -141,3 +141,11 @@ class SNMPPrivProtocol(IntEnum): AES256 = 3 AES192C = 4 AES256C = 5 + + +class MonitoredBy(IntEnum): # >= 7.0 + """Type of entity that monitors the host.""" + + SERVER = 0 + PROXY = 1 + PROXY_GROUP = 2