Merge pull request #3 from q1x/vm_inventory

Vm inventory
This commit is contained in:
Raymond Kuiper 2025-02-12 15:15:43 +01:00 committed by GitHub
commit 4264dc9b31
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 38 additions and 18 deletions

2
.gitignore vendored
View File

@ -1,6 +1,8 @@
*.log *.log
.venv .venv
config.py config.py
Pipfile
Pipfile.lock
# Byte-compiled / optimized / DLL files # Byte-compiled / optimized / DLL files
__pycache__/ __pycache__/
*.py[cod] *.py[cod]

View File

@ -80,10 +80,10 @@ inventory_sync = False
# For nested properties, you can use the '/' seperator. # 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: # For example, the following map will assign the custom field 'mycustomfield' to the 'alias' Zabbix inventory field:
# #
# inventory_map = { "custom_fields/mycustomfield/name": "alias"} # device_inventory_map = { "custom_fields/mycustomfield/name": "alias"}
# #
# The following map should provide some nice defaults: # The following maps should provide some nice defaults:
inventory_map = { "asset_tag": "asset_tag", device_inventory_map = { "asset_tag": "asset_tag",
"virtual_chassis/name": "chassis", "virtual_chassis/name": "chassis",
"status/label": "deployment_status", "status/label": "deployment_status",
"location/name": "location", "location/name": "location",
@ -96,3 +96,8 @@ inventory_map = { "asset_tag": "asset_tag",
"device_type/model": "type", "device_type/model": "type",
"device_type/manufacturer/name": "vendor", "device_type/manufacturer/name": "vendor",
"oob_ip/address": "oob_ip" } "oob_ip/address": "oob_ip" }
# We also support inventory mapping on Virtual Machines.
vm_inventory_map = { "status/label": "deployment_status",
"comments": "notes",
"name": "name" }

View File

@ -11,6 +11,7 @@ from modules.exceptions import (SyncInventoryError, TemplateError, SyncExternalE
InterfaceConfigError, JournalError) InterfaceConfigError, JournalError)
from modules.interface import ZabbixInterface from modules.interface import ZabbixInterface
from modules.hostgroups import Hostgroup from modules.hostgroups import Hostgroup
try: try:
from config import ( from config import (
template_cf, device_cf, template_cf, device_cf,
@ -18,7 +19,7 @@ try:
traverse_regions, traverse_regions,
inventory_sync, inventory_sync,
inventory_mode, inventory_mode,
inventory_map device_inventory_map
) )
except ModuleNotFoundError: except ModuleNotFoundError:
print("Configuration file config.py not found in main directory." print("Configuration file config.py not found in main directory."
@ -62,6 +63,10 @@ class PhysicalDevice():
def __str__(self): def __str__(self):
return self.__repr__() return self.__repr__()
def _inventory_map(self):
""" Use device inventory maps """
return device_inventory_map
def _setBasics(self): def _setBasics(self):
""" """
Sets basic information like IP address. Sets basic information like IP address.
@ -181,7 +186,7 @@ class PhysicalDevice():
if inventory_sync and self.inventory_mode in [0,1]: if inventory_sync and self.inventory_mode in [0,1]:
self.logger.debug(f"Host {self.name}: Starting inventory mapper") self.logger.debug(f"Host {self.name}: Starting inventory mapper")
# Let's build an inventory dict for each property in the inventory_map # Let's build an inventory dict for each property in the inventory_map
for nb_inv_field, zbx_inv_field in inventory_map.items(): for nb_inv_field, zbx_inv_field in self._inventory_map().items():
field_list = nb_inv_field.split("/") # convert str to list based on delimiter field_list = nb_inv_field.split("/") # convert str to list based on delimiter
# start at the base of the dict... # start at the base of the dict...
value = nbdevice value = nbdevice
@ -542,7 +547,7 @@ class PhysicalDevice():
selectGroups=["groupid"], selectGroups=["groupid"],
selectHostGroups=["groupid"], selectHostGroups=["groupid"],
selectParentTemplates=["templateid"], selectParentTemplates=["templateid"],
selectInventory=list(inventory_map.values())) selectInventory=list(self._inventory_map().values()))
if len(host) > 1: if len(host) > 1:
e = (f"Got {len(host)} results for Zabbix hosts " e = (f"Got {len(host)} results for Zabbix hosts "
f"with ID {self.zabbix_id} - hostname {self.name}.") f"with ID {self.zabbix_id} - hostname {self.name}.")

View File

@ -9,6 +9,9 @@ from modules.interface import ZabbixInterface
from modules.exceptions import TemplateError, InterfaceConfigError, SyncInventoryError from modules.exceptions import TemplateError, InterfaceConfigError, SyncInventoryError
try: try:
from config import ( from config import (
inventory_sync,
inventory_mode,
vm_inventory_map,
traverse_site_groups, traverse_site_groups,
traverse_regions traverse_regions
) )
@ -24,6 +27,10 @@ class VirtualMachine(PhysicalDevice):
self.hostgroup = None self.hostgroup = None
self.zbx_template_names = None self.zbx_template_names = None
def _inventory_map(self):
""" use VM inventory maps """
return vm_inventory_map
def set_hostgroup(self, hg_format, nb_site_groups, nb_regions): def set_hostgroup(self, hg_format, nb_site_groups, nb_regions):
"""Set the hostgroup for this device""" """Set the hostgroup for this device"""
# Create new Hostgroup instance # Create new Hostgroup instance

View File

@ -171,6 +171,7 @@ def main(arguments):
# Check if a valid hostgroup has been found for this VM. # Check if a valid hostgroup has been found for this VM.
if not vm.hostgroup: if not vm.hostgroup:
continue continue
vm.set_inventory(nb_vm)
# Checks if device is in cleanup state # Checks if device is in cleanup state
if vm.status in zabbix_device_removal: if vm.status in zabbix_device_removal:
if vm.zabbix_id: if vm.zabbix_id: