added option "secured_connections", shell script, README updated

This commit is contained in:
Domisiding 2023-04-12 17:19:57 +02:00
parent f03926753a
commit 81113af519
6 changed files with 40 additions and 12 deletions

1
.gitignore vendored
View File

@ -2,4 +2,5 @@
config.py
# Byte-compiled / optimized / DLL files
__pycache__/
.idea/
*.py[cod]

View File

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

View File

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

8
netbox-zabbix-sync.sh Normal file
View File

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

View File

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

View File

@ -1,2 +1,4 @@
pynetbox
pyzabbix
pynetbox~=7.0.1
pyzabbix~=1.3.0
config~=0.5.1