TheNetworkGuy / netbox-zabbix-sync

Python script to synchronise Netbox devices to Zabbix.
MIT License
130 stars 32 forks source link

Syncing issues #15

Closed taofineberg closed 2 years ago

taofineberg commented 2 years ago

Zabbix v6.2 Netbox v3.2.5

I am having a problem syncing. This was the first error I got after running

netbox_zabbix_sync.py -c -H -t -v

Traceback (most recent call last): File "netbox_zabbix_sync.py", line 716, in <module> main(args) File "netbox_zabbix_sync.py", line 79, in main if(device.promoteMasterDevice()): File "netbox_zabbix_sync.py", line 254, in promoteMasterDevice masterid = self.getClusterMaster() File "netbox_zabbix_sync.py", line 246, in getClusterMaster return self.nb.virtual_chassis.master.id AttributeError: 'NoneType' object has no attribute 'id'

now running this I get error: python3 netbox_zabbix_sync.py -c -t -v Traceback (most recent call last): File "netbox_zabbix_sync.py", line 716, in <module> main(args) File "netbox_zabbix_sync.py", line 133, in main device.createInZabbix(zabbix_groups, zabbix_templates, File "netbox_zabbix_sync.py", line 384, in createInZabbix if(not self.getZabbixGroup(groups)): File "netbox_zabbix_sync.py", line 299, in getZabbixGroup self.group_id = group['groupid'] KeyError: 'groupid'

only the Host groups are been added to Zabbix no hosts

Any assisantnce would be very helfpfull

Tao

TheNetworkGuy commented 2 years ago

Thanks for reporting! :)

It looks like the ID field in Netbox is not returned via the API. I'll check the release notes for version 3.2. Coming back to this issue later today.

taofineberg commented 2 years ago

ZBX v6.2.2 Netbox v3.3.2

Thank you for your swift response. I have setup a LAB environment to test so clean database for both ZBX and Netbox.

1) create a single device with all relevant info - Sync worked but with error 2022-09-06 11:00:14,940 - Netbox-Zabbix-sync - DEBUG - Added Tenant FineNet to hostgroup format of RackSwitch1. 2022-09-06 11:00:14,975 - Netbox-Zabbix-sync - INFO - Added hostgroup 'Geneva/FineNet/Mikrotik/Router'. 2022-09-06 11:00:15,002 - Netbox-Zabbix-sync - DEBUG - Found group Geneva/FineNet/Mikrotik/Router for host RackSwitch1. 2022-09-06 11:00:15,002 - Netbox-Zabbix-sync - DEBUG - Found template ID 10449 for host RackSwitch1. Traceback (most recent call last): File "netbox_zabbix_sync.py", line 716, in <module> main(args) File "netbox_zabbix_sync.py", line 133, in main device.createInZabbix(zabbix_groups, zabbix_templates, File "netbox_zabbix_sync.py", line 409, in createInZabbix self.nb.save() File "/usr/local/lib/python3.8/dist-packages/pynetbox/core/response.py", line 538, in save if req.patch(updates): File "/usr/local/lib/python3.8/dist-packages/pynetbox/core/query.py", line 427, in patch return self._make_call(verb="patch", data=data) File "/usr/local/lib/python3.8/dist-packages/pynetbox/core/query.py", line 287, in _make_call raise RequestError(req) pynetbox.core.query.RequestError: The request failed with code 400 Bad Request: {'__all__': ["Invalid value for custom field 'zabbix_hostid': Value must be a string."]} 2) update from planned to active to see if it updates in zabbix - does not make any changes in Zabbix Output: 2022-09-06 11:26:08,799 - Netbox-Zabbix-sync - DEBUG - Added Tenant FineNet to hostgroup format of RackSwitch1. 2022-09-06 11:26:08,826 - Netbox-Zabbix-sync - WARNING - Unable to add RackSwitch1 to Zabbix: host already present.

3) create a 2nd device in same site with all relevant info and set to active - Get error Output: ` 2022-09-06 12:18:30,831 - Netbox-Zabbix-sync - DEBUG - Added Tenant FineNet to hostgroup format of RackSwitch1. 2022-09-06 12:18:30,865 - Netbox-Zabbix-sync - WARNING - Unable to add RackSwitch1 to Zabbix: host already present. 2022-09-06 12:18:30,899 - Netbox-Zabbix-sync - DEBUG - Added Tenant FineNet to hostgroup format of Router2. Traceback (most recent call last): File "netbox_zabbix_sync.py", line 716, in main(args) File "netbox_zabbix_sync.py", line 133, in main device.createInZabbix(zabbix_groups, zabbix_templates, File "netbox_zabbix_sync.py", line 384, in createInZabbix if(not self.getZabbixGroup(groups)): File "netbox_zabbix_sync.py", line 299, in getZabbixGroup self.group_id = group['groupid'] KeyError: 'groupid'

`

4) remove the host group and device from Zabbix and run the script again. - host group and 1st device gets added only Output: 2022-09-06 12:22:29,850 - Netbox-Zabbix-sync - DEBUG - Added Tenant FineNet to hostgroup format of RackSwitch1. 2022-09-06 12:22:29,906 - Netbox-Zabbix-sync - INFO - Added hostgroup 'Geneva/FineNet/Mikrotik/Router'. 2022-09-06 12:22:29,938 - Netbox-Zabbix-sync - DEBUG - Found group Geneva/FineNet/Mikrotik/Router for host RackSwitch1. 2022-09-06 12:22:29,939 - Netbox-Zabbix-sync - DEBUG - Found template ID 10449 for host RackSwitch1. Traceback (most recent call last): File "netbox_zabbix_sync.py", line 716, in <module> main(args) File "netbox_zabbix_sync.py", line 133, in main device.createInZabbix(zabbix_groups, zabbix_templates, File "netbox_zabbix_sync.py", line 409, in createInZabbix self.nb.save() File "/usr/local/lib/python3.8/dist-packages/pynetbox/core/response.py", line 538, in save if req.patch(updates): File "/usr/local/lib/python3.8/dist-packages/pynetbox/core/query.py", line 427, in patch return self._make_call(verb="patch", data=data) File "/usr/local/lib/python3.8/dist-packages/pynetbox/core/query.py", line 287, in _make_call raise RequestError(req) pynetbox.core.query.RequestError: The request failed with code 400 Bad Request: {'__all__': ["Invalid value for custom field 'zabbix_hostid': Value must be a string."]} ' 5) run the script again Output: 2022-09-06 12:28:23,189 - Netbox-Zabbix-sync - DEBUG - Added Tenant FineNet to hostgroup format of RackSwitch1. 2022-09-06 12:28:23,219 - Netbox-Zabbix-sync - WARNING - Unable to add RackSwitch1 to Zabbix: host already present. 2022-09-06 12:28:23,245 - Netbox-Zabbix-sync - DEBUG - Added Tenant FineNet to hostgroup format of Router2. Traceback (most recent call last): File "netbox_zabbix_sync.py", line 716, in main(args) File "netbox_zabbix_sync.py", line 133, in main device.createInZabbix(zabbix_groups, zabbix_templates, File "netbox_zabbix_sync.py", line 384, in createInZabbix if(not self.getZabbixGroup(groups)): File "netbox_zabbix_sync.py", line 299, in getZabbixGroup self.group_id = group['groupid'] KeyError: 'groupid' `

Let me know if you would like me to test anything else Thank you so much for looking into this

Kind Regards

Tao

TheNetworkGuy commented 2 years ago

Okay so for your first reply, i managed to run the script using NB 3.3.2 and Zabbix 6.2.2 without any issues first try. So what i would assume is that you actually didn't define the master under the Virtual Chassis configuration, which is why Netbox doesn't return an ID.

I'll fix the script so that this error is properly handled instead of this ugly business. As for your second message haven't taken a look at it yet but will do once i fix (or properly escape) your initial problem.

TheNetworkGuy commented 2 years ago

So it turns out that the rest of the problems we're all connected to the Zabbix API. For some reason the output worked fine up until 5.0. I've modified the Zabbix config (especially parts of the consistency check, which is why you didn't have any problems on the first run) to work with 6.0 and 6.2. The config still works for Zabbix 4.0 and 5.0 so all of the currently supported Zabbix versions are working which is good enough for me.

My output after the changes:

python netbox_zabbix_sync.py -cHtv 2022-09-08 22:17:12,360 - Netbox-Zabbix-sync - DEBUG - Device FW01 is primary cluster member. Modifying hostname from FW01 to FWC. 2022-09-08 22:17:12,361 - Netbox-Zabbix-sync - INFO - Device FWC is part of cluster and primary. 2022-09-08 22:17:12,361 - Netbox-Zabbix-sync - DEBUG - FWC is not linked to a tenant. Using HG format 'Testsite/Juniper/Firewall'. 2022-09-08 22:17:12,415 - Netbox-Zabbix-sync - DEBUG - Found group Testsite/Juniper/Firewall for host FWC. 2022-09-08 22:17:12,415 - Netbox-Zabbix-sync - DEBUG - Found template ID 10186 for host FWC. 2022-09-08 22:17:12,785 - Netbox-Zabbix-sync - INFO - Created host FWC in Zabbix. 2022-09-08 22:17:12,827 - Netbox-Zabbix-sync - DEBUG - Device FW02 is non-primary cluster member. 2022-09-08 22:17:12,827 - Netbox-Zabbix-sync - INFO - Device FW02 is part of cluster but not primary. Skipping this host... 2022-09-08 22:17:12,886 - Netbox-Zabbix-sync - ERROR - FW03 is part of a Netbox virtual chassis which does not have a master configured. Skipping for this reason. 2022-09-08 22:17:12,923 - Netbox-Zabbix-sync - ERROR - FW04 is part of a Netbox virtual chassis which does not have a master configured. Skipping for this reason. 2022-09-08 22:17:12,962 - Netbox-Zabbix-sync - DEBUG - Added Tenant CustA to hostgroup format of RTR01. 2022-09-08 22:17:13,023 - Netbox-Zabbix-sync - DEBUG - Found group Testsite/CustA/MikroTik/Router for host RTR01. 2022-09-08 22:17:13,024 - Netbox-Zabbix-sync - DEBUG - Found template ID 10186 for host RTR01. 2022-09-08 22:17:13,425 - Netbox-Zabbix-sync - INFO - Created host RTR01 in Zabbix. 2022-09-08 22:17:13,463 - Netbox-Zabbix-sync - DEBUG - SW06 is not linked to a tenant. Using HG format 'Testsite/Cisco/Switch'. 2022-09-08 22:17:13,522 - Netbox-Zabbix-sync - DEBUG - Found group Testsite/Cisco/Switch for host SW06. 2022-09-08 22:17:13,522 - Netbox-Zabbix-sync - DEBUG - Found template ID 10186 for host SW06. 2022-09-08 22:17:13,883 - Netbox-Zabbix-sync - INFO - Created host SW06 in Zabbix.

python netbox_zabbix_sync.py -cHtv 2022-09-08 22:17:27,008 - Netbox-Zabbix-sync - DEBUG - Device FW01 is primary cluster member. Modifying hostname from FW01 to FWC. 2022-09-08 22:17:27,008 - Netbox-Zabbix-sync - INFO - Device FWC is part of cluster and primary. 2022-09-08 22:17:27,008 - Netbox-Zabbix-sync - DEBUG - FWC is not linked to a tenant. Using HG format 'Testsite/Juniper/Firewall'. 2022-09-08 22:17:27,009 - Netbox-Zabbix-sync - DEBUG - Found group Testsite/Juniper/Firewall for host FWC. 2022-09-08 22:17:27,009 - Netbox-Zabbix-sync - DEBUG - Found template ID 10186 for host FWC. 2022-09-08 22:17:27,079 - Netbox-Zabbix-sync - DEBUG - Device FWC: hostname in-sync. 2022-09-08 22:17:27,080 - Netbox-Zabbix-sync - DEBUG - Device FWC: template in-sync. 2022-09-08 22:17:27,080 - Netbox-Zabbix-sync - DEBUG - Device FWC: hostgroup in-sync. 2022-09-08 22:17:27,080 - Netbox-Zabbix-sync - DEBUG - Device FWC: status in-sync. 2022-09-08 22:17:27,080 - Netbox-Zabbix-sync - DEBUG - Device FWC: interface in-sync. 2022-09-08 22:17:27,252 - Netbox-Zabbix-sync - DEBUG - Device FW02 is non-primary cluster member. 2022-09-08 22:17:27,252 - Netbox-Zabbix-sync - INFO - Device FW02 is part of cluster but not primary. Skipping this host... 2022-09-08 22:17:27,286 - Netbox-Zabbix-sync - ERROR - FW03 is part of a Netbox virtual chassis which does not have a master configured. Skipping for this reason. 2022-09-08 22:17:27,321 - Netbox-Zabbix-sync - ERROR - FW04 is part of a Netbox virtual chassis which does not have a master configured. Skipping for this reason. 2022-09-08 22:17:27,353 - Netbox-Zabbix-sync - DEBUG - Added Tenant CustA to hostgroup format of RTR01. 2022-09-08 22:17:27,354 - Netbox-Zabbix-sync - DEBUG - Found group Testsite/CustA/MikroTik/Router for host RTR01. 2022-09-08 22:17:27,354 - Netbox-Zabbix-sync - DEBUG - Found template ID 10186 for host RTR01. 2022-09-08 22:17:27,413 - Netbox-Zabbix-sync - DEBUG - Device RTR01: hostname in-sync. 2022-09-08 22:17:27,413 - Netbox-Zabbix-sync - DEBUG - Device RTR01: template in-sync. 2022-09-08 22:17:27,413 - Netbox-Zabbix-sync - DEBUG - Device RTR01: hostgroup in-sync. 2022-09-08 22:17:27,413 - Netbox-Zabbix-sync - DEBUG - Device RTR01: status in-sync. 2022-09-08 22:17:27,413 - Netbox-Zabbix-sync - DEBUG - Device RTR01: interface in-sync. 2022-09-08 22:17:27,448 - Netbox-Zabbix-sync - DEBUG - SW06 is not linked to a tenant. Using HG format 'Testsite/Cisco/Switch'. 2022-09-08 22:17:27,448 - Netbox-Zabbix-sync - DEBUG - Found group Testsite/Cisco/Switch for host SW06. 2022-09-08 22:17:27,448 - Netbox-Zabbix-sync - DEBUG - Found template ID 10186 for host SW06. 2022-09-08 22:17:27,506 - Netbox-Zabbix-sync - DEBUG - Device SW06: hostname in-sync. 2022-09-08 22:17:27,507 - Netbox-Zabbix-sync - DEBUG - Device SW06: template in-sync. 2022-09-08 22:17:27,507 - Netbox-Zabbix-sync - DEBUG - Device SW06: hostgroup in-sync. 2022-09-08 22:17:27,507 - Netbox-Zabbix-sync - DEBUG - Device SW06: status in-sync. 2022-09-08 22:17:27,507 - Netbox-Zabbix-sync - DEBUG - Device SW06: interface in-sync

If you have any other problems feel free to post them in a new bug report. Thanks for bringing my attention to this!

TheNetworkGuy commented 2 years ago

Just to reopen this, can you confirm that your issues have been resolved?

taofineberg commented 2 years ago

Thank you, I have tested and it is working now. Appreciate your commitment to keeping this update