Skip to content

Instantly share code, notes, and snippets.

@pyKun
Created May 12, 2013 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pyKun/5562772 to your computer and use it in GitHub Desktop.
Save pyKun/5562772 to your computer and use it in GitHub Desktop.
megacli python client
#!/usr/bin/env python
#-*- coding:utf-8 -*-
# Author: Kun Huang <academicgareth@gmail.com>
# Created Time: 05/09/13 11:04:46 (CST)
# Modified Time: 05/12/13 15:35:42 (CST)
# do raid 1 more better
# do raid with hot
# remvoe all hot
import argparse
import sys
import subprocess
parser = argparse.ArgumentParser(description='Apply friendly MegaCli command.')
subparsers = parser.add_subparsers(help='sub-command: all of operations you'
' could use')
parser_pd = subparsers.add_parser('pd', help='operations of physics disks')
parser_pd.add_argument('-c', '--count', action='store_true',
help='return num of physics disks')
parser_pd.add_argument('-l', '--list', action='store_true',
help='return list of physics disks')
parser_raid = subparsers.add_parser('raid', help='operations of MegaRaid')
parser_raid.add_argument('-c', '--clear', action='store_true',
help='clear/delete logic volume of raid')
parser_raid.add_argument('-l', '--level', type=int,
help='standard level of raid you wanna build')
parser_raid.add_argument('--hot', action='store_true',
help='use the last disk as a hot sparse')
_args = parser.parse_args()
def runProcess(exe, rt='line'):
exe = exe.split(' ')
p = subprocess.Popen(exe, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
retcode = p.poll()
if rt == 'all':
out = p.stdout.read()
elif rt == 'line':
out = p.stdout.readlines()
if not retcode:
return out
else:
print 'Command %s error.' % exe
def _get_pd_num():
exe = 'MegaCli -PDList -aALL'
ct_lines = runProcess(exe)
_pds = [i for i in ct_lines if i.startswith('Slot Number') and i.find('255') == -1]
return len(_pds)
def _get_pd_list():
exe = "MegaCli -PDList -aALL"
ct_lines = runProcess(exe)
enc = [i.split(':')[-1].strip() for i in ct_lines if i.startswith('Enclosure Device') and i.find('65535') == -1]
slot = [i.split(':')[-1].strip() for i in ct_lines if i.startswith('Slot Number') and i.find('255') == -1]
pd = zip(enc, slot)
return pd
def _hot_last_disk(pd):
hot = pd.pop(-1)
hot = '[%s:%s]' % hot
exe = 'MegaCli -PDHSP -Set -PhysDrv %s -a0' % hot
runProcess(exe)
exe = 'MegaCli -AdpSetProp RstrHotSpareOnInsert 1 -aALL'
runProcess(exe)
return pd
def _clear_foreign():
exe ='MegaCli -CfgForeign -Clear -aALL'
runProcess(exe)
return
def main():
#import ipdb;ipdb.set_trace()
#print _args
if sys.argv[1] == 'pd':
if _args.count:
num = _get_pd_num()
print num
return 0
elif _args.list:
pd = _get_pd_list()
print pd
return 0
return 1
if sys.argv[1] == 'raid':
if _args.clear:
exe = 'MegaCli -CfgLdDel -LALL -aALL'
runProcess(exe)
return 0
if _args.level in [1, 0]:
_clear_foreign()
pd = _get_pd_list()
if _args.hot:
_hot_last_disk(pd)
_pd = ','.join(['%s:%s' % p for p in pd])
_pd = '[' + _pd + ']'
exe = 'MegaCli -CfgLdAdd -r%s%s -a0' % (_args.level, _pd)
runProcess(exe)
print 'do raid %d by %s' % (_args.level, exe)
elif _args.level == 10:
_clear_foreign()
pd = _get_pd_list()
if _args.hot:
_hot_last_disk(pd)
_pd = ['%s:%s' % p for p in pd]
_array = list()
for i in range(len(pd) / 2):
_arr = '-Array%d[%s,%s]' % (i, _pd[i * 2], _pd[i * 2 + 1])
_array.append(_arr)
exe = 'MegaCli -CfgSpanAdd -r10 %s -a0' % ' '.join(_array)
runProcess(exe)
print 'do raid 10 by %s' % exe
return 1
return 1
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment