Skip to content

Commit

Permalink
Fix setting proxies on Zabbix 7
Browse files Browse the repository at this point in the history
  • Loading branch information
pederhan committed Aug 15, 2024
1 parent 53b75b6 commit 1f03651
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 30 deletions.
36 changes: 6 additions & 30 deletions zabbix_auto_config/pyzabbix/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions zabbix_auto_config/pyzabbix/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1f03651

Please sign in to comment.