Skip to content
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

Update smartctl.py #46

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Update smartctl.py #46

wants to merge 3 commits into from

Conversation

1234Erwan
Copy link

Try to add hard raid support for smartctl.py

Try to add hard raid support for smartctl.py

Signed-off-by: 1234Erwan <84467245+1234Erwan@users.noreply.github.com>
Signed-off-by: 1234Erwan <84467245+1234Erwan@users.noreply.github.com>
@olivierlambert
Copy link
Member

Hey thanks a lot for your contribution! We'll review your PR ASAP 👍

Copy link
Contributor

@gthvn1 gthvn1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx a lot for your PR. I added some comments.

@@ -14,32 +14,47 @@ def _list_disks():
disks = []
result = run_command(['smartctl', '--scan'])
for line in result['stdout'].splitlines():
if line.startswith('/dev/') and not line.startswith('/dev/bus/'):
disks.append(line.split()[0])
disks.append(line.split()[0])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that you can get the type of the disk here. No need to an extra function _list_raids.
Maybe return a tuple here with line.split()[0] and line.split()[2]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'v tryed te solve that.

Copy link
Contributor

@gthvn1 gthvn1 Aug 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the most readable way to do that is to use a dict. For example you can do:
devices.append({'name': line.split()[0], 'type': line.split()[2]})
By doing this way we know what is expecting and when calling the smartctl later in the code you won't need the index you will be able to use the explicit field.

Copy link
Contributor

@gthvn1 gthvn1 Aug 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that I used devices here because it looks like it is closer from the man page of the smartctl and IMHO we are getting the device name and the device type so it looks better :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And also renamed _list_disks into _list_devices if you decide to rename it.

return disks

@error_wrapped
def _list_raids():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like you are getting the type. It is not specifically for raids. And I think that this function is not needed because we are already doing the scan in _list_disks(). See my previous comment

@error_wrapped
def get_information(session, args):
results = {}
raids = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed if disks holds a tuple with device and type

with OperationLocker():
disks = _list_disks()
raids = _list_raids()
for disk in disks:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here you can get disk, disk_type from disks if it holds the tuple

Removed unnecessary lines from smartctl.py

Signed-off-by: 1234Erwan <84467245+1234Erwan@users.noreply.github.com>
@gthvn1
Copy link
Contributor

gthvn1 commented Sep 3, 2024

I thought about a solution like this instead of managing an index.

--- a/SOURCES/etc/xapi.d/plugins/smartctl.py
+++ b/SOURCES/etc/xapi.d/plugins/smartctl.py
@@ -14,36 +14,31 @@ def _list_disks():
     disks = []
     result = run_command(['smartctl', '--scan'])
     for line in result['stdout'].splitlines():
-        disks.append(line.split()[2])
-        disks.append(line.split()[0])
+        disks.append({'name': line.split()[0], 'type': line.split()[2]})
     return disks

 @error_wrapped
 def get_information(session, args):
     results = {}
-    i = 0
     with OperationLocker():
         disks = _list_disks()
         for disk in disks:
-            cmd = run_command(["smartctl", "-j", "-a", "-d", disks[i], disks[i+1]], check=False)
-            results[disk] = json.loads(cmd['stdout'])
-            i = i + 2
+            cmd = run_command(["smartctl", "-j", "-a", "-d", disk['type'], disk['name']], check=False)
+            results[disk['name']] = json.loads(cmd['stdout'])
         return json.dumps(results)

 @error_wrapped
 def get_health(session, args):
     results = {}
-    i = 0
     with OperationLocker():
         disks = _list_disks()
         for disk in disks:
-            cmd = run_command(["smartctl", "-j", "-H", "-d", disks[i], disks[i+1]])
+            cmd = run_command(["smartctl", "-j", "-H", "-d", disk['type'], disk['name']])
             json_output = json.loads(cmd['stdout'])
             if json_output['smart_status']['passed']:
-                results[disk] = "PASSED"
+                results[disk['name']] = "PASSED"
             else:
-                results[disk] = "FAILED"
-            i = i + 2
+                results[disk['name']] = "FAILED"
         return json.dumps(results)

@stormi
Copy link
Member

stormi commented Oct 17, 2024

How do we move on on this PR?

@gthvn1
Copy link
Contributor

gthvn1 commented Oct 17, 2024

@

How do we move on on this PR?

I think that managing index is error prone and IMHO using the solution I posted looks better but I don't have a strong opinion so if it is ok for you we can merge it and fix it later. @1234Erwan what do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants