diff --git a/README.md b/README.md index a587860..e90ac89 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,9 @@ Make sure that you have a python environment with the following packages install pynetbox pyzabbix ``` +### Config file +First time user? Copy the config.py.example file to config.py. This file is used for modifying filters and setting variables such as custom field names. + ### Cloning the repository ``` git clone https://github.com/TheNetworkGuy/netbox-zabbix-sync.git diff --git a/config.py.example b/config.py.example new file mode 100644 index 0000000..9009d1b --- /dev/null +++ b/config.py.example @@ -0,0 +1,17 @@ +# Set template and device Netbox "custom field" names +template_cf = "zabbix_template" +device_cf = "zabbix_hostid" + +# Netbox to Zabbix device state convertion +zabbix_device_removal = ["Decommissioning", "Inventory"] +zabbix_device_disable = ["Offline", "Planned", "Staged", "Failed"] + +# Custom filter for device filtering. Variable must be present but can be left empty with no filtering. +# A couple of examples are as follows: + +# nb_device_filter = {} #No filter +# nb_device_filter = {"tag": "zabbix"} #Use a tag +# nb_device_filter = {"site": "HQ-AMS"} #Use a site name + +# Default device filter, only get devices which have a name in Netbox. +nb_device_filter = {"name__n": "null"} \ No newline at end of file diff --git a/netbox_zabbix_sync.py b/netbox_zabbix_sync.py index 60decf0..4ab8acb 100755 --- a/netbox_zabbix_sync.py +++ b/netbox_zabbix_sync.py @@ -1,11 +1,16 @@ #!/usr/bin/python3 """Netbox to Zabbix sync script.""" -from os import environ, path +from os import environ, path, sys import logging import argparse from pynetbox import api from pyzabbix import ZabbixAPI, ZabbixAPIException +try: + from config import * +except ModuleNotFoundError: + print(f"Configuration file config.py not found in main directory. Please create the file or rename the config.py.example file to config.py.") + sys.exit(0) # Set logging log_format = logging.Formatter('%(asctime)s - %(name)s - ' @@ -24,14 +29,6 @@ logger.addHandler(lgout) logger.addHandler(lgfile) logger.setLevel(logging.WARNING) -# Set template and device Netbox "custom field" names -template_cf = "zabbix_template" -device_cf = "zabbix_hostid" - -# Netbox to Zabbix device state convertion -zabbix_device_removal = ["Decommissioning", "Inventory"] -zabbix_device_disable = ["Offline", "Planned", "Staged", "Failed"] - def main(arguments): """Run the sync process.""" @@ -75,7 +72,7 @@ def main(arguments): e = f"Zabbix returned the following error: {str(e)}." logger.error(e) # Get all Zabbix and Netbox data - netbox_devices = netbox.dcim.devices.filter(name__n="null") + netbox_devices = netbox.dcim.devices.filter(**nb_device_filter) netbox_journals = netbox.extras.journal_entries zabbix_groups = zabbix.hostgroup.get(output=['groupid', 'name']) zabbix_templates = zabbix.template.get(output=['templateid', 'name'])