2017-04-22 11:10:01 +02:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
|
|
|
"""Displays network IO for interfaces.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
* traffic.exclude: Comma-separated list of interface prefixes to exclude (defaults to "lo,virbr,docker,vboxnet,veth")
|
2017-06-03 15:20:27 +02:00
|
|
|
* traffic.states: Comma-separated list of states to show (prefix with "^" to invert - i.e. ^down -> show all devices that are not in state down)
|
2017-09-10 09:16:21 +02:00
|
|
|
* traffic.showname: If set to False, hide network interface name (defaults to True)
|
2017-04-22 11:10:01 +02:00
|
|
|
"""
|
|
|
|
|
2018-10-22 18:34:58 +02:00
|
|
|
import time
|
2017-04-22 08:11:55 +02:00
|
|
|
import psutil
|
|
|
|
import netifaces
|
2017-04-21 07:16:16 +02:00
|
|
|
|
|
|
|
import bumblebee.util
|
|
|
|
import bumblebee.input
|
|
|
|
import bumblebee.output
|
|
|
|
import bumblebee.engine
|
|
|
|
|
|
|
|
class Module(bumblebee.engine.Module):
|
|
|
|
def __init__(self, engine, config):
|
2017-04-22 08:11:55 +02:00
|
|
|
widgets = []
|
2017-04-21 07:16:16 +02:00
|
|
|
super(Module, self).__init__(engine, config, widgets)
|
|
|
|
self._exclude = tuple(filter(len, self.parameter("exclude", "lo,virbr,docker,vboxnet,veth").split(",")))
|
2017-04-22 08:11:55 +02:00
|
|
|
self._status = ""
|
2017-04-21 08:10:02 +02:00
|
|
|
|
2017-09-10 09:16:21 +02:00
|
|
|
self._showname = bumblebee.util.asbool(self.parameter("showname", True))
|
2017-06-05 15:06:56 +02:00
|
|
|
self._prev = {}
|
2017-06-03 15:20:27 +02:00
|
|
|
self._states = {}
|
2018-10-22 18:34:58 +02:00
|
|
|
self._lastcheck = 0
|
2017-06-03 15:20:27 +02:00
|
|
|
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)
|
|
|
|
self._update_widgets(widgets)
|
|
|
|
|
2017-04-21 08:10:02 +02:00
|
|
|
def state(self, widget):
|
2017-04-22 08:11:55 +02:00
|
|
|
if "traffic.rx" in widget.name:
|
|
|
|
return "rx"
|
|
|
|
if "traffic.tx" in widget.name:
|
|
|
|
return "tx"
|
2017-04-21 08:10:02 +02:00
|
|
|
return self._status
|
2017-04-21 07:16:16 +02:00
|
|
|
|
|
|
|
def update(self, widgets):
|
|
|
|
self._update_widgets(widgets)
|
|
|
|
|
2017-05-05 20:58:15 +02:00
|
|
|
def create_widget(self, widgets, name, txt=None, attributes={}):
|
|
|
|
widget = bumblebee.output.Widget(name=name)
|
|
|
|
widget.full_text(txt)
|
|
|
|
widgets.append(widget)
|
|
|
|
|
|
|
|
for key in attributes:
|
|
|
|
widget.set(key, attributes[key])
|
|
|
|
|
|
|
|
return widget
|
|
|
|
|
2017-06-03 15:20:27 +02: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
|
|
|
|
|
2017-04-21 07:16:16 +02: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)]
|
2017-04-21 07:16:16 +02:00
|
|
|
|
2017-06-03 15:26:00 +02:00
|
|
|
del widgets[:]
|
|
|
|
|
2017-04-22 08:11:55 +02:00
|
|
|
counters = psutil.net_io_counters(pernic=True)
|
2018-10-23 18:10:55 +02:00
|
|
|
now = time.time()
|
2018-10-22 18:34:58 +02:00
|
|
|
timediff = now - (self._lastcheck if self._lastcheck else now)
|
|
|
|
if timediff <= 0: timediff = 1
|
|
|
|
self._lastcheck = now
|
2017-04-22 08:11:55 +02:00
|
|
|
for interface in interfaces:
|
|
|
|
if not interface: interface = "lo"
|
2017-06-03 15:20:27 +02:00
|
|
|
state = "down"
|
|
|
|
if len(self.get_addresses(interface)) > 0:
|
|
|
|
state = "up"
|
2018-04-01 19:39:14 +02:00
|
|
|
elif bumblebee.util.asbool(self.parameter("hide_down", True)):
|
|
|
|
continue
|
2017-06-03 15:20:27 +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
|
|
|
|
|
2017-05-05 20:58:15 +02:00
|
|
|
data = {
|
|
|
|
"rx": counters[interface].bytes_recv,
|
|
|
|
"tx": counters[interface].bytes_sent,
|
|
|
|
}
|
2017-04-22 08:11:55 +02:00
|
|
|
|
|
|
|
name = "traffic-{}".format(interface)
|
2017-10-13 17:09:09 +02:00
|
|
|
|
2017-09-10 09:16:21 +02:00
|
|
|
if self._showname:
|
2017-09-09 20:53:41 +02:00
|
|
|
self.create_widget(widgets, name, interface)
|
2017-05-05 20:58:15 +02:00
|
|
|
|
|
|
|
for direction in ["rx", "tx"]:
|
|
|
|
name = "traffic.{}-{}".format(direction, interface)
|
|
|
|
widget = self.create_widget(widgets, name, attributes={"theme.minwidth": "1000.00MB"})
|
2017-06-05 15:06:56 +02:00
|
|
|
prev = self._prev.get(name, 0)
|
2018-10-22 18:34:58 +02:00
|
|
|
speed = bumblebee.util.bytefmt((int(data[direction]) - int(prev))/timediff)
|
2017-05-05 20:58:15 +02:00
|
|
|
widget.full_text(speed)
|
2017-06-05 15:06:56 +02:00
|
|
|
self._prev[name] = data[direction]
|
2017-04-21 07:16:16 +02:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|