Skip to content

Update hostsync.py - #147

Open
ChalinaM wants to merge 1 commit into
OpensourceICTSolutions:mainfrom
ChalinaM:patch-1
Open

ChalinaM wants to merge 1 commit into
OpensourceICTSolutions:mainfrom
ChalinaM:patch-1

Conversation

@ChalinaM

Copy link
Copy Markdown

fix: keep untracked host interfaces when flipping the default interface
Related to #97.

Problem

HostSync.check_default_hostinterface() flips the default ("main") interface with a single atomic call:

self.api.host.update(hostid=hostid, interfaces=desired_hostinterfaces)

desired_hostinterfaces is built exclusively from ZabbixHostInterface objects known to NetBox. Zabbix, however, treats the interfaces property of host.update() as the complete, authoritative list: any interface currently on the host but absent from it is deleted. An interface that still carries items cannot be deleted, so Zabbix rejects the entire call:

Invalid params. Interface is linked to item "ICMP ping" on "<host>".

This affects any host that existed in Zabbix before being adopted by nbxsync — i.e. an existing host taken over by pre-filling ZabbixServerAssignment.hostid. The plugin has no record of the existing interface, so it creates a second one as non-main, and from then on tries to promote its own interface by sending a list that silently omits the original.

Two properties make this worse than a single failed API call:

  1. The host never syncs at all. check_default_hostinterface() runs first in SyncHostJob.run(), before sync_host(). The exception propagates through run_zabbix_operation() and aborts the job, so host name, templates, macros and tags are never pushed. From the outside it looks like the sync runs but does nothing.

  2. It cannot recover. The branch is guarded by nb_default_id != zbx_default_id. NetBox's interface can only become main through the very call being rejected, so the condition stays true and the identical error repeats every interval indefinitely.

Change

Carry the interfaces NetBox doesn't know about along in the payload instead of dropping them, demoted to main=0 where NetBox supplies the default for that type. Removing stale interfaces is already verify_hostinterfaces()'s responsibility; this method should only flip the main flag.

zabbix_hostinterfaces is already fetched at the top of the method, so no additional API call is made.

Behaviour is unchanged for hosts created by the plugin itself: they have no interfaces outside NetBox's view, the added loop appends nothing, and the payload is identical to before.

Verification

Reproduced and fixed against nbxsync 1.0.5, NetBox 4.6.8, Zabbix 7.0.30, on 6 SNMP hosts that had been created manually in Zabbix and were later taken over by nbxsync. Before: the same error on every scheduled run for ~20 hours, last_sync_state: false on all 6. After: Synchronization successful on the next run, host names and templates pushed as configured, duplicate interfaces cleaned up by verify_hostinterfaces(), item history preserved. Still green three days and ~70 job runs later.

The two hosts on the same server that had been created by the plugin were unaffected throughout, before and after.

fix: keep untracked host interfaces when flipping the default interface

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant