Skip to content

Instantly share code, notes, and snippets.

@adzhurinskij
Created April 24, 2019 14:21
Show Gist options
  • Save adzhurinskij/fa76478969205c3319b9b92452167ed4 to your computer and use it in GitHub Desktop.
Save adzhurinskij/fa76478969205c3319b9b92452167ed4 to your computer and use it in GitHub Desktop.
Pretty disks map for LSI HBA
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import re
import json
from io import open
from subprocess import Popen, PIPE
from tabulate import tabulate
def bash_command(command):
pipe = Popen(command, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
stdout, stderr = pipe.communicate()
return ( stdout, stderr )
def search(string, lines):
pattern = re.compile(string)
out = []
for line in lines:
if pattern.search(line) is not None:
out.append(pattern.search(line).group())
return out
def main():
sasinfo_file = os.path.realpath('sasinfo.txt')
if os.path.isfile(sasinfo_file) is False:
sasinfo, _ = bash_command('/usr/local/bin/sas2ircu 0 DISPLAY')
with open(sasinfo_file, 'wb') as fd:
fd.write(sasinfo)
sasinfo = sasinfo.decode('utf8')
else:
with open(sasinfo_file, 'r') as fd:
sasinfo = fd.read()
lsblk, _ = bash_command('/bin/lsblk -J -o +SERIAL')
lsblk = json.loads(lsblk.decode('utf8'))['blockdevices']
table = []
for item in lsblk:
_name = '/dev/{}'.format(item['name'])
_size = item['size']
_serial = item['serial']
_slot = None
_parts = []
if 'children' in item:
_parts = [ part['name'] for part in item['children'] ]
if _serial.startswith('WD-') is True:
_serial = _serial.split('WD-')[1]
_sasinfo = re.search(r'Enclosure(.*\n){8}.*' + _serial, sasinfo)
if _sasinfo:
_slot = search(r'Slot.+', _sasinfo.group().splitlines())[0].split()[3]
table.append([
_name,
_serial,
_size,
_slot,
', '.join(_parts),
])
print(tabulate(table, headers=['Name', 'Serial', 'Size', 'Slot', 'Partitions']))
print('\nAll disks: {}'.format(len(lsblk)))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment