-
Notifications
You must be signed in to change notification settings - Fork 3
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
SSDUTIL DIAG FUNCTION #3
Open
carycelestica
wants to merge
5
commits into
SW-CSV:master
Choose a base branch
from
carycelestica:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
Empty file.
This file contains 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,249 @@ | ||
#!/usr/bin/env python | ||
# | ||
# main.py | ||
# | ||
# Command-line utility for interacting with ssd in SONiC | ||
# | ||
|
||
|
||
|
||
try: | ||
from ssdutil import SsdUtil | ||
import imp | ||
import syslog | ||
import click | ||
import os | ||
import sys | ||
except ImportError as e: | ||
raise ImportError("%s - required module not found" % str(e)) | ||
|
||
VERSION = '1.0' | ||
|
||
VENDOR_PATH = "ssdvendors" | ||
|
||
|
||
SYSLOG_IDENTIFIER = "ssdutil" | ||
|
||
# Global ssd vendor class instance | ||
global ssd_vendor | ||
|
||
|
||
# ========================== Syslog wrappers ========================== | ||
|
||
|
||
def log_info(msg, also_print_to_console=False): | ||
syslog.openlog(SYSLOG_IDENTIFIER) | ||
syslog.syslog(syslog.LOG_INFO, msg) | ||
syslog.closelog() | ||
|
||
if also_print_to_console: | ||
click.echo(msg) | ||
|
||
|
||
def log_warning(msg, also_print_to_console=False): | ||
syslog.openlog(SYSLOG_IDENTIFIER) | ||
syslog.syslog(syslog.LOG_WARNING, msg) | ||
syslog.closelog() | ||
|
||
if also_print_to_console: | ||
click.echo(msg) | ||
|
||
|
||
def log_error(msg, also_print_to_console=False): | ||
syslog.openlog(SYSLOG_IDENTIFIER) | ||
syslog.syslog(syslog.LOG_ERR, msg) | ||
syslog.closelog() | ||
|
||
if also_print_to_console: | ||
click.echo(msg) | ||
|
||
# load ssd vendor module | ||
def load_ssd_vendor(device): | ||
global ssd_vendor | ||
dvModel = SsdUtil().get_device_model(device) | ||
if "InnoDisk" in dvModel: | ||
try: | ||
module_path = os.path.dirname(os.path.abspath(__file__)) | ||
module_file = "/".join([module_path, VENDOR_PATH, "innodisk.py"]) | ||
module = imp.load_source("innodisk", module_file) | ||
except IOError, e: | ||
log_error("Failed to load VENDOR module '%s': %s" % ("innodisk" , str(e)), True) | ||
return -1 | ||
try: | ||
ssd_vendor_class = getattr(module, "InnoDisk") | ||
ssd_vendor = ssd_vendor_class() | ||
except AttributeError, e: | ||
log_error("Failed to instantiate '%s' class: %s" % ("InnoDisk", str(e)), True) | ||
return -2 | ||
elif "SanDisk" in dvModel: | ||
try: | ||
module_path = os.path.dirname(os.path.abspath(__file__)) | ||
module_file = "/".join([module_path, VENDOR_PATH, "sandisk.py"]) | ||
module = imp.load_source("sandisk", module_file) | ||
except IOError, e: | ||
log_error("Failed to load VENDOR module '%s': %s" % ("sandisk" , str(e)), True) | ||
return -1 | ||
try: | ||
ssd_vendor_class = getattr(module, "SanDisk") | ||
ssd_vendor = ssd_vendor_class() | ||
except AttributeError, e: | ||
log_error("Failed to instantiate '%s' class: %s" % ("SanDisk", str(e)), True) | ||
return -2 | ||
|
||
pass | ||
# extend the ssd vendor module here | ||
|
||
return 0 | ||
|
||
|
||
# show ssd info | ||
|
||
def print_test_title(testname): | ||
click.echo("{0:-^80s}".format("-")) | ||
click.echo("{name: ^80s}".format(name=testname)) | ||
click.echo("{0:-^80s}".format("-")) | ||
|
||
|
||
|
||
|
||
|
||
# ==================== CLI commands and groups ==================== | ||
|
||
|
||
# This is our main entrypoint - the main 'ssdutil' command | ||
@click.group() | ||
def cli(): | ||
"""ssdutil - Command line utility for providing SSD info""" | ||
if os.geteuid() != 0: | ||
click.echo("Root privileges are required for this operation") | ||
sys.exit(1) | ||
|
||
|
||
# 'version' subcommand | ||
@cli.command() | ||
def version(): | ||
"""Display version info""" | ||
click.echo("ssdutil version {0}".format(VERSION)) | ||
|
||
|
||
# Vendor ssd info | ||
|
||
@cli.command() | ||
@click.argument("device") | ||
def peCycle(device): | ||
"""Show SSD P/E cycle""" | ||
load_ssd_vendor(device) | ||
pecycle = ssd_vendor.get_pecycle(device) | ||
testname = "Show SSD P/E Cycle" | ||
print_test_title(testname) | ||
click.echo("Device P/E Cycle : {0}".format(pecycle)) | ||
|
||
|
||
@cli.command() | ||
@click.argument("device") | ||
def health(device): | ||
"""Show SSD Health""" | ||
load_ssd_vendor(device) | ||
health = ssd_vendor.get_health(device) | ||
testname = "Show SSD Health" | ||
print_test_title(testname) | ||
click.echo("Device Health : {:.1%}".format(health)) | ||
|
||
|
||
|
||
@cli.command() | ||
@click.argument("device") | ||
def remaintime(device): | ||
"""Show SSD Remaining Time""" | ||
load_ssd_vendor(device) | ||
rmtime = ssd_vendor.get_remain_time(device) | ||
testname = "Show SSD Remaining Time" | ||
print_test_title(testname) | ||
click.echo("Device Remaining Time : {} Hours".format(rmtime)) | ||
|
||
|
||
|
||
|
||
#['0', '0', '0', '0'] | ||
# for 'Later_Bad_Block' and 'Later_Bad_Blk_Inf_R/W/E' | ||
@cli.command() | ||
@click.argument("device") | ||
def badblock(device): | ||
"""Show SSD Bad Block""" | ||
load_ssd_vendor(device) | ||
infoList = ssd_vendor.get_bad_block(device) | ||
testname = "Show SSD Bad Block" | ||
print_test_title(testname) | ||
Later_Bad_Block = infoList[0] | ||
Later_Bad_Block_Read = infoList[1] | ||
Later_Bad_Block_Write = infoList[2] | ||
Later_Bad_Block_Erase = infoList[3] | ||
click.echo("Device Later_Bad_Block : {}".format(Later_Bad_Block)) | ||
click.echo("Device Later_Bad_Block Read : {}".format(Later_Bad_Block_Read)) | ||
click.echo("Device Later_Bad_Block Write : {}".format(Later_Bad_Block_Write)) | ||
click.echo("Device Later_Bad_Block Erase : {}".format(Later_Bad_Block_Erase)) | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
@cli.command() | ||
@click.argument("device") | ||
def temp(device): | ||
"""Show SSD Temperature""" | ||
load_ssd_vendor(device) | ||
temp = ssd_vendor.get_temp(device) | ||
testname = "Show SSD Temperature" | ||
print_test_title(testname) | ||
click.echo("Device Temperature : {} C".format(temp)) | ||
|
||
|
||
#common ssd info | ||
|
||
@cli.command() | ||
@click.argument("device") | ||
def device(device): | ||
"""Show SSD Device Model""" | ||
dvModel = SsdUtil().get_device_model(device) | ||
testname = "Show SSD Device Model" | ||
print_test_title(testname) | ||
click.echo("Device Model : {}".format(dvModel)) | ||
|
||
|
||
|
||
|
||
@cli.command() | ||
@click.argument("device") | ||
def serialnum(device): | ||
"""Show SSD Serial Number""" | ||
serialNum = SsdUtil().get_serial_num(device) | ||
testname = "Show SSD Serial Number" | ||
print_test_title(testname) | ||
click.echo("Device Serial Number : {}".format(serialNum)) | ||
|
||
|
||
@cli.command() | ||
@click.argument("device") | ||
def firmware(device): | ||
"""Show SSD Firmware Version""" | ||
fwVersion = SsdUtil().get_firmware(device) | ||
testname = "Show SSD Firmware Version" | ||
print_test_title(testname) | ||
click.echo("Device Firmware Version : {}".format(fwVersion)) | ||
|
||
|
||
@cli.command() | ||
@click.argument("device") | ||
def capacity(device): | ||
"""Show SSD Capacity""" | ||
capacity = SsdUtil().get_capacity(device) | ||
testname = "Show SSD Capacity" | ||
print_test_title(testname) | ||
click.echo("Device Capacity : {}".format(capacity)) | ||
|
||
|
||
if __name__ == '__main__': | ||
cli() |
This file contains 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,143 @@ | ||
#!/usr/bin/env python | ||
|
||
|
||
|
||
|
||
# ssdutil.py | ||
# platform-common SSD interface for SONIC | ||
|
||
|
||
|
||
|
||
|
||
try: | ||
import os | ||
import yaml | ||
import subprocess | ||
except ImportError as e: | ||
raise ImportError("%s - required module not found" % str(e)) | ||
|
||
|
||
|
||
class SsdUtil(): | ||
|
||
def __init__(self): | ||
pass | ||
|
||
# get ssd firmware version info | ||
def get_firmware(self, device): | ||
check = 0 | ||
command = "sudo smartctl -i " + device | ||
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) | ||
output = proc.stdout.readlines() | ||
(out, err) = proc.communicate() | ||
if proc.returncode > 0: | ||
for line in output: | ||
print(line.strip()) | ||
return | ||
else: | ||
for line in output: | ||
if "Firmware Version" in line: | ||
# Firmware Version: S17411 | ||
check = 1 | ||
fwVersion = line.split(':')[-1].strip() | ||
if check == 0: | ||
print("Can't get 'Firmware Version' attributes") | ||
return | ||
else: | ||
return fwVersion | ||
# S17411 | ||
|
||
|
||
# get ssd serial number info | ||
def get_serial_num(self, device): | ||
check = 0 | ||
command = "sudo smartctl -i " + device | ||
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) | ||
output = proc.stdout.readlines() | ||
(out, err) = proc.communicate() | ||
if proc.returncode > 0: | ||
for line in output: | ||
print(line.strip()) | ||
return | ||
else: | ||
for line in output: | ||
if "Serial Number" in line: | ||
#Serial Number: BCA11709250230058 | ||
check = 1 | ||
serialNum = line.split(':')[-1].strip() | ||
if check == 0: | ||
print("Can't get 'Serial Number' attributes") | ||
return | ||
else: | ||
return serialNum | ||
# 'BCA11709250230058' | ||
|
||
|
||
# get ssd device model info | ||
def get_device_model(self, device): | ||
check = 0 | ||
command = "sudo smartctl -i " + device | ||
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) | ||
output = proc.stdout.readlines() | ||
(out, err) = proc.communicate() | ||
if proc.returncode > 0: | ||
for line in output: | ||
print(line.strip()) | ||
return | ||
else: | ||
for line in output: | ||
if "Device Model" in line: | ||
#Device Model: InnoDisk Corp. - mSATA 3ME3 | ||
check = 1 | ||
dvModel = line.split(':')[-1].strip() | ||
if check == 0: | ||
print("Can't get 'Device Model' attributes") | ||
return | ||
else: | ||
return dvModel | ||
# 'InnoDisk Corp. - mSATA 3ME3' | ||
|
||
|
||
|
||
def get_capacity(self, device): | ||
check = 0 | ||
command = "sudo smartctl -i " + device | ||
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) | ||
output = proc.stdout.readlines() | ||
(out, err) = proc.communicate() | ||
if proc.returncode > 0: | ||
for line in output: | ||
print(line.strip()) | ||
return | ||
else: | ||
for line in output: | ||
if "User Capacity" in line: | ||
# User Capacity: 16,013,942,784 bytes [16.0 GB] | ||
check = 1 | ||
capacity = line.split(':')[-1].strip() | ||
if check == 0: | ||
print("Can't get 'User Capacity' attributes") | ||
return | ||
else: | ||
return capacity | ||
# '16,013,942,784 bytes [16.0 GB]' | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
Empty file.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Cary,here is for others to extend.Currently SanDisk is just for our test right now.Not the official release code.Please delete this part and detail the comment that other vendors can extend here.