diff --git a/netbox_agent/config.py b/netbox_agent/config.py index 7bb67d7..702c9b3 100644 --- a/netbox_agent/config.py +++ b/netbox_agent/config.py @@ -43,6 +43,8 @@ def get_config(): p.add_argument('--virtual.cluster_name', help='Cluster name of VM') p.add_argument('--hostname_cmd', default=None, help="Command to output hostname, used as Device's name in netbox") + p.add_argument('--device.platform', default=None, + help='Override device platform. Here we use OS distribution.') p.add_argument('--device.tags', default=r'', help='tags to use for a host') p.add_argument('--preserve-tags', action='store_true', help='Append new unique tags, preserve those already present') diff --git a/netbox_agent/misc.py b/netbox_agent/misc.py index df4638c..6e7e92d 100644 --- a/netbox_agent/misc.py +++ b/netbox_agent/misc.py @@ -29,6 +29,26 @@ def get_device_type(type): return device_type +def get_device_platform(device_platform): + if device_platform is None: + try: + import platform + + linux_distribution = " ".join(platform.linux_distribution()) + if not linux_distribution: + return None + except (ModuleNotFoundError, NameError): + return None + else: + linux_distribution = device_platform + + device_platform = nb.dcim.platforms.get(name=linux_distribution) + if device_platform is None: + device_platform = nb.dcim.platforms.create( + name=linux_distribution, slug=slugify(linux_distribution) + ) + return device_platform + def get_vendor(name): vendors = { 'PERC': 'Dell', diff --git a/netbox_agent/server.py b/netbox_agent/server.py index 5904a60..f7008bf 100644 --- a/netbox_agent/server.py +++ b/netbox_agent/server.py @@ -3,7 +3,7 @@ from netbox_agent.config import config from netbox_agent.config import netbox_instance as nb from netbox_agent.inventory import Inventory from netbox_agent.location import Datacenter, Rack, Tenant -from netbox_agent.misc import create_netbox_tags, get_device_role, get_device_type +from netbox_agent.misc import create_netbox_tags, get_device_role, get_device_type, get_device_platform from netbox_agent.network import ServerNetwork from netbox_agent.power import PowerSupply from pprint import pprint @@ -24,6 +24,7 @@ class ServerBase(): self.bios = dmidecode.get_by_type(self.dmi, 'BIOS') self.chassis = dmidecode.get_by_type(self.dmi, 'Chassis') self.system = dmidecode.get_by_type(self.dmi, 'System') + self.device_platform = get_device_platform(config.device.platform) self.network = None @@ -273,6 +274,7 @@ class ServerBase(): serial=serial, device_role=device_role.id, device_type=device_type.id, + platform=self.device_platform, site=datacenter.id if datacenter else None, tenant=tenant.id if tenant else None, rack=rack.id if rack else None, @@ -463,6 +465,10 @@ class ServerBase(): ret, server = self.update_netbox_location(server) update += ret + if server.platform != self.device_platform: + server.platform = self.device_platform + update += 1 + if update: server.save() @@ -487,6 +493,7 @@ class ServerBase(): print('Is blade:', self.is_blade()) print('Got expansion:', self.own_expansion_slot()) print('Product Name:', self.get_product_name()) + print('Platform:', self.device_platform) print('Chassis:', self.get_chassis()) print('Chassis service tag:', self.get_chassis_service_tag()) print('Service tag:', self.get_service_tag()) diff --git a/netbox_agent/virtualmachine.py b/netbox_agent/virtualmachine.py index 6669b3b..f86c082 100644 --- a/netbox_agent/virtualmachine.py +++ b/netbox_agent/virtualmachine.py @@ -5,7 +5,7 @@ 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 -from netbox_agent.misc import create_netbox_tags, get_hostname +from netbox_agent.misc import create_netbox_tags, get_hostname, get_device_platform from netbox_agent.network import VirtualNetwork @@ -31,6 +31,7 @@ class VirtualMachine(object): else: self.dmi = dmidecode.parse() self.network = None + self.device_platform = get_device_platform(config.device.platform) self.tags = list(set(config.device.tags.split(','))) if config.device.tags else [] if self.tags and len(self.tags): @@ -94,6 +95,8 @@ class VirtualMachine(object): vm = nb.virtualization.virtual_machines.create( name=hostname, cluster=cluster.id, + platform=self.device_platform, + device_platform=self.device_platform, vcpus=vcpus, memory=memory, tenant=tenant.id if tenant else None, @@ -114,6 +117,9 @@ class VirtualMachine(object): if sorted(set(vm.tags)) != sorted(set(self.tags)): vm.tags = self.tags updated += 1 + if vm.platform != self.device_platform: + vm.platform = self.device_platform + updated += 1 if updated: vm.save()