TheNetworkGuy / netbox-zabbix-sync

Python script to synchronise Netbox devices to Zabbix.
MIT License
109 stars 27 forks source link

Filter devices by tag or something else #25

Closed zbx-sadman closed 1 year ago

zbx-sadman commented 1 year ago

Hello Twan.

Thanks for your work.

I have in the Netbox many devices, but not all of they must be transferred to Zabbix. Currently i try to use dirty hack for filter:

    netbox_host = environ.get("NETBOX_HOST")
    # Add new environment var
    netbox_tag = environ.get("NETBOX_TAG")  
...
    # Get all Zabbix and Netbox data
    #netbox_devices = netbox.dcim.devices.filter(name__n="null")
    # filter devices by predifined tag
    netbox_devices = netbox.dcim.devices.filter(tag=netbox_tag)

And running script with export NETBOX_TAG="zabbix"

This is ok or another method exist?

TheNetworkGuy commented 1 year ago

I would say that right now the method that you are using is valid. I haven't created a more elegant solution and the filter will provide the functionallity you need.

Although i do agree that there might be a more elegant solution like a seperate options.py file which could be provided to manipulate the filtering instead of modifying the main script.

TheNetworkGuy commented 1 year ago

I've seen this use case and request before so i made a simple config.py file which you can modify. This is a lot cleaner instead of modifying the source code :) Feel free to pull the latest "main" and try it out! :)

zbx-sadman commented 1 year ago

Thanks for impetuous improvement. I try to finish testing on this week.

I have one more question, because do not programming on python: i can use more that one item for filter dictionary (i guess right name of this hash array?) like that:

nb_device_filter = {"tag": "zabbix", "site": "HQ-AMS"}
TheNetworkGuy commented 1 year ago

Yes, you can use multiple keys. This will become a logical AND :) Your example (Device must have the tag zabbix and be part of site HQ-AMS) is valid.

-------- Oorspronkelijk bericht -------- Op 29 nov. 2022 18:35, schreef Grigory Prigodin :

Thanks for impetuous improvement. I try to finish testing on this week.

I have one more question, because do not program on python: i can use more that one item for filter dictionary (i guess right name of this hash array?) like that:

nb_device_filter = {"tag": "zabbix", "site": "HQ-AMS"}

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were assigned.Message ID: @.***>

zbx-sadman commented 1 year ago

Seems all OK

NetBox: image

Utility log:

cat sync.log
2022-11-30 09:40:17,520 - Netbox-Zabbix-sync - DEBUG - Found group XXXXXXXXXX for host AnyplaceUSB.
2022-11-30 09:40:17,522 - Netbox-Zabbix-sync - DEBUG - Found template ID 10186 for host AnyplaceUSB.
2022-11-30 09:40:18,340 - Netbox-Zabbix-sync - INFO - Created host AnyplaceUSB in Zabbix.

Zabbix v6 image

And i was make one more change in the config/source to ignore SSL cert if it not valid (i'm so lazy to generate valid cert on test NetBox instance). You can add it to src, if you think it's necessary.

config.py

nb_enable_ssl_verify = False

netbox_zabbix_sync.py

    # Set Netbox API
    netbox = api(netbox_host, token=netbox_token, threading=True)
    netbox.http_session.verify = nb_enable_ssl_verify

Thanks again.

TheNetworkGuy commented 1 year ago

Good day,

I will create the variable in the config.py file later this evening.

As for your other question with Zabbix interfaces which i got on the mail, isn't this something that will help you or is your use case something else (like 2 interfaces on 1 host object?): https://github.com/TheNetworkGuy/netbox-zabbix-sync#set-interface-parameters-within-netbox

zbx-sadman commented 1 year ago

Yes, you right - custom context is more flexible.

May be I had overengineering.

I looked at NetBox hosts and thinked: what if device has a Service (Z.Agent or IPMI), which binds not to primary IP (or non-standart port). We must create custom context for every device and sync context with Service record data for correct IP address transfer. But, if Service record already contain IP/Port/Proto fieds - can use they for autocreating Zabbix Interface items, and do not worry about copypaste errors.

What you think about it?

TheNetworkGuy commented 1 year ago

There is a use case where there are 2 interfaces on one Zabbix host. However this is currently not in scope of this project since it would take a tremendous amount of time for a very niche problem.

If you encounter a situation where you must configure 2 interfaces on one host, let the script sync a host to a Zabbix server and manually modify it with an extra IP address. The script will continue to function properly. For example, set the interface type in Netbox to Agent and manually create an interface as IPMI. After that you can change the item types which need to point to the IPMI to that interface.

zbx-sadman commented 1 year ago

Okay, thanks for answer. I'll try to make it for myself.