Skip to content

Commit e24351a

Browse files
Merge pull request #49 from TheNetworkGuy/hostgroup_vars
Hostgroup vars
2 parents 2407a7a + 3f28986 commit e24351a

File tree

3 files changed

+173
-154
lines changed

3 files changed

+173
-154
lines changed

README.md

+89-82
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@ A script to create, update and delete Zabbix hosts using Netbox device objects.
55

66

77
## Installation
8-
### Packages
9-
Make sure that you have a python environment with the following packages installed.
10-
```
11-
pynetbox
12-
pyzabbix
13-
```
148

159
### Cloning the repository
1610
```
1711
git clone https://github.com/TheNetworkGuy/netbox-zabbix-sync.git
1812
```
1913

14+
### Packages
15+
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.
16+
```
17+
pynetbox
18+
pyzabbix
19+
```
20+
2021
### Config file
2122
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.
2223
```
@@ -52,6 +53,83 @@ You can make the hostID field hidden or read-only to prevent human intervention.
5253

5354
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.
5455

56+
## Config file
57+
58+
### Hostgroup
59+
Setting the create_hostgroups variable to False requires manual hostgroup creation for devices in a new category.
60+
61+
The format can be set with the hostgroup_format variable.
62+
63+
Make sure that the Zabbix user has proper permissions to create hosts.
64+
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.
65+
66+
#### layout
67+
The default hostgroup layout is "site/manufacturer/device_role".
68+
69+
**Variables**
70+
71+
You can change this behaviour with the hostgroup_format variable. The following values can be used:
72+
| name | description |
73+
| ------------ | ------------ |
74+
|dev_location|The device location name|
75+
|dev_role|The device role name|
76+
|manufacturer|Manufacturer name|
77+
|region|The region name of the device|
78+
|site|Site name|
79+
|site_group|Site group name|
80+
|tenant|Tenant name|
81+
|tenant_group|Tenant group name|
82+
83+
84+
You can specify the value like so, sperated by a "/":
85+
```
86+
hostgroup_format = "tenant/site/dev_location/dev_role"
87+
```
88+
**custom fields**
89+
90+
You can also use the value of custom fields under the device object.
91+
92+
This allows more freedom and even allows a full static mapping instead of a dynamic rendered hostgroup name.
93+
```
94+
hostgroup_format = "site/mycustomfieldname"
95+
```
96+
**Empty variables or hostgroups**
97+
98+
Should the content of a variable be empty, then the hostgroup position is skipped.
99+
100+
For example, consider the following scenario with 2 devices, both the same device type and site. One of them is linked to a tenant, the other one does not have a relationship with a tenant.
101+
- Device_role: PDU
102+
- Site: HQ-AMS
103+
```
104+
hostgroup_format = "site/tenant/device_role"
105+
```
106+
When running the script like above, the following hostgroup (HG) will be generated for both hosts:
107+
- Device A with no relationship with a tenant: HQ-AMS/PDU
108+
- Device B with a relationship to tenant "Fork Industries": HQ-AMS/Fork Industries/PDU
109+
110+
The same logic applies to custom fields being used in the HG format:
111+
```
112+
hostgroup_format = "site/mycustomfieldname"
113+
```
114+
For device A with the value "ABC123" in the custom field "mycustomfieldname" -> HQ-AMS/ABC123
115+
For a device which does not have a value in the custom field "mycustomfieldname" -> HQ-AMS
116+
117+
Should there be a scenario where a custom field does not have a value under a device, and the HG format only uses this single variable, then this will result in an error:
118+
```
119+
hostgroup_format = "mycustomfieldname"
120+
121+
Netbox-Zabbix-sync - ERROR - ESXI1 has no reliable hostgroup. This is most likely due to the use of custom fields that are empty.
122+
```
123+
### Device status
124+
By setting a status on a Netbox device you determine how the host is added (or updated) in Zabbix. There are, by default, 3 options:
125+
* Delete the host from Zabbix (triggered by Netbox status "Decommissioning" and "Inventory")
126+
* Create the host in Zabbix but with a disabled status (Trigger by "Offline", "Planned", "Staged" and "Failed")
127+
* Create the host in Zabbix with an enabled status (For now only enabled with the "Active" status)
128+
129+
You can modify this behaviour by changing the following list variables in the script:
130+
- zabbix_device_removal
131+
- zabbix_device_disable
132+
55133
### Template source
56134
You can either use a Netbox device type custom field or Netbox config context for the Zabbix template information.
57135

@@ -106,83 +184,9 @@ python3 netbox_zabbix_sync.py
106184
### Flags
107185
| Flag | Option | Description |
108186
| ------------ | ------------ | ------------ |
109-
| -c | cluster | For clustered devices: only add the primary node of a cluster and use the cluster name as hostname. |
110-
| -H | hostgroup | Create non-existing hostgroups in Zabbix. Usefull for a first run to add all required hostgroups. |
111-
| -l | layout | Set the hostgroup layout. Default is site/manufacturer/dev_role. Posible options (seperated with '/'): site, manufacturer, dev_role, tenant |
112187
| -v | verbose | Log with debugging on. |
113-
| -j | journal | Create journal entries in Netbox when a host gets added, modified or deleted in Zabbix |
114-
| -p | proxy-power | Force a full proxy sync (includes deleting the proxy in Zabbix if not present in config context in Netbox) |
115-
116-
#### Hostgroup
117-
In case of omitting the -H flag, manual hostgroup creation is required for devices in a new category.
118-
119-
The format can be set with the -l flag. If not provided the default format will be:
120-
{Site name}/{Manufacturer name}/{Device role name}
121-
122-
Make sure that the Zabbix user has proper permissions to create hosts.
123-
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.
124-
125-
#### layout
126-
The default hostgroup layout is "site/manufacturer/device_role".
127-
128-
**Variables**
129-
130-
You can change this behaviour with the --layout flag. The following variables can be used:
131-
| name | description |
132-
| ------------ | ------------ |
133-
|tenant|Tenant name|
134-
|site|Site name|
135-
|manufacturer|Manufacturer name|
136-
|device_role|The device role name|
137-
138-
You can specify the variables like so, sperated by a "/":
139-
```
140-
python3 netbox_zabbix_sync.py -l tenant/site/device_role
141-
```
142-
**custom fields**
143-
144-
You can also use the value of custom fields under the device object.
145-
146-
This allows more freedom and even allows a ful static mapping instead of a dynamic rendered hostgroup name.
147-
```
148-
python3 netbox_zabbix_sync.py -l site/mycustomfieldname
149-
```
150-
**Empty variables or hostgroups**
151188

152-
Should the content of a variable be empty, then the hostgroup position is skipped.
153-
154-
For example, consider the following scenario with 2 devices, both the same device type and site. One of them is linked to a tenant, the other one does not have a relationship with a tenant.
155-
- Device_role: PDU
156-
- Site: HQ-AMS
157-
```
158-
python3 netbox_zabbix_sync.py -l site/tenant/device_role
159-
```
160-
When running the script like above, the following hostgroup (HG) will be generated for both hosts:
161-
- Device A with no relationship with a tenant: HQ-AMS/PDU
162-
- Device B with a relationship to tenant "Fork Industries": HQ-AMS/Fork Industries/PDU
163-
164-
The same logic applies to custom fields being used in the HG format:
165-
```
166-
python3 netbox_zabbix_sync.py -l site/mycustomfieldname
167-
```
168-
For device A with the value "ABC123" in the custom field "mycustomfieldname" -> HQ-AMS/ABC123
169-
For a device which does not have a value in the custom field "mycustomfieldname" -> HQ-AMS
170-
171-
Should there be a scenario where a custom field does not have a value under a device, and the HG format only uses this signle variable, then this will result in an error:
172-
```
173-
python3 netbox_zabbix_sync.py -l mycustomfieldname
174-
175-
Netbox-Zabbix-sync - ERROR - ESXI1 has no reliable hostgroup. This is most likely due to the use of custom fields that are empty.
176-
```
177-
### Device status
178-
By setting a status on a Netbox device you determine how the host is added (or updated) in Zabbix. There are, by default, 3 options:
179-
* Delete the host from Zabbix (triggered by Netbox status "Decommissioning" and "Inventory")
180-
* Create the host in Zabbix but with a disabled status (Trigger by "Offline", "Planned", "Staged" and "Failed")
181-
* Create the host in Zabbix with an enabled status (For now only enabled with the "Active" status)
182-
183-
You can modify this behaviour by changing the following list variables in the script:
184-
- zabbix_device_removal
185-
- zabbix_device_disable
189+
## Config context
186190

187191
### Zabbix proxy
188192
You can set the proxy for a device using the 'proxy' key in config context.
@@ -193,7 +197,7 @@ You can set the proxy for a device using the 'proxy' key in config context.
193197
}
194198
}
195199
```
196-
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, use the -p flag.
200+
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.
197201

198202
### Set interface parameters within Netbox
199203
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:
@@ -256,4 +260,7 @@ To configure the interface parameters you'll need to use custom context. Custom
256260
}
257261
}
258262
```
263+
264+
I would recommend using macros for sensitive data such as community strings since the data in Netbox is plain-text.
265+
259266
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.

config.py.example

+19
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,29 @@ templates_config_context_overrule = False
1111
template_cf = "zabbix_template"
1212
device_cf = "zabbix_hostid"
1313

14+
# Enable clustering of devices with virtual chassis setup
15+
clustering = False
16+
17+
# Enable hostgroup generation. Requires permissions in Zabbix
18+
create_hostgroups = True
19+
20+
# Create journal entries
21+
create_journal = False
22+
23+
# Set to true to enable removal of proxy's under hosts. Use with caution and make sure that you specified
24+
# all the required proxy's in the device config context before enabeling this option.
25+
# With this option disabled proxy's will only be added and modified for Zabbix hosts.
26+
full_proxy_sync = False
27+
1428
# Netbox to Zabbix device state convertion
1529
zabbix_device_removal = ["Decommissioning", "Inventory"]
1630
zabbix_device_disable = ["Offline", "Planned", "Staged", "Failed"]
1731

32+
# Hostgroup mapping
33+
# Available choices: dev_location, dev_role, manufacturer, region, site, site_group, tenant, tenant_group
34+
# You can also use CF (custom field) names under the device. The CF content will be used for the hostgroup generation.
35+
hostgroup_format = "site/manufacturer/dev_role"
36+
1837
# Custom filter for device filtering. Variable must be present but can be left empty with no filtering.
1938
# A couple of examples are as follows:
2039

0 commit comments

Comments
 (0)