2020-03-06 14:31:29 +01:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
"""Displays CPU utilization across all CPUs.
|
2020-03-06 14:31:29 +01:00
|
|
|
|
2020-07-18 08:16:57 +02:00
|
|
|
By default, opens `gnome-system-monitor` on left mouse click.
|
|
|
|
|
2020-05-21 13:10:53 +02:00
|
|
|
Requirements:
|
|
|
|
* the psutil Python module for the first three items from the list above
|
2020-07-18 08:16:57 +02:00
|
|
|
* gnome-system-monitor for default mouse click action
|
2020-05-21 13:10:53 +02:00
|
|
|
|
2020-03-06 14:31:29 +01:00
|
|
|
Parameters:
|
|
|
|
* cpu.warning : Warning threshold in % of CPU usage (defaults to 70%)
|
|
|
|
* cpu.critical: Critical threshold in % of CPU usage (defaults to 80%)
|
|
|
|
* cpu.format : Format string (defaults to '{:.01f}%')
|
2021-04-27 17:16:54 +02:00
|
|
|
* cpu.percpu : If set to true, show each individual cpu (defaults to false)
|
2020-05-03 11:15:52 +02:00
|
|
|
"""
|
2020-03-06 14:31:29 +01:00
|
|
|
|
|
|
|
import psutil
|
|
|
|
|
|
|
|
import core.module
|
|
|
|
import core.widget
|
|
|
|
import core.input
|
|
|
|
|
2021-04-27 17:16:54 +02:00
|
|
|
import util.format
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-03-06 14:31:29 +01:00
|
|
|
class Module(core.module.Module):
|
2020-04-26 16:39:24 +02:00
|
|
|
def __init__(self, config, theme):
|
2021-04-27 17:17:13 +02:00
|
|
|
super().__init__(config, theme, [])
|
2021-04-27 17:16:54 +02:00
|
|
|
self._percpu = util.format.asbool(self.parameter("percpu", False))
|
2021-04-27 17:17:13 +02:00
|
|
|
|
|
|
|
for idx, cpu_perc in enumerate(self.cpu_utilization()):
|
|
|
|
widget = self.add_widget(name="cpu#{}".format(idx), full_text=self.utilization)
|
|
|
|
widget.set("utilization", cpu_perc)
|
|
|
|
widget.set("theme.minwidth", self._format.format(100.0 - 10e-20))
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
core.input.register(
|
|
|
|
self, button=core.input.LEFT_MOUSE, cmd="gnome-system-monitor"
|
|
|
|
)
|
2020-03-06 14:31:29 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def _format(self):
|
2021-04-27 17:17:13 +02:00
|
|
|
return self.parameter("format", "{:.01f}%")
|
|
|
|
|
|
|
|
def utilization(self, widget):
|
|
|
|
return self._format.format(widget.get("utilization", 0.0))
|
2020-03-06 14:31:29 +01:00
|
|
|
|
2021-04-27 17:17:13 +02:00
|
|
|
def cpu_utilization(self):
|
|
|
|
tmp = psutil.cpu_percent(percpu=self._percpu)
|
|
|
|
return tmp if self._percpu else [tmp]
|
2020-03-06 14:31:29 +01:00
|
|
|
|
|
|
|
def update(self):
|
2021-04-27 17:17:13 +02:00
|
|
|
for idx, cpu_perc in enumerate(self.cpu_utilization()):
|
|
|
|
self.widgets()[idx].set("utilization", cpu_perc)
|
2020-03-06 14:31:29 +01:00
|
|
|
|
2021-04-27 17:17:13 +02:00
|
|
|
def state(self, widget):
|
|
|
|
return self.threshold_state(widget.get("utilization", 0.0), 70, 80)
|
2020-03-06 14:31:29 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-03-06 14:31:29 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|