diff --git a/netbox_agent.yaml.example b/netbox_agent.yaml.example index 3a0481a..9c85d50 100644 --- a/netbox_agent.yaml.example +++ b/netbox_agent.yaml.example @@ -25,4 +25,10 @@ rack_location: # driver: "file:/tmp/datacenter" # regex: "(.*)" +# Some servers dont report the slot, since most people put it in the hostname +# here's a way to extract it and maintain correct slot location in Netbox +# slot_location: +# driver: 'cmd:hostname' +# regex: '.*-(\d+)' + inventory: true diff --git a/netbox_agent/config.py b/netbox_agent/config.py index 194c78f..bf6c34e 100644 --- a/netbox_agent/config.py +++ b/netbox_agent/config.py @@ -18,6 +18,9 @@ DATACENTER_LOCATION_REGEX = None RACK_LOCATION_DRIVER_FILE = None RACK_LOCATION = None RACK_LOCATION_REGEX = None +SLOT_LOCATION_DRIVER_FILE = None +SLOT_LOCATION = None +SLOT_LOCATION_REGEX = None if config.get('datacenter_location'): dc_loc = config.get('datacenter_location') @@ -31,6 +34,13 @@ if config.get('rack_location'): RACK_LOCATION = rack_location.get('driver') RACK_LOCATION_REGEX = rack_location.get('regex') +if config.get('slot_location'): + slot_location = config['slot_location'] + SLOT_LOCATION_DRIVER_FILE = slot_location.get('driver_file') + SLOT_LOCATION = slot_location.get('driver') + SLOT_LOCATION_REGEX = slot_location.get('regex') + + NETWORK_IGNORE_INTERFACES = None NETWORK_IGNORE_IPS = None NETWORK_LLDP = None diff --git a/netbox_agent/location.py b/netbox_agent/location.py index f01ece5..0fb4fcf 100644 --- a/netbox_agent/location.py +++ b/netbox_agent/location.py @@ -2,7 +2,8 @@ import importlib import importlib.machinery from netbox_agent.config import DATACENTER_LOCATION, DATACENTER_LOCATION_DRIVER_FILE, \ - DATACENTER_LOCATION_REGEX, RACK_LOCATION, RACK_LOCATION_DRIVER_FILE, RACK_LOCATION_REGEX + DATACENTER_LOCATION_REGEX, RACK_LOCATION, RACK_LOCATION_DRIVER_FILE, RACK_LOCATION_REGEX, \ + SLOT_LOCATION, SLOT_LOCATION_DRIVER_FILE, SLOT_LOCATION_REGEX class LocationBase(): @@ -67,3 +68,12 @@ class Rack(LocationBase): driver_file = RACK_LOCATION_DRIVER_FILE regex = RACK_LOCATION_REGEX super().__init__(driver, driver_value, driver_file, regex) + + +class Slot(LocationBase): + def __init__(self): + driver = SLOT_LOCATION.split(':')[0] if SLOT_LOCATION else None + driver_value = ':'.join(SLOT_LOCATION.split(':')[1:]) if SLOT_LOCATION else None + driver_file = SLOT_LOCATION_DRIVER_FILE + regex = SLOT_LOCATION_REGEX + super().__init__(driver, driver_value, driver_file, regex) diff --git a/netbox_agent/vendors/supermicro.py b/netbox_agent/vendors/supermicro.py index 115ab4a..a697e03 100644 --- a/netbox_agent/vendors/supermicro.py +++ b/netbox_agent/vendors/supermicro.py @@ -1,3 +1,4 @@ +from netbox_agent.location import Slot from netbox_agent.server import ServerBase @@ -6,6 +7,11 @@ class SupermicroHost(ServerBase): return self.get_product_name().startswith('SBI') def get_blade_slot(self): + if self.is_blade(): + # Some Supermicro servers don't report the slot in dmidecode + # let's use a regex + slot = Slot() + return slot.get() # No supermicro on hands return None