2020-04-29 16:20:20 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""Displays network traffic
|
|
|
|
* No extra configuration needed
|
2020-05-08 20:58:35 +02:00
|
|
|
|
|
|
|
contributed by `izn <https://github.com/izn>`_ - many thanks!
|
2020-04-29 16:20:20 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
import psutil
|
|
|
|
import netifaces
|
|
|
|
|
|
|
|
import core.module
|
|
|
|
import core.widget
|
|
|
|
|
|
|
|
import util.format
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
WIDGET_NAME = "network_traffic"
|
|
|
|
|
2020-05-17 13:16:33 +02:00
|
|
|
|
2020-04-29 16:20:20 +02:00
|
|
|
class Module(core.module.Module):
|
|
|
|
def __init__(self, config, theme):
|
|
|
|
widgets = [
|
2020-05-03 11:15:52 +02:00
|
|
|
core.widget.Widget(
|
|
|
|
name="{0}.rx".format(WIDGET_NAME),
|
|
|
|
full_text=self.download_rate,
|
|
|
|
),
|
|
|
|
core.widget.Widget(
|
|
|
|
name="{0}.tx".format(WIDGET_NAME),
|
|
|
|
full_text=self.upload_rate,
|
|
|
|
),
|
2020-04-29 16:20:20 +02:00
|
|
|
]
|
|
|
|
super().__init__(config, theme, widgets)
|
|
|
|
|
2020-05-16 13:24:14 +02:00
|
|
|
self.widgets()[0].module = self
|
|
|
|
self.widgets()[1].module = self
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
self.widgets()[0].set("theme.minwidth", "0000000KiB/s")
|
|
|
|
self.widgets()[1].set("theme.minwidth", "0000000KiB/s")
|
2020-04-29 16:20:20 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
self._bandwidth = BandwidthInfo()
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
self._rate_recv = "?"
|
|
|
|
self._rate_sent = "?"
|
2020-04-29 16:20:20 +02:00
|
|
|
self._bytes_recv = self._bandwidth.bytes_recv()
|
|
|
|
self._bytes_sent = self._bandwidth.bytes_sent()
|
|
|
|
except Exception:
|
|
|
|
""" We do not want do explode anything """
|
|
|
|
pass
|
|
|
|
|
|
|
|
def state(self, widget):
|
|
|
|
"""Return the widget state"""
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
if widget.name == "{}.rx".format(WIDGET_NAME):
|
|
|
|
return "rx"
|
|
|
|
elif widget.name == "{}.tx".format(WIDGET_NAME):
|
|
|
|
return "tx"
|
2020-04-29 16:20:20 +02:00
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
try:
|
|
|
|
bytes_recv = self._bandwidth.bytes_recv()
|
|
|
|
bytes_sent = self._bandwidth.bytes_sent()
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
self._rate_recv = bytes_recv - self._bytes_recv
|
|
|
|
self._rate_sent = bytes_sent - self._bytes_sent
|
2020-04-29 16:20:20 +02:00
|
|
|
|
|
|
|
self._bytes_recv, self._bytes_sent = bytes_recv, bytes_sent
|
|
|
|
except Exception:
|
|
|
|
""" We do not want do explode anything """
|
|
|
|
pass
|
|
|
|
|
|
|
|
def download_rate(self, _):
|
2020-05-03 11:15:52 +02:00
|
|
|
return "{}/s".format(util.format.byte(self._rate_recv))
|
2020-04-29 16:20:20 +02:00
|
|
|
|
|
|
|
def upload_rate(self, _):
|
2020-05-03 11:15:52 +02:00
|
|
|
return "{}/s".format(util.format.byte(self._rate_sent))
|
|
|
|
|
2020-04-29 16:20:20 +02:00
|
|
|
|
|
|
|
class BandwidthInfo(object):
|
|
|
|
"""Get received/sent bytes from network adapter"""
|
|
|
|
|
|
|
|
def bytes_recv(self):
|
|
|
|
"""Return received bytes"""
|
|
|
|
return self.bandwidth().bytes_recv
|
|
|
|
|
|
|
|
def bytes_sent(self):
|
|
|
|
"""Return sent bytes"""
|
|
|
|
return self.bandwidth().bytes_sent
|
|
|
|
|
|
|
|
def bandwidth(self):
|
|
|
|
"""Return bandwidth information"""
|
|
|
|
io_counters = self.io_counters()
|
|
|
|
return io_counters[self.default_network_adapter()]
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def default_network_adapter(cls):
|
|
|
|
"""Return default active network adapter"""
|
2020-05-03 11:15:52 +02:00
|
|
|
gateway = netifaces.gateways()["default"]
|
2020-04-29 16:20:20 +02:00
|
|
|
|
|
|
|
if not gateway:
|
2020-05-03 11:15:52 +02:00
|
|
|
raise "No default gateway found"
|
2020-04-29 16:20:20 +02:00
|
|
|
|
|
|
|
return gateway[netifaces.AF_INET][1]
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def io_counters(cls):
|
|
|
|
"""Return IO counters"""
|
|
|
|
return psutil.net_io_counters(pernic=True)
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-04-29 16:20:20 +02:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|