Added configuration file in response to #25

This commit is contained in:
TheNetworkGuy 2022-11-29 17:03:05 +01:00
parent e749829cf0
commit eef905acbe
3 changed files with 27 additions and 10 deletions

View File

@ -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

17
config.py.example Normal file
View File

@ -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"}

View File

@ -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'])