[modules] Re-enable nic module + allow widget states
All callback from a widget into a module (e.g. for retrieving the status or the criticality state) now get a widget passed. This has the purpose of allowing a module to store state/widget specific data somewhere. This way, for instance, it is possible to store the interface name as part of the widget, thus making it possible to show the status of the correct interface.
This commit is contained in:
parent
bd0089dac0
commit
a9a62a738d
7 changed files with 74 additions and 84 deletions
|
@ -36,24 +36,16 @@ class Module(object):
|
|||
self._config = config
|
||||
self._output = output
|
||||
|
||||
def data(self):
|
||||
pass
|
||||
|
||||
def critical(self):
|
||||
def critical(self, widget):
|
||||
return False
|
||||
|
||||
def warning(self):
|
||||
def warning(self, widget):
|
||||
return False
|
||||
|
||||
def state(self):
|
||||
def state(self, widget):
|
||||
return "default"
|
||||
|
||||
def instance(self):
|
||||
def instance(self, widget):
|
||||
return None
|
||||
|
||||
def next(self):
|
||||
return False
|
||||
|
||||
|
||||
|
||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||
|
|
|
@ -24,13 +24,13 @@ class Module(bumblebee.module.Module):
|
|||
|
||||
return bumblebee.output.Widget(self,"{:02d}%".format(self._capacity))
|
||||
|
||||
def warning(self):
|
||||
def warning(self, widget):
|
||||
return self._capacity < self._config.parameter("battery.warning", 20)
|
||||
|
||||
def critical(self):
|
||||
def critical(self, widget):
|
||||
return self._capacity < self._config.parameter("battery.critical", 10)
|
||||
|
||||
def state(self):
|
||||
def state(self, widget):
|
||||
with open("/sys/class/power_supply/{}/status".format(self._battery)) as f:
|
||||
self._status = f.read().strip()
|
||||
|
||||
|
|
|
@ -23,10 +23,10 @@ class Module(bumblebee.module.Module):
|
|||
self._perc = psutil.cpu_percent(percpu=False)
|
||||
return bumblebee.output.Widget(self, "{:05.02f}%".format(self._perc))
|
||||
|
||||
def warning(self):
|
||||
def warning(self, widget):
|
||||
return self._perc > self._config.parameter("cpu.warning", 70)
|
||||
|
||||
def critical(self):
|
||||
def critical(self, widget):
|
||||
return self._perc > self._config.parameter("cpu.critical", 80)
|
||||
|
||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||
|
|
|
@ -33,13 +33,13 @@ class Module(bumblebee.module.Module):
|
|||
bumblebee.util.bytefmt(self._size), self._perc)
|
||||
)
|
||||
|
||||
def instance(self):
|
||||
def instance(self, widget):
|
||||
return self._path
|
||||
|
||||
def warning(self):
|
||||
def warning(self, widget):
|
||||
return self._perc > self._config.parameter("disk.warning", 80)
|
||||
|
||||
def critical(self):
|
||||
def critical(self, widget):
|
||||
return self._perc > self._config.parameter("disk.critical", 90)
|
||||
|
||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||
|
|
|
@ -12,24 +12,28 @@ def description():
|
|||
return "Shows available RAM, total amount of RAM and the percentage of available RAM."
|
||||
|
||||
class Module(bumblebee.module.Module):
|
||||
def __init__(self, output, args):
|
||||
super(Module, self).__init__(args)
|
||||
def __init__(self, output, config):
|
||||
super(Module, self).__init__(output, config)
|
||||
self._mem = psutil.virtual_memory()
|
||||
|
||||
output.add_callback(module=self.__module__, button=1,
|
||||
cmd="gnome-system-monitor")
|
||||
|
||||
def data(self):
|
||||
def widgets(self):
|
||||
self._mem = psutil.virtual_memory()
|
||||
|
||||
used = self._mem.total - self._mem.available
|
||||
|
||||
return "{}/{} ({:05.02f}%)".format(bumblebee.util.bytefmt(used), bumblebee.util.bytefmt(self._mem.total), self._mem.percent)
|
||||
return bumblebee.output.Widget(self, "{}/{} ({:05.02f}%)".format(
|
||||
bumblebee.util.bytefmt(used),
|
||||
bumblebee.util.bytefmt(self._mem.total),
|
||||
self._mem.percent)
|
||||
)
|
||||
|
||||
def warning(self):
|
||||
return self._mem.percent < 20
|
||||
def warning(self, widget):
|
||||
return self._mem.percent < self._config.parameter("memory.warning", 20)
|
||||
|
||||
def critical(self):
|
||||
return self._mem.percent < 10
|
||||
def critical(self, widget):
|
||||
return self._mem.percent < self._config.parameter("memory.critical", 10)
|
||||
|
||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||
|
|
|
@ -12,72 +12,59 @@ def description():
|
|||
return "Displays the names, IP addresses and status of each available interface."
|
||||
|
||||
class Module(bumblebee.module.Module):
|
||||
def __init__(self, output, args):
|
||||
super(Module, self).__init__(args)
|
||||
def __init__(self, output, config):
|
||||
super(Module, self).__init__(output, config)
|
||||
self._exclude = ( "lo", "virbr", "docker", "vboxnet" )
|
||||
self._interfaces = [ i for i in netifaces.interfaces() if not i.startswith(self._exclude) ]
|
||||
self._index = 0
|
||||
self._intf = self._interfaces[0] if len(self._interfaces) > 0 else None
|
||||
self._cache = {}
|
||||
self._state = "down"
|
||||
|
||||
def data(self):
|
||||
if len(self._interfaces) <= self._index:
|
||||
return "n/a"
|
||||
self._intf = self._interfaces[self._index]
|
||||
self._state = "down"
|
||||
def widgets(self):
|
||||
result = []
|
||||
interfaces = [ i for i in netifaces.interfaces() if not i.startswith(self._exclude) ]
|
||||
addr = []
|
||||
|
||||
state = "down"
|
||||
for intf in interfaces:
|
||||
try:
|
||||
if netifaces.AF_INET in netifaces.ifaddresses(self._intf):
|
||||
for ip in netifaces.ifaddresses(self._intf)[netifaces.AF_INET]:
|
||||
if netifaces.AF_INET in netifaces.ifaddresses(intf):
|
||||
for ip in netifaces.ifaddresses(intf)[netifaces.AF_INET]:
|
||||
if "addr" in ip and ip["addr"] != "":
|
||||
addr.append(ip["addr"])
|
||||
self._state = "up"
|
||||
state = "up"
|
||||
except Exception as e:
|
||||
self._state = "down"
|
||||
addr = []
|
||||
widget = bumblebee.output.Widget(self, "{} {} {}".format(
|
||||
intf, state, ", ".join(addr)
|
||||
))
|
||||
widget.set("intf", intf)
|
||||
widget.set("state", state)
|
||||
result.append(widget)
|
||||
|
||||
return "{} {} {}".format(self._intf, self._state, ", ".join(addr))
|
||||
|
||||
def next(self):
|
||||
self._index += 1
|
||||
if self._index < len(self._interfaces):
|
||||
return True
|
||||
self._index = 0
|
||||
# reload to support hotplug
|
||||
self._interfaces = [ i for i in netifaces.interfaces() if not i.startswith(self._exclude) ]
|
||||
return False
|
||||
return result
|
||||
|
||||
def _iswlan(self, intf):
|
||||
if not "wlan{}".format(intf) in self._cache:
|
||||
iw = pyroute2.IW()
|
||||
ip = pyroute2.IPRoute()
|
||||
idx = ip.link_lookup(ifname=intf)[0]
|
||||
try:
|
||||
iw.get_interface_by_ifindex(idx)
|
||||
self._cache["wlan{}".format(intf)] = True
|
||||
return True
|
||||
except Exception as e:
|
||||
self._cache["wlan{}".format(intf)] = False
|
||||
return self._cache["wlan{}".format(intf)]
|
||||
return False
|
||||
|
||||
def _istunnel(self, intf):
|
||||
return intf.startswith("tun")
|
||||
|
||||
def instance(self):
|
||||
return self._intf
|
||||
def state(self, widget):
|
||||
intf = widget.get("intf")
|
||||
t = "wireless" if self._iswlan(intf) else "wired"
|
||||
|
||||
def state(self):
|
||||
t = "wireless" if self._iswlan(self._intf) else "wired"
|
||||
t = "tunnel" if self._istunnel(intf) else t
|
||||
|
||||
t = "tunnel" if self._istunnel(self._intf) else t
|
||||
return "{}-{}".format(t, widget.get("state"))
|
||||
|
||||
return "{}-{}".format(t, self._state)
|
||||
def warning(self, widget):
|
||||
return widget.get("state") != "up"
|
||||
|
||||
def warning(self):
|
||||
return self._state != "up"
|
||||
|
||||
def critical(self):
|
||||
return self._state == "down"
|
||||
def critical(self, widget):
|
||||
return widget.get("state") == "down"
|
||||
|
||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||
|
|
|
@ -8,15 +8,22 @@ class Widget(object):
|
|||
def __init__(self, obj, text):
|
||||
self._obj = obj
|
||||
self._text = text
|
||||
self._store = {}
|
||||
|
||||
def set(self, key, value):
|
||||
self._store[key] = value
|
||||
|
||||
def get(self, key, default=None):
|
||||
return self._store.get(key, default)
|
||||
|
||||
def state(self):
|
||||
return self._obj.state()
|
||||
return self._obj.state(self)
|
||||
|
||||
def warning(self):
|
||||
return self._obj.warning()
|
||||
return self._obj.warning(self)
|
||||
|
||||
def critical(self):
|
||||
return self._obj.critical()
|
||||
return self._obj.critical(self)
|
||||
|
||||
def module(self):
|
||||
return self._obj.__module__.split(".")[-1]
|
||||
|
@ -25,7 +32,7 @@ class Widget(object):
|
|||
return self._obj.__module__
|
||||
|
||||
def instance(self):
|
||||
rv = getattr(self._obj, "instance")()
|
||||
rv = getattr(self._obj, "instance")(self)
|
||||
|
||||
def text(self):
|
||||
return self._text
|
||||
|
|
Loading…
Reference in a new issue