-
Notifications
You must be signed in to change notification settings - Fork 23
Add support for NVMe drives in smartmon.py #2035
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
Open
technowhizz
wants to merge
1
commit into
stackhpc/2025.1
Choose a base branch
from
updated_smartmon_nvme_support
base: stackhpc/2025.1
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 all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| #!/usr/bin/env python3 | ||
| import json | ||
| import re | ||
| import subprocess | ||
| from pySMART import DeviceList | ||
|
|
||
| SMARTMON_ATTRS = { | ||
|
|
@@ -63,6 +64,8 @@ | |
| "critical_comp_time", | ||
| } | ||
|
|
||
| SMARTCTL_PATH = "/usr/sbin/smartctl" | ||
|
|
||
| DISK_INFO = { | ||
| "name", | ||
| "interface", | ||
|
|
@@ -84,6 +87,17 @@ def camel_to_snake(name): | |
| """ | ||
| return re.sub(r'(?<!^)(?=[A-Z])', '_', name).lower() | ||
|
|
||
| def canonical_device_path(name): | ||
| """ | ||
| Ensure device name is returned as absolute /dev path for smartctl. | ||
|
|
||
| pySMART sometimes reports bare device names (e.g. 'nvme0'); smartctl on the | ||
| CLI expects the canonical /dev path, so normalise here to avoid surprises. | ||
| """ | ||
| if not name: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like line 97/98 can be removed? |
||
| return name | ||
| return name if name.startswith("/dev/") else f"/dev/{name}" | ||
|
|
||
| def attrs_to_dict(obj, allowed_keys): | ||
| """ | ||
| Build {attr: value} for every public, non-callable attribute whose | ||
|
|
@@ -105,14 +119,52 @@ def attrs_to_dict(obj, allowed_keys): | |
| attributes[name] = value | ||
| return attributes | ||
|
|
||
| def smartctl_json(device_name, device_type): | ||
| """ | ||
| Execute smartctl -x -j for the given device and return the parsed JSON payload. | ||
|
|
||
| The goal is to mirror the exact data smartmon.py consumes at runtime so our | ||
| fixtures stay faithful to real hardware output. | ||
| """ | ||
| if not device_name: | ||
| return {} | ||
|
|
||
| target = canonical_device_path(device_name) | ||
|
|
||
| cmd = [SMARTCTL_PATH, "-x", "-j", target] | ||
| if device_type and device_type.lower() not in (None, "", "nvme"): | ||
| cmd.insert(3, device_type) | ||
| cmd.insert(3, "-d") | ||
|
|
||
| try: | ||
| result = subprocess.run( | ||
| cmd, | ||
| stdout=subprocess.PIPE, | ||
| stderr=subprocess.PIPE, | ||
| check=False, | ||
| text=True, | ||
| ) | ||
| except OSError: | ||
| return {} | ||
|
|
||
| if not result.stdout: | ||
| return {} | ||
|
|
||
| try: | ||
| return json.loads(result.stdout) | ||
| except json.JSONDecodeError: | ||
| return {} | ||
|
|
||
| for disk in DeviceList().devices: | ||
|
|
||
| fixtures = {} | ||
| disk_info = attrs_to_dict(disk, DISK_INFO) | ||
| if_stats = attrs_to_dict(disk.if_attributes, SMARTMON_ATTRS) | ||
| smartctl_payload = smartctl_json(disk.name, disk.interface) | ||
|
|
||
| fixtures["device_info"] = disk_info | ||
| fixtures["if_attributes"] = if_stats | ||
| fixtures["smartctl"] = smartctl_payload | ||
|
|
||
| print(f'Disk: {disk.name}: \n') | ||
| print(json.dumps(fixtures, indent=2, default=str)) | ||
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.
I think you could use
-d nvmeto only get nvmes?: