bumblebee-status/bumblebee_status/modules/contrib/cpu2.py

155 lines
5.6 KiB
Python
Raw Permalink Normal View History

2020-04-26 10:08:01 +02:00
"""Multiwidget CPU module
Can display any combination of:
* max CPU frequency
* total CPU load in percents (integer value)
* per-core CPU load as graph - either mono or colored
* CPU temperature (in Celsius degrees)
* CPU fan speed
Requirements:
* the psutil Python module for the first three items from the list above
* sensors executable for the rest
Parameters:
* cpu2.layout: Space-separated list of widgets to add.
Possible widgets are:
2020-05-06 12:57:38 +02:00
* cpu2.maxfreq
* cpu2.cpuload
* cpu2.coresload
* cpu2.temp
* cpu2.fanspeed
2020-04-26 10:08:01 +02:00
* cpu2.colored: 1 for colored per core load graph, 0 for mono (default)
* cpu2.temp_pattern: pattern to look for in the output of 'sensors -u';
2022-07-21 08:07:33 +02:00
required if cpu2.temp widget is used
2020-04-26 10:08:01 +02:00
* cpu2.fan_pattern: pattern to look for in the output of 'sensors -u';
2022-07-21 08:07:33 +02:00
required if cpu2.fanspeed widget is used
2020-04-26 10:08:01 +02:00
2020-04-26 10:08:17 +02:00
Note: if you are getting 'n/a' for CPU temperature / fan speed, then you're
2020-04-26 10:08:01 +02:00
lacking the aforementioned pattern settings or they have wrong values.
contributed by `somospocos <https://github.com/somospocos>`_ - many thanks!
2020-04-26 10:08:01 +02:00
"""
2020-04-26 10:33:41 +02:00
import psutil
2020-04-26 10:08:01 +02:00
2020-04-26 10:33:41 +02:00
import core.module
2020-04-26 10:08:01 +02:00
2020-04-26 10:33:41 +02:00
import util.cli
import util.graph
import util.format
2020-04-26 10:08:01 +02:00
2020-04-26 10:33:41 +02:00
class Module(core.module.Module):
def __init__(self, config, theme):
super().__init__(config, theme, [])
2020-04-26 10:08:01 +02:00
self.__layout = self.parameter(
"layout", "cpu2.maxfreq cpu2.cpuload cpu2.coresload cpu2.temp cpu2.fanspeed"
)
2020-04-26 10:33:41 +02:00
self.__widget_names = self.__layout.split()
self.__colored = util.format.asbool(self.parameter("colored", False))
2020-04-26 10:33:41 +02:00
for widget_name in self.__widget_names:
if widget_name == "cpu2.maxfreq":
widget = self.add_widget(name=widget_name, full_text=self.maxfreq)
widget.set("type", "freq")
elif widget_name == "cpu2.cpuload":
widget = self.add_widget(name=widget_name, full_text=self.cpuload)
widget.set("type", "load")
elif widget_name == "cpu2.coresload":
widget = self.add_widget(name=widget_name, full_text=self.coresload)
widget.set("type", "loads")
elif widget_name == "cpu2.temp":
widget = self.add_widget(name=widget_name, full_text=self.temp)
widget.set("type", "temp")
elif widget_name == "cpu2.fanspeed":
widget = self.add_widget(name=widget_name, full_text=self.fanspeed)
widget.set("type", "fan")
2020-04-26 10:33:41 +02:00
if self.__colored:
widget.set("pango", True)
self.__temp_pattern = self.parameter("temp_pattern")
2020-04-26 10:33:41 +02:00
if self.__temp_pattern is None:
self.__temp = "n/a"
self.__fan_pattern = self.parameter("fan_pattern")
2020-04-26 10:33:41 +02:00
if self.__fan_pattern is None:
self.__fan = "n/a"
2020-04-26 10:08:01 +02:00
# maxfreq is loaded only once at startup
if "cpu2.maxfreq" in self.__widget_names:
2020-04-26 10:33:41 +02:00
self.__maxfreq = psutil.cpu_freq().max / 1000
2020-04-26 10:08:01 +02:00
def maxfreq(self, _):
return "{:.2f}GHz".format(self.__maxfreq)
2020-04-26 10:08:01 +02:00
def cpuload(self, _):
return "{:>3}%".format(self.__cpuload)
2020-04-26 10:08:01 +02:00
def add_color(self, bar):
"""add color as pango markup to a bar"""
if bar in ["", ""]:
color = self.theme.color("green", "green")
elif bar in ["", ""]:
color = self.theme.color("yellow", "yellow")
elif bar in ["", ""]:
color = self.theme.color("orange", "orange")
elif bar in ["", ""]:
color = self.theme.color("red", "red")
2020-04-26 10:33:41 +02:00
colored_bar = '<span foreground="{}">{}</span>'.format(color, bar)
2020-04-26 10:08:01 +02:00
return colored_bar
def coresload(self, _):
2020-04-26 10:33:41 +02:00
mono_bars = [util.graph.hbar(x) for x in self.__coresload]
if not self.__colored:
return "".join(mono_bars)
2020-04-26 10:08:01 +02:00
colored_bars = [self.add_color(x) for x in mono_bars]
return "".join(colored_bars)
2020-04-26 10:08:01 +02:00
def temp(self, _):
if self.__temp == "n/a" or self.__temp == 0:
return "n/a"
return "{}°C".format(self.__temp)
2020-04-26 10:08:01 +02:00
def fanspeed(self, _):
if self.__fanspeed == "n/a":
return "n/a"
return "{}RPM".format(self.__fanspeed)
2020-04-26 10:08:01 +02:00
def _parse_sensors_output(self):
output = util.cli.execute("sensors -u")
lines = output.split("\n")
temp = "n/a"
fan = "n/a"
2020-04-26 10:08:01 +02:00
temp_line = None
fan_line = None
for line in lines:
2020-04-26 10:33:41 +02:00
if self.__temp_pattern is not None and self.__temp_pattern in line:
2020-04-26 10:08:01 +02:00
temp_line = line
2020-04-26 10:33:41 +02:00
if self.__fan_pattern is not None and self.__fan_pattern in line:
2020-04-26 10:08:01 +02:00
fan_line = line
if temp_line is not None and fan_line is not None:
break
if temp_line is not None:
temp = round(float(temp_line.split(":")[1].strip()))
2020-04-26 10:08:01 +02:00
if fan_line is not None:
fan = int(fan_line.split(":")[1].strip()[:-4])
2020-04-26 10:08:01 +02:00
return temp, fan
2020-04-26 10:33:41 +02:00
def update(self):
if "cpu2.maxfreq" in self.__widget_names:
2020-04-26 10:33:41 +02:00
self.__maxfreq = psutil.cpu_freq().max / 1000
if "cpu2.cpuload" in self.__widget_names:
2020-04-26 10:33:41 +02:00
self.__cpuload = round(psutil.cpu_percent(percpu=False))
if "cpu2.coresload" in self.__widget_names:
2020-04-26 10:33:41 +02:00
self.__coresload = psutil.cpu_percent(percpu=True)
if "cpu2.temp" in self.__widget_names or "cpu2.fanspeed" in self.__widget_names:
2020-04-26 10:33:41 +02:00
self.__temp, self.__fanspeed = self._parse_sensors_output()
2020-04-26 10:08:01 +02:00
def state(self, widget):
"""for having per-widget icons"""
return [widget.get("type", "")]
2020-04-26 10:33:41 +02:00
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4