Fix: per-host error handling in sync loop
A single host failure (e.g. macro already exists, permission error) was aborting the entire sync loop for all 310 hosts. Each host is now wrapped in try/except — failures are logged as warnings and the loop continues. Also logs when template is linked to an existing host. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -200,12 +200,13 @@ def sync_to_zabbix(players, add_log_fn):
|
||||
log.warning("Template '%s' not found in Zabbix — import zabbix_template.yaml first", ZABBIX_TEMPLATE)
|
||||
existing = {h['host']: h for h in zbx.get_hosts_in_group(groupid)}
|
||||
|
||||
created = updated = 0
|
||||
created = updated = skipped = 0
|
||||
for player in players:
|
||||
yid = player['id']
|
||||
hostname = f"yodeck-{yid}"
|
||||
visible = f"QRS-{player['name'][:4]}MB-{player['name'][4:]}"
|
||||
|
||||
try:
|
||||
if hostname not in existing:
|
||||
hostid = zbx.create_host(hostname, visible, groupid)
|
||||
zbx.upsert_host_macro(hostid, '{$YODECK_ID}', str(yid))
|
||||
@@ -217,18 +218,26 @@ def sync_to_zabbix(players, add_log_fn):
|
||||
log.info("Created Zabbix host: %s (%s)", hostname, visible)
|
||||
else:
|
||||
h = existing[hostname]
|
||||
changed = False
|
||||
if h['name'] != visible:
|
||||
zbx.update_host_name(h['hostid'], visible)
|
||||
updated += 1
|
||||
# Link template and set macro on existing hosts that don't have it yet
|
||||
changed = True
|
||||
if template_id:
|
||||
linked = {t['templateid'] for t in h.get('parentTemplates', [])}
|
||||
if template_id not in linked:
|
||||
zbx.upsert_host_macro(h['hostid'], '{$YODECK_ID}', str(yid))
|
||||
zbx.link_template(h['hostid'], template_id)
|
||||
log.info("Linked template to existing host: %s", hostname)
|
||||
changed = True
|
||||
if changed:
|
||||
updated += 1
|
||||
except Exception as exc:
|
||||
log.warning("Skipped host %s: %s", hostname, exc)
|
||||
skipped += 1
|
||||
|
||||
msg = f"Zabbix sync complete: {created} created, {updated} updated ({len(players)} total)"
|
||||
msg = (f"Zabbix sync complete: {created} created, {updated} updated"
|
||||
+ (f", {skipped} skipped" if skipped else "")
|
||||
+ f" ({len(players)} total)")
|
||||
add_log_fn('zabbix_sync', msg, {'created': created, 'updated': updated, 'total': len(players)})
|
||||
log.info(msg)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user