[modules/sensors] auto-determine the correct thermal zone
This commit is contained in:
parent
618a22c122
commit
0dc6a95ac2
2 changed files with 44 additions and 27 deletions
|
@ -18,6 +18,7 @@ contributed by `mijoharas <https://github.com/mijoharas>`_ - many thanks!
|
|||
"""
|
||||
|
||||
import re
|
||||
import os
|
||||
import json
|
||||
import logging
|
||||
|
||||
|
@ -49,19 +50,25 @@ class Module(core.module.Module):
|
|||
self.determine_method()
|
||||
|
||||
def determine_method(self):
|
||||
if self.parameter("use_sensors") == "True":
|
||||
self.use_sensors = True
|
||||
return
|
||||
if self.parameter("use_sensors") == "False":
|
||||
self.use_sensors = False
|
||||
return
|
||||
if self.parameter("path") != None and self._json == False:
|
||||
self.use_sensors = False # use thermal zone
|
||||
else:
|
||||
# try to use output of sensors -u
|
||||
try:
|
||||
output = util.cli.execute("sensors -u")
|
||||
self.use_sensors = True
|
||||
log.debug("Sensors command available")
|
||||
except FileNotFoundError as e:
|
||||
log.info(
|
||||
"Sensors command not available, using /sys/class/thermal/thermal_zone*/"
|
||||
)
|
||||
self.use_sensors = False
|
||||
self.use_sensors = False # use thermal zone
|
||||
return
|
||||
# try to use output of sensors -u
|
||||
try:
|
||||
output = util.cli.execute("sensors -u")
|
||||
self.use_sensors = True
|
||||
log.debug("Sensors command available")
|
||||
except FileNotFoundError as e:
|
||||
log.info(
|
||||
"Sensors command not available, using /sys/class/thermal/thermal_zone*/"
|
||||
)
|
||||
self.use_sensors = False
|
||||
|
||||
def _get_temp_from_sensors(self):
|
||||
if self._json == True:
|
||||
|
@ -92,22 +99,30 @@ class Module(core.module.Module):
|
|||
|
||||
def get_temp(self):
|
||||
if self.use_sensors:
|
||||
temperature = self._get_temp_from_sensors()
|
||||
log.debug("Retrieve temperature from sensors -u")
|
||||
else:
|
||||
try:
|
||||
temperature = open(
|
||||
self.parameter("path", "/sys/class/thermal/thermal_zone0/temp")
|
||||
).read().strip()
|
||||
log.debug("retrieved temperature from /sys/class/")
|
||||
# TODO: Iterate through all thermal zones to determine the correct one and use its value
|
||||
# https://unix.stackexchange.com/questions/304845/discrepancy-between-number-of-cores-and-thermal-zones-in-sys-class-thermal
|
||||
|
||||
except IOError:
|
||||
temperature = "unknown"
|
||||
log.info("Can not determine temperature, please install lm-sensors")
|
||||
|
||||
return temperature
|
||||
return self._get_temp_from_sensors()
|
||||
try:
|
||||
path = None
|
||||
# use path provided by the user
|
||||
if self.parameter("path") is not None:
|
||||
path = self.parameter("path")
|
||||
# find the thermal zone that provides cpu temperature
|
||||
for zone in os.listdir("/sys/class/thermal"):
|
||||
if not zone.startswith("thermal_zone"):
|
||||
continue
|
||||
if open(f"/sys/class/thermal/{zone}/type").read().strip() != "x86_pkg_temp":
|
||||
continue
|
||||
path = f"/sys/class/thermal/{zone}/temp"
|
||||
# use zone 0 as fallback
|
||||
if path is None:
|
||||
log.info("Can not determine temperature path, using thermal_zone0")
|
||||
path = "/sys/class/thermal/thermal_zone0/temp"
|
||||
log.debug(f"retrieving temperature from {path}")
|
||||
# the values are t°C * 1000, so divide by 1000
|
||||
return str(int(open(path).read()) / 1000)
|
||||
except IOError:
|
||||
log.info("Can not determine temperature, please install lm-sensors")
|
||||
return "unknown"
|
||||
|
||||
def get_mhz(self):
|
||||
mhz = None
|
||||
|
|
|
@ -1195,6 +1195,8 @@ sensors
|
|||
Displays sensor temperature
|
||||
|
||||
Parameters:
|
||||
* sensors.use_sensors (True/False): whether to use the 'sensors' command.
|
||||
If set to 'False', the sysfs-interface at '/sys/class/thermal' is used
|
||||
* sensors.path: path to temperature file (default /sys/class/thermal/thermal_zone0/temp).
|
||||
* sensors.json: if set to 'true', interpret sensors.path as JSON 'path' in the output
|
||||
of 'sensors -j' (i.e. <key1>/<key2>/.../<value>), for example, path could
|
||||
|
|
Loading…
Reference in a new issue