From 14943a69cb5a0161a6646d1bcf44cad8f2099b60 Mon Sep 17 00:00:00 2001 From: Abin Simon Date: Fri, 21 Apr 2017 10:46:16 +0530 Subject: [PATCH] traffic module added --- bumblebee/modules/traffic.py | 45 +++++++++++++++++++++++++++++++++ themes/icons/awesome-fonts.json | 3 +++ 2 files changed, 48 insertions(+) create mode 100644 bumblebee/modules/traffic.py diff --git a/bumblebee/modules/traffic.py b/bumblebee/modules/traffic.py new file mode 100644 index 0000000..6e2a6b2 --- /dev/null +++ b/bumblebee/modules/traffic.py @@ -0,0 +1,45 @@ +import netifaces +import re + +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(","))) + self._update_widgets(widgets) + + def update(self, widgets): + self._update_widgets(widgets) + + def _update_widgets(self, widgets): + _ifconfdata = bumblebee.util.execute('ifconfig') + interfaces = [ i for i in netifaces.interfaces() if not i.startswith(self._exclude) ] + + interface = interfaces[0] + if interface is '': + interface = 'lo' + + _block = re.compile(r"" + interface + ":(.*\n)*", re.MULTILINE) + _down = re.compile(r"RX packets .* bytes (.*) \(", re.MULTILINE) + _current_down = re.search(_down,re.search(_block,_ifconfdata).group(0)).group(1) + _up = re.compile(r"TX packets .* bytes (.*) \(", re.MULTILINE) + _current_up = re.search(_up,re.search(_block,_ifconfdata).group(0)).group(1) + + widget = self.widget("traffic") + if not widget: + widget = bumblebee.output.Widget(name="traffic") + widgets.append(widget) + _prev_up = widget.get("absup", 0) + _prev_down = widget.get("absdown", 0) + _speed_down = bumblebee.util.bytefmt(int(_current_down) - int(_prev_down)) + _speed_up = bumblebee.util.bytefmt(int(_current_up) - int(_prev_up)) + widget.full_text("{} {} {}".format(interface, _speed_down, _speed_up)) + widget.set("absdown", _current_down) + widget.set("absup", _current_up) + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/themes/icons/awesome-fonts.json b/themes/icons/awesome-fonts.json index b3b2867..b12f73e 100644 --- a/themes/icons/awesome-fonts.json +++ b/themes/icons/awesome-fonts.json @@ -78,5 +78,8 @@ }, "sensors": { "prefix": "🌡" + }, + "traffic":{ + "prefix": "" } }