2016-10-31 13:03:16 +01:00
|
|
|
import netifaces
|
|
|
|
import bumblebee.module
|
|
|
|
|
2016-10-31 16:08:03 +01:00
|
|
|
def description():
|
|
|
|
return "Displays the names, IP addresses and status of each available interface."
|
|
|
|
|
2016-11-05 16:18:53 +01:00
|
|
|
def parameters():
|
|
|
|
return [ "none" ]
|
|
|
|
|
2016-10-31 13:03:16 +01:00
|
|
|
class Module(bumblebee.module.Module):
|
2016-11-05 14:26:02 +01:00
|
|
|
def __init__(self, output, config, alias):
|
|
|
|
super(Module, self).__init__(output, config, alias)
|
2016-11-03 19:45:12 +01:00
|
|
|
self._exclude = ( "lo", "virbr", "docker", "vboxnet" )
|
2016-10-31 13:03:16 +01:00
|
|
|
self._state = "down"
|
2016-11-05 13:47:27 +01:00
|
|
|
self._typecache = {}
|
2016-10-31 13:03:16 +01:00
|
|
|
|
2016-11-05 13:42:26 +01:00
|
|
|
def widgets(self):
|
|
|
|
result = []
|
|
|
|
interfaces = [ i for i in netifaces.interfaces() if not i.startswith(self._exclude) ]
|
|
|
|
state = "down"
|
|
|
|
for intf in interfaces:
|
2016-11-06 08:21:33 +01:00
|
|
|
addr = []
|
2016-11-05 13:42:26 +01:00
|
|
|
try:
|
|
|
|
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"])
|
|
|
|
state = "up"
|
|
|
|
except Exception as e:
|
|
|
|
addr = []
|
|
|
|
widget = bumblebee.output.Widget(self, "{} {} {}".format(
|
|
|
|
intf, state, ", ".join(addr)
|
|
|
|
))
|
|
|
|
widget.set("intf", intf)
|
|
|
|
widget.set("state", state)
|
|
|
|
result.append(widget)
|
2016-10-31 13:03:16 +01:00
|
|
|
|
2016-11-05 13:42:26 +01:00
|
|
|
return result
|
2016-10-31 13:03:16 +01:00
|
|
|
|
|
|
|
def _iswlan(self, intf):
|
2016-11-05 16:33:35 +01:00
|
|
|
# wifi, wlan, wlp, seems to work for me
|
|
|
|
if intf.startswith("w"): return True
|
|
|
|
return False
|
2016-10-31 13:03:16 +01:00
|
|
|
|
2016-10-31 13:34:48 +01:00
|
|
|
def _istunnel(self, intf):
|
|
|
|
return intf.startswith("tun")
|
|
|
|
|
2016-11-05 13:42:26 +01:00
|
|
|
def state(self, widget):
|
|
|
|
intf = widget.get("intf")
|
2016-10-31 13:09:52 +01:00
|
|
|
|
2016-11-05 13:47:27 +01:00
|
|
|
if not intf in self._typecache:
|
|
|
|
t = "wireless" if self._iswlan(intf) else "wired"
|
|
|
|
t = "tunnel" if self._istunnel(intf) else t
|
|
|
|
self._typecache[intf] = t
|
2016-10-31 13:34:48 +01:00
|
|
|
|
2016-11-05 13:47:27 +01:00
|
|
|
return "{}-{}".format(self._typecache[intf], widget.get("state"))
|
2016-10-31 13:03:16 +01:00
|
|
|
|
2016-11-05 13:42:26 +01:00
|
|
|
def warning(self, widget):
|
|
|
|
return widget.get("state") != "up"
|
2016-10-31 13:03:16 +01:00
|
|
|
|
2016-11-05 13:42:26 +01:00
|
|
|
def critical(self, widget):
|
|
|
|
return widget.get("state") == "down"
|
2016-10-31 13:03:16 +01:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|