From 3889bc971a7bc96742403adad359f97fba43791c Mon Sep 17 00:00:00 2001 From: Christoph Gasser Date: Fri, 17 Apr 2026 10:52:10 +0200 Subject: [PATCH] Fix template lookup and surface template-not-found errors to web UI - get_template_id: switch from filter to search so the API token user can find templates they have read access to (filter requires exact visibility that search does not) - When the template is not found, write the warning to the DB log so it appears in the web dashboard, not only in Docker container logs Co-Authored-By: Claude Sonnet 4.6 --- app/zabbix.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/app/zabbix.py b/app/zabbix.py index 30a837e..67caf99 100644 --- a/app/zabbix.py +++ b/app/zabbix.py @@ -120,7 +120,16 @@ class ZabbixClient: # ------------------------------------------------------------------ templates def get_template_id(self, name): - result = self._call('template.get', {'filter': {'host': name}, 'output': ['templateid']}) + # Search by technical name (host field) — case-insensitive search with exactMatch fallback + result = self._call('template.get', { + 'output': ['templateid', 'host'], + 'search': {'host': name}, + 'searchByAny': True, + }) + # Exact match from search results + for t in result: + if t['host'] == name: + return t['templateid'] return result[0]['templateid'] if result else None def link_template(self, hostid, templateid): @@ -197,7 +206,9 @@ def sync_to_zabbix(players, add_log_fn): groupid = zbx.ensure_hostgroup(ZABBIX_HOST_GROUP) template_id = zbx.get_template_id(ZABBIX_TEMPLATE) if ZABBIX_TEMPLATE else None if ZABBIX_TEMPLATE and not template_id: - log.warning("Template '%s' not found in Zabbix — import zabbix_template.yaml first", ZABBIX_TEMPLATE) + msg = f"Template '{ZABBIX_TEMPLATE}' not found in Zabbix — import zabbix_template.yaml first" + log.warning(msg) + add_log_fn('error', msg) existing = {h['host']: h for h in zbx.get_hosts_in_group(groupid)} created = updated = skipped = 0