Skip to content

Commit

Permalink
NAS-132548 / 25.04 / Add NVME plugin (#248)
Browse files Browse the repository at this point in the history
* smartctl -a is legacy, use -x

* remove unnecessary comment

* add NVME plugin

* prettify the header
  • Loading branch information
yocalebo authored Nov 17, 2024
1 parent c2b318f commit 80422c7
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
2 changes: 2 additions & 0 deletions ixdiagnose/plugins/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from .ldap import LDAP
from .network import Network
from .nfs import NFS
from .nvme import NVME
from .rbac import RBAC
from .replication import Replication
from .reporting import Reporting
Expand Down Expand Up @@ -58,6 +59,7 @@
LDAP,
Network,
NFS,
NVME,
RBAC,
Replication,
Reporting,
Expand Down
87 changes: 87 additions & 0 deletions ixdiagnose/plugins/nvme.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import os
import re
from typing import Any

from .base import Plugin
from .metrics import CommandMetric, PythonMetric
from ixdiagnose.utils.command import Command
from ixdiagnose.utils.middleware import MiddlewareClient
from ixdiagnose.utils.run import run

NVME_RE = re.compile(r"^nvme\dn\d+$")


def get_nvme_devs() -> list[str]:
nvmes = list()
with os.scandir("/dev/") as sdir:
for i in filter(lambda x: NVME_RE.match(x.name), sdir):
nvmes.append(i.path)
return nvmes


def run_nvme_cmd(action: str, nvme: str, add_header: bool = True) -> str:
header = '' if not add_header else f'{nvme:#^30}\n'
cp = run(["nvme", action, nvme], check=False)
if cp.returncode:
footer = f"FAILED: {cp.stderr}\n\n"
else:
footer = cp.stdout + "\n\n"

return header + footer


def get_nvme_id_ctrl(client: MiddlewareClient, context: Any) -> str:
output = ""
for nvme in get_nvme_devs():
output += run_nvme_cmd("id-ctrl", nvme)
return output


def get_nvme_id_ns(client: MiddlewareClient, context: Any) -> str:
output = ""
for nvme in get_nvme_devs():
output += run_nvme_cmd("id-ns", nvme)
return output


def get_nvme_smart_log(client: MiddlewareClient, context: Any) -> str:
output = ""
for nvme in get_nvme_devs():
output += run_nvme_cmd("smart-log", nvme, add_header=False)
return output


class NVME(Plugin):
name = "nvme"
metrics = [
CommandMetric(
"nvme_list_v",
[
Command(["nvme", "list", "-v"], "nvme list -v", serializable=False),
],
),
CommandMetric(
"nvme_discover",
[
Command(["nvme", "discover"], "nvme discover", serializable=False),
],
),
PythonMetric(
"nvme_id_ctrl",
callback=get_nvme_id_ctrl,
description="Identify Controller",
serializable=False,
),
PythonMetric(
"nvme_id_ns",
callback=get_nvme_id_ns,
description="Identify Namespace",
serializable=False,
),
PythonMetric(
"nvme_smart_log",
callback=get_nvme_smart_log,
description="SMART Log",
serializable=False,
),
]
3 changes: 1 addition & 2 deletions ixdiagnose/plugins/smart.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def smart_output(client: MiddlewareClient, context: Any) -> str:
except Exception:
continue

cmd = ['smartctl', '-a', f'/dev/{disk}']
cmd = ['smartctl', '-x', f'/dev/{disk}']
nvme_msg = ''
if any(('nvme' in disk, vendor.lower().strip() == 'nvme')):
# is an nvme device
Expand All @@ -54,7 +54,6 @@ def smart_output(client: MiddlewareClient, context: Any) -> str:
output += f' {msg}\n'
output += f'{"=" * (len(msg) + 5)}\n\n{cp.stderr if cp.returncode else cp.stdout}\n\n'

# TODO: Check the awk script and see what it normalizes
return output


Expand Down

0 comments on commit 80422c7

Please sign in to comment.