52 Commits
1.0 ... 1.1

Author SHA1 Message Date
TheNetworkGuy
0b9b8a4898 Fixed container path 2024-05-22 09:40:28 +02:00
TheNetworkGuy
441d7e7e95 Temporary disabled too-many-arguments error for Pylint. 2024-05-22 09:35:33 +02:00
TheNetworkGuy
c185b7364d Fixes #57. 2024-05-22 09:24:17 +02:00
Twan K
e56451f5e1 Update README.md
Fixed Docker package link
2024-05-21 15:38:10 +02:00
Twan K
dee6a079a5 Update README.md 2024-05-21 15:23:20 +02:00
Twan K
2b62caca85 Update README.md
Updated to force pipeline container build
2024-05-21 15:06:41 +02:00
Twan K
4eed151e22 Merge pull request #55 from q1x/main
Code cleanup and automated GitHub pylint workflow
2024-04-10 22:05:34 +02:00
Raymond Kuiper
3e638c6f78 Update netbox_zabbix_sync.py
Minor bug fix for empty Netbox to zabbix inventory field mappings.
2024-04-10 14:57:08 +02:00
Raymond Kuiper
634f4b77d5 tweaked exception handling 2024-03-28 09:51:08 +01:00
Raymond Kuiper
c006e7feb5 Let's make pylint happy :) 2024-03-27 20:35:32 +01:00
Raymond Kuiper
091c9746c0 Fixed proxy issue, rewrite of inventory logic (eval was ugly) 2024-03-27 20:33:02 +01:00
Raymond Kuiper
364d376f55 corrected even more linting errors 2024-03-27 16:33:06 +01:00
Raymond Kuiper
ab2a341fa7 Corrected more linting errors 2024-03-27 16:26:15 +01:00
Raymond Kuiper
fbb9eeb48c Corrected linting errors 2024-03-27 16:24:26 +01:00
Raymond Kuiper
5b08d27a5e Added support for syncing Zabbix Inventory, this is also a fix for https://github.com/TheNetworkGuy/netbox-zabbix-sync/issues/44 2024-03-27 15:37:50 +01:00
Raymond Kuiper
583d845c40 revert because of file formatting issue 2024-03-27 15:22:43 +01:00
Raymond Kuiper
27a4a5c6eb Corrected more pylint errors 2024-03-25 11:57:46 +01:00
Raymond Kuiper
537710a4b9 Corrected pylint errors 2024-03-25 11:56:17 +01:00
Raymond Kuiper
5defc1a25e Corrected another MarkDown error 2024-03-25 11:52:10 +01:00
Raymond Kuiper
d6973dc32d Corrected MarkDown error 2024-03-25 11:51:12 +01:00
Raymond Kuiper
71f604a6f6 Added functionality to build full region and site_group paths to be used in hostgroup names. 2024-03-25 11:49:41 +01:00
Raymond Kuiper
b94a0df02d Merge pull request #1 from q1x/dockertest
Added Dockerfile and workflow to build images
2024-03-13 14:29:41 +01:00
Raymond Kuiper
3079a88de8 better docker logs example. 2024-03-13 14:00:03 +01:00
Raymond Kuiper
4aa8b6d2fb updated README.md with Docker instructions 2024-03-13 13:55:46 +01:00
Raymond Kuiper
e82631c89d modified tags 2024-03-13 13:16:22 +01:00
Raymond Kuiper
18d29c98d3 updated tags 2024-03-13 11:58:40 +01:00
Raymond Kuiper
661ce88287 updated versions 2024-03-13 11:50:43 +01:00
Raymond Kuiper
4b7f3ec0b9 try a different way of publishing 2024-03-13 11:47:45 +01:00
Raymond Kuiper
3a39c314be removed on: push from pylint 2024-03-13 11:42:28 +01:00
Raymond Kuiper
bf325c6839 testing workflow 2024-03-13 11:38:07 +01:00
Raymond Kuiper
5922d3e8ae allow call from another workflow 2024-03-11 11:06:31 +01:00
Raymond Kuiper
dcd84e836b Chained in quality check 2024-03-11 11:03:37 +01:00
Raymond Kuiper
33cf3e5358 changed back to checkout@v4 and commited Dockerfile 2024-03-08 22:53:34 +01:00
Raymond Kuiper
7c988f9ff8 changed over to checkout@v3 2024-03-08 22:51:53 +01:00
Raymond Kuiper
d46b749af0 corrected typos 2024-03-08 22:48:57 +01:00
Raymond Kuiper
e05c35a3ea added container building workflow 2024-03-08 22:44:25 +01:00
Raymond Kuiper
142aae75e0 removed directory 2024-03-08 14:56:30 +01:00
Raymond Kuiper
c538c51b7b minor README.md update 2024-03-08 14:54:48 +01:00
Raymond Kuiper
0d7c581ee2 fixed undefined-variable 2024-03-08 14:29:12 +01:00
Raymond Kuiper
c684ac4a9d Futher cleanup 2024-03-08 14:23:47 +01:00
Raymond Kuiper
2fcd21a723 code cleanup 2024-03-08 14:00:15 +01:00
Raymond Kuiper
23bef6b549 disable pylint module name checks 2024-03-08 13:52:08 +01:00
Raymond Kuiper
0d02e096e9 Disable pylint name checking 2024-03-08 13:49:18 +01:00
Raymond Kuiper
3c7079117a changed versions for workflow (again) 2024-03-08 13:40:31 +01:00
Raymond Kuiper
89d5f22064 changed versions for workflow 2024-03-08 13:36:42 +01:00
Raymond Kuiper
15d40873b0 rename actions to workflows 2024-03-08 13:32:29 +01:00
Raymond Kuiper
de8143e89f Cleaning up code 2024-03-08 13:30:21 +01:00
Twan K
173fdbf19f Merge pull request #54 from q1x/zabbix-api-token
Support for Zabbix api token
2024-03-06 17:37:00 +01:00
Twan K
d55bb4053b Merge pull request #53 from q1x/zabbix-7.0.0-compatibility
Zabbix 7.0.0 compatibility
2024-03-06 17:35:27 +01:00
Raymond Kuiper
c8e42b366f Added support for Zabbix API tokens and updated README.md 2024-03-01 08:51:07 +01:00
Raymond Kuiper
1f4a81e2e4 Implemented workaround for Zabbix 7.0.x API changes. 2024-03-01 03:33:31 +01:00
Raymond Kuiper
8aba95525b Update in Zabbix proxy logic
- Fixed var typo (`proxys` vs `proxies`)
- Workaround for Zabbix 7.0.x API changes in `proxy.get()`: `host` was replaced with `name`.
2024-02-23 15:53:48 +01:00
6 changed files with 538 additions and 217 deletions

46
.github/workflows/publish-image.yml vendored Normal file
View File

@@ -0,0 +1,46 @@
name: Publish Docker image to GHCR on a new version
on:
push:
branches:
- main
- dockertest
# tags:
# - [0-9]+.*
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
test_quality:
uses: ./.github/workflows/quality.yml
build_and_publish:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v4
- name: Log in to the container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GHCR_PAT }}
- name: Extract metadata (tags, labels)
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=semver,pattern={{ version }}
type=ref,event=branch
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'master') }}
type=sha
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

26
.github/workflows/quality.yml vendored Normal file
View File

@@ -0,0 +1,26 @@
---
name: Pylint Quality control
on:
workflow_call
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.11","3.12"]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pylint
pip install -r requirements.txt
- name: Analysing the code with pylint
run: |
pylint --module-naming-style=any $(git ls-files '*.py')

9
Dockerfile Normal file
View File

@@ -0,0 +1,9 @@
# syntax=docker/dockerfile:1
FROM python:3.12-alpine
RUN mkdir -p /opt/netbox-zabbix
COPY . /opt/netbox-zabbix
WORKDIR /opt/netbox-zabbix
RUN if ! [ -f ./config.py ]; then cp ./config.py.example ./config.py; fi
RUN pip install -r ./requirements.txt
ENTRYPOINT ["python"]
CMD ["/opt/netbox-zabbix/netbox_zabbix_sync.py", "-v"]

View File

@@ -4,7 +4,33 @@
A script to create, update and delete Zabbix hosts using Netbox device objects.
## Installation
## Installation via Docker
To pull the latest stable version to your local cache, use the following docker pull command:
```
docker pull ghcr.io/thenetworkguy/netbox-zabbix-sync:main
```
Make sure to specify the needed environment variables for the script to work (see [here](#set-environment-variables))
on the command line or use an [env file](https://docs.docker.com/reference/cli/docker/container/run/#env).
```
docker run -d -t -i -e ZABBIX_HOST='https://zabbix.local' \
-e ZABBIX_TOKEN='othersecrettoken' \
-e NETBOX_HOST='https://netbox.local' \
-e NETBOX_TOKEN='secrettoken' \
--name netbox-zabbix-sync ghcr.io/TheNetworkGuy/netbox-zabbix-sync:latest
```
This should run a one-time sync, you can check the sync with `docker logs netbox-zabbix-sync`.
The image uses the default `config.py` for it's configuration, you can use a volume mount in the docker run command
to override with your own config file if needed (see [config file](#config-file)):
```
docker run -d -t -i -v $(pwd)/config.py:/opt/netbox-zabbix/config.py ...
```
## Installation from Source
### Cloning the repository
```
@@ -12,20 +38,20 @@ git clone https://github.com/TheNetworkGuy/netbox-zabbix-sync.git
```
### Packages
Make sure that you have a python environment with the following packages installed. You can also use the requirements.txt file for installation with pip.
Make sure that you have a python environment with the following packages installed. You can also use the `requirements.txt` file for installation with pip.
```
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.
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.
```
cp config.py.example config.py
```
### Set environment variables
Set the following environment variables
Set the following environment variables:
```
ZABBIX_HOST="https://zabbix.local"
ZABBIX_USER="username"
@@ -33,6 +59,14 @@ ZABBIX_PASS="Password"
NETBOX_HOST="https://netbox.local"
NETBOX_TOKEN="secrettoken"
```
Or, you can use a Zabbix API token to login instead of using a username and password.
In that case `ZABBIX_USER` and `ZABBIX_PASS` will be ignored.
```
ZABBIX_TOKEN=othersecrettoken
```
### Netbox custom fields
Use the following custom fields in Netbox (if you are using config context for the template information then the zabbix_template field is not required):
```
@@ -49,21 +83,21 @@ Use the following custom fields in Netbox (if you are using config context for t
* Default: null
* Object: dcim > device_type
```
You can make the hostID field hidden or read-only to prevent human intervention.
You can make the `zabbix_hostid` field hidden or read-only to prevent human intervention.
This is optional and there is a use case for leaving it read-write in the UI to manually change the ID. For example to re-run a sync.
## Config file
### Hostgroup
Setting the create_hostgroups variable to False requires manual hostgroup creation for devices in a new category.
Setting the `create_hostgroups` variable to `False` requires manual hostgroup creation for devices in a new category.
The format can be set with the hostgroup_format variable.
The format can be set with the `hostgroup_format` variable.
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.
#### layout
#### Layout
The default hostgroup layout is "site/manufacturer/device_role".
**Variables**
@@ -85,7 +119,13 @@ You can specify the value like so, sperated by a "/":
```
hostgroup_format = "tenant/site/dev_location/dev_role"
```
**custom fields**
**Group traversal**
The default behaviour for `region` is to only use the directly assigned region in the rendered hostgroup name.
However, by setting `traverse_region` to `True` in `config.py` the script will render a full region path of all parent regions for the hostgroup name.
`traverse_site_groups` controls the same behaviour for site_groups.
**Custom fields**
You can also use the value of custom fields under the device object.
@@ -127,8 +167,26 @@ By setting a status on a Netbox device you determine how the host is added (or u
* Create the host in Zabbix with an enabled status (For now only enabled with the "Active" status)
You can modify this behaviour by changing the following list variables in the script:
- zabbix_device_removal
- zabbix_device_disable
- `zabbix_device_removal`
- `zabbix_device_disable`
### Zabbix Inventory
This script allows you to enable the inventory on managed Zabbix hosts and sync NetBox device properties to the specified inventory fields.
To enable, set `inventory_sync` to `True`.
Set `inventory_automatic` to `False` to use manual inventory, or `True` for automatic.
See [Zabix Manual](https://www.zabbix.com/documentation/current/en/manual/config/hosts/inventory#building-inventory) for more information about the modes.
Use the `inventory_map` variable to map which NetBox properties are used in which Zabbix Inventory fields.
For nested properties, you can use the '/' seperator.
For example, the following map will assign the custom field 'mycustomfield' to the 'alias' Zabbix inventory field:
```
inventory_sync = True
inventory_automatic = True
inventory_map = { "custom_fields/mycustomfield/name": "alias"}
```
See `config.py.example` for an extensive example map.
Any Zabix Inventory fields that are not included in the map will not be touched by the script,
so you can safely add manual values or use items to automatically add values to other fields.
### Template source
You can either use a Netbox device type custom field or Netbox config context for the Zabbix template information.
@@ -197,7 +255,7 @@ You can set the proxy for a device using the 'proxy' key in config context.
}
}
```
Because of the posible amount of destruction when setting up Netbox but forgetting the proxy command, the sync works a bit different. By default everything is synced except in a situation where the Zabbix host has a proxy configured but nothing is configured in Netbox. To force deletion and a full sync, set the full_proxy_sync variable in the config file.
Because of the possible amount of destruction when setting up Netbox but forgetting the proxy command, the sync works a bit different. By default everything is synced except in a situation where the Zabbix host has a proxy configured but nothing is configured in Netbox. To force deletion and a full sync, set the `full_proxy_sync` variable in the config file.
### Set interface parameters within Netbox
When adding a new device, you can set the interface type with custom context. By default, the following configuration is applied when no config context is provided:
@@ -263,4 +321,4 @@ To configure the interface parameters you'll need to use custom context. Custom
I would recommend using macros for sensitive data such as community strings since the data in Netbox is plain-text.
Note: Not all SNMP data is required for a working configuration. [The following parameters are allowed ](https://www.zabbix.com/documentation/current/manual/api/reference/hostinterface/object#details_tag "The following parameters are allowed ")but are not all required, depending on your environment.
> **_NOTE:_** Not all SNMP data is required for a working configuration. [The following parameters are allowed ](https://www.zabbix.com/documentation/current/manual/api/reference/hostinterface/object#details_tag "The following parameters are allowed ")but are not all required, depending on your environment.

View File

@@ -1,7 +1,8 @@
# Template logic.
## Template logic.
# Set to true to enable the template source information
# coming from config context instead of a custom field.
templates_config_context = False
# Set to true to give config context templates a
# higher priority then custom field templates
templates_config_context_overrule = False
@@ -11,37 +12,76 @@ templates_config_context_overrule = False
template_cf = "zabbix_template"
device_cf = "zabbix_hostid"
# Enable clustering of devices with virtual chassis setup
## Enable clustering of devices with virtual chassis setup
clustering = False
# Enable hostgroup generation. Requires permissions in Zabbix
## Enable hostgroup generation. Requires permissions in Zabbix
create_hostgroups = True
# Create journal entries
## Create journal entries
create_journal = False
## Proxy Sync
# Set to true to enable removal of proxy's under hosts. Use with caution and make sure that you specified
# all the required proxy's in the device config context before enabeling this option.
# With this option disabled proxy's will only be added and modified for Zabbix hosts.
full_proxy_sync = False
# Netbox to Zabbix device state convertion
## Netbox to Zabbix device state convertion
zabbix_device_removal = ["Decommissioning", "Inventory"]
zabbix_device_disable = ["Offline", "Planned", "Staged", "Failed"]
# Hostgroup mapping
## Hostgroup mapping
# Available choices: dev_location, dev_role, manufacturer, region, site, site_group, tenant, tenant_group
# You can also use CF (custom field) names under the device. The CF content will be used for the hostgroup generation.
#
# When using region in the group name, the default behaviour is to use name of the directly assigned region.
# By setting traverse_regions to True the full path of all parent regions will be used in the hostgroup, e.g.:
#
# 'Global/Europe/Netherlands/Amsterdam' instead of just 'Amsterdam'.
#
# traverse_site_groups controls the same behaviour for any assigned site_groups.
hostgroup_format = "site/manufacturer/dev_role"
traverse_regions = False
traverse_site_groups = False
# Custom filter for device filtering. Variable must be present but can be left empty with no filtering.
# A couple of examples are as follows:
## Filtering
# Custom device filter, variable must be present but can be left empty with no filtering.
# A couple of examples:
# nb_device_filter = {} #No filter
# nb_device_filter = {"tag": "zabbix"} #Use a tag
# nb_device_filter = {"site": "HQ-AMS"} #Use a site name
# nb_device_filter = {"site": ["HQ-AMS", "HQ-FRA"]} #Device must be in either one of these sites
# nb_device_filter = {"site": "HQ-AMS", "tag": "zabbix", "role__n": ["PDU", "console-server"]} #Device must be in site HQ-AMS, have the tag zabbix and must not be part of the PDU or console-server role
# nb_device_filter = {} #No filter
# nb_device_filter = {"tag": "zabbix"} #Use a tag
# nb_device_filter = {"site": "HQ-AMS"} #Use a site name
# nb_device_filter = {"site": ["HQ-AMS", "HQ-FRA"]} #Device must be in either one of these sites
# nb_device_filter = {"site": "HQ-AMS", "tag": "zabbix", "role__n": ["PDU", "console-server"]} #Device must be in site HQ-AMS, have the tag zabbix and must not be part of the PDU or console-server role
# Default device filter, only get devices which have a name in Netbox:
nb_device_filter = {"name__n": "null"}
# Default device filter, only get devices which have a name in Netbox.
nb_device_filter = {"name__n": "null"}
## Inventory
# To allow syncing of NetBox device properties, set inventory_sync to True
inventory_sync = False
# Set inventory_automatic to False to use manual inventory, True for automatic
# See https://www.zabbix.com/documentation/current/en/manual/config/hosts/inventory#building-inventory
inventory_automatic = True
# inventory_map is used to map NetBox properties to Zabbix Inventory fields.
# For nested properties, you can use the '/' seperator.
# For example, the following map will assign the custom field 'mycustomfield' to the 'alias' Zabbix inventory field:
#
# inventory_map = { "custom_fields/mycustomfield/name": "alias"}
#
# The following map should provide some nice defaults:
inventory_map = { "asset_tag": "asset_tag",
"virtual_chassis/name": "chassis",
"status/label": "deployment_status",
"location/name": "location",
"latitude": "location_lat",
"longitude": "location_lon",
"comments": "notes",
"name": "name",
"rack/name": "site_rack",
"serial": "serialno_a",
"device_type/model": "type",
"device_type/manufacturer/name": "vendor",
"oob_ip/address": "oob_ip" }

File diff suppressed because it is too large Load Diff