Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 11 additions & 24 deletions sysmonitor_common/sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ def get_default_iface():
except Exception:
return None

def _default_iface_usage():
iface = get_default_iface()
if iface:
iostat = ps.net_io_counters(pernic=True).get(iface)
if iostat:
return [iostat.bytes_recv, iostat.bytes_sent]
return [0, 0]

def bytes_to_human(num):
for unit in B_UNITS:
if abs(num) < 1000.0:
Expand Down Expand Up @@ -538,10 +546,7 @@ def get_value(self, sensor_data):

def _fetch_net(self):
"""It returns the bytes sent and received in bytes/second"""
current = [0, 0]
for _, iostat in list(ps.net_io_counters(pernic=True).items()):
current[0] += iostat.bytes_recv
current[1] += iostat.bytes_sent
current = _default_iface_usage()
dummy = copy.deepcopy(current)

current[0] -= self._last_net_usage[0]
Expand All @@ -562,10 +567,7 @@ def get_value(self, sensor_data):

def _fetch_net(self):
"""It returns the bytes sent and received in bytes/second"""
current = [0, 0]
for _, iostat in list(ps.net_io_counters(pernic=True).items()):
current[0] += iostat.bytes_recv
current[1] += iostat.bytes_sent
current = _default_iface_usage()
dummy = copy.deepcopy(current)

current[0] -= self._last_net_usage[0]
Expand Down Expand Up @@ -604,22 +606,7 @@ def get_value(self, sensor_data):
return self._fetch_net()

def _fetch_net(self):
rx = 0
tx = 0

iface = get_default_iface()

if not iface:
iface = "enp3s0"

net = ps.net_io_counters(pernic=True)

if iface in net:
iostat = net[iface]
rx = iostat.bytes_recv
tx = iostat.bytes_sent

current = [rx, tx]
current = _default_iface_usage()

if self._last_net_usage == [0, 0]:
self._last_net_usage = copy.deepcopy(current)
Expand Down