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 <noreply@anthropic.com>
This commit is contained in:
2026-04-17 10:52:10 +02:00
parent adadf9de76
commit 3889bc971a

View File

@@ -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