[modules/ping] Re-enable ping module

Show RTT measured by ICMP echo request/replies for a given host.

For that to work correctly, change the "full_text" callback for a widget
so that the widget itself is also passed as argument in the callback
method. That actually makes a lot of sense, since the widget can now be
used as a repository of state information.

see #23
This commit is contained in:
Tobi-wan Kenobi 2016-12-11 11:37:24 +01:00
parent 4f6ec89d3c
commit 71582cbcd7
15 changed files with 98 additions and 21 deletions

View file

@ -24,7 +24,7 @@ class Module(bumblebee.engine.Module):
self._capacity = 100
self._ac = False
def capacity(self):
def capacity(self, widget):
if self._ac:
return "ac"
if self._capacity == -1:

View file

@ -24,7 +24,7 @@ class Module(bumblebee.engine.Module):
engine.input.register_callback(self, button=bumblebee.input.WHEEL_DOWN,
cmd="xbacklight -{}%".format(step))
def brightness(self):
def brightness(self, widget):
return "{:03.0f}%".format(self._brightness)
def update(self, widgets):

View file

@ -16,7 +16,7 @@ class Module(bumblebee.engine.Module):
cmd=self._toggle
)
def caffeine(self):
def caffeine(self, widget):
return ""
def state(self, widget):
@ -33,9 +33,6 @@ class Module(bumblebee.engine.Module):
return False
return False
def update(self, widgets):
pass
def _toggle(self, widget):
if self._active():
bumblebee.util.execute("xset s default")

View file

@ -41,8 +41,9 @@ class Module(bumblebee.engine.Module):
self._status = None
self._shuffle = False
self._repeat = False
self._tags = defaultdict(lambda: '')
def description(self):
def description(self, widget):
return string.Formatter().vformat(self._fmt, (), self._tags)
def update(self, widgets):

View file

@ -21,7 +21,7 @@ class Module(bumblebee.engine.Module):
engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE,
cmd="gnome-system-monitor")
def utilization(self):
def utilization(self, widget):
return "{:06.02f}%".format(self._utilization)
def update(self, widgets):

View file

@ -29,7 +29,7 @@ class Module(bumblebee.engine.Module):
)
self._fmt = self.parameter("format", default_format(self.name))
def get_time(self):
def get_time(self, widget):
return datetime.datetime.now().strftime(self._fmt)
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -21,11 +21,13 @@ class Module(bumblebee.engine.Module):
)
self._path = self.parameter("path", "/")
self._perc = 0
self._used = 0
self._size = 0
engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE,
cmd="nautilus {}".format(self._path))
def diskspace(self):
def diskspace(self, widget):
return "{} {}/{} ({:05.02f}%)".format(self._path,
bumblebee.util.bytefmt(self._used),
bumblebee.util.bytefmt(self._size), self._perc

View file

@ -19,6 +19,7 @@ class Module(bumblebee.engine.Module):
super(Module, self).__init__(engine, config,
bumblebee.output.Widget(full_text=self.load)
)
self._load = [0, 0, 0]
try:
self._cpus = multiprocessing.cpu_count()
except multiprocessing.NotImplementedError as e:
@ -26,7 +27,7 @@ class Module(bumblebee.engine.Module):
engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE,
cmd="gnome-system-monitor")
def load(self):
def load(self, widget):
return "{:.02f}/{:.02f}/{:.02f}".format(
self._load[0], self._load[1], self._load[2]
)

View file

@ -23,7 +23,7 @@ class Module(bumblebee.engine.Module):
engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE,
cmd="gnome-system-monitor")
def memory_usage(self):
def memory_usage(self, widget):
used = self._mem.total - self._mem.available
return "{}/{} ({:05.02f}%)".format(
bumblebee.util.bytefmt(used),

81
bumblebee/modules/ping.py Normal file
View file

@ -0,0 +1,81 @@
# pylint: disable=C0111,R0903
"""Periodically checks the RTT of a configurable host using ICMP echos
Parameters:
* ping.interval: Time in seconds between two RTT checks (defaults to 60)
* ping.address : IP address to check
* ping.timeout : Timeout for waiting for a reply (defaults to 5.0)
* ping.probes : Number of probes to send (defaults to 5)
* ping.warning : Threshold for warning state, in seconds (defaults to 1.0)
* ping.critical: Threshold for critical state, in seconds (defaults to 2.0)
"""
import re
import time
import threading
import bumblebee.input
import bumblebee.output
import bumblebee.engine
def get_rtt(module, widget):
main = None
for thread in threading.enumerate():
if thread.name == "MainThread":
main = thread
interval = widget.get("interval")
next_check = 0
while main.is_alive():
try:
if int(time.time()) < next_check:
time.sleep(1)
continue
widget.set("rtt-unreachable", False)
res = bumblebee.util.execute("ping -n -q -c {} -W {} {}".format(
widget.get("rtt-probes"), widget.get("rtt-timeout"), widget.get("address")
))
next_check = int(time.time()) + interval
for line in res.split("\n"):
if not line.startswith("rtt"): continue
m = re.search(r'([0-9\.]+)/([0-9\.]+)/([0-9\.]+)/([0-9\.]+)\s+(\S+)', line)
widget.set("rtt-min", float(m.group(1)))
widget.set("rtt-avg", float(m.group(2)))
widget.set("rtt-max", float(m.group(3)))
widget.set("rtt-unit", m.group(5))
except Exception as e:
widget.set("rtt-unreachable", True)
class Module(bumblebee.engine.Module):
def __init__(self, engine, config):
widget = bumblebee.output.Widget(full_text=self.rtt)
super(Module, self).__init__(engine, config, widget)
widget.set("address", self.parameter("address", "8.8.8.8"))
widget.set("interval", self.parameter("interval", 60))
widget.set("rtt-probes", self.parameter("probes", 5))
widget.set("rtt-timeout", self.parameter("timeout", 5.0))
widget.set("rtt-avg", 0.0)
widget.set("rtt-unit", "")
self._thread = threading.Thread(target=get_rtt, args=(self,widget,))
self._thread.start()
def rtt(self, widget):
if widget.get("rtt-unreachable"):
return "{}: unreachable".format(widget.get("address"))
return "{}: {:.1f}{}".format(
widget.get("address"),
widget.get("rtt-avg"),
widget.get("rtt-unit")
)
def state(self, widget):
if widget.get("rtt-unreachable"): return ["critical"]
return self.threshold_state(widget.get("rtt-avg"), 1000.0, 2000.0)
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -42,7 +42,7 @@ class Module(bumblebee.engine.Module):
return line.replace(pattern, "")
return "n/a"
def volume(self):
def volume(self, widget):
if int(self._mono) > 0:
return "{}%".format(self._mono)
elif self._left == self._right:

View file

@ -17,10 +17,7 @@ class Module(bumblebee.engine.Module):
)
self._text = self.parameter("text", "")
def text(self):
def text(self, widget):
return self._text
def update(self, widgets):
pass
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -10,7 +10,4 @@ class Module(bumblebee.engine.Module):
bumblebee.output.Widget(full_text="test")
)
def update(self, widgets):
pass
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -41,7 +41,7 @@ class Widget(bumblebee.store.Store):
self._full_text = value
else:
if callable(self._full_text):
return self._full_text()
return self._full_text(self)
else:
return self._full_text

View file

@ -35,6 +35,7 @@ class TestGenericModules(unittest.TestCase):
assertWidgetAttributes(self, widget)
widget.set("variable", "value")
self.assertEquals(widget.get("variable", None), "value")
self.assertTrue(isinstance(widget.full_text(), str))
@mock.patch("subprocess.Popen")
def test_update(self, mock_output):