2016-10-31 13:03:16 +01:00
|
|
|
import pyroute2
|
|
|
|
import netifaces
|
|
|
|
import bumblebee.module
|
|
|
|
|
|
|
|
class Module(bumblebee.module.Module):
|
|
|
|
def __init__(self, args):
|
|
|
|
super(Module, self).__init__(args)
|
|
|
|
self._exclude = ( "lo", "virbr" )
|
|
|
|
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"
|
|
|
|
addr = []
|
|
|
|
|
2016-10-31 13:34:48 +01:00
|
|
|
try:
|
|
|
|
if netifaces.AF_INET in netifaces.ifaddresses(self._intf):
|
|
|
|
for ip in netifaces.ifaddresses(self._intf)[netifaces.AF_INET]:
|
|
|
|
if "addr" in ip and ip["addr"] != "":
|
|
|
|
addr.append(ip["addr"])
|
|
|
|
self._state = "up"
|
|
|
|
except Exception as e:
|
|
|
|
self._state = "down"
|
|
|
|
addr = []
|
2016-10-31 13:03:16 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
2016-10-31 13:09:52 +01:00
|
|
|
except Exception as e:
|
2016-10-31 13:03:16 +01:00
|
|
|
self._cache["wlan{}".format(intf)] = False
|
2016-10-31 13:09:52 +01:00
|
|
|
return self._cache["wlan{}".format(intf)]
|
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-10-31 13:03:16 +01:00
|
|
|
def state(self):
|
|
|
|
t = "wireless" if self._iswlan(self._intf) else "wired"
|
2016-10-31 13:09:52 +01:00
|
|
|
|
2016-10-31 13:34:48 +01:00
|
|
|
t = "tunnel" if self._istunnel(self._intf) else t
|
|
|
|
|
2016-10-31 13:03:16 +01:00
|
|
|
return "{}-{}".format(t, self._state)
|
|
|
|
|
|
|
|
def warning(self):
|
|
|
|
return self._state != "up"
|
|
|
|
|
|
|
|
def critical(self):
|
|
|
|
return self._state == "down"
|
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|