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):
|
|
|
|
super().__init__(config, theme, core.widget.Widget(self.utilization))
|
2021-04-27 17:16:54 +02:00
|
|
|
self._percpu = util.format.asbool(self.parameter("percpu", False))
|
|
|
|
self.update()
|
|
|
|
self.widget().set("theme.minwidth", self._format.format(*[100.0 - 10e-20]*len(self._utilization)))
|
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:16:54 +02:00
|
|
|
fmt = self.parameter("format", "{:.01f}%")
|
|
|
|
if self._percpu:
|
|
|
|
fmt = [fmt]*len(self._utilization)
|
|
|
|
fmt = " ".join(fmt)
|
|
|
|
return fmt
|
2020-03-06 14:31:29 +01:00
|
|
|
|
|
|
|
def utilization(self, _):
|
2021-04-27 17:16:54 +02:00
|
|
|
return self._format.format(*self._utilization)
|
2020-03-06 14:31:29 +01:00
|
|
|
|
|
|
|
def update(self):
|
2021-04-27 17:16:54 +02:00
|
|
|
self._utilization = psutil.cpu_percent(percpu=self._percpu)
|
|
|
|
if not self._percpu:
|
|
|
|
self._utilization = [self._utilization]
|
2020-03-06 14:31:29 +01:00
|
|
|
|
|
|
|
def state(self, _):
|
2021-04-27 17:16:54 +02:00
|
|
|
return self.threshold_state(sum(self._utilization)/len(self._utilization), 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
|