From 87c0b170d40c9585daeff6615146e507fdd005a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thayn=C3=A3=20B=2E=20Moretti?= Date: Tue, 30 Apr 2019 19:50:16 -0300 Subject: [PATCH] Create an alternate network traffic module --- bumblebee/modules/network_traffic.py | 42 ++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 bumblebee/modules/network_traffic.py diff --git a/bumblebee/modules/network_traffic.py b/bumblebee/modules/network_traffic.py new file mode 100644 index 0000000..1b0c315 --- /dev/null +++ b/bumblebee/modules/network_traffic.py @@ -0,0 +1,42 @@ +"""Displays network traffic +""" + +import psutil +import netifaces +import bytefmt + +import bumblebee.input +import bumblebee.output +import bumblebee.engine + +class Module(bumblebee.engine.Module): + def __init__(self, engine, config): + super(Module, self).__init__(engine, config, + bumblebee.output.Widget(full_text=self.utilization) + ) + + self._default_adapter = netifaces.gateways()['default'][netifaces.AF_INET][1] + + self._download_tx, self._upload_tx = self.network_tx() + + def utilization(self, widget): + return "{0} {1}".format( + bytefmt.humanize(self._final_download_tx), + bytefmt.humanize(self._final_upload_tx) + ) + + def update(self, widgets): + download_tx, upload_tx = self.network_tx() + + self._final_download_tx = (download_tx - self._download_tx) + self._final_upload_tx = (upload_tx - self._upload_tx) + + self._download_tx, self._upload_tx = download_tx, upload_tx + + def network_tx(self): + io_counters = psutil.net_io_counters(pernic=True) + network = io_counters[self._default_adapter] + + return network.bytes_recv, network.bytes_sent + +