use lshw for inventory #58

Merged
ThomasADavis merged 15 commits from lshw-inventory into master 2019-09-05 15:13:37 +02:00
2 changed files with 13 additions and 16 deletions
Showing only changes of commit 88387a595c - Show all commits

View file

@ -73,7 +73,7 @@ class Inventory():
logging.info('Creating missing manufacturer {name}'.format(name=name)) logging.info('Creating missing manufacturer {name}'.format(name=name))
manufacturer = nb.dcim.manufacturers.create( manufacturer = nb.dcim.manufacturers.create(
name=name, name=name,
slug=name.replace(' ','-').lower(), slug=name.replace(' ', '-').lower(),
) )
logging.info('Creating missing manufacturer {name}'.format(name=name)) logging.info('Creating missing manufacturer {name}'.format(name=name))
@ -86,7 +86,7 @@ class Inventory():
device_id=device_id, device_id=device_id,
tag=tag tag=tag
) )
except: except BaseException:
logging.info('Tag {tag} is missing, returning empty array.'.format(tag=tag)) logging.info('Tag {tag} is missing, returning empty array.'.format(tag=tag))
items = [] items = []
@ -116,10 +116,10 @@ class Inventory():
motherboards = [] motherboards = []
m = {} m = {}
m['serial']=self.lshw.motherboard_serial m['serial'] = self.lshw.motherboard_serial
m['vendor']=self.lshw.vendor m['vendor'] = self.lshw.vendor
m['name']='{} {}'.format(self.lshw.vendor, self.lshw.motherboard) m['name'] = '{} {}'.format(self.lshw.vendor, self.lshw.motherboard)
m['description']='{} Motherboard'.format(self.lshw.motherboard) m['description'] = '{} Motherboard'.format(self.lshw.motherboard)
motherboards.append(m) motherboards.append(m)
@ -144,7 +144,7 @@ class Inventory():
for motherboard in motherboards: for motherboard in motherboards:
if motherboard.get('serial') not in [x.serial for x in nb_motherboards]: if motherboard.get('serial') not in [x.serial for x in nb_motherboards]:
self.create_netbox_inventory_item( self.create_netbox_inventory_item(
device_id = self.device_id, device_id=self.device_id,
tags=[INVENTORY_TAG['motherboard']['slug']], tags=[INVENTORY_TAG['motherboard']['slug']],
vendor='{}'.format(motherboard.get('vendor', 'N/A')), vendor='{}'.format(motherboard.get('vendor', 'N/A')),
serial='{}'.format(motherboard.get('serial', '000000')), serial='{}'.format(motherboard.get('serial', '000000')),
@ -156,7 +156,7 @@ class Inventory():
manufacturer = self.find_or_create_manufacturer(iface["vendor"]) manufacturer = self.find_or_create_manufacturer(iface["vendor"])
_ = nb.dcim.inventory_items.create( _ = nb.dcim.inventory_items.create(
device=self.device_id, device=self.device_id,
manufacturer = manufacturer.id, manufacturer=manufacturer.id,
discovered=True, discovered=True,
tags=[INVENTORY_TAG['interface']['name']], tags=[INVENTORY_TAG['interface']['name']],
name="{}".format(iface['product']), name="{}".format(iface['product']),

View file

@ -1,5 +1,4 @@
import subprocess import subprocess
import getpass
import json import json
@ -7,9 +6,8 @@ class LSHW():
def __init__(self): def __init__(self):
self.hw_info = json.loads( self.hw_info = json.loads(
subprocess.check_output(["lshw", "-quiet", "-json"], subprocess.check_output(["lshw", "-quiet", "-json"],
encoding='utf8') encoding='utf8')) # noqa: E128
)
self.info = {} self.info = {}
self.memories = [] self.memories = []
@ -81,9 +79,8 @@ class LSHW():
elif "nvme" in obj["configuration"]["driver"]: elif "nvme" in obj["configuration"]["driver"]:
nvme = json.loads( nvme = json.loads(
subprocess.check_output(["nvme", '-list', '-o', 'json'], subprocess.check_output(["nvme", '-list', '-o', 'json'],
encoding='utf8') encoding='utf8')) # noqa: E128
)
d = {} d = {}
d["vendor"] = obj["vendor"] d["vendor"] = obj["vendor"]