mirror of
https://github.com/TheNetworkGuy/netbox-zabbix-sync.git
synced 2025-07-14 01:41:25 -06:00
Added some logging and fixed role assignment for VM's
This commit is contained in:
parent
c1504987f1
commit
5093823287
@ -1,13 +1,17 @@
|
||||
"""Module for all hostgroup related code"""
|
||||
from logging import getLogger
|
||||
from modules.exceptions import HostgroupError
|
||||
from modules.tools import build_path
|
||||
|
||||
class Hostgroup():
|
||||
"""Hostgroup class for devices and VM's
|
||||
Takes type (vm or dev) and NB object"""
|
||||
def __init__(self, obj_type, nb_obj, version):
|
||||
def __init__(self, obj_type, nb_obj, version, logger=None):
|
||||
self.logger = logger if logger else getLogger(__name__)
|
||||
if obj_type not in ("vm", "dev"):
|
||||
raise HostgroupError(f"Unable to create hostgroup with type {type}")
|
||||
msg = f"Unable to create hostgroup with type {type}"
|
||||
self.logger.error()
|
||||
raise HostgroupError(msg)
|
||||
self.type = str(obj_type)
|
||||
self.nb = nb_obj
|
||||
self.name = self.nb.name
|
||||
@ -33,9 +37,9 @@ class Hostgroup():
|
||||
# Role fix for Netbox <=3
|
||||
role = None
|
||||
if self.nb_version.startswith(("2", "3")) and self.type == "dev":
|
||||
role = self.nb.device_role.name
|
||||
role = self.nb.device_role.name if self.nb.device_role else None
|
||||
else:
|
||||
role = self.nb.role.name
|
||||
role = self.nb.role.name if self.nb.role else None
|
||||
# Add default formatting options
|
||||
# Check if a site is configured. A site is optional for VMs
|
||||
format_options["region"] = None
|
||||
@ -86,8 +90,10 @@ class Hostgroup():
|
||||
cf_data = self.custom_field_lookup(hg_item)
|
||||
# CF does not exist
|
||||
if not cf_data["result"]:
|
||||
raise HostgroupError(f"Unable to generate hostgroup for host {self.name}. "
|
||||
msg = (f"Unable to generate hostgroup for host {self.name}. "
|
||||
f"Item type {hg_item} not supported.")
|
||||
self.logger.error(msg)
|
||||
raise HostgroupError(msg)
|
||||
# CF data is populated
|
||||
if cf_data["cf"]:
|
||||
hg_output.append(cf_data["cf"])
|
||||
@ -100,9 +106,12 @@ class Hostgroup():
|
||||
# Check if the hostgroup is populated with at least one item.
|
||||
if bool(hg_output):
|
||||
return "/".join(hg_output)
|
||||
raise HostgroupError(f"Unable to generate hostgroup for host {self.name}."
|
||||
"Not enough valid items. This is most likely"
|
||||
"due to the use of custom fields that are empty.")
|
||||
msg = (f"Unable to generate hostgroup for host {self.name}."
|
||||
" Not enough valid items. This is most likely"
|
||||
" due to the use of custom fields that are empty"
|
||||
" or an invalid hostgroup format.")
|
||||
self.logger.error(msg)
|
||||
raise HostgroupError(msg)
|
||||
|
||||
def list_formatoptions(self):
|
||||
"""
|
||||
|
@ -3,6 +3,7 @@
|
||||
"""Module that hosts all functions for virtual machine processing"""
|
||||
|
||||
from os import sys
|
||||
from logging import getLogger
|
||||
from modules.device import PhysicalDevice
|
||||
from modules.hostgroups import Hostgroup
|
||||
from modules.interface import ZabbixInterface
|
||||
@ -23,11 +24,13 @@ class VirtualMachine(PhysicalDevice):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.hostgroup = None
|
||||
self.zbx_template_names = None
|
||||
if "logger" not in kwargs:
|
||||
self.logger = getLogger(__name__)
|
||||
|
||||
def set_hostgroup(self, hg_format, nb_site_groups, nb_regions):
|
||||
"""Set the hostgroup for this device"""
|
||||
# Create new Hostgroup instance
|
||||
hg = Hostgroup("vm", self.nb, self.nb_api_version)
|
||||
hg = Hostgroup("vm", self.nb, self.nb_api_version, logger=self.logger)
|
||||
hg.set_nesting(traverse_site_groups, traverse_regions, nb_site_groups, nb_regions)
|
||||
# Generate hostgroup based on hostgroup format
|
||||
self.hostgroup = hg.generate(hg_format)
|
||||
@ -35,7 +38,6 @@ class VirtualMachine(PhysicalDevice):
|
||||
def set_vm_template(self):
|
||||
""" Set Template for VMs. Overwrites default class
|
||||
to skip a lookup of custom fields."""
|
||||
self.zbx_template_names = None
|
||||
# Gather templates ONLY from the device specific context
|
||||
try:
|
||||
self.zbx_template_names = self.get_templates_context()
|
||||
|
@ -135,6 +135,7 @@ def main(arguments):
|
||||
try:
|
||||
vm = VirtualMachine(nb_vm, zabbix, netbox_journals, nb_version,
|
||||
create_journal, logger)
|
||||
logger.debug(f"Host {vm.name}: started operations on VM.")
|
||||
vm.set_hostgroup(vm_hostgroup_format,netbox_site_groups,netbox_regions)
|
||||
vm.set_vm_template()
|
||||
vm.set_inventory(nb_vm)
|
||||
@ -180,6 +181,7 @@ def main(arguments):
|
||||
# Set device instance set data such as hostgroup and template information.
|
||||
device = PhysicalDevice(nb_device, zabbix, netbox_journals, nb_version,
|
||||
create_journal, logger)
|
||||
logger.debug(f"Host {device.name}: started operations on device.")
|
||||
device.set_hostgroup(hostgroup_format,netbox_site_groups,netbox_regions)
|
||||
device.set_template(templates_config_context, templates_config_context_overrule)
|
||||
device.set_inventory(nb_device)
|
||||
|
Loading…
Reference in New Issue
Block a user