Skip to content

Commit

Permalink
Use a dict to avoid usage of index
Browse files Browse the repository at this point in the history
Using index is error prone and less readable than using a dict a naming
things. This patch also unifies the usage of quotes.

Signed-off-by: Guillaume <[email protected]>
  • Loading branch information
gthvn1 committed Oct 25, 2024
1 parent 075077e commit edbb122
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions SOURCES/etc/xapi.d/plugins/smartctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,50 @@
import sys
import XenAPIPlugin

sys.path.append('.')
sys.path.append(".")
from xcpngutils import configure_logging, run_command, error_wrapped
from xcpngutils.operationlocker import OperationLocker


@error_wrapped
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])
result = run_command(["smartctl", "--scan"])
for line in result["stdout"].splitlines():
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] = 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]])
json_output = json.loads(cmd['stdout'])
if json_output['smart_status']['passed']:
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"
else:
results[disk] = "FAILED"
i = i + 2
return json.dumps(results)


_LOGGER = configure_logging('smartctl')
_LOGGER = configure_logging("smartctl")
if __name__ == "__main__":
XenAPIPlugin.dispatch({
'information': get_information,
'health': get_health
})
XenAPIPlugin.dispatch({"information": get_information, "health": get_health})

0 comments on commit edbb122

Please sign in to comment.