Supermicro server can guess their slot via the config file (#50)

This commit is contained in:
Solvik 2019-08-29 16:13:04 +02:00 committed by GitHub
parent 136a356cec
commit 0faee59bde
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 1 deletions

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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