Skip to content

Instantly share code, notes, and snippets.

@classmember
Created August 11, 2017 15:07
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 classmember/00977a5f5ef9db9fdfc86c03e9ba1355 to your computer and use it in GitHub Desktop.
Save classmember/00977a5f5ef9db9fdfc86c03e9ba1355 to your computer and use it in GitHub Desktop.
MegaPy - MegaCLI wrapper
#!/usr/bin/env python
'''
MegaPy - MegaCLI wrapper
'''
import os
import subprocess
import argparse
# import sh
__version__ = 'MegaPy v.1'
__author__ = 'Riley - riley@fasterdevops.com'
MEGACLI_INSTALLATION_PATH = "/opt/MegaRAID/"
class MegaCLI(object):
'''
MegaCLI wrapper
Example Usage:
>>> from megapy import MegaCLI
>>> command = MegaCLI.make_command()
>>> command.execute()
>>>
'''
megadir = os.path.dirname(MEGACLI_INSTALLATION_PATH)
def check_install(self):
'''
Checks if MegaCLI is installed.
'''
if not os.path.exists(self.megadir): # Check if MegaCLI is installed
valid_input = False
while not valid_input:
# Ask user if they would like to install
install = raw_input('MegaCLI not found!!\n' +
'Install MegaCLI? (yes/no): ').lower()
if install in ('yes', 'ye', 'y'):
self.install_megacli()
valid_input = True
if install in ('no', 'n'):
print('Closing....')
valid_input = True
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 install_megacli(self):
'''
RedHat Command to install imh-megapkg
$ /usr/bin/yum install imh-megapkg -y
'''
subprocess.Popen(['/usr/bin/yum',
'install',
'imh-megapkg',
'-y'])
def parse_arguments(self):
'''
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('--alarm',
help=('Turn the alarm off or on '
'by passing off or on argument'))
parser.add_argument('--basedir',
help='Specify the base directory where' +
'MegaCLI is installed.',
dest='basedir')
parser.add_argument('--hello',
help='Hello World message',
action='store_true')
self.args = parser.parse_args()
def __init__(self, megacli_dir=''):
'''
Define program installation directory
Then parse the arguments
:param megacli_dir - The installation directory of MegaCLI
'''
if megacli_dir is not '': # If user supplies installation path,
self.megadir = os.path.dirname(megacli_dir) # use supplied path
self.parse_arguments() # Then parse arguments
def make_command(self):
'''
Factory method which returns a command as an object
'''
if self.args.basedir:
self.set_basedir(self.args.basedir)
if self.args.phys:
return ViewPhysicalDrives()
if self.args.vdrive:
return ViewVirtualDrives()
if self.args.control:
return ViewPhysicalDrives()
if self.args.alarm:
return DisableAlarm()
if self.args.alarm:
return EnableAlarm()
if self.args.hello:
return HelloWorld()
# If no options were given, return help function
return HelpMenu()
class ViewEnclosures(MegaCLI):
'''
View the servers enclosure information
'''
def execute(self):
'''
$ MegaCli64 -EncInfo -aALL
'''
subprocess.Popen(self.megadir, ['MegaCli64',
'-EncInfo',
'-aALL'])
class ViewPhysicalDrives(MegaCLI):
'''
View the servers physical drive information
'''
def execute(self):
'''
$ MegaCli64 -Pdlist -aALL
'''
subprocess.Popen(self.megadir, ['MegaCli64',
'-Pdlist',
'-aALL'])
class ViewVirtualDrives(MegaCLI):
'''
View the servers virtual drive information
'''
def execute(self):
'''
$ MegaCli64 -LDInfo -Lall -aALL
'''
subprocess.Popen(self.megadir, ['MegaCli64',
'-LDInfo',
'-Lall',
'-aLL'])
class ViewBatteryInfo(MegaCLI):
'''
View battery information
'''
def execute(self):
'''
$ MegaCLI64 -AdpBbuCmd -aALL
'''
subprocess.Popen(self.megadir, ['MegaCli64',
'-AdpBbuCmd',
'-aALL'])
class StartRebuildDrive(MegaCLI):
'''
Start rebuilding drive
:param enclosure - physical enclosure from view_enclosures()
:param slot - slot of the enclosure
'''
def execute(self, enclosure, slot):
'''
$ MegaCli64 -PDRbld -Start -PhysDrv [E,S] -aN
'''
subprocess.Popen(self.megadir, ['MegaCli64',
'-PDRbld',
'-Start',
'-PhysDrv',
'[' + enclosure + ',' + slot + ']',
'-aN'])
class StopRebuildDrive(MegaCLI):
'''
Stop drive rebuild
:param enclosure - physical enclosure from view_enclosures()
:param slot - slot of the enclosure
'''
def execute(self, enclosure, slot):
'''
$ MegaCli42 -PDRbld -Stop -PhysDrv [E,S] -aN
'''
subprocess.Popen(self.megadir, ['MegaCli64',
'-PDRbld',
'-Stop',
'-PhysDrv',
'[' + enclosure + ',' + slot + ']',
'-aN'])
class ViewRebuildDriveProgress(MegaCLI):
'''
Stop drive rebuild
:param enclosure - physical enclosure from view_enclosures()
:param slot - slot of the enclosure
'''
def execute(self, enclosure, slot):
'''
$ MegaCli42 -PDRbld -ShowProg -PhysDrv [E,S] -aN
'''
subprocess.Popen(self.megadir, ['MegaCli64',
'-PDRbld',
'-ShowProg',
'-PhysDrv',
'[' + enclosure + ',' + slot + ']',
'-aN'])
class DisableAlarm(MegaCLI):
'''
Disable the alarm
'''
def execute(self):
'''
$ MegaCli42 -AdpSetProp AlarmDsbl -aALL
'''
subprocess.Popen(self.megadir, ['MegaCli64',
'-AdpSetProp',
'AlarmDsbl',
'-aALL'])
class EnableAlarm(MegaCLI):
'''
Enable the alarm
'''
def execute(self):
'''
$ MegaCli42 -AdpSetProp AlarmEnbl -aALL
'''
subprocess.Popen(self.megadir, ['MegaCli64',
'-AdpSetProp',
'AlarmEnbl',
'-aALL'])
class HelloWorld(MegaCLI):
'''
Test "Hello World" message
'''
def execute(self):
'''
$ echo Hello World
'''
subprocess.Popen(['/bin/echo', 'Hello World'])
class HelpMenu(MegaCLI):
'''
Display Help Menu
'''
def execute(self):
'''
output of MegaCLI -h
'''
print('''usage: MegaPy.py [-h] [--enclosure ENCL] [--physical PHYS] [--vdrive VDRIVE]
[--controller CONTROL] [--alarm ALARM] [--basedir BASEDIR]
A wrapper to make MegaCLI commands more user friendly and easier to remember
optional arguments:
-h, --help show this help message and exit
--enclosure ENCL View the servers enclosure information
--physical PHYS View the servers physical drive information
--vdrive VDRIVE View the servers virtual drive information
--controller CONTROL View the servers controller information
--alarm ALARM Turn the alarm off or on by passing off or on argument
--basedir BASEDIR Specify the base directory whereMegaCLI is installed.''')
if __name__ == '__main__':
print __version__ # print version
MEGA = MegaCLI() # initialize MegaCLI object
CMD = MEGA.make_command() # get MegaCLI command
CMD.execute() # then execute command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment