Skip to content

Commit

Permalink
Merge pull request #6 from redhat-cip/master
Browse files Browse the repository at this point in the history
Move detect_megacli in the megacli module
  • Loading branch information
elfosardo authored Oct 9, 2020
2 parents d14a46f + dce749e commit aedac88
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 151 deletions.
114 changes: 1 addition & 113 deletions hardware/detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,118 +52,6 @@
AUXV_OPT_FLAGS = ["AT_BASE_PLATFORM"]


def detect_megacli(hw_lst):
'Detect LSI MegaRAID controller configuration.'
ctrl_num = megacli.adp_count()
disk_count = 0
global_pdisk_size = 0
if ctrl_num > 0:
for ctrl in range(ctrl_num):
ctrl_info = megacli.adp_all_info(ctrl)
for entry in ctrl_info.keys():
hw_lst.append(('megaraid', 'Controller_%d' % (ctrl),
'%s' % entry, '%s' % ctrl_info[entry]))

for enc in megacli.enc_info(ctrl):
if "Enclosure" in enc.keys():
for key in enc.keys():
if "ExitCode" in key or "Enclosure" in key:
continue
hw_lst.append(('megaraid',
'Controller_%d/Enclosure_%s' %
(ctrl, enc["Enclosure"]),
'%s' % key, '%s' % enc[key]))

for slot_num in range(enc['NumberOfSlots']):
disk = 'disk%d' % slot_num
info = megacli.pdinfo(ctrl,
enc['DeviceId'],
slot_num)

# If no PdType, it means that's not a disk
if 'PdType' not in info.keys():
continue

disk_count += 1
hw_lst.append(('pdisk',
disk,
'ctrl',
str(ctrl_num)))
hw_lst.append(('pdisk',
disk,
'type',
info['PdType']))
hw_lst.append(('pdisk',
disk,
'id',
'%s:%d' % (info['EnclosureDeviceId'],
slot_num)))
disk_size = detect_utils.size_in_gb(
"%s %s" % (info['CoercedSize'].split()[0],
info['CoercedSize'].split()[1]))
global_pdisk_size = global_pdisk_size + float(disk_size)
hw_lst.append(('pdisk',
disk,
'size',
disk_size))

for key in info.keys():
ignore_list = ['PdType', 'EnclosureDeviceId',
'CoercedSize', 'ExitCode']
if key not in ignore_list:
if "DriveTemperature" in key:
if "C" in str(info[key].split()[0]):
pdisk = info[key].split()[0].split("C")[0]
hw_lst.append(('pdisk', disk, key,
str(pdisk).strip()))
hw_lst.append(('pdisk', disk,
"%s_units" % key,
"Celsius"))
else:
hw_lst.append(('pdisk', disk, key,
str(info[key]).strip()))

elif "InquiryData" in key:
count = 0
for mystring in info[key].split():
hw_lst.append(('pdisk', disk,
"%s[%d]" % (key, count),
str(mystring.strip())))
count = count + 1

else:
hw_lst.append(('pdisk', disk, key,
str(info[key]).strip()))

if global_pdisk_size > 0:
hw_lst.append(('pdisk',
'all',
'size',
"%.2f" % global_pdisk_size))

for ld_num in range(megacli.ld_get_num(ctrl)):
disk = 'disk%d' % ld_num
info = megacli.ld_get_info(ctrl, ld_num)
ignore_list = ['Size']

for item in info.keys():
if item not in ignore_list:
hw_lst.append(('ldisk',
disk,
item,
str(info[item])))
if 'Size' in info:
hw_lst.append(
('ldisk',
disk,
'Size',
detect_utils.size_in_gb(info['Size'])))
hw_lst.append(('disk', 'megaraid', 'count', str(disk_count)))
return True
else:
return False


def detect_disks(hw_lst):
"""Detect disks."""

Expand Down Expand Up @@ -876,7 +764,7 @@ def main():
hrdw = []
hrdw.append(areca.detect())
hrdw.append(hpacucli.detect())
detect_megacli(hrdw)
hrdw.append(megacli.detect())
detect_disks(hrdw)
if not detect_system(hrdw):
sys.exit(1)
Expand Down
144 changes: 106 additions & 38 deletions hardware/megacli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@
# License for the specific language governing permissions and limitations
# under the License.

'''Wrapper functions around the megacli command.'''
"""Wrapper functions around the megacli command."""

import os
import re
from subprocess import PIPE
from subprocess import Popen
import sys

from hardware import detect_utils


SEP_REGEXP = re.compile(r'\s*:\s*')


Expand Down Expand Up @@ -86,7 +89,7 @@ def search_exec(possible_names):


def parse_output(output):
'Parse the output of the megacli command into an associative array.'
"""Parse the output of the megacli command into an associative array."""
res = {}
for line in output.split('\n'):
lis = re.split(SEP_REGEXP, line.strip())
Expand All @@ -101,7 +104,7 @@ def parse_output(output):


def split_parts(sep, output):
'Split the output string according to the regexp sep.'
"""Split the output string according to the regexp sep."""
regexp = re.compile(sep)
lines = output.split('\n')
idx = []
Expand All @@ -120,7 +123,7 @@ def split_parts(sep, output):


def run_megacli(*args):
'Run the megacli command in a subprocess and return the output.'
"""Run the megacli command in a subprocess and return the output."""
prog_exec = search_exec(["megacli", "MegaCli", "MegaCli64"])
if prog_exec:
cmd = prog_exec + ' - ' + ' '.join(args)
Expand All @@ -132,24 +135,24 @@ def run_megacli(*args):


def run_and_parse(*args):
'''Run the megacli command in a subprocess.
"""Run the megacli command in a subprocess.
Returns the output as an associative array.
'''
Returns the output as an associative array.
"""
res = run_megacli(*args)
return parse_output(res)


def adp_count():
'Get the numberof adaptaters.'
"""Get the numberof adaptaters."""
arr = run_and_parse('adpCount')
if 'ControllerCount' in arr:
return int(arr['ControllerCount'])
return 0


def adp_all_info(ctrl):
'Get adaptater info.'
"""Get adaptater info."""
arr = run_and_parse('adpallinfo -a%d' % ctrl)
for key in ('RaidLevelSupported', 'SupportedDrives'):
if key in arr:
Expand All @@ -158,7 +161,7 @@ def adp_all_info(ctrl):


def pd_get_num(ctrl):
'Get the number of physical drives on a controller.'
"""Get the number of physical drives on a controller."""
try:
key = 'NumberOfPhysicalDrivesOnAdapter%d' % ctrl
return run_and_parse('PDGetNum -a%d' % ctrl)[key]
Expand All @@ -167,7 +170,7 @@ def pd_get_num(ctrl):


def enc_info(ctrl):
'Get enclosing info on a controller.'
"""Get enclosing info on a controller."""
parts = split_parts(' +Enclosure [0-9]+:',
run_megacli('EncInfo -a%d' % ctrl))
all_ = list(map(parse_output, parts))
Expand All @@ -181,12 +184,12 @@ def enc_info(ctrl):


def pdinfo(ctrl, encl, disk):
'Get info about a physical drive on an enclosure and a controller.'
"""Get info about a physical drive on an enclosure and a controller."""
return run_and_parse('pdinfo -PhysDrv[%d:%d] -a%d' % (encl, disk, ctrl))


def ld_get_num(ctrl):
'Get the number of logical drives on a controller.'
"""Get the number of logical drives on a controller."""
try:
key = 'NumberOfVirtualDrivesConfiguredOnAdapter%d' % ctrl
return run_and_parse('LDGetNum -a%d' % ctrl)[key]
Expand All @@ -195,31 +198,96 @@ def ld_get_num(ctrl):


def ld_get_info(ctrl, ldrv):
'Get info about a logical drive on a controller.'
"""Get info about a logical drive on a controller."""
return run_and_parse('LDInfo -L%d -a%d' % (ldrv, ctrl))


if __name__ == "__main__":
import pprint

for ctrl_num in range(adp_count()):
print('Controler', ctrl_num)
pprint.pprint(adp_all_info(ctrl_num))

encs = enc_info(ctrl_num)

print()

print('Enclosing:')
pprint.pprint(encs)

for enc in encs:
for disk_num in range(enc['NumberOfPhysicalDrives']):
print()
print('Physical disk', disk_num)
pprint.pprint(pdinfo(ctrl_num, enc['DeviceId'], disk_num))

for ld_num in range(ld_get_num(ctrl_num)):
print()
print('Logical disk', ld_num)
pprint.pprint(ld_get_info(ctrl_num, ld_num))
def detect():
"""Detect LSI MegaRAID controller configuration."""
hw_lst = []
ctrl_num = adp_count()
if ctrl_num == 0:
return hw_lst

disk_count = 0
global_pdisk_size = 0

for ctrl in range(ctrl_num):
ctrl_info = adp_all_info(ctrl)
for entry in ctrl_info.keys():
hw_lst.append(('megaraid', 'Controller_%d' % ctrl, '%s' % entry,
'%s' % ctrl_info[entry]))

for enc in enc_info(ctrl):
if "Enclosure" in enc.keys():
for key in enc.keys():
ignore_list = ["ExitCode", "Enclosure"]
if key in ignore_list:
continue
hw_lst.append(('megaraid',
'Controller_%d/Enclosure_%s' %
(ctrl, enc["Enclosure"]),
'%s' % key, '%s' % enc[key]))

for slot_num in range(enc['NumberOfSlots']):
disk = 'disk%d' % slot_num
info = pdinfo(ctrl, enc['DeviceId'], slot_num)

# If no PdType, it means that's not a disk
if 'PdType' not in info.keys():
continue

disk_count += 1
hw_lst.append(('pdisk', disk, 'ctrl', str(ctrl_num)))
hw_lst.append(('pdisk', disk, 'type', info['PdType']))
hw_lst.append(('pdisk', disk, 'id',
'%s:%d' % (info['EnclosureDeviceId'],
slot_num)))
disk_size = detect_utils.size_in_gb(
"%s %s" % (info['CoercedSize'].split()[0],
info['CoercedSize'].split()[1]))
global_pdisk_size = global_pdisk_size + float(disk_size)
hw_lst.append(('pdisk', disk, 'size', disk_size))

for key in info.keys():
ignore_list = ['PdType', 'EnclosureDeviceId',
'CoercedSize', 'ExitCode']
if key not in ignore_list:
if "DriveTemperature" in key:
if "C" in str(info[key].split()[0]):
pdisk = info[key].split()[0].split("C")[0]
hw_lst.append(('pdisk', disk, key,
str(pdisk).strip()))
hw_lst.append(('pdisk', disk,
"%s_units" % key,
"Celsius"))
else:
hw_lst.append(('pdisk', disk, key,
str(info[key]).strip()))
elif "InquiryData" in key:
count = 0
for mystring in info[key].split():
hw_lst.append(('pdisk', disk,
"%s[%d]" % (key, count),
str(mystring.strip())))
count = count + 1
else:
hw_lst.append(('pdisk', disk, key,
str(info[key]).strip()))
if global_pdisk_size > 0:
hw_lst.append(('pdisk', 'all', 'size',
"%.2f" % global_pdisk_size))
for ld_num in range(ld_get_num(ctrl)):
disk = 'disk%d' % ld_num
info = ld_get_info(ctrl, ld_num)
ignore_list = ['Size']

for item in info.keys():
if item not in ignore_list:
hw_lst.append(('ldisk', disk, item,
str(info[item])))
if 'Size' in info:
hw_lst.append(('ldisk', disk, 'Size',
detect_utils.size_in_gb(info['Size'])))
hw_lst.append(('disk', 'megaraid', 'count', str(disk_count)))
return hw_lst

0 comments on commit aedac88

Please sign in to comment.