Local inventory #16
3 changed files with 68 additions and 11 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
import logging
|
||||||
import subprocess
|
import subprocess
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
@ -86,7 +87,7 @@ class Inventory():
|
||||||
|
|
||||||
controllers = self.raid.get_controllers()
|
controllers = self.raid.get_controllers()
|
||||||
if len(self.raid.get_controllers()):
|
if len(self.raid.get_controllers()):
|
||||||
return self.raid.get_controllers()
|
return controllers
|
||||||
|
|
||||||
def get_netbox_raid_cards(self):
|
def get_netbox_raid_cards(self):
|
||||||
raid_cards = nb.dcim.inventory_items.filter(
|
raid_cards = nb.dcim.inventory_items.filter(
|
||||||
|
@ -97,7 +98,7 @@ class Inventory():
|
||||||
|
|
||||||
def find_or_create_manufacturer(self, name):
|
def find_or_create_manufacturer(self, name):
|
||||||
if name is None:
|
if name is None:
|
||||||
return none
|
return None
|
||||||
manufacturer = nb.dcim.manufacturers.get(
|
manufacturer = nb.dcim.manufacturers.get(
|
||||||
name=name,
|
name=name,
|
||||||
)
|
)
|
||||||
|
@ -115,7 +116,7 @@ class Inventory():
|
||||||
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,
|
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(raid_card.get_product_name()),
|
||||||
serial='{}'.format(raid_card.get_serial_number()),
|
serial='{}'.format(raid_card.get_serial_number()),
|
||||||
|
@ -151,16 +152,48 @@ class Inventory():
|
||||||
self.create_netbox_raid_card(raid_card)
|
self.create_netbox_raid_card(raid_card)
|
||||||
|
|
||||||
def get_disks(self):
|
def get_disks(self):
|
||||||
pass
|
ret = []
|
||||||
|
for raid_card in self.get_raid_cards():
|
||||||
|
ret += raid_card.get_physical_disks()
|
||||||
|
return ret
|
||||||
|
|
||||||
def get_netbox_disks(self):
|
def get_netbox_disks(self):
|
||||||
pass
|
disks = nb.dcim.inventory_items.filter(
|
||||||
|
device_id=self.device_id,
|
||||||
|
tag=INVENTORY_TAG['disk']['slug'],
|
||||||
|
)
|
||||||
|
return disks
|
||||||
|
|
||||||
def create_netbox_disks(self):
|
def create_netbox_disks(self):
|
||||||
pass
|
for disk in self.get_disks():
|
||||||
|
disk = nb.dcim.inventory_items.create(
|
||||||
|
device=self.device_id,
|
||||||
|
discovered=True,
|
||||||
|
tags=[INVENTORY_TAG['disk']['name']],
|
||||||
|
name='{} ({})'.format(disk['Model'], disk['Size']),
|
||||||
|
serial=disk['SN'],
|
||||||
|
)
|
||||||
|
|
||||||
def update_netbox_disks(self):
|
def update_netbox_disks(self):
|
||||||
pass
|
nb_disks = self.get_netbox_disks()
|
||||||
|
disks = self.get_disks()
|
||||||
|
|
||||||
|
# delete disks that are in netbox but not locally
|
||||||
|
# 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]:
|
||||||
|
nb_disk.delete()
|
||||||
|
|
||||||
|
# create disks that are not in netbox
|
||||||
|
for disk in disks:
|
||||||
|
if disk['SN'] not in [x.serial for x in nb_disks]:
|
||||||
|
disk = nb.dcim.inventory_items.create(
|
||||||
|
device=self.device_id,
|
||||||
|
discovered=True,
|
||||||
|
tags=[INVENTORY_TAG['disk']['name']],
|
||||||
|
name='{} ({})'.format(disk['Model'], disk['Size']),
|
||||||
|
serial=disk['SN'],
|
||||||
|
)
|
||||||
|
|
||||||
def get_memory(self):
|
def get_memory(self):
|
||||||
memories = []
|
memories = []
|
||||||
|
@ -221,3 +254,4 @@ class Inventory():
|
||||||
# assume we don't update CPU?
|
# assume we don't update CPU?
|
||||||
self.update_netbox_memory()
|
self.update_netbox_memory()
|
||||||
self.update_netbox_raid_cards()
|
self.update_netbox_raid_cards()
|
||||||
|
self.update_netbox_disks()
|
||||||
|
|
|
@ -4,8 +4,9 @@ import json
|
||||||
from netbox_agent.raid.base import Raid, RaidController
|
from netbox_agent.raid.base import Raid, RaidController
|
||||||
|
|
||||||
class StorcliController(RaidController):
|
class StorcliController(RaidController):
|
||||||
def __init__(self, data):
|
def __init__(self, controller_index, data):
|
||||||
self.data = data
|
self.data = data
|
||||||
|
self.controller_index = controller_index
|
||||||
|
|
||||||
def get_product_name(self):
|
def get_product_name(self):
|
||||||
return self.data['Product Name']
|
return self.data['Product Name']
|
||||||
|
@ -19,6 +20,25 @@ class StorcliController(RaidController):
|
||||||
def get_firmware_version(self):
|
def get_firmware_version(self):
|
||||||
return self.data['FW Package Build']
|
return self.data['FW Package Build']
|
||||||
|
|
||||||
|
def get_physical_disks(self):
|
||||||
|
ret = []
|
||||||
|
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_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(),
|
||||||
|
'Size': size,
|
||||||
|
})
|
||||||
|
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')
|
||||||
|
@ -31,7 +51,10 @@ class StorcliRaid(Raid):
|
||||||
]) > 0:
|
]) > 0:
|
||||||
for controller in self.data['Controllers']:
|
for controller in self.data['Controllers']:
|
||||||
self.controllers.append(
|
self.controllers.append(
|
||||||
StorcliController(controller['Response Data'])
|
StorcliController(
|
||||||
|
controller['Command Status']['Controller'],
|
||||||
|
controller['Response Data']
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_controllers(self):
|
def get_controllers(self):
|
||||||
|
|
|
@ -282,12 +282,12 @@ class ServerBase():
|
||||||
logging.debug('Finished updating Server!')
|
logging.debug('Finished updating Server!')
|
||||||
|
|
||||||
def print_debug(self):
|
def print_debug(self):
|
||||||
|
self.inventory = Inventory(server=self)
|
||||||
# FIXME: do something more generic by looping on every get_* methods
|
# FIXME: do something more generic by looping on every get_* methods
|
||||||
print(self.inventory.get_memory())
|
print(self.inventory.get_memory())
|
||||||
print(self.inventory.get_raid_cards())
|
print(self.inventory.get_raid_cards())
|
||||||
print(self.inventory.get_netbox_raid_cards())
|
print(self.inventory.get_netbox_raid_cards())
|
||||||
# print(self.inventory.get_netbox_memory())
|
print(self.inventory.get_disks())
|
||||||
# print(self.inventory.update_netbox_memory())
|
|
||||||
return
|
return
|
||||||
print('Datacenter:', self.get_datacenter())
|
print('Datacenter:', self.get_datacenter())
|
||||||
print('Netbox Datacenter:', self.get_netbox_datacenter())
|
print('Netbox Datacenter:', self.get_netbox_datacenter())
|
||||||
|
|
Loading…
Add table
Reference in a new issue