helper-scripts/drivetemp.py

68 lines
1.8 KiB
Python
Raw Normal View History

#!/usr/bin/python3
2018-10-30 21:52:43 -07:00
"""Check the temperature of a drive.
2018-08-27 13:30:54 -07:00
Functions:
verify_device_node(query)
- Check if query is a device node
retrieve_smart_temp(device_node)
- Retrieve specified drive temperature in mKelvin
2018-10-30 21:52:43 -07:00
convert_to_celsius(mkel_temp)
2018-08-27 13:30:54 -07:00
- Given mkel_temp, convert it into °C
"""
import argparse
import pathlib
2018-10-30 21:52:43 -07:00
import subprocess
2018-08-27 13:30:54 -07:00
2019-01-20 22:02:39 -08:00
# ========== Constants ==========
DUMP_CMD = ['skdump', '--temperature']
2018-08-27 13:30:54 -07:00
2019-01-20 22:02:39 -08:00
# ========== Functions ==========
2018-08-27 13:30:54 -07:00
def verify_device_node(query):
2018-10-30 21:52:43 -07:00
"""Check if query is a device node.
:param query: input that refers to a device
:type query: a path-like object
:returns: True if query is a device node, False if otherwise
:rtype: bool
2018-08-27 13:30:54 -07:00
"""
return pathlib.Path(query).is_block_device()
def retrieve_smart_temp(device_node):
2018-10-30 21:52:43 -07:00
"""Retrieve specified drive's temperature.
:param device_node: device to retrieve temperature for
:type device_node: str
:returns: output of skdump in mKelvin
2018-11-09 20:07:06 -08:00
:rtype: float
2018-08-27 13:30:54 -07:00
"""
2019-01-20 22:02:39 -08:00
temp = subprocess.run(DUMP_CMD + device_node,
capture_output=True,
text=True).stdout
return float(temp)
2018-10-30 21:52:43 -07:00
def convert_to_celsius(mkel_temp):
"""Given mkel_temp, convert it into °C.
:param mkel_temp: the temperature in mKelvin
:type mkel_temp: str
:returns: temperature converted into degrees celsius
:rtype: str
2018-08-27 13:30:54 -07:00
"""
2018-11-09 20:07:06 -08:00
return (mkel_temp/1000) - 273.15
2018-08-27 13:30:54 -07:00
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('device', help='device node to retrieve\
the temperature for', metavar='dev')
args = parser.parse_args()
dev = args.device
2018-09-15 12:35:49 -07:00
2018-08-27 13:30:54 -07:00
if verify_device_node(dev):
mkel = retrieve_smart_temp(dev)
2018-10-30 21:52:43 -07:00
print(f"{dev}: {convert_to_celsius(mkel)}°C")
2018-08-27 13:30:54 -07:00
else:
print("Not a device node.")
exit(1)