提交 70085b9d 编写于 作者: N nicolargo

Issue #1514 already corrected by issue #1348. Nevertheless, this commit...

Issue #1514 already corrected by issue #1348. Nevertheless, this commit improve code quality off the network plugin
上级 e10dbe86
......@@ -78,18 +78,18 @@ class Plugin(GlancesPlugin):
# Grab network interface stat using the psutil net_io_counter method
try:
netiocounters = psutil.net_io_counters(pernic=True)
except UnicodeDecodeError:
except UnicodeDecodeError as e:
logger.debug('Can not get network interface counters ({})'.format(e))
return self.stats
# New in psutil 3.0.0
# - import the interface's status (issue #765)
# - import the interface's speed (issue #718)
# Grab interface's status (issue #765)
# Grab interface's speed (issue #718)
netstatus = {}
try:
netstatus = psutil.net_if_stats()
except OSError:
except OSError as e:
# see psutil #797/glances #1106
pass
logger.debug('Can not get network interface status ({})'.format(e))
# Previous network interface stats are stored in the network_old variable
if not hasattr(self, 'network_old'):
......@@ -98,50 +98,51 @@ class Plugin(GlancesPlugin):
self.network_old = netiocounters
except (IOError, UnboundLocalError):
pass
else:
# By storing time data we enable Rx/s and Tx/s calculations in the
# XML/RPC API, which would otherwise be overly difficult work
# for users of the API
time_since_update = getTimeSinceLastUpdate('net')
# Loop over interfaces
network_new = netiocounters
for net in network_new:
# Do not take hidden interface into account
# or KeyError: 'eth0' when interface is not connected #1348
if self.is_hide(net) or net not in netstatus:
continue
try:
cumulative_rx = network_new[net].bytes_recv
cumulative_tx = network_new[net].bytes_sent
cumulative_cx = cumulative_rx + cumulative_tx
rx = cumulative_rx - self.network_old[net].bytes_recv
tx = cumulative_tx - self.network_old[net].bytes_sent
cx = rx + tx
netstat = {
'interface_name': net,
'time_since_update': time_since_update,
'cumulative_rx': cumulative_rx,
'rx': rx,
'cumulative_tx': cumulative_tx,
'tx': tx,
'cumulative_cx': cumulative_cx,
'cx': cx}
except KeyError:
continue
else:
# Interface status
netstat['is_up'] = netstatus[net].isup
# Interface speed in Mbps, convert it to bps
# Can be always 0 on some OSes
netstat['speed'] = netstatus[net].speed * 1048576
# Finaly, set the key
netstat['key'] = self.get_key()
stats.append(netstat)
return self.stats
# Save stats to compute next bitrate
self.network_old = network_new
# By storing time data we enable Rx/s and Tx/s calculations in the
# XML/RPC API, which would otherwise be overly difficult work
# for users of the API
time_since_update = getTimeSinceLastUpdate('net')
# Loop over interfaces
network_new = netiocounters
for net in network_new:
# Do not take hidden interface into account
# or KeyError: 'eth0' when interface is not connected #1348
if self.is_hide(net) or net not in netstatus:
continue
try:
cumulative_rx = network_new[net].bytes_recv
cumulative_tx = network_new[net].bytes_sent
cumulative_cx = cumulative_rx + cumulative_tx
rx = cumulative_rx - self.network_old[net].bytes_recv
tx = cumulative_tx - self.network_old[net].bytes_sent
cx = rx + tx
netstat = {'interface_name': net,
'time_since_update': time_since_update,
'cumulative_rx': cumulative_rx,
'rx': rx,
'cumulative_tx': cumulative_tx,
'tx': tx,
'cumulative_cx': cumulative_cx,
'cx': cx,
# Interface status
'is_up': netstatus[net].isup,
# Interface speed in Mbps, convert it to bps
# Can be always 0 on some OSes
'speed': netstatus[net].speed * 1048576,
# Set the key for the dict
'key': self.get_key()
}
except KeyError:
continue
else:
# Append the interface stats to the list
stats.append(netstat)
# Save stats to compute next bitrate
self.network_old = network_new
elif self.input_method == 'snmp':
# Update stats using SNMP
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册