From 0a0e39b5161adb8b8b9b05f5f0a33ff17e3ce623 Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Thu, 5 Mar 2020 21:13:15 +0100 Subject: [PATCH] [modules] Re-add load module --- core/module.py | 7 +++++++ modules/load.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 modules/load.py diff --git a/core/module.py b/core/module.py index 5e1139a..abf24d4 100644 --- a/core/module.py +++ b/core/module.py @@ -65,6 +65,13 @@ class Module(core.input.Object): def state(self, widget): return [] + def threshold_state(self, value, warn, crit): + if value > float(self.parameter('critical', crit)): + return 'critical' + if value > float(self.parameter('warning', warn)): + return 'warning' + return None + class Error(Module): def __init__(self, config, module, error): super().__init__(config, core.widget.Widget(self.full_text)) diff --git a/modules/load.py b/modules/load.py new file mode 100644 index 0000000..87f4d3a --- /dev/null +++ b/modules/load.py @@ -0,0 +1,38 @@ +# pylint: disable=C0111,R0903 + +'''Displays system load. + +Parameters: + * load.warning : Warning threshold for the one-minute load average (defaults to 70% of the number of CPUs) + * load.critical: Critical threshold for the one-minute load average (defaults to 80% of the number of CPUs) +''' + +import os +import multiprocessing + +import core.module +import core.input + +class Module(core.module.Module): + def __init__(self, config): + super().__init__(config, core.widget.Widget(self.load)) + self._load = [0, 0, 0] + try: + self._cpus = multiprocessing.cpu_count() + except NotImplementedError as e: + self._cpus = 1 + core.input.register(self, button=core.input.LEFT_MOUSE, + cmd='gnome-system-monitor') + + def load(self, widget): + return '{:.02f}/{:.02f}/{:.02f}'.format( + self._load[0], self._load[1], self._load[2] + ) + + def update(self): + self._load = os.getloadavg() + + def state(self, widget): + return self.threshold_state(self._load[0], self._cpus*0.7, self._cpus*0.8) + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4