From 81113af519918c3a64a4a03989685fe6bdbea370 Mon Sep 17 00:00:00 2001 From: Domisiding Date: Wed, 12 Apr 2023 17:19:57 +0200 Subject: [PATCH] added option "secured_connections", shell script, README updated --- .gitignore | 1 + README.md | 27 +++++++++++++++++++-------- config.py.example | 3 +++ netbox-zabbix-sync.sh | 8 ++++++++ netbox_zabbix_sync.py | 7 +++++-- requirements.txt | 6 ++++-- 6 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 netbox-zabbix-sync.sh diff --git a/.gitignore b/.gitignore index ce91e5a..afef20e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ config.py # Byte-compiled / optimized / DLL files __pycache__/ +.idea/ *.py[cod] diff --git a/README.md b/README.md index da28931..d57abc1 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,12 @@ First time user? Copy the config.py.example file to config.py. This file is used cp config.py.example config.py ``` +#### Possible configurations +SSL check can be disabled for using the script in local environment. (Not recommended for productive systems) +``` +secured_connections = False +``` + ### Set environment variables Set the following environment variables ``` @@ -73,15 +79,20 @@ To make the user experience easier you could add a custom link that redirects us ``` python3 netbox_zabbix_sync.py ``` + +## Running the script as Shell Script +``` +./netbox-zabbix-sync.sh +``` ### 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. | -| -l | layout | Set the hostgroup layout. Default is site/manufacturer/dev_role. Posible options (seperated with '/'): site, manufacturer, dev_role, tenant | -| -v | verbose | Log with debugging on. | -| -j | journal | Create journal entries in Netbox when a host gets added, modified or deleted in Zabbix | -| -p | proxy-power | Force a full proxy sync (includes deleting the proxy in Zabbix if not present in config context in Netbox) | +| 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. | +| -l | layout | Set the hostgroup layout. Default is site/manufacturer/dev_role. Posible options (seperated with '/'): site, manufacturer, dev_role, tenant | +| -v | verbose | Log with debugging on. | +| -j | journal | Create journal entries in Netbox when a host gets added, modified or deleted in Zabbix | +| -p | proxy-power | Force a full proxy sync (includes deleting the proxy in Zabbix if not present in config context in Netbox) | #### Hostgroup In case of omitting the -H flag, manual hostgroup creation is required for devices in a new category. diff --git a/config.py.example b/config.py.example index 1c399f2..8dc1333 100644 --- a/config.py.example +++ b/config.py.example @@ -1,3 +1,6 @@ +# Set Options (Not recommended for productive use) +secured_connections = True + # Set template and device Netbox "custom field" names template_cf = "zabbix_template" device_cf = "zabbix_hostid" diff --git a/netbox-zabbix-sync.sh b/netbox-zabbix-sync.sh new file mode 100644 index 0000000..7c28f24 --- /dev/null +++ b/netbox-zabbix-sync.sh @@ -0,0 +1,8 @@ +#!/bin/bash +export ZABBIX_HOST="https://zabbix.local" +export ZABBIX_USER="username" +export ZABBIX_PASS="Password" +export NETBOX_HOST="https://netbox.local" +export NETBOX_TOKEN="secrettoken" + +python3 netbox_zabbix_sync.py \ No newline at end of file diff --git a/netbox_zabbix_sync.py b/netbox_zabbix_sync.py index b515616..3188d07 100755 --- a/netbox_zabbix_sync.py +++ b/netbox_zabbix_sync.py @@ -9,7 +9,7 @@ from pyzabbix import ZabbixAPI, ZabbixAPIException try: from config import * except ModuleNotFoundError: - print(f"Configuration file config.py not found in main directory." + print(f"Configuration file config.py.example not found in main directory." "Please create the file or rename the config.py.example file to config.py.") sys.exit(0) @@ -51,6 +51,9 @@ def main(arguments): netbox_token = environ.get("NETBOX_TOKEN") # Set Netbox API netbox = api(netbox_host, token=netbox_token, threading=True) + # Set Connection Rules + if not secured_connections: + netbox.http_session.verify = False # Check if the provided Hostgroup layout is valid if(arguments.layout): hg_objects = arguments.layout.split("/") @@ -460,7 +463,7 @@ class NetworkDevice(): # Set Netbox custom field to hostID value. self.nb.custom_fields[device_cf] = int(self.zabbix_id) self.nb.save() - msg = f"Created host {self.name} in Zabbix." + msg = f"Created host {self.name} in Zabbix and edited the custom field in the netbox with {self.nb.custom_fields[device_cf]}" logger.info(msg) self.create_journal_entry("success", msg) else: diff --git a/requirements.txt b/requirements.txt index e6a1247..5d4ebf3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,4 @@ -pynetbox -pyzabbix +pynetbox~=7.0.1 +pyzabbix~=1.3.0 + +config~=0.5.1 \ No newline at end of file