2019-05-14 01:32:16 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-05-01 00:50:16 +02:00
|
|
|
"""Displays network traffic
|
2019-07-10 03:27:59 +02:00
|
|
|
No extra configuratio needed.
|
2019-05-01 00:50:16 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
import psutil
|
|
|
|
import netifaces
|
|
|
|
|
|
|
|
import bumblebee.input
|
|
|
|
import bumblebee.output
|
|
|
|
import bumblebee.engine
|
2019-05-14 01:14:13 +02:00
|
|
|
import bumblebee.util
|
2019-05-01 00:50:16 +02:00
|
|
|
|
2019-07-10 03:27:59 +02:00
|
|
|
WIDGET_NAME = 'network_traffic'
|
|
|
|
|
2019-05-01 00:50:16 +02:00
|
|
|
class Module(bumblebee.engine.Module):
|
2019-07-10 03:27:59 +02:00
|
|
|
"""Bumblebee main module """
|
|
|
|
|
2019-05-01 00:50:16 +02:00
|
|
|
def __init__(self, engine, config):
|
2019-05-14 01:14:13 +02:00
|
|
|
super(Module, self).__init__(engine, config)
|
2019-05-01 00:50:16 +02:00
|
|
|
|
2019-06-21 18:09:58 +02:00
|
|
|
try:
|
|
|
|
self._bandwidth = BandwidthInfo()
|
2019-05-01 00:50:16 +02:00
|
|
|
|
2019-06-21 18:09:58 +02:00
|
|
|
self._download_tx = self._bandwidth.bytes_recv()
|
|
|
|
self._upload_tx = self._bandwidth.bytes_sent()
|
2019-07-10 03:27:59 +02:00
|
|
|
except Exception:
|
|
|
|
""" We do not want do explode anything """
|
2019-06-21 18:09:58 +02:00
|
|
|
pass
|
2019-05-01 00:50:16 +02:00
|
|
|
|
2019-07-10 03:27:59 +02:00
|
|
|
@classmethod
|
|
|
|
def state(cls, widget):
|
|
|
|
"""Return the widget state"""
|
|
|
|
|
|
|
|
if widget.name == '{}.rx'.format(WIDGET_NAME):
|
|
|
|
return 'rx'
|
|
|
|
elif widget.name == '{}.tx'.format(WIDGET_NAME):
|
|
|
|
return 'tx'
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2019-05-01 00:50:16 +02:00
|
|
|
def update(self, widgets):
|
2019-06-21 18:09:58 +02:00
|
|
|
try:
|
|
|
|
download_tx = self._bandwidth.bytes_recv()
|
|
|
|
upload_tx = self._bandwidth.bytes_sent()
|
2019-05-14 01:14:13 +02:00
|
|
|
|
2019-06-21 18:09:58 +02:00
|
|
|
download_rate = (download_tx - self._download_tx)
|
|
|
|
upload_rate = (upload_tx - self._upload_tx)
|
2019-05-14 01:14:13 +02:00
|
|
|
|
2019-06-21 18:09:58 +02:00
|
|
|
self.update_widgets(widgets, download_rate, upload_rate)
|
2019-05-01 00:50:16 +02:00
|
|
|
|
2019-06-21 18:09:58 +02:00
|
|
|
self._download_tx, self._upload_tx = download_tx, upload_tx
|
2019-07-10 03:27:59 +02:00
|
|
|
except Exception:
|
|
|
|
""" We do not want do explode anything """
|
2019-06-21 18:09:58 +02:00
|
|
|
pass
|
2019-05-01 00:50:16 +02:00
|
|
|
|
2019-07-10 03:27:59 +02:00
|
|
|
@classmethod
|
|
|
|
def update_widgets(cls, widgets, download_rate, upload_rate):
|
|
|
|
"""Update tx/rx widgets with new rates"""
|
2019-05-14 01:14:13 +02:00
|
|
|
del widgets[:]
|
|
|
|
|
|
|
|
widgets.extend((
|
2019-07-10 03:27:59 +02:00
|
|
|
TrafficWidget(text=download_rate, direction='tx'),
|
|
|
|
TrafficWidget(text=upload_rate, direction='rx')
|
2019-05-14 01:14:13 +02:00
|
|
|
))
|
|
|
|
|
|
|
|
|
2019-05-14 01:53:31 +02:00
|
|
|
class BandwidthInfo(object):
|
2019-07-10 03:27:59 +02:00
|
|
|
"""Get received/sent bytes from network adapter"""
|
|
|
|
|
2019-05-14 01:14:13 +02:00
|
|
|
def bytes_recv(self):
|
2019-07-10 03:27:59 +02:00
|
|
|
"""Return received bytes"""
|
2019-05-14 01:14:13 +02:00
|
|
|
return self.bandwidth().bytes_recv
|
|
|
|
|
|
|
|
def bytes_sent(self):
|
2019-07-10 03:27:59 +02:00
|
|
|
"""Return sent bytes"""
|
2019-05-14 01:14:13 +02:00
|
|
|
return self.bandwidth().bytes_sent
|
|
|
|
|
|
|
|
def bandwidth(self):
|
2019-07-10 03:27:59 +02:00
|
|
|
"""Return bandwidth information"""
|
2019-05-14 01:14:13 +02:00
|
|
|
io_counters = self.io_counters()
|
|
|
|
return io_counters[self.default_network_adapter()]
|
|
|
|
|
2019-07-10 03:27:59 +02:00
|
|
|
@classmethod
|
|
|
|
def default_network_adapter(cls):
|
|
|
|
"""Return default active network adapter"""
|
2019-06-21 18:09:58 +02:00
|
|
|
gateway = netifaces.gateways()['default']
|
|
|
|
|
2019-07-10 03:27:59 +02:00
|
|
|
if not gateway:
|
2019-06-21 18:09:58 +02:00
|
|
|
raise 'No default gateway found'
|
|
|
|
|
|
|
|
return gateway[netifaces.AF_INET][1]
|
2019-05-14 01:14:13 +02:00
|
|
|
|
2019-07-10 03:27:59 +02:00
|
|
|
@classmethod
|
|
|
|
def io_counters(cls):
|
|
|
|
"""Return IO counters"""
|
2019-05-14 01:14:13 +02:00
|
|
|
return psutil.net_io_counters(pernic=True)
|
|
|
|
|
2019-05-14 01:53:31 +02:00
|
|
|
class TrafficWidget(object):
|
2019-07-10 03:27:59 +02:00
|
|
|
"""Create a traffic widget with humanized bytes string with proper icon (up/down)"""
|
|
|
|
def __new__(cls, text, direction):
|
|
|
|
widget = bumblebee.output.Widget(name='{0}.{1}'.format(WIDGET_NAME, direction))
|
|
|
|
widget.set('theme.minwidth', '0000000KiB/s')
|
|
|
|
widget.full_text(cls.humanize(text))
|
2019-05-01 00:50:16 +02:00
|
|
|
|
2019-05-14 01:14:13 +02:00
|
|
|
return widget
|
2019-05-01 00:50:16 +02:00
|
|
|
|
2019-05-14 01:14:13 +02:00
|
|
|
@staticmethod
|
2019-07-10 03:27:59 +02:00
|
|
|
def humanize(text):
|
|
|
|
"""Return humanized bytes"""
|
2019-05-14 01:14:13 +02:00
|
|
|
humanized_byte_format = bumblebee.util.bytefmt(text)
|
2019-07-10 03:27:59 +02:00
|
|
|
return '{0}/s'.format(humanized_byte_format)
|