From b30591b382321023e549e66997a1ef3e773ac36e Mon Sep 17 00:00:00 2001 From: Solvik Blum Date: Wed, 21 Aug 2019 10:40:32 +0200 Subject: [PATCH] pep8 fixes and logging --- netbox_agent/inventory.py | 36 ++++++++++++++++++++++++++++++------ netbox_agent/misc.py | 1 + netbox_agent/raid/base.py | 1 + netbox_agent/raid/dell.py | 5 +++-- netbox_agent/raid/hp.py | 4 ++-- netbox_agent/vendors/dell.py | 2 +- 6 files changed, 38 insertions(+), 11 deletions(-) diff --git a/netbox_agent/inventory.py b/netbox_agent/inventory.py index 91f69e2..f451cf9 100644 --- a/netbox_agent/inventory.py +++ b/netbox_agent/inventory.py @@ -25,6 +25,7 @@ for key, tag in INVENTORY_TAG.items(): comments=tag['name'], ) + class Inventory(): """ Better Inventory items coming, see: @@ -72,6 +73,7 @@ class Inventory(): name=model, discovered=True, ) + logging.info('Created CPU model {model}'.format(model=model)) def update_netbox_cpus(self): cpus_number, model = self.get_cpus() @@ -119,20 +121,27 @@ class Inventory(): name=name, slug=name.lower(), ) + logging.info('Created missing manufacturer {name}'.format(name=name)) return manufacturer def create_netbox_raid_card(self, raid_card): manufacturer = self.find_or_create_manufacturer( raid_card.get_manufacturer() ) + name = raid_card.get_product_name() + serial = raid_card.get_serial_number() nb_raid_card = nb.dcim.inventory_items.create( device=self.device_id, discovered=True, manufacturer=manufacturer.id if manufacturer else None, tags=[INVENTORY_TAG['raid_card']['name']], - name='{}'.format(raid_card.get_product_name()), - serial='{}'.format(raid_card.get_serial_number()), + name='{}'.format(name), + serial='{}'.format(serial), ) + logging.info('Created RAID Card {name} (SN: {serial})'.format( + name=name, + serial=serial, + )) return nb_raid_card def create_netbox_raid_cards(self): @@ -157,6 +166,9 @@ class Inventory(): # use the serial_number has the comparison element for nb_raid_card in nb_raid_cards: if nb_raid_card.serial not in [x.get_serial_number() for x in raid_cards]: + logging.info('Deleting unknown locally RAID Card {serial}'.format( + serial=nb_raid_card.serial, + )) nb_raid_card.delete() # create card that are not in netbox @@ -195,6 +207,9 @@ class Inventory(): # use the serial_number has the comparison element for nb_disk in nb_disks: if nb_disk.serial not in [x['SN'] for x in disks]: + logging.info('Deleting unknown locally Disk {serial}'.format( + serial=nb_disk.serial, + )) nb_disk.delete() # create disks that are not in netbox @@ -207,6 +222,10 @@ class Inventory(): name='{} ({})'.format(disk['Model'], disk['Size']), serial=disk['SN'], ) + logging.info('Creating Disk {model} {serial}'.format( + model=disk['Model'], + serial=disk['SN'], + )) def get_memory(self): memories = [] @@ -254,6 +273,10 @@ class Inventory(): part_id=memory['PN'], serial=memory['SN'], ) + logging.info('Creating Memory {type} {size}'.format( + type=memory['Type'], + size=memory['Size'], + )) return memory def create_netbox_memories(self): @@ -266,8 +289,10 @@ class Inventory(): for nb_memory in nb_memories: if nb_memory.serial not in [x['SN'] for x in memories]: + logging.info('Deleting unknown locally Memory {serial}'.format( + serial=nb_memory.serial, + )) nb_memory.delete() - for memory in memories: if memory['SN'] not in [x.serial for x in nb_memories]: self.create_netbox_memory(memory) @@ -276,11 +301,10 @@ class Inventory(): self.create_netbox_cpus() self.create_netbox_memory() self.create_netbox_raid_cards() + self.create_netbox_disks() def update(self): - # assume we don't update CPU? + self.update_netbox_cpus() self.update_netbox_memory() self.update_netbox_raid_cards() self.update_netbox_disks() - self.update_netbox_cpus() - self.update_netbox_memory() diff --git a/netbox_agent/misc.py b/netbox_agent/misc.py index 1142745..b28e4b3 100644 --- a/netbox_agent/misc.py +++ b/netbox_agent/misc.py @@ -1,5 +1,6 @@ from shutil import which + def is_tool(name): '''Check whether `name` is on PATH and marked as executable.''' return which(name) is not None diff --git a/netbox_agent/raid/base.py b/netbox_agent/raid/base.py index 230756e..69cec1a 100644 --- a/netbox_agent/raid/base.py +++ b/netbox_agent/raid/base.py @@ -12,6 +12,7 @@ class RaidController(): def get_firmware_version(self): raise NotImplementedError + class Raid(): def get_controllers(self): raise NotImplementedError diff --git a/netbox_agent/raid/dell.py b/netbox_agent/raid/dell.py index 26bdf05..167dd33 100644 --- a/netbox_agent/raid/dell.py +++ b/netbox_agent/raid/dell.py @@ -35,8 +35,8 @@ class StorcliController(RaidController): drive_identifier = 'Drive /c{}/e{}/s{}'.format( str(self.controller_index), str(enclosure), str(slot) ) - drive_attr = drive_infos['{} - Detailed Information'.format(drive_identifier)]\ - ['{} Device attributes'.format(drive_identifier)] + drive_attr = drive_infos['{} - Detailed Information'.format(drive_identifier)][ + '{} Device attributes'.format(drive_identifier)] ret.append({ 'Model': drive_attr.get('Model Number', '').strip(), 'SN': drive_attr.get('SN', '').strip(), @@ -44,6 +44,7 @@ class StorcliController(RaidController): }) return ret + class StorcliRaid(Raid): def __init__(self): self.output = subprocess.getoutput('storcli /call show J') diff --git a/netbox_agent/raid/hp.py b/netbox_agent/raid/hp.py index 991ef0d..d284021 100644 --- a/netbox_agent/raid/hp.py +++ b/netbox_agent/raid/hp.py @@ -123,8 +123,8 @@ class HPRaid(Raid): _product_name = list(info_dict.keys())[0] product_name = REGEXP_CONTROLLER_HP.search(_product_name) if product_name: - self.controllers.append( - HPRaidController(product_name.group(1), info_dict[_product_name]) + self.controllers.append( + HPRaidController(product_name.group(1), info_dict[_product_name]) ) def get_controllers(self): diff --git a/netbox_agent/vendors/dell.py b/netbox_agent/vendors/dell.py index 14f1a3a..8c0493b 100644 --- a/netbox_agent/vendors/dell.py +++ b/netbox_agent/vendors/dell.py @@ -5,7 +5,7 @@ class DellHost(ServerBase): def __init__(self, *args, **kwargs): super(DellHost, self).__init__(*args, **kwargs) self.manufacturer = 'Dell' - + def is_blade(self): return self.get_product_name().startswith('PowerEdge M')