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,35 +200,44 @@ def sync_to_zabbix(players, add_log_fn):
|
|||||||
log.warning("Template '%s' not found in Zabbix — import zabbix_template.yaml first", ZABBIX_TEMPLATE)
|
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)}
|
existing = {h['host']: h for h in zbx.get_hosts_in_group(groupid)}
|
||||||
|
|
||||||
created = updated = 0
|
created = updated = skipped = 0
|
||||||
for player in players:
|
for player in players:
|
||||||
yid = player['id']
|
yid = player['id']
|
||||||
hostname = f"yodeck-{yid}"
|
hostname = f"yodeck-{yid}"
|
||||||
visible = f"QRS-{player['name'][:4]}MB-{player['name'][4:]}"
|
visible = f"QRS-{player['name'][:4]}MB-{player['name'][4:]}"
|
||||||
|
|
||||||
if hostname not in existing:
|
try:
|
||||||
hostid = zbx.create_host(hostname, visible, groupid)
|
if hostname not in existing:
|
||||||
zbx.upsert_host_macro(hostid, '{$YODECK_ID}', str(yid))
|
hostid = zbx.create_host(hostname, visible, groupid)
|
||||||
if template_id:
|
zbx.upsert_host_macro(hostid, '{$YODECK_ID}', str(yid))
|
||||||
zbx.link_template(hostid, template_id)
|
if template_id:
|
||||||
|
zbx.link_template(hostid, template_id)
|
||||||
|
else:
|
||||||
|
zbx.add_player_items(hostid, yid)
|
||||||
|
created += 1
|
||||||
|
log.info("Created Zabbix host: %s (%s)", hostname, visible)
|
||||||
else:
|
else:
|
||||||
zbx.add_player_items(hostid, yid)
|
h = existing[hostname]
|
||||||
created += 1
|
changed = False
|
||||||
log.info("Created Zabbix host: %s (%s)", hostname, visible)
|
if h['name'] != visible:
|
||||||
else:
|
zbx.update_host_name(h['hostid'], visible)
|
||||||
h = existing[hostname]
|
changed = True
|
||||||
if h['name'] != visible:
|
if template_id:
|
||||||
zbx.update_host_name(h['hostid'], visible)
|
linked = {t['templateid'] for t in h.get('parentTemplates', [])}
|
||||||
updated += 1
|
if template_id not in linked:
|
||||||
# Link template and set macro on existing hosts that don't have it yet
|
zbx.upsert_host_macro(h['hostid'], '{$YODECK_ID}', str(yid))
|
||||||
if template_id:
|
zbx.link_template(h['hostid'], template_id)
|
||||||
linked = {t['templateid'] for t in h.get('parentTemplates', [])}
|
log.info("Linked template to existing host: %s", hostname)
|
||||||
if template_id not in linked:
|
changed = True
|
||||||
zbx.upsert_host_macro(h['hostid'], '{$YODECK_ID}', str(yid))
|
if changed:
|
||||||
zbx.link_template(h['hostid'], template_id)
|
|
||||||
updated += 1
|
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)})
|
add_log_fn('zabbix_sync', msg, {'created': created, 'updated': updated, 'total': len(players)})
|
||||||
log.info(msg)
|
log.info(msg)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user