[modules] critical/warning threshold refactoring

Quite a lot of modules use the "if higher X -> critical, if higher Y ->
warning" idiom now, so extracted that into a common function for reuse.

see #23
This commit is contained in:
Tobi-wan Kenobi 2016-12-11 08:25:54 +01:00
parent edfccd2d31
commit 23d7d53fca
5 changed files with 14 additions and 22 deletions

View file

@ -51,6 +51,13 @@ class Module(object):
name = "{}.{}".format(self.name, name)
return self._config["config"].get(name, default)
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 Engine(object):
"""Engine for driving the application

View file

@ -72,14 +72,10 @@ class Module(bumblebee.engine.Module):
if line.startswith("tag"):
key, value = line.split(" ", 2)[1:]
self._tags.update({ key: value })
if line.startswith("duration"):
self._tags.update({
"duration": bumblebee.util.durationfmt(int(line.split(" ")[1]))
})
if line.startswith("position"):
self._tags.update({
"position": bumblebee.util.durationfmt(int(line.split(" ")[1]))
})
for key in ["duration", "position"]:
if line.startswith(key):
dur = int(line.split(" ")[1])
self._tags.update({key:bumblebee.util.durationfmt(dur)})
if line.startswith("set repeat "):
self._repeat = False if "false" in line else True
if line.startswith("set shuffle "):

View file

@ -28,10 +28,6 @@ class Module(bumblebee.engine.Module):
self._utilization = psutil.cpu_percent(percpu=False)
def state(self, widget):
if self._utilization > int(self.parameter("critical", 80)):
return "critical"
if self._utilization > int(self.parameter("warning", 70)):
return "warning"
return None
return self.threshold_state(self._utilization, 70, 80)
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -38,10 +38,6 @@ class Module(bumblebee.engine.Module):
self._perc = 100.0*self._used/self._size
def state(self, widget):
if self._perc > float(self.parameter("critical", 90)):
return "critical"
if self._perc > float(self.parameter("warning", 80)):
return "warning"
return None
return self.threshold_state(self._perc, 80, 90)
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -35,9 +35,6 @@ class Module(bumblebee.engine.Module):
self._load = os.getloadavg()
def state(self, widget):
if self._load[0] > float(self.parameter("critical", self._cpus*0.8)):
return "critical"
if self._load[0] > float(self.parameter("warning", self._cpus*0.7)):
return "warning"
return self.threshold_state(self._load[0], self._cpus*0.7, self._cpus*0.8)
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4