Created
August 6, 2017 19:36
-
-
Save classmember/d2fb437b36b1bfd9896a33bbf09d7f4d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
''' | |
MegaPy - MegaCLI wrapper | |
The Python wrapper is an attempt to make RAID management easier | |
for the MegaRAID line of RAID devices. | |
MegaRAID and MegaCLI are currently supported by Broadcom. | |
LSI Documentation on MegaCLI: | |
ftp://ftp.sgi.com/public/Technical%20Support/Pdf%20files/LSI/84016E_mr_sas_sw_ug.pdf | |
ftp://ftp0.broadcom.com/private/MegaRAID/doc/MegaRAIDSAS_VMware_UserGuide_RevB_51530-00.pdf | |
List of MegaRAID Adapters: | |
ftp://ftp0.broadcom.com/private/MegaRAID/doc/LSI_Product_Selector_Guide_EN.pdf | |
Broadcom's Documentation section: | |
https://www.broadcom.com/support/knowledgebase/1211161494780/megaraid-documentation | |
MegaCLI Cheatsheet: | |
http://erikimh.com/megacli-cheatsheet/ | |
''' | |
import sh | |
import os | |
import subprocess | |
import argparse | |
__version__ = 'MegaPy v.1' | |
__author__ = 'Riley - [email protected]' | |
MEGACLI_INSTALLATION_PATH = "/opt/MegaCli/" | |
class MegaCLI(object): | |
''' | |
MegaCLI wrapper | |
Example Usage: | |
>>> from megapy import MegaCLI | |
>>> mega = MegaCLI() | |
>>> mega.check_install() | |
>>> mega.view_enclosures() | |
>>> | |
''' | |
megadir = os.path.dirname(MEGACLI_INSTALLATION_PATH) | |
def check_install(self): | |
''' | |
Checks if MegaCLI is installed. | |
''' | |
# RedHat Command to install imh-megapkg | |
install_megacli = ['/usr/bin/yum', | |
'install', | |
'imh-megapkg', | |
'-y'] | |
# If MegaCLI is not installed, prompt user | |
# to see if they would like to install | |
if not os.path.exists(self.megadir): | |
valid_input = False | |
while not valid_input: | |
install = raw_input('MegaCLI does not exist on this' + | |
'server!\n' + | |
' Would you like' + | |
'to install it now?(yes/no): ').lower() | |
if install in ('yes', 'y'): | |
subprocess.Popen(install_megacli) | |
valid_input = True | |
if install in ('no', 'n'): | |
print('Closing....') | |
valid_input = True | |
def __init__(self, megacli_dir=''): | |
''' | |
define program installation directory and commands | |
:param megacli_dir - The installation directory of MegaCLI | |
''' | |
# If user supplies different installation path, | |
# use the installation path | |
if megacli_dir is not '': | |
self.megadir = os.path.dirname(megacli_dir) | |
self.get_enclosure_info_command = ['MegaCli64', | |
'-EncInfo', | |
'-aALL'] | |
self.get_physical_info_command = ['MegaCli64', | |
'-Pdlist', | |
'-aALL'] | |
self.vdrive_command = ['MegaCli64', | |
'-LDInfo', | |
'-Lall', | |
'-aLL'] | |
self.battery_command = ['MegaCli64', | |
'-AdpBbuCmd', | |
'-aALL'] | |
self.start_rebuild_drive_command = ['MegaCli64', | |
'-PDRbld', | |
'-Start', | |
'-PhysDrv', | |
'[E:S]', | |
'-aN'] | |
self.stop_rebuild_drive_command = ['MegaCli64', | |
'-PDRbld', | |
'-Stop', | |
'-PhysDrv', | |
'[E:S]', | |
'-aN'] | |
self.showprog_rebuild_drive_command = ['MegaCli64', | |
'-PDRbld', | |
'-ShowProg', | |
'-PhysDrv', | |
'[E:S]', | |
'-aN'] | |
# Using subprocess to to run megadir with command | |
def view_enclosures(self): | |
''' | |
View the servers enclosure information | |
''' | |
subprocess.Popen(self.megadir, self.get_enclosure_info_command) | |
def view_physical_drives(self): | |
''' | |
View the servers physical drive information | |
''' | |
subprocess.Popen(self.megadir, self.get_physical_info_command) | |
def view_vdrive(self): | |
''' | |
View the servers virtual drive information | |
''' | |
subprocess.Popen(self.megadir, self.vdrive_command) | |
def battery_info(self): | |
''' | |
View battery information | |
''' | |
subprocess.Popen(self.megadir, self.battery_command) | |
def start_rebuild_drive(self, enclosure, slot): | |
''' | |
Start rebuilding drive | |
:param enclosure - physical enclosure from view_enclosures() | |
:param slot - slot of the enclosure | |
''' | |
command = self.start_rebuild_drive_command | |
command[4] = '[' + enclosure + ',' + slot + ']' | |
subprocess.Popen(self.megadir, command) | |
def stop_rebuild_drive(self, enclosure, slot): | |
''' | |
Stop rebuilding drive | |
:param enclosure - physical enclosure from view_enclosures() | |
:param slot - slot of the enclosure | |
''' | |
command = self.start_rebuild_drive_command | |
command[4] = '[' + enclosure + ',' + slot + ']' | |
subprocess.Popen(self.megadir, command) | |
def show_rebuild_drive(self, enclosure, slot): | |
''' | |
Show status of rebuilding drive | |
:param enclosure - physical enclosure from view_enclosures() | |
:param slot - slot of the enclosure | |
''' | |
command = self.start_rebuild_drive_command | |
command[4] = '[' + enclosure + ',' + slot + ']' | |
subprocess.Popen(self.megadir, command) | |
def rebuild_drive(self, enclosure, slot): | |
''' | |
run the commands: | |
start_rebuild_drive(enclosure, slot) | |
stop_rebuild_drive(enclosure, slot) | |
show_rebuild_drive(enclosure, slot) | |
:param enclosure - physical enclosure from view_enclosures() | |
:param slot - slot of the enclosure | |
''' | |
self.start_rebuild_drive(enclosure, slot) | |
self.stop_rebuild_drive(enclosure, slot) | |
self.show_rebuild_drive(enclosure, slot) | |
def set_basedir(self, megacli_dir): | |
''' | |
Set the base directory for MegaCLI | |
:param megacli_dir - The installation directory of MegaCLI | |
''' | |
self.megadir = os.path.dirname(megacli_dir) | |
def parse_arguments(): | |
''' | |
Parsing user arguments. Use -h to see available commands. | |
''' | |
parser = argparse.ArgumentParser(description=('A wrapper to make ' | |
'MegaCLI commands more ' | |
'user friendly and easier ' | |
'to remember') | |
) | |
parser.add_argument('--enclosure', | |
help='View the servers enclosure information', | |
dest='encl' | |
) | |
parser.add_argument('--physical', | |
help='View the servers physical drive information', | |
dest='phys' | |
) | |
parser.add_argument('--vdrive', | |
help='View the servers virtual drive information', | |
dest='vdrive' | |
) | |
parser.add_argument('--controller', | |
help='View the servers controller information', | |
dest='control' | |
) | |
parser.add_argument('--basedir', | |
help='Specify the base directory where' + | |
'MegaCLI is installed.', | |
dest='basedir' | |
) | |
return parser.parse_args() | |
def main(): | |
''' | |
main function. | |
Initilizing MegaCLI object,parsing arguments, and calling functions. | |
''' | |
# display version and parse arguments | |
print __version__ | |
args = parse_arguments() | |
# initialize MegaCLI object | |
mega = MegaCLI() | |
mega.check_install() | |
if args.basedir: | |
mega.set_basedir(args.basedir) | |
if args.encl: | |
mega.view_enclosures() | |
if args.vdrive: | |
mega.view_vdrive() | |
if args.phys: | |
mega.view_physical_drives() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment