Merge pull request #4 from Solvik/master

merging from org
This commit is contained in:
Christoffer Nicklasson 2022-03-12 10:12:15 +01:00 committed by GitHub
commit 3a802cb4a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 106 additions and 52 deletions

View file

@ -70,15 +70,21 @@ class LSHW():
return self.memories
def find_network(self, obj):
d = {}
d["name"] = obj["logicalname"]
d["macaddress"] = obj["serial"]
d["serial"] = obj["serial"]
d["product"] = obj["product"]
d["vendor"] = obj["vendor"]
d["description"] = obj["description"]
self.interfaces.append(d)
# Some interfaces do not have device (logical) name (eth0, for
# instance), such as not connected network mezzanine cards in blade
# servers. In such situations, the card will be named `unknown[0-9]`.
unkn_intfs = [
i for i in self.interfaces if i["name"].startswith("unknown")
]
unkn_name = "unknown{}".format(len(unkn_intfs))
self.interfaces.append({
"name": obj.get("logicalname", unkn_name),
"macaddress": obj.get("serial", ""),
"serial": obj.get("serial", ""),
"product": obj["product"],
"vendor": obj["vendor"],
"description": obj["description"],
})
def find_storage(self, obj):
if "children" in obj:
@ -121,13 +127,12 @@ class LSHW():
def find_cpus(self, obj):
if "product" in obj:
c = {}
c["product"] = obj["product"]
c["vendor"] = obj["vendor"]
c["description"] = obj["description"]
c["location"] = obj["slot"]
self.cpus.append(c)
self.cpus.append({
"product": obj["product"],
"vendor": obj["vendor"],
"description": obj["description"],
"location": obj["slot"],
})
def find_memories(self, obj):
if "children" not in obj:
@ -138,25 +143,23 @@ class LSHW():
if "empty" in dimm["description"]:
continue
d = {}
d["slot"] = dimm.get("slot")
d["description"] = dimm.get("description")
d["id"] = dimm.get("id")
d["serial"] = dimm.get("serial", 'N/A')
d["vendor"] = dimm.get("vendor", 'N/A')
d["product"] = dimm.get("product", 'N/A')
d["size"] = dimm.get("size", 0) / 2 ** 20 / 1024
self.memories.append(d)
self.memories.append({
"slot": dimm.get("slot"),
"description": dimm.get("description"),
"id": dimm.get("id"),
"serial": dimm.get("serial", 'N/A'),
"vendor": dimm.get("vendor", 'N/A'),
"product": dimm.get("product", 'N/A'),
"size": dimm.get("size", 0) / 2 ** 20 / 1024,
})
def find_gpus(self, obj):
if "product" in obj:
c = {}
c["product"] = obj["product"]
c["vendor"] = obj["vendor"]
c["description"] = obj["description"]
self.gpus.append(c)
self.gpus.append({
"product": obj["product"],
"vendor": obj["vendor"],
"description": obj["description"],
})
def walk_bridge(self, obj):
if "children" not in obj:

View file

@ -261,7 +261,7 @@ class Network(object):
def create_netbox_nic(self, nic, mgmt=False):
# TODO: add Optic Vendor, PN and Serial
type = self.get_netbox_type_for_nic(nic)
nic_type = self.get_netbox_type_for_nic(nic)
logging.info('Creating NIC {name} ({mac}) on {device}'.format(
name=nic['name'], mac=nic['mac'], device=self.device.name))
@ -270,11 +270,10 @@ class Network(object):
params = dict(self.custom_arg)
params.update({
'name': nic['name'],
'type': type,
'type': nic_type,
'mgmt_only': mgmt,
})
if not nic.get('virtual', False):
if nic['mac']:
params['mac_address'] = nic['mac']
interface = self.nb_net.interfaces.create(**params)

View file

@ -7,10 +7,26 @@ import re
REGEXP_CONTROLLER_HP = re.compile(r'Smart Array ([a-zA-Z0-9- ]+) in Slot ([0-9]+)')
class HPRaidControllerError(Exception):
pass
def ssacli(command):
output = subprocess.getoutput('ssacli {}'.format(command) )
lines = output.split('\n')
def ssacli(sub_command):
command = ["ssacli"]
command.extend(sub_command.split())
p = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
)
p.wait()
stdout = p.stdout.read().decode("utf-8")
if p.returncode != 0:
mesg = "Failed to execute command '{}':\n{}".format(
" ".join(command), stdout
)
raise HPRaidControllerError(mesg)
lines = stdout.split('\n')
lines = list(filter(None, lines))
return lines
@ -92,6 +108,8 @@ class HPRaidController(RaidController):
self.controller_name = controller_name
self.data = data
self.pdrives = self._get_physical_disks()
arrays = [d['Array'] for d in self.pdrives.values() if d.get('Array')]
if arrays:
self.ldrives = self._get_logical_drives()
self._get_virtual_drives_map()
@ -135,6 +153,7 @@ class HPRaidController(RaidController):
'Type': 'SSD' if attrs.get('Interface Type') == 'Solid State SATA'
else 'HDD',
'_src': self.__class__.__name__,
'custom_fields': {'pd_identifier': name}
}
return ret
@ -164,8 +183,7 @@ class HPRaidController(RaidController):
" Ignoring.".format(name)
)
continue
attrs['custom_fields'] = ld
attrs['custom_fields']['pd_identifier'] = name
attrs['custom_fields'].update(ld)
def get_physical_disks(self):
return list(self.pdrives.values())

View file

@ -6,15 +6,32 @@ import logging
import re
class OmreportControllerError(Exception):
pass
def omreport(sub_command):
command = 'omreport {}'.format(sub_command)
output = subprocess.getoutput(command)
command = ["omreport"]
command.extend(sub_command.split())
p = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
)
p.wait()
stdout = p.stdout.read().decode("utf-8")
if p.returncode != 0:
mesg = "Failed to execute command '{}':\n{}".format(
" ".join(command), stdout
)
raise OmreportControllerError(mesg)
res = {}
section_re = re.compile('^[A-Z]')
current_section = None
current_obj = None
for line in output.split('\n'):
for line in stdout.split('\n'):
if ': ' in line:
attr, value = line.split(': ', 1)
attr = attr.strip()

View file

@ -8,10 +8,27 @@ import re
import os
class StorcliControllerError(Exception):
pass
def storecli(sub_command):
command = 'storcli {} J'.format(sub_command)
output = subprocess.getoutput(command)
data = json.loads(output)
command = ["storcli"]
command.extend(sub_command.split())
command.append("J")
p = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
)
p.wait()
stdout = p.stdout.read().decode("utf-8")
if p.returncode != 0:
mesg = "Failed to execute command '{}':\n{}".format(
" ".join(command), stdout
)
raise StorcliControllerError(mesg)
data = json.loads(stdout)
controllers = dict([
(
c['Command Status']['Controller'],
@ -22,7 +39,7 @@ def storecli(sub_command):
if not controllers:
logging.error(
"Failed to execute command '{}'. "
"Ignoring data.".format(command)
"Ignoring data.".format(" ".join(command))
)
return {}
return controllers

View file

@ -16,7 +16,7 @@ def get_requirements():
setup(
name='netbox_agent',
version='0.7.0',
version='0.7.1',
description='NetBox agent for server',
long_description=open('README.md', encoding="utf-8").read(),
long_description_content_type='text/markdown',