Skip to content

Instantly share code, notes, and snippets.

@misodengaku
Created November 16, 2021 13:30
Show Gist options
  • Save misodengaku/ff89d4cf40e406498ae9b6663a81e430 to your computer and use it in GitHub Desktop.
Save misodengaku/ff89d4cf40e406498ae9b6663a81e430 to your computer and use it in GitHub Desktop.
megaraidとdisk id良い感じにするやつ
import subprocess
from pprint import pprint
import re
import os
import json
import humanize
cfg = subprocess.run(
["sudo", "megacli", "-CfgDsply", "-a0"], capture_output=True, text=True)
# print(cfg)
hw = subprocess.run(
["sudo", "lshw", "-class", "disk", "-json"], capture_output=True, text=True)
hw_json = json.loads(hw.stdout)
disk_group = re.split('DISK GROUP: ', cfg.stdout)
# pprint(hw_json)
for disk in filter(lambda x: x['product'] == 'RAID 5/6 SAS 6G', hw_json):
print('---- ' + disk['id'])
print('\tsize:\t' + humanize.naturalsize(disk['size']))
s = [disk['serial'][i: i+2]
for i in range(0, len(disk['serial']), 2)]
s.reverse()
# print(s)
by_id_path = '/dev/disk/by-id/scsi-360' + (''.join(s[:-1]))
print('\tpath:\t%s (validity: %r)' %
(by_id_path, os.path.exists(by_id_path)))
if os.path.exists(by_id_path):
sd = os.readlink(by_id_path)
print('\tblk:\t%s' % os.path.basename(sd))
match_disks = list(filter(lambda x: 'Virtual Drive: %d' %
int(disk['id'].lstrip('disk:')) in x, disk_group))
for d in match_disks:
disk_info = d.splitlines()
inq = list(map(lambda x: re.sub(r'Inquiry Data:\s+', '', x).strip(),
filter(lambda x: 'Inquiry Data' in x, disk_info)))
print('\tmodel:\t' + inq[0])
enc_id = re.search('Enclosure Device ID: (\d+)', d)
if enc_id:
slot = re.search('Slot Number: (\d+)', d)
print('\tloc:\tmegacli -PdLocate -start -physdrv\[%d:%d\] -a0' %
(int(enc_id.group(1)), int(slot.group(1))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment