Skip to content

Commit

Permalink
Add "virt cluster type" to physical servers in a cluster.
Browse files Browse the repository at this point in the history
Also additional debug logging.
  • Loading branch information
eric-eisenhart committed Jun 18, 2024
1 parent 5e80139 commit adf1131
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
25 changes: 19 additions & 6 deletions src/sonic/netbox_zabbix/netbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ def __init__(self, logger, configobj):
@functools.cache
def get_vms_all(self) -> list:
"""Get all VMs from Netbox"""
log.debug("Get all VMs from Netbox")
return list(self.vms.all())

@functools.cache
def get_vms_active_soc_server(self) -> list:
log.debug("Get active SOC server VMs")
vms = self.get_vms_all()
return list(self.vms.filter(role="server", tenant="soc", status="active"))

Expand All @@ -50,23 +52,27 @@ def get_vms_active_soc_server(self) -> list:
####################
@functools.cache
def get_devices_all(self):
log.debug("Get all devices")
return self.devices.all()

@functools.cache
def get_devices_active_soc_server(self) -> list:
log.debug("Get Active SOC server devices")
return list(self.devices.filter(role="server", tenant="soc", status="active"))

##########################
# Hosts == VMs + Devices #
##########################
@functools.cache
def get_hosts_all(self) -> list:
log.debug("Get all hosts")
vms = self.get_vms_all()
devices = self.get_devices_all()
return list(vms) + list(devices)

@functools.cache
def get_hosts_active_soc_server(self) -> list:
log.debug("Get all active soc server hosts")
vms = self.get_vms_active_soc_server()
devices = self.get_devices_active_soc_server()
return list(vms) + list(devices)
Expand All @@ -77,14 +83,17 @@ def get_hosts_active_soc_server(self) -> list:

@functools.cache
def get_regions_all(self):
log.debug("Get all regions")
return self.api.dcim.regions.all()

@functools.cache
def get_sites_all(self):
log.debug("Get all sites")
return self.api.dcim.sites.all()

@functools.cache
def get_sites_smart_filter(self) -> list:
log.debug("Get sites that have at least 1 device")
sites = []
for site in self.get_sites_all():
if site.device_count >= 1:
Expand All @@ -93,13 +102,15 @@ def get_sites_smart_filter(self) -> list:

@functools.cache
def is_physical(self, server) -> bool:
log.debug(server)
if "device_type" in dict(server):
return True
else:
return False

@functools.cache
def is_virtual(self, server) -> bool:
log.debug(server)
if "memory" in dict(server):
return True
else:
Expand All @@ -108,18 +119,19 @@ def is_virtual(self, server) -> bool:
@functools.cache
def get_cluster_by_id(self, id):
"""This basically just exists for caching"""
log.debug(id)
return self.api.virtualization.clusters.get(id)

@functools.cache
def virt_type(self, server) -> bool:
log.debug(server)
if self.is_physical(server):
log.debug("Physical Server")
return False
elif server.cluster:
if config.verbose >= 4:
log.debug(pformat(dict(server)))
if server.cluster:
log.debug("Has a cluster")
log.debug(server.cluster)
log.debug(pformat(dict(server.cluster)))
if config.verbose >= 4:
log.debug(pformat(dict(server.cluster)))
cluster = self.get_cluster_by_id(server.cluster["id"])
log.debug(f"resulting cluster: {pformat(cluster)}")
# server.cluster.full_details()
Expand All @@ -134,4 +146,5 @@ def virt_type(self, server) -> bool:
type = "VMware"
return type
else:
return "UNKNOWN"
log.debug("No server.cluster")
return False
22 changes: 21 additions & 1 deletion src/sonic/netbox_zabbix/netbox_zabbix.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,22 @@ def copy_netbox_info_to_zabbix_macros(self, netbox_servers, zabbix_servers):
}
)

if self.netbox.is_virtual(srv) and self.netbox.virt_type(srv):
macros.append(
{
"macro": "{$NETBOX.VIRT_TYPE}",
"value": self.netbox.virt_type(srv),
}
)

if self.netbox.is_physical(srv) and self.netbox.virt_type(srv):
macros.append(
{
"macro": "{$NETBOX.VIRT_CLUSTER_TYPE}",
"value": self.netbox.virt_type(srv),
}
)

macros.append(
{
"macro": "{$NETBOX.DATE.CREATED}",
Expand Down Expand Up @@ -272,6 +288,9 @@ def copy_netbox_info_to_zabbix_tags(self, netbox_servers, zabbix_servers):
if self.netbox.is_virtual(srv) and self.netbox.virt_type(srv):
tags.append({"tag": "netbox-virt-type", "value": self.netbox.virt_type(srv)})

if self.netbox.is_physical(srv) and self.netbox.virt_type(srv):
tags.append({"tag": "netbox-virt-cluster-type", "value": self.netbox.virt_type(srv)})

if self.netbox.is_physical(srv) and srv.device_type and srv.device_type["slug"]:
tags.append({"tag": "netbox-device-type", "value": srv.device_type["slug"]})

Expand Down Expand Up @@ -427,7 +446,8 @@ def copy_netbox_info_to_zabbix_inventory(self, netbox_servers, zabbix_servers):
inventory["site_notes"] = "\n".join(site_info)
if self.netbox.is_virtual(srv):
inventory["hardware"] = "Virtual"
inventory["vendor"] = self.netbox.virt_type(srv)
if self.netbox.virt_type(srv):
inventory["vendor"] = self.netbox.virt_type(srv)

if self.netbox.is_physical(srv):
inventory["hardware"] = "Physical" # Discard if have better answer
Expand Down

0 comments on commit adf1131

Please sign in to comment.