[modules/traffic] Minor refactoring
* use psutil instead of "ifconfig" in order to avoid external command calls * fix a small bug in the ascii theme (missing colon) * show statistics per-nic
This commit is contained in:
parent
16359c883b
commit
430c9f5e93
3 changed files with 50 additions and 52 deletions
|
@ -1,75 +1,73 @@
|
||||||
import netifaces
|
|
||||||
import re
|
import re
|
||||||
|
import psutil
|
||||||
|
import netifaces
|
||||||
|
|
||||||
import bumblebee.util
|
import bumblebee.util
|
||||||
import bumblebee.input
|
import bumblebee.input
|
||||||
import bumblebee.output
|
import bumblebee.output
|
||||||
import bumblebee.engine
|
import bumblebee.engine
|
||||||
|
|
||||||
|
"""Displays network IO for interfaces.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
* traffic.exclude: Comma-separated list of interface prefixes to exclude (defaults to "lo,virbr,docker,vboxnet,veth")
|
||||||
|
"""
|
||||||
|
|
||||||
class Module(bumblebee.engine.Module):
|
class Module(bumblebee.engine.Module):
|
||||||
def __init__(self, engine, config):
|
def __init__(self, engine, config):
|
||||||
widgets = [
|
widgets = []
|
||||||
bumblebee.output.Widget(name="traffic.down"),
|
|
||||||
bumblebee.output.Widget(name="traffic.up"),
|
|
||||||
]
|
|
||||||
super(Module, self).__init__(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._exclude = tuple(filter(len, self.parameter("exclude", "lo,virbr,docker,vboxnet,veth").split(",")))
|
||||||
self._update_widgets(widgets)
|
self._update_widgets(widgets)
|
||||||
self._status = None
|
self._status = ""
|
||||||
|
|
||||||
def state(self, widget):
|
def state(self, widget):
|
||||||
if widget.name == "traffic.down":
|
if "traffic.rx" in widget.name:
|
||||||
return "down"
|
return "rx"
|
||||||
if widget.name == "traffic.up":
|
if "traffic.tx" in widget.name:
|
||||||
return "up"
|
return "tx"
|
||||||
return self._status
|
return self._status
|
||||||
|
|
||||||
def update(self, widgets):
|
def update(self, widgets):
|
||||||
self._update_widgets(widgets)
|
self._update_widgets(widgets)
|
||||||
|
|
||||||
def _update_widgets(self, widgets):
|
def _update_widgets(self, widgets):
|
||||||
_ifconfdata = bumblebee.util.execute('ifconfig')
|
|
||||||
interfaces = [ i for i in netifaces.interfaces() if not i.startswith(self._exclude) ]
|
interfaces = [ i for i in netifaces.interfaces() if not i.startswith(self._exclude) ]
|
||||||
|
|
||||||
interface = interfaces[0]
|
counters = psutil.net_io_counters(pernic=True)
|
||||||
if interface is '':
|
for interface in interfaces:
|
||||||
interface = 'lo'
|
if not interface: interface = "lo"
|
||||||
|
rx = counters[interface].bytes_recv
|
||||||
|
tx = counters[interface].bytes_sent
|
||||||
|
|
||||||
try:
|
name = "traffic-{}".format(interface)
|
||||||
_block = re.compile(r"" + interface + ":(.*\n)*", re.MULTILINE)
|
txname = "traffic.tx-{}".format(interface)
|
||||||
_down = re.compile(r"RX packets .* bytes (.*) \(", re.MULTILINE)
|
rxname = "traffic.rx-{}".format(interface)
|
||||||
_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)
|
|
||||||
except:
|
|
||||||
_current_up = -1
|
|
||||||
_current_down = -1
|
|
||||||
|
|
||||||
widget_down = self.widget("traffic.down")
|
widget = self.widget(name)
|
||||||
widget_up = self.widget("traffic.up")
|
if not widget:
|
||||||
if not widget_down:
|
widget = bumblebee.output.Widget(name=name)
|
||||||
widget_down = bumblebee.output.Widget(name="traffic.down")
|
widgets.append(widget)
|
||||||
widgets.append(widget_down)
|
widget.full_text(interface)
|
||||||
if not widget_up:
|
|
||||||
widget_up = bumblebee.output.Widget(name="traffic.down")
|
|
||||||
widgets.append(widget_up)
|
|
||||||
|
|
||||||
_prev_down = widget_down.get("absdown", 0)
|
widget_rx = self.widget(rxname)
|
||||||
if _current_down is not -1:
|
widget_tx = self.widget(txname)
|
||||||
_speed_down = bumblebee.util.bytefmt(int(_current_down) - int(_prev_down))
|
if not widget_rx:
|
||||||
widget_down.set("absdown", _current_down)
|
widget_rx = bumblebee.output.Widget(name=rxname)
|
||||||
else:
|
widgets.append(widget_rx)
|
||||||
_speed_down = bumblebee.util.bytefmt(0)
|
if not widget_tx:
|
||||||
widget_down.set("absdown", _prev_down)
|
widget_tx = bumblebee.output.Widget(name=txname)
|
||||||
widget_down.full_text("{}".format(_speed_down))
|
widgets.append(widget_tx)
|
||||||
|
|
||||||
_prev_up = widget_up.get("absup", 0)
|
prev_rx = widget_rx.get("rx", 0)
|
||||||
if _current_up is not -1:
|
prev_tx = widget_tx.get("tx", 0)
|
||||||
_speed_up = bumblebee.util.bytefmt(int(_current_up) - int(_prev_up))
|
rxspeed = bumblebee.util.bytefmt(int(rx) - int(prev_rx))
|
||||||
widget_up.set("absup", _current_up)
|
txspeed = bumblebee.util.bytefmt(int(tx) - int(prev_tx))
|
||||||
else:
|
|
||||||
_speed_up = bumblebee.util.bytefmt(0)
|
widget_rx.full_text(rxspeed)
|
||||||
widget_up.set("absup", _prev_up)
|
widget_tx.full_text(txspeed)
|
||||||
widget_up.full_text("{}".format(_speed_up))
|
|
||||||
|
widget_rx.set("rx", rx)
|
||||||
|
widget_tx.set("tx", tx)
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
|
@ -62,9 +62,9 @@
|
||||||
},
|
},
|
||||||
"sensors": {
|
"sensors": {
|
||||||
"prefix": "sensors"
|
"prefix": "sensors"
|
||||||
}
|
},
|
||||||
"traffic": {
|
"traffic": {
|
||||||
"down": { "prefix": "down"},
|
"rx": { "prefix": "down"},
|
||||||
"up": {"prefix": "up"}
|
"tx": {"prefix": "up"}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,7 +80,7 @@
|
||||||
"prefix": "🌡"
|
"prefix": "🌡"
|
||||||
},
|
},
|
||||||
"traffic":{
|
"traffic":{
|
||||||
"down": { "prefix": "" },
|
"rx": { "prefix": "" },
|
||||||
"up": { "prefix": "" }
|
"tx": { "prefix": "" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue