Improve template-not-found diagnostics

When template.get returns nothing, make a second call to list up to 5
visible templates so the log shows whether the API token can see any
templates at all. This pinpoints whether the issue is a name mismatch
or a template-group permission gap.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-17 10:54:29 +02:00
parent 3889bc971a
commit 2034f275d0

View File

@@ -120,7 +120,7 @@ class ZabbixClient:
# ------------------------------------------------------------------ templates
def get_template_id(self, name):
# Search by technical name (host field) — case-insensitive search with exactMatch fallback
# Search by technical name (host field) — case-insensitive search with exact-match fallback
result = self._call('template.get', {
'output': ['templateid', 'host'],
'search': {'host': name},
@@ -130,7 +130,21 @@ class ZabbixClient:
for t in result:
if t['host'] == name:
return t['templateid']
return result[0]['templateid'] if result else None
# Log what IS visible to help diagnose permission issues
if result:
visible = [t['host'] for t in result]
log.warning("Template '%s' not found; visible templates: %s", name, visible)
else:
all_tpl = self._call('template.get', {'output': ['host'], 'limit': 5})
log.warning(
"Template '%s' not found and API token can see %d template(s) total (first 5: %s). "
"Fix in Zabbix: Administration → User groups → [your group] → "
"Template permissions → add 'Templates' group with Read access.",
name,
len(all_tpl),
[t['host'] for t in all_tpl],
)
return None
def link_template(self, hostid, templateid):
# host.update with templates replaces the list — fetch existing first to avoid unlinking others