Local inventory #16

Merged
Solvik merged 12 commits from feature/inventory into master 2019-08-26 16:54:49 +02:00
4 changed files with 22 additions and 12 deletions
Showing only changes of commit 4afd3b9170 - Show all commits

View file

@ -6,7 +6,6 @@ from netbox_agent.config import netbox_instance as nb
from netbox_agent.misc import is_tool
from netbox_agent.raid.hp import HPRaid
from netbox_agent.raid.dell import StorcliRaid
import netbox_agent.dmidecode as dmidecode
INVENTORY_TAG = {
'cpu': {'name': 'hw:cpu', 'slug': 'hw-cpu'},
@ -67,7 +66,7 @@ class Inventory():
def create_netbox_cpus(self):
nb_cpus, model = self.get_cpus()
for i in range(nb_cpus):
cpu = nb.dcim.inventory_items.create(
_ = nb.dcim.inventory_items.create(
device=self.device_id,
tags=[INVENTORY_TAG['cpu']['name']],
name=model,
@ -134,6 +133,7 @@ class Inventory():
name='{}'.format(raid_card.get_product_name()),
serial='{}'.format(raid_card.get_serial_number()),
)
return nb_raid_card
def create_netbox_raid_cards(self):
for raid_card in self.get_raid_cards():

View file

@ -1,17 +1,17 @@
class RaidController():
def get_product_name(self):
raise NotImplemented
raise NotImplementedError
def get_serial_number(self):
raise NotImplemented
raise NotImplementedError
def get_manufacturer(self):
raise NotImplemented
raise NotImplementedError
def get_firmware_version(self):
raise NotImplemented
raise NotImplementedError
class Raid():
def get_controllers(self):
raise NotImplemented
raise NotImplementedError

View file

@ -3,6 +3,7 @@ import json
from netbox_agent.raid.base import Raid, RaidController
class StorcliController(RaidController):
def __init__(self, controller_index, data):
self.data = data
@ -22,14 +23,18 @@ class StorcliController(RaidController):
def get_physical_disks(self):
ret = []
output = subprocess.getoutput('storcli /c{}/eall/sall show all J'.format(self.controller_index))
output = subprocess.getoutput(
'storcli /c{}/eall/sall show all J'.format(self.controller_index)
)
drive_infos = json.loads(output)['Controllers'][self.controller_index]['Response Data']
for physical_drive in self.data['PD LIST']:
enclosure = physical_drive.get('EID:Slt').split(':')[0]
slot = physical_drive.get('EID:Slt').split(':')[1]
size = physical_drive.get('Size').strip()
drive_identifier = 'Drive /c{}/e{}/s{}'.format(str(self.controller_index), str(enclosure), str(slot))
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)]
ret.append({
@ -46,7 +51,7 @@ class StorcliRaid(Raid):
self.controllers = []
if len([
x for x in self.data['Controllers'] \
x for x in self.data['Controllers']
if x['Command Status']['Status'] == 'Success'
]) > 0:
for controller in self.data['Controllers']:

View file

@ -5,10 +5,12 @@ from netbox_agent.raid.base import Raid, RaidController
REGEXP_CONTROLLER_HP = re.compile(r'Smart Array ([a-zA-Z0-9- ]+) in Slot ([0-9]+)')
def _get_indentation(string):
"""Return the number of spaces before the current line."""
return len(string) - len(string.lstrip(' '))
def _get_key_value(string):
"""Return the (key, value) as a tuple from a string."""
# Normally all properties look like this:
@ -36,6 +38,7 @@ def _get_key_value(string):
return key.lstrip(' ').rstrip(' '), value.lstrip(' ').rstrip(' ')
def _get_dict(lines, start_index, indentation):
"""Recursive function for parsing hpssacli/ssacli output."""
@ -85,6 +88,7 @@ def _get_dict(lines, start_index, indentation):
return info, i
class HPRaidController(RaidController):
def __init__(self, controller_name, data):
self.controller_name = controller_name
@ -102,6 +106,7 @@ class HPRaidController(RaidController):
def get_firmware_version(self):
return self.data['Firmware Version']
class HPRaid(Raid):
def __init__(self):
self.output = subprocess.getoutput('ssacli ctrl all show detail')
@ -119,7 +124,7 @@ class HPRaid(Raid):
product_name = REGEXP_CONTROLLER_HP.search(_product_name)
if product_name:
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):