pep8 fixes and logging

This commit is contained in:
Solvik Blum 2019-08-21 10:40:32 +02:00
parent 4afd3b9170
commit b30591b382
No known key found for this signature in database
GPG key ID: CC12B3DC262B6C47
6 changed files with 38 additions and 11 deletions

View file

@ -25,6 +25,7 @@ for key, tag in INVENTORY_TAG.items():
comments=tag['name'], comments=tag['name'],
) )
class Inventory(): class Inventory():
""" """
Better Inventory items coming, see: Better Inventory items coming, see:
@ -72,6 +73,7 @@ class Inventory():
name=model, name=model,
discovered=True, discovered=True,
) )
logging.info('Created CPU model {model}'.format(model=model))
def update_netbox_cpus(self): def update_netbox_cpus(self):
cpus_number, model = self.get_cpus() cpus_number, model = self.get_cpus()
@ -119,20 +121,27 @@ class Inventory():
name=name, name=name,
slug=name.lower(), slug=name.lower(),
) )
logging.info('Created missing manufacturer {name}'.format(name=name))
return manufacturer return manufacturer
def create_netbox_raid_card(self, raid_card): def create_netbox_raid_card(self, raid_card):
manufacturer = self.find_or_create_manufacturer( manufacturer = self.find_or_create_manufacturer(
raid_card.get_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( nb_raid_card = nb.dcim.inventory_items.create(
device=self.device_id, device=self.device_id,
discovered=True, discovered=True,
manufacturer=manufacturer.id if manufacturer else None, manufacturer=manufacturer.id if manufacturer else None,
tags=[INVENTORY_TAG['raid_card']['name']], tags=[INVENTORY_TAG['raid_card']['name']],
name='{}'.format(raid_card.get_product_name()), name='{}'.format(name),
serial='{}'.format(raid_card.get_serial_number()), serial='{}'.format(serial),
) )
logging.info('Created RAID Card {name} (SN: {serial})'.format(
name=name,
serial=serial,
))
return nb_raid_card return nb_raid_card
def create_netbox_raid_cards(self): def create_netbox_raid_cards(self):
@ -157,6 +166,9 @@ class Inventory():
# use the serial_number has the comparison element # use the serial_number has the comparison element
for nb_raid_card in nb_raid_cards: for nb_raid_card in nb_raid_cards:
if nb_raid_card.serial not in [x.get_serial_number() for x in 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() nb_raid_card.delete()
# create card that are not in netbox # create card that are not in netbox
@ -195,6 +207,9 @@ class Inventory():
# use the serial_number has the comparison element # use the serial_number has the comparison element
for nb_disk in nb_disks: for nb_disk in nb_disks:
if nb_disk.serial not in [x['SN'] for x in 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() nb_disk.delete()
# create disks that are not in netbox # create disks that are not in netbox
@ -207,6 +222,10 @@ class Inventory():
name='{} ({})'.format(disk['Model'], disk['Size']), name='{} ({})'.format(disk['Model'], disk['Size']),
serial=disk['SN'], serial=disk['SN'],
) )
logging.info('Creating Disk {model} {serial}'.format(
model=disk['Model'],
serial=disk['SN'],
))
def get_memory(self): def get_memory(self):
memories = [] memories = []
@ -254,6 +273,10 @@ class Inventory():
part_id=memory['PN'], part_id=memory['PN'],
serial=memory['SN'], serial=memory['SN'],
) )
logging.info('Creating Memory {type} {size}'.format(
type=memory['Type'],
size=memory['Size'],
))
return memory return memory
def create_netbox_memories(self): def create_netbox_memories(self):
@ -266,8 +289,10 @@ class Inventory():
for nb_memory in nb_memories: for nb_memory in nb_memories:
if nb_memory.serial not in [x['SN'] for x in 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() nb_memory.delete()
for memory in memories: for memory in memories:
if memory['SN'] not in [x.serial for x in nb_memories]: if memory['SN'] not in [x.serial for x in nb_memories]:
self.create_netbox_memory(memory) self.create_netbox_memory(memory)
@ -276,11 +301,10 @@ class Inventory():
self.create_netbox_cpus() self.create_netbox_cpus()
self.create_netbox_memory() self.create_netbox_memory()
self.create_netbox_raid_cards() self.create_netbox_raid_cards()
self.create_netbox_disks()
def update(self): def update(self):
# assume we don't update CPU? self.update_netbox_cpus()
self.update_netbox_memory() self.update_netbox_memory()
self.update_netbox_raid_cards() self.update_netbox_raid_cards()
self.update_netbox_disks() self.update_netbox_disks()
self.update_netbox_cpus()
self.update_netbox_memory()

View file

@ -1,5 +1,6 @@
from shutil import which from shutil import which
def is_tool(name): def is_tool(name):
'''Check whether `name` is on PATH and marked as executable.''' '''Check whether `name` is on PATH and marked as executable.'''
return which(name) is not None return which(name) is not None

View file

@ -12,6 +12,7 @@ class RaidController():
def get_firmware_version(self): def get_firmware_version(self):
raise NotImplementedError raise NotImplementedError
class Raid(): class Raid():
def get_controllers(self): def get_controllers(self):
raise NotImplementedError raise NotImplementedError

View file

@ -35,8 +35,8 @@ class StorcliController(RaidController):
drive_identifier = 'Drive /c{}/e{}/s{}'.format( drive_identifier = 'Drive /c{}/e{}/s{}'.format(
str(self.controller_index), str(enclosure), str(slot) str(self.controller_index), str(enclosure), str(slot)
) )
drive_attr = drive_infos['{} - Detailed Information'.format(drive_identifier)]\ drive_attr = drive_infos['{} - Detailed Information'.format(drive_identifier)][
['{} Device attributes'.format(drive_identifier)] '{} Device attributes'.format(drive_identifier)]
ret.append({ ret.append({
'Model': drive_attr.get('Model Number', '').strip(), 'Model': drive_attr.get('Model Number', '').strip(),
'SN': drive_attr.get('SN', '').strip(), 'SN': drive_attr.get('SN', '').strip(),
@ -44,6 +44,7 @@ class StorcliController(RaidController):
}) })
return ret return ret
class StorcliRaid(Raid): class StorcliRaid(Raid):
def __init__(self): def __init__(self):
self.output = subprocess.getoutput('storcli /call show J') self.output = subprocess.getoutput('storcli /call show J')

View file

@ -123,8 +123,8 @@ class HPRaid(Raid):
_product_name = list(info_dict.keys())[0] _product_name = list(info_dict.keys())[0]
product_name = REGEXP_CONTROLLER_HP.search(_product_name) product_name = REGEXP_CONTROLLER_HP.search(_product_name)
if product_name: if product_name:
self.controllers.append( self.controllers.append(
HPRaidController(product_name.group(1), info_dict[_product_name]) HPRaidController(product_name.group(1), info_dict[_product_name])
) )
def get_controllers(self): def get_controllers(self):