2016-12-10 11:25:02 +01:00
|
|
|
#pylint: disable=C0111,R0903
|
|
|
|
|
2016-12-10 12:03:58 +01:00
|
|
|
"""Displays the name, IP address(es) and status of each available network interface.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
* nic.exclude: Comma-separated list of interface prefixes to exclude (defaults to "lo,virbr,docker,vboxnet,veth")
|
2017-05-22 18:09:00 +02:00
|
|
|
* nic.states: Comma-separated list of states to show (prefix with "^" to invert - i.e. ^down -> show all devices that are not in state down)
|
2018-04-11 07:30:16 +02:00
|
|
|
* nic.format: Format string (defaults to "{intf} {state} {ip}")
|
2016-12-10 12:03:58 +01:00
|
|
|
"""
|
|
|
|
|
2017-03-26 03:32:11 +02:00
|
|
|
try:
|
|
|
|
import netifaces
|
|
|
|
except ImportError:
|
|
|
|
pass
|
2016-12-10 11:25:02 +01:00
|
|
|
|
|
|
|
import bumblebee.util
|
|
|
|
import bumblebee.input
|
|
|
|
import bumblebee.output
|
|
|
|
import bumblebee.engine
|
|
|
|
|
|
|
|
class Module(bumblebee.engine.Module):
|
|
|
|
def __init__(self, engine, config):
|
|
|
|
widgets = []
|
|
|
|
super(Module, self).__init__(engine, config, widgets)
|
|
|
|
self._exclude = tuple(filter(len, self.parameter("exclude", "lo,virbr,docker,vboxnet,veth").split(",")))
|
2017-05-22 18:09:00 +02:00
|
|
|
|
|
|
|
self._states = {}
|
|
|
|
self._states["include"] = []
|
|
|
|
self._states["exclude"] = []
|
|
|
|
for state in tuple(filter(len, self.parameter("states", "").split(","))):
|
|
|
|
if state[0] == "^":
|
|
|
|
self._states["exclude"].append(state[1:])
|
|
|
|
else:
|
|
|
|
self._states["include"].append(state)
|
2018-04-11 07:30:16 +02:00
|
|
|
self._format = self.parameter("format","{intf} {state} {ip}");
|
2016-12-10 11:25:02 +01:00
|
|
|
self._update_widgets(widgets)
|
|
|
|
|
|
|
|
def update(self, widgets):
|
|
|
|
self._update_widgets(widgets)
|
|
|
|
|
|
|
|
def state(self, widget):
|
|
|
|
states = []
|
|
|
|
|
|
|
|
if widget.get("state") == "down":
|
|
|
|
states.append("critical")
|
|
|
|
elif widget.get("state") != "up":
|
|
|
|
states.append("warning")
|
|
|
|
|
|
|
|
intf = widget.get("intf")
|
|
|
|
iftype = "wireless" if self._iswlan(intf) else "wired"
|
|
|
|
iftype = "tunnel" if self._istunnel(intf) else iftype
|
|
|
|
|
|
|
|
states.append("{}-{}".format(iftype, widget.get("state")))
|
|
|
|
|
|
|
|
return states
|
|
|
|
|
|
|
|
def _iswlan(self, intf):
|
|
|
|
# wifi, wlan, wlp, seems to work for me
|
|
|
|
if intf.startswith("w"): return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _istunnel(self, intf):
|
|
|
|
return intf.startswith("tun")
|
|
|
|
|
2017-02-26 09:08:36 +01:00
|
|
|
def get_addresses(self, intf):
|
|
|
|
retval = []
|
|
|
|
try:
|
|
|
|
for ip in netifaces.ifaddresses(intf).get(netifaces.AF_INET, []):
|
|
|
|
if ip.get("addr", "") != "":
|
|
|
|
retval.append(ip.get("addr"))
|
|
|
|
except Exception:
|
|
|
|
return []
|
|
|
|
return retval
|
|
|
|
|
2016-12-10 11:25:02 +01:00
|
|
|
def _update_widgets(self, widgets):
|
2017-10-13 17:06:18 +02:00
|
|
|
interfaces = [i for i in netifaces.interfaces() if not i.startswith(self._exclude)]
|
2016-12-10 11:33:35 +01:00
|
|
|
|
|
|
|
for widget in widgets:
|
|
|
|
widget.set("visited", False)
|
|
|
|
|
2016-12-10 11:25:02 +01:00
|
|
|
for intf in interfaces:
|
|
|
|
addr = []
|
|
|
|
state = "down"
|
2017-02-26 09:08:36 +01:00
|
|
|
for ip in self.get_addresses(intf):
|
|
|
|
addr.append(ip)
|
|
|
|
state = "up"
|
2017-05-22 18:09:00 +02:00
|
|
|
|
|
|
|
if len(self._states["exclude"]) > 0 and state in self._states["exclude"]: continue
|
|
|
|
if len(self._states["include"]) > 0 and state not in self._states["include"]: continue
|
|
|
|
|
2016-12-10 11:25:02 +01:00
|
|
|
widget = self.widget(intf)
|
|
|
|
if not widget:
|
|
|
|
widget = bumblebee.output.Widget(name=intf)
|
|
|
|
widgets.append(widget)
|
2018-04-11 07:30:16 +02:00
|
|
|
widget.full_text(self._format.format(
|
|
|
|
**{
|
|
|
|
"intf": intf,
|
|
|
|
"state": state,
|
|
|
|
"ip": ", ".join(addr)
|
|
|
|
}))
|
2016-12-10 11:25:02 +01:00
|
|
|
widget.set("intf", intf)
|
|
|
|
widget.set("state", state)
|
2016-12-10 11:33:35 +01:00
|
|
|
widget.set("visited", True)
|
|
|
|
|
|
|
|
for widget in widgets:
|
|
|
|
if widget.get("visited") == False:
|
|
|
|
widgets.remove(widget)
|
2016-12-10 11:25:02 +01:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|