netbox-agent/netbox_agent/virtualmachine.py

152 lines
4.7 KiB
Python
Raw Permalink Normal View History

import os
2024-10-21 12:55:54 +02:00
from pprint import pprint
import netbox_agent.dmidecode as dmidecode
from netbox_agent.config import config
from netbox_agent.config import netbox_instance as nb
from netbox_agent.location import Tenant
from netbox_agent.logging import logging # NOQA
2024-10-21 12:55:54 +02:00
from netbox_agent.misc import create_netbox_tags, get_device_platform, get_hostname
from netbox_agent.network import VirtualNetwork
def is_vm(dmi):
2024-10-21 12:55:54 +02:00
bios = dmidecode.get_by_type(dmi, "BIOS")[0]
system = dmidecode.get_by_type(dmi, "System")[0]
2023-02-25 01:15:23 +01:00
return (
2024-10-21 12:55:54 +02:00
"Hyper-V" in bios["Version"]
or "Xen" in bios["Version"]
or "Google Compute Engine" in system["Product Name"]
) or (
2023-02-25 01:15:23 +01:00
(
2024-10-21 12:55:54 +02:00
"Amazon EC2" in system["Manufacturer"]
and not system["Product Name"].endswith(".metal")
2023-02-25 01:15:23 +01:00
)
2024-10-21 12:55:54 +02:00
or "RHEV Hypervisor" in system["Product Name"]
or "QEMU" in system["Manufacturer"]
or "VirtualBox" in bios["Version"]
or "VMware" in system["Manufacturer"]
2023-02-25 01:15:23 +01:00
)
class VirtualMachine(object):
def __init__(self, dmi=None):
if dmi:
self.dmi = dmi
else:
self.dmi = dmidecode.parse()
self.network = None
self.device_platform = get_device_platform(config.device.platform)
2024-10-21 12:55:54 +02:00
self.tags = (
list(set(config.device.tags.split(","))) if config.device.tags else []
)
2024-03-28 12:07:17 +01:00
self.nb_tags = create_netbox_tags(self.tags)
def get_memory(self):
2024-10-21 12:55:54 +02:00
mem_bytes = os.sysconf("SC_PAGE_SIZE") * os.sysconf(
"SC_PHYS_PAGES"
) # e.g. 4015976448
mem_gib = mem_bytes / (1024.0**2) # e.g. 3.74
return int(mem_gib)
def get_vcpus(self):
return os.cpu_count()
def get_netbox_vm(self):
hostname = get_hostname(config)
2024-10-21 12:55:54 +02:00
vm = nb.virtualization.virtual_machines.get(name=hostname)
return vm
def get_netbox_cluster(self, name):
cluster = nb.virtualization.clusters.get(
name=name,
)
return cluster
def get_netbox_datacenter(self, name):
cluster = self.get_netbox_cluster()
if cluster.datacenter:
return cluster.datacenter
return None
def get_tenant(self):
tenant = Tenant()
return tenant.get()
def get_netbox_tenant(self):
tenant = self.get_tenant()
if tenant is None:
return None
2024-10-21 12:55:54 +02:00
nb_tenant = nb.tenancy.tenants.get(slug=self.get_tenant())
return nb_tenant
def netbox_create_or_update(self, config):
2024-10-21 12:55:54 +02:00
logging.debug("It's a virtual machine")
created = False
updated = 0
hostname = get_hostname(config)
vm = self.get_netbox_vm()
vcpus = self.get_vcpus()
memory = self.get_memory()
tenant = self.get_netbox_tenant()
if not vm:
2024-10-21 12:55:54 +02:00
logging.debug("Creating Virtual machine..")
cluster = self.get_netbox_cluster(config.virtual.cluster_name)
vm = nb.virtualization.virtual_machines.create(
name=hostname,
cluster=cluster.id,
platform=self.device_platform.id,
vcpus=vcpus,
memory=memory,
tenant=tenant.id if tenant else None,
2024-10-21 12:55:54 +02:00
tags=[{"name": x} for x in self.tags],
)
created = True
self.network = VirtualNetwork(server=self)
self.network.create_or_update_netbox_network_cards()
if not created:
if vm.vcpus != vcpus:
vm.vcpus = vcpus
updated += 1
if vm.memory != memory:
vm.memory = memory
updated += 1
2024-03-28 12:07:17 +01:00
vm_tags = sorted(set([x.name for x in vm.tags]))
tags = sorted(set(self.tags))
if vm_tags != tags:
new_tags_ids = [x.id for x in self.nb_tags]
if not config.preserve_tags:
vm.tags = new_tags_ids
else:
vm_tags_ids = [x.id for x in vm.tags]
vm.tags = sorted(set(new_tags_ids + vm_tags_ids))
updated += 1
2024-03-28 12:07:17 +01:00
if vm.platform != self.device_platform:
vm.platform = self.device_platform
updated += 1
if updated:
vm.save()
2023-08-21 20:38:41 +02:00
def print_debug(self):
self.network = VirtualNetwork(server=self)
2024-10-21 12:55:54 +02:00
print("Cluster:", self.get_netbox_cluster(config.virtual.cluster_name))
print("Platform:", self.device_platform)
print("VM:", self.get_netbox_vm())
print("vCPU:", self.get_vcpus())
print("Memory:", f"{self.get_memory()} MB")
print(
"NIC:",
)
2023-08-21 20:38:41 +02:00
pprint(self.network.get_network_cards())
pass