Skip to content

Instantly share code, notes, and snippets.

@luzfcb
Created October 12, 2022 18:06
Show Gist options
  • Save luzfcb/acfef7e90d909a393359a39556c1b8c1 to your computer and use it in GitHub Desktop.
Save luzfcb/acfef7e90d909a393359a39556c1b8c1 to your computer and use it in GitHub Desktop.
extract nvme ssd temperatures
#!/usr/bin/env python3
import sys
import getpass
import json
from io import StringIO
try:
import sh
except ImportError:
print("Please, install the sh library http://amoffat.github.io/sh/#installation")
sys.exit(1)
try:
from sh import nvme
except ImportError:
print("Please, install the nvme-cli package. \nsudo apt install nvme-cli")
sys.exit(1)
prompt = "[sudo] password for %s: " % getpass.getuser()
pw = getpass.getpass(prompt=prompt)
with sh.contrib.sudo(password=pw, _with=True):
buf = StringIO()
nvme("list", "-o", "json", _out=buf)
nvme_query = buf.getvalue()
nvme_devices = json.loads(nvme_query)
nvme_devices = nvme_devices.get('Devices', [])
# pprint(nvme_devices)
result = []
for device in nvme_devices:
device_path = device.get('DevicePath', None)
device_name = device.get('ModelNumber', None)
if device_path:
buf = StringIO()
nvme("smart-log", device_path, "-o", "json", _out=buf)
device_smart_log_query = buf.getvalue()
device_smart_log = json.loads(device_smart_log_query)
temperature = device_smart_log.get('temperature', None)
if temperature:
temperature = int(temperature) / 10
temperature = f'{temperature}ºC'
print(f'{device_name}\t{device_path}\t{temperature}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment