-
Notifications
You must be signed in to change notification settings - Fork 721
Add SSD Health CLI utility #587
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
454b1f3
Add SSD Health CLI utility
andriymoroz-mlnx fc2d9f5
Merge remote-tracking branch 'upstream/master' into ssdhealth
andriymoroz-mlnx dd02351
Add SSD Health command reference
andriymoroz-mlnx ea024e0
Rename ssdutility entrypoint to ssdutil
andriymoroz-mlnx f59e51f
Fix - do not add units if value is N/A
andriymoroz-mlnx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
#!/usr/bin/env python | ||
# | ||
# main.py | ||
# | ||
# Command-line utility to check SSD health and parameters | ||
# | ||
|
||
try: | ||
import sys | ||
import os | ||
import subprocess | ||
import argparse | ||
import syslog | ||
except ImportError as e: | ||
raise ImportError("%s - required module not found" % str(e)) | ||
|
||
DEFAULT_DEVICE="/dev/sda" | ||
SYSLOG_IDENTIFIER = "ssdutil" | ||
|
||
PLATFORM_ROOT_PATH = '/usr/share/sonic/device' | ||
SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen' | ||
HWSKU_KEY = 'DEVICE_METADATA.localhost.hwsku' | ||
PLATFORM_KEY = 'DEVICE_METADATA.localhost.platform' | ||
|
||
def syslog_msg(severity, msg, stdout=False): | ||
""" | ||
Prints to syslog (and stdout if needed) message with specified severity | ||
|
||
Args: | ||
severity : message severity | ||
msg : message | ||
stdout : also primt message to stdout | ||
|
||
""" | ||
syslog.openlog(SYSLOG_IDENTIFIER) | ||
syslog.syslog(severity, msg) | ||
syslog.closelog() | ||
|
||
if stdout: | ||
print msg | ||
|
||
def get_platform_and_hwsku(): | ||
""" | ||
Retrieves current platform name and hwsku | ||
Raises an OSError exception when failed to fetch | ||
|
||
Returns: | ||
tuple of strings platform and hwsku | ||
e.g. ("x86_64-mlnx_msn2700-r0", "ACS-MSN2700") | ||
""" | ||
try: | ||
proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-H', '-v', PLATFORM_KEY], | ||
stdout=subprocess.PIPE, | ||
shell=False, | ||
stderr=subprocess.STDOUT) | ||
stdout = proc.communicate()[0] | ||
proc.wait() | ||
platform = stdout.rstrip('\n') | ||
|
||
proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-d', '-v', HWSKU_KEY], | ||
stdout=subprocess.PIPE, | ||
shell=False, | ||
stderr=subprocess.STDOUT) | ||
stdout = proc.communicate()[0] | ||
proc.wait() | ||
hwsku = stdout.rstrip('\n') | ||
except OSError, e: | ||
raise OSError("Cannot detect platform") | ||
|
||
return (platform, hwsku) | ||
|
||
def import_ssd_api(diskdev): | ||
""" | ||
Loads platform specific or generic ssd_util module from source | ||
Raises an ImportError exception if none of above available | ||
|
||
Returns: | ||
Instance of the class with SSD API implementation (vendor or generic) | ||
""" | ||
|
||
# Get platform and hwsku | ||
(platform, hwsku) = get_platform_and_hwsku() | ||
|
||
# try to load platform specific module | ||
try: | ||
hwsku_plugins_path = "/".join([PLATFORM_ROOT_PATH, platform, "plugins"]) | ||
sys.path.append(os.path.abspath(hwsku_plugins_path)) | ||
from ssd_util import SsdUtil | ||
except ImportError as e: | ||
syslog_msg(syslog.LOG_WARNING, "Platform specific SsdUtil module not found. Falling down to the generic implementation") | ||
try: | ||
from sonic_platform_base.sonic_ssd.ssd_generic import SsdUtil | ||
except ImportError as e: | ||
syslog_msg(syslog.LOG_ERR, "Failed to import default SsdUtil. Error: {}".format(str(e)), True) | ||
raise e | ||
|
||
return SsdUtil(diskdev) | ||
|
||
# ==================== Entry point ==================== | ||
def cli(): | ||
if os.geteuid() != 0: | ||
print "Root privileges are required for this operation" | ||
sys.exit(1) | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument("-d", "--device", help="Device name to show health info", default=DEFAULT_DEVICE) | ||
parser.add_argument("-v", "--verbose", action="store_true", default=False, help="Show verbose output (some additional parameters)") | ||
parser.add_argument("-e", "--vendor", action="store_true", default=False, help="Show vendor output (extended output if provided by platform vendor)") | ||
args = parser.parse_args() | ||
|
||
ssd = import_ssd_api(args.device) | ||
|
||
print "Device Model : {}".format(ssd.get_model()) | ||
if args.verbose: | ||
print "Firmware : {}".format(ssd.get_firmware()) | ||
print "Serial : {}".format(ssd.get_serial()) | ||
print "Health : {}%".format(ssd.get_health()) | ||
print "Temperature : {}C".format(ssd.get_temperature()) | ||
if args.vendor: | ||
print ssd.get_vendor_output() | ||
|
||
if __name__ == '__main__': | ||
cli() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.