2020-03-15 13:44:38 +01:00
|
|
|
# -*- coding: UTF-8 -*-
|
|
|
|
|
2020-04-19 09:43:44 +02:00
|
|
|
"""Displays sensor temperature and CPU frequency
|
2020-03-15 13:44:38 +01:00
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
2020-03-15 13:44:54 +01:00
|
|
|
* sensors2.chip: 'sensors -u' compatible filter for chip to display (default to empty - show all chips)
|
2020-03-15 13:44:38 +01:00
|
|
|
* sensors2.showcpu: Enable or disable CPU frequency display (default: true)
|
|
|
|
* sensors2.showtemp: Enable or disable temperature display (default: true)
|
|
|
|
* sensors2.showfan: Enable or disable fan display (default: true)
|
2020-03-15 13:44:54 +01:00
|
|
|
* sensors2.showother: Enable or display 'other' sensor readings (default: false)
|
2020-03-15 13:44:38 +01:00
|
|
|
* sensors2.showname: Enable or disable show of sensor name (default: false)
|
2020-04-19 09:43:44 +02:00
|
|
|
* sensors2.chip_include: Comma-separated list of chip to include (defaults to '' will include all by default, example: 'coretemp,bat')
|
|
|
|
* sensors2.chip_exclude:Comma separated list of chip to exclude (defaults to '' will exlude none by default)
|
|
|
|
* sensors2.field_include: Comma separated list of chip to include (defaults to '' will include all by default, example: 'temp,fan')
|
|
|
|
* sensors2.field_exclude: Comma separated list of chip to exclude (defaults to '' will exclude none by default)
|
|
|
|
* sensors2.chip_field_exclude: Comma separated list of chip field to exclude (defaults to '' will exclude none by default, example: 'coretemp-isa-0000.temp1,coretemp-isa-0000.fan1')
|
|
|
|
* sensors2.chip_field_include: Comma-separated list of adaper field to include (defaults to '' will include all by default)
|
|
|
|
"""
|
2020-03-15 13:44:38 +01:00
|
|
|
|
|
|
|
import re
|
|
|
|
|
2020-03-15 13:53:12 +01:00
|
|
|
import core.module
|
|
|
|
import core.widget
|
2020-03-15 13:44:38 +01:00
|
|
|
|
2020-03-15 13:53:12 +01:00
|
|
|
import util.cli
|
|
|
|
import util.format
|
2020-03-15 13:44:38 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-03-15 13:53:12 +01:00
|
|
|
class Module(core.module.Module):
|
2020-04-26 16:39:24 +02:00
|
|
|
def __init__(self, config, theme):
|
|
|
|
super().__init__(config, theme, [])
|
2020-03-15 13:44:38 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
self.__chip = self.parameter("chip", "")
|
2020-03-15 13:53:12 +01:00
|
|
|
self.__data = {}
|
2020-04-19 09:43:44 +02:00
|
|
|
self.__update()
|
2020-03-15 13:53:12 +01:00
|
|
|
|
2020-05-01 10:03:50 +02:00
|
|
|
self.__create_widgets()
|
2020-04-19 09:43:44 +02:00
|
|
|
|
|
|
|
def update(self):
|
2020-03-15 13:53:12 +01:00
|
|
|
self.__update()
|
|
|
|
for widget in self.widgets():
|
|
|
|
self.__update_widget(widget)
|
2020-03-15 13:44:38 +01:00
|
|
|
|
|
|
|
def state(self, widget):
|
2020-05-03 11:15:52 +02:00
|
|
|
widget_type = widget.get("type", "")
|
2020-03-15 13:44:38 +01:00
|
|
|
try:
|
2020-05-03 11:15:52 +02:00
|
|
|
data = self.__data[widget.get("adapter")][widget.get("package")][
|
|
|
|
widget.get("field")
|
|
|
|
]
|
|
|
|
if "crit" in data and float(data["input"]) > float(data["crit"]):
|
|
|
|
return ["critical", widget_type]
|
|
|
|
if "max" in data and float(data["input"]) > float(data["max"]):
|
|
|
|
return ["warning", widget_type]
|
2020-03-15 13:44:38 +01:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
return [widget_type]
|
|
|
|
|
2020-03-15 13:53:12 +01:00
|
|
|
def __create_widgets(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
show_temp = util.format.asbool(self.parameter("showtemp", True))
|
|
|
|
show_fan = util.format.asbool(self.parameter("showfan", True))
|
|
|
|
show_other = util.format.asbool(self.parameter("showother", False))
|
|
|
|
include_chip = tuple(
|
|
|
|
filter(len, util.format.aslist(self.parameter("chip_include", "")))
|
|
|
|
)
|
|
|
|
exclude_chip = tuple(
|
|
|
|
filter(len, util.format.aslist(self.parameter("chip_exclude", "")))
|
|
|
|
)
|
|
|
|
include_field = tuple(
|
|
|
|
filter(len, util.format.aslist(self.parameter("field_include", "")))
|
|
|
|
)
|
|
|
|
exclude_field = tuple(
|
|
|
|
filter(len, util.format.aslist(self.parameter("field_exclude", "")))
|
|
|
|
)
|
|
|
|
include_chip_field = tuple(
|
|
|
|
filter(len, util.format.aslist(self.parameter("chip_field_include", "")))
|
|
|
|
)
|
|
|
|
exclude_chip_field = tuple(
|
|
|
|
filter(len, util.format.aslist(self.parameter("chip_field_exclude", "")))
|
|
|
|
)
|
|
|
|
|
|
|
|
if util.format.asbool(self.parameter("showcpu", True)):
|
2020-05-01 10:03:50 +02:00
|
|
|
widget = self.add_widget(full_text=self.__cpu)
|
2020-05-03 11:15:52 +02:00
|
|
|
widget.set("type", "cpu")
|
2020-03-15 13:44:38 +01:00
|
|
|
|
2020-03-15 13:53:12 +01:00
|
|
|
for adapter in self.__data:
|
2020-04-19 09:43:44 +02:00
|
|
|
if include_chip or exclude_chip:
|
|
|
|
if include_chip:
|
|
|
|
if all([chip not in adapter for chip in include_chip]):
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
if any([chip in adapter for chip in exclude_chip]):
|
|
|
|
continue
|
|
|
|
|
|
|
|
if include_chip_field:
|
|
|
|
try:
|
2020-05-03 11:15:52 +02:00
|
|
|
if all(
|
|
|
|
[i.split(".")[0] not in adapter for i in include_chip_field]
|
|
|
|
):
|
2020-04-19 09:43:44 +02:00
|
|
|
continue
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2020-03-15 13:53:12 +01:00
|
|
|
for package in self.__data[adapter]:
|
2020-05-03 11:15:52 +02:00
|
|
|
if util.format.asbool(self.parameter("showname", False)):
|
2020-05-01 10:03:50 +02:00
|
|
|
widget = self.add_widget(full_text=package)
|
2020-05-03 11:15:52 +02:00
|
|
|
widget.set("data", self.__data[adapter][package])
|
|
|
|
widget.set("package", package)
|
|
|
|
widget.set("field", "")
|
|
|
|
widget.set("adapter", adapter)
|
2020-03-15 13:53:12 +01:00
|
|
|
for field in self.__data[adapter][package]:
|
2020-04-19 09:43:44 +02:00
|
|
|
|
|
|
|
if include_field or exclude_field:
|
|
|
|
if include_field:
|
2020-05-03 11:15:52 +02:00
|
|
|
if all(
|
|
|
|
[included not in field for included in include_field]
|
|
|
|
):
|
2020-04-19 09:43:44 +02:00
|
|
|
continue
|
|
|
|
else:
|
|
|
|
if any([excluded in field for excluded in exclude_field]):
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
if include_chip_field or exclude_chip_field:
|
|
|
|
if include_chip_field:
|
2020-05-03 11:15:52 +02:00
|
|
|
if all(
|
|
|
|
[
|
|
|
|
i.split(".")[1] not in field
|
|
|
|
for i in include_chip_field
|
|
|
|
if i.split(".")[0] in adapter
|
|
|
|
]
|
|
|
|
):
|
2020-04-19 09:43:44 +02:00
|
|
|
continue
|
|
|
|
else:
|
2020-05-03 11:15:52 +02:00
|
|
|
if any(
|
|
|
|
[
|
|
|
|
i.split(".")[1] in field
|
|
|
|
for i in exclude_chip_field
|
|
|
|
if i.split(".")[0] in adapter
|
|
|
|
]
|
|
|
|
):
|
2020-04-19 09:43:44 +02:00
|
|
|
continue
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2020-05-01 10:03:50 +02:00
|
|
|
widget = None
|
2020-05-03 11:15:52 +02:00
|
|
|
if "temp" in field and show_temp:
|
2020-03-15 13:44:38 +01:00
|
|
|
# seems to be a temperature
|
2020-05-01 10:03:50 +02:00
|
|
|
widget = self.add_widget()
|
2020-05-03 11:15:52 +02:00
|
|
|
widget.set("type", "temp")
|
|
|
|
if "fan" in field and show_fan:
|
2020-03-15 13:44:38 +01:00
|
|
|
# seems to be a fan
|
2020-05-01 10:03:50 +02:00
|
|
|
widget = self.add_widget()
|
2020-05-03 11:15:52 +02:00
|
|
|
widget.set("type", "fan")
|
2020-03-15 13:44:38 +01:00
|
|
|
elif show_other:
|
|
|
|
# everything else
|
2020-05-01 10:03:50 +02:00
|
|
|
widget = self.add_widget()
|
2020-05-03 11:15:52 +02:00
|
|
|
widget.set("type", "other")
|
2020-05-01 10:03:50 +02:00
|
|
|
if widget:
|
2020-05-03 11:15:52 +02:00
|
|
|
widget.set("package", package)
|
|
|
|
widget.set("field", field)
|
|
|
|
widget.set("adapter", adapter)
|
2020-03-15 13:44:38 +01:00
|
|
|
|
2020-03-15 13:53:12 +01:00
|
|
|
def __update_widget(self, widget):
|
2020-05-03 11:15:52 +02:00
|
|
|
if widget.get("field", "") == "":
|
|
|
|
return # nothing to do
|
|
|
|
data = self.__data[widget.get("adapter")][widget.get("package")][
|
|
|
|
widget.get("field")
|
|
|
|
]
|
|
|
|
if "temp" in widget.get("field"):
|
|
|
|
widget.full_text("{:0.01f}°C".format(data["input"]))
|
|
|
|
elif "fan" in widget.get("field"):
|
|
|
|
widget.full_text("{:0.0f}RPM".format(data["input"]))
|
2020-03-15 13:44:38 +01:00
|
|
|
else:
|
2020-05-03 11:15:52 +02:00
|
|
|
widget.full_text("{:0.0f}".format(data["input"]))
|
2020-03-15 13:44:38 +01:00
|
|
|
|
2020-03-15 13:53:12 +01:00
|
|
|
def __update(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
output = util.cli.execute(
|
|
|
|
"sensors -u {}".format(self.__chip), ignore_errors=True
|
|
|
|
)
|
2020-03-15 13:53:12 +01:00
|
|
|
self.__data = self.__parse(output)
|
2020-03-15 13:44:38 +01:00
|
|
|
|
2020-03-15 13:53:12 +01:00
|
|
|
def __parse(self, data):
|
2020-03-15 13:44:38 +01:00
|
|
|
output = {}
|
2020-05-03 11:15:52 +02:00
|
|
|
package = ""
|
2020-03-15 13:44:38 +01:00
|
|
|
adapter = None
|
|
|
|
chip = None
|
2020-05-03 11:15:52 +02:00
|
|
|
for line in data.split("\n"):
|
|
|
|
if "Adapter" in line:
|
2020-03-15 13:44:38 +01:00
|
|
|
# new adapter
|
2020-05-03 11:15:52 +02:00
|
|
|
line = line.replace("Adapter: ", "")
|
|
|
|
output[chip + " " + line] = {}
|
|
|
|
adapter = chip + " " + line
|
|
|
|
chip = line # default - line before adapter is always the chip
|
|
|
|
if not adapter:
|
|
|
|
continue
|
|
|
|
key, value = (line.split(":") + ["", ""])[:2]
|
|
|
|
if not line.startswith(" "):
|
2020-03-15 13:44:38 +01:00
|
|
|
# assume this starts a new package
|
|
|
|
if package in output[adapter] and output[adapter][package] == {}:
|
|
|
|
del output[adapter][package]
|
|
|
|
output[adapter][key] = {}
|
|
|
|
package = key
|
|
|
|
else:
|
|
|
|
# feature for this chip
|
|
|
|
try:
|
2020-05-03 11:15:52 +02:00
|
|
|
name, variant = (key.strip().split("_", 1) + ["", ""])[:2]
|
2020-03-15 13:44:38 +01:00
|
|
|
if not name in output[adapter][package]:
|
2020-05-03 11:15:52 +02:00
|
|
|
output[adapter][package][name] = {}
|
2020-03-15 13:44:38 +01:00
|
|
|
if variant:
|
|
|
|
output[adapter][package][name][variant] = {}
|
|
|
|
output[adapter][package][name][variant] = float(value)
|
|
|
|
except Exception as e:
|
|
|
|
pass
|
|
|
|
return output
|
|
|
|
|
2020-03-15 13:53:12 +01:00
|
|
|
def __cpu(self, _):
|
2020-03-15 13:44:38 +01:00
|
|
|
mhz = None
|
|
|
|
try:
|
2020-05-03 11:15:52 +02:00
|
|
|
output = open(
|
|
|
|
"/sys/devices/system/cpu/cpufreq/policy0/scaling_cur_freq"
|
|
|
|
).read()
|
|
|
|
mhz = int(float(output) / 1000.0)
|
2020-03-15 13:44:38 +01:00
|
|
|
except:
|
2020-05-03 11:15:52 +02:00
|
|
|
output = open("/proc/cpuinfo").read()
|
|
|
|
m = re.search(r"cpu MHz\s+:\s+(\d+)", output)
|
2020-03-15 13:44:38 +01:00
|
|
|
if m:
|
|
|
|
mhz = int(m.group(1))
|
|
|
|
else:
|
2020-05-03 11:15:52 +02:00
|
|
|
m = re.search(r"BogoMIPS\s+:\s+(\d+)", output)
|
2020-03-15 13:44:38 +01:00
|
|
|
if m:
|
2020-05-03 11:15:52 +02:00
|
|
|
return "{} BogoMIPS".format(int(m.group(1)))
|
2020-03-15 13:44:38 +01:00
|
|
|
if not mhz:
|
2020-05-03 11:15:52 +02:00
|
|
|
return "n/a"
|
2020-03-15 13:44:38 +01:00
|
|
|
|
|
|
|
if mhz < 1000:
|
2020-05-03 11:15:52 +02:00
|
|
|
return "{} MHz".format(mhz)
|
2020-03-15 13:44:38 +01:00
|
|
|
else:
|
2020-05-03 11:15:52 +02:00
|
|
|
return "{:0.01f} GHz".format(float(mhz) / 1000.0)
|
|
|
|
|
2020-03-15 13:44:38 +01:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|