Python script to synchronise Netbox devices to Zabbix.
Go to file
Twan K 1ce73b3457
Update README.md
Fixed formatting in README
2021-04-29 09:45:57 +02:00
LICENSE Initial commit 2021-04-11 19:39:48 +02:00
netbox_zabbix_sync.py Updated README and modified interface update log messages. 2021-04-29 09:40:23 +02:00
README.md Update README.md 2021-04-29 09:45:57 +02:00
requirements.txt Added sync script 2021-04-11 20:07:37 +02:00

A script to sync the Netbox device inventory to Zabbix.

Requires pyzabbix and pynetbox.

Script settings

Enviroment variables

Optional:

  • NETBOX_KEY="Private user key"

Flags

Flag Option Description
-c cluster For clustered devices: only add the primary node of a cluster and use the cluster name as hostname.
-H hostgroup Create non-existing hostgroups in Zabbix. Usefull for a first run to add all required hostgroups.
-t tenant Add the tenant name to the hostgroup format (Tenant/Site/Manufacturer/Role)
-s secret Use Netbox secrets if present on device for SNMP parameters
-v verbose Log with debugging on.

Logging

Logs are generated under sync.log, use -v for debugging.

Hostgroups: manual mode

In case of omitting the -H flag, manual hostgroup creation is required for devices in a new category.

This is in the format: {Site name}/{Manufacturer name}/{Device role name} And with tenants (-t flag): {Site name}/{Tenant name}/{Manufacturer name}/{Device role name}

Make sure that the Zabbix user has proper permissions to create hosts. The hostgroups are in a nested format. This means that proper permissions only need to be applied to the site name hostgroup and cascaded to any child hostgroups.

Netbox settings

Custom fields

Use the following custom fields in Netbox to map the Zabbix URL:

  • Type: Integer
  • Name: zabbix_hostid
  • Required: False
  • Default: null
  • Object: dcim > device

And this field for the Zabbix template

  • Type: Text
  • Name: zabbix_template
  • Required: False
  • Default: null
  • Object: dcim > device_type

Set interface parameters within Netbox

When adding a new device, you can set the interface type with custom context. Due to Zabbix limitations of changing interface type with a linked template, changing the interface type from within Netbox is not supported and the script will generate an error.

For example when changing a SNMP interface to an Agent interface:

Netbox-Zabbix-sync - WARNING - Device: Interface OUT of sync.
Netbox-Zabbix-sync - ERROR - Device: changing interface type to 1 is not supported.

To configure the interface parameters you'll need to use custom context. Custom context was used to make this script as customizable as posible for each environment. For example, you could:

  • Set the custom context directly on a device
  • Set the custom context on a label, which you would add to a device (for instance, SNMPv3)
  • Set the custom context on a device role
  • Set the custom context on a site or region
Agent interface configuration example
{
    "zabbix": {
        "interface_port": 1500,
        "interface_type": 1
    }
}
SNMPv2 interface configuration example
{
    "zabbix": {
        "interface_port": 161,
        "interface_type": 2,
        "snmp": {
            "bulk": 1,
            "community": "SecretCommunity",
            "version": 2
        }
    }
}
SNMPv3 interface configuration example
{
    "zabbix": {
        "interface_port": 1610,
        "interface_type": 2,
        "snmp": {
            "authpassphrase": "SecretAuth",
            "bulk": 1,
            "securitylevel": 1,
            "securityname": "MySecurityName",
            "version": 3
        }
    }
}

Note: Not all SNMP data is required for a working configuration. The following parameters are allowed but are not all required, depending on your environment.

Secrets

Instead of having the password in plain-text in the config context, you can also set the password as secret in the Netbox device configuration. You will need to use the -s option for this. Keep in mind that you will need a Netbox private user key for this functionality.

This method of setting device SNMP parameters is working, but i would recommend going for a "secret macro" implementation to keep your environment more predictable. Refer to the macro from the config context and set the macro inside of Zabbix to the actual community string / authentication secret etc.

Permissions

Make sure that the user has proper permissions for device read and modify (modify to set the Zabbix HostID custom field) operations.

To make the user experience easier you could add a custom link that redirects users to the Zabbix latest data.

  • Name: zabbix_latestData
  • Text: {% if obj.cf["zabbix_hostid"] %}Show host in Zabbix{% endif %}
  • URL: {ZABBIX_URL} /zabbix.php?action=latest.view&filter_hostids[]={{ obj.cf["zabbix_hostid"] }}&filter_application=&filter_select=&filter_set=1