mirror of https://github.com/nicolargo/glances
Refactor the network plugin
parent
9ee8950b16
commit
9c6c41a4a2
|
@ -41,6 +41,8 @@ class glancesCurses:
|
||||||
|
|
||||||
# Init args
|
# Init args
|
||||||
self.args = args
|
self.args = args
|
||||||
|
# By default, display bitrate instead of cumulative in the network plugin
|
||||||
|
self.args.network_stats_cumulative = False
|
||||||
|
|
||||||
# Init windows positions
|
# Init windows positions
|
||||||
self.term_w = 80
|
self.term_w = 80
|
||||||
|
@ -219,7 +221,7 @@ class glancesCurses:
|
||||||
self.network_stats_combined = not self.network_stats_combined
|
self.network_stats_combined = not self.network_stats_combined
|
||||||
elif self.pressedkey == ord('u'):
|
elif self.pressedkey == ord('u'):
|
||||||
# 'u' > View cumulative network IO
|
# 'u' > View cumulative network IO
|
||||||
self.network_stats_cumulative = not self.network_stats_cumulative
|
self.args.network_stats_cumulative = not self.args.network_stats_cumulative
|
||||||
elif self.pressedkey == ord('w'):
|
elif self.pressedkey == ord('w'):
|
||||||
# 'w' > Delete finished warning logs
|
# 'w' > Delete finished warning logs
|
||||||
glances_logs.clean()
|
glances_logs.clean()
|
||||||
|
|
|
@ -17,6 +17,9 @@
|
||||||
#
|
#
|
||||||
# You should have received a copy of the GNU Lesser General Public License
|
# You should have received a copy of the GNU Lesser General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
"""
|
||||||
|
Glances load plugin
|
||||||
|
"""
|
||||||
|
|
||||||
# Import system libs
|
# Import system libs
|
||||||
from os import getloadavg
|
from os import getloadavg
|
||||||
|
|
|
@ -17,6 +17,9 @@
|
||||||
#
|
#
|
||||||
# You should have received a copy of the GNU Lesser General Public License
|
# You should have received a copy of the GNU Lesser General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
"""
|
||||||
|
Glances virtual memory plugin
|
||||||
|
"""
|
||||||
|
|
||||||
# Import system libs
|
# Import system libs
|
||||||
# Check for PSUtil already done in the glances_core script
|
# Check for PSUtil already done in the glances_core script
|
||||||
|
|
|
@ -17,6 +17,9 @@
|
||||||
#
|
#
|
||||||
# You should have received a copy of the GNU Lesser General Public License
|
# You should have received a copy of the GNU Lesser General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
"""
|
||||||
|
Glances swap memory plugin
|
||||||
|
"""
|
||||||
|
|
||||||
# Import system libs
|
# Import system libs
|
||||||
# Check for PSUtil already done in the glances_core script
|
# Check for PSUtil already done in the glances_core script
|
||||||
|
|
|
@ -17,20 +17,15 @@
|
||||||
#
|
#
|
||||||
# You should have received a copy of the GNU Lesser General Public License
|
# You should have received a copy of the GNU Lesser General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
"""
|
||||||
|
Glances Network interface plugin
|
||||||
|
"""
|
||||||
|
|
||||||
# Import system libs
|
# Import system libs
|
||||||
try:
|
|
||||||
# psutil >= 1.0.0
|
|
||||||
from psutil import net_io_counters
|
from psutil import net_io_counters
|
||||||
except:
|
|
||||||
# psutil < 1.0.0
|
|
||||||
try:
|
|
||||||
from psutil import network_io_counters
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# Import Glances lib
|
# Import Glances lib
|
||||||
from glances_plugin import GlancesPlugin
|
from glances.plugins.glances_plugin import GlancesPlugin
|
||||||
from glances.core.glances_timer import getTimeSinceLastUpdate
|
from glances.core.glances_timer import getTimeSinceLastUpdate
|
||||||
|
|
||||||
|
|
||||||
|
@ -53,36 +48,35 @@ class Plugin(GlancesPlugin):
|
||||||
# Enter -1 to diplay bottom
|
# Enter -1 to diplay bottom
|
||||||
self.line_curse = 2
|
self.line_curse = 2
|
||||||
|
|
||||||
|
# Init stats
|
||||||
|
self.network_old = []
|
||||||
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""
|
"""
|
||||||
Update network stats
|
Update network stats
|
||||||
|
Stats is a list of dict (one dict per interface)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# Grab network interface stat using the PsUtil net_io_counter method
|
||||||
|
netiocounters = net_io_counters(pernic=True)
|
||||||
|
|
||||||
|
# Previous network interface stats are stored in the network_old variable
|
||||||
network = []
|
network = []
|
||||||
|
if (self.network_old == []):
|
||||||
# psutil >= 1.0.0
|
# First call, we init the network_old var
|
||||||
try:
|
try:
|
||||||
get_net_io_counters = net_io_counters(pernic=True)
|
self.network_old = netiocounters
|
||||||
except IOError:
|
except (IOError, UnboundLocalError):
|
||||||
# psutil < 1.0.0
|
|
||||||
try:
|
|
||||||
get_net_io_counters = network_io_counters(pernic=True)
|
|
||||||
except IOError:
|
|
||||||
pass
|
pass
|
||||||
|
else:
|
||||||
# By storing time data we enable Rx/s and Tx/s calculations in the
|
# By storing time data we enable Rx/s and Tx/s calculations in the
|
||||||
# XML/RPC API, which would otherwise be overly difficult work
|
# XML/RPC API, which would otherwise be overly difficult work
|
||||||
# for users of the API
|
# for users of the API
|
||||||
time_since_update = getTimeSinceLastUpdate('net')
|
time_since_update = getTimeSinceLastUpdate('net')
|
||||||
|
|
||||||
# Previous network interface stats are stored in the network_old variable
|
# Loop over interfaces
|
||||||
if not hasattr(self, 'network_old'):
|
network_new = netiocounters
|
||||||
# First call, we init the network_old var
|
|
||||||
try:
|
|
||||||
self.network_old = get_net_io_counters
|
|
||||||
except (IOError, UnboundLocalError):
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
network_new = get_net_io_counters
|
|
||||||
for net in network_new:
|
for net in network_new:
|
||||||
try:
|
try:
|
||||||
# Try necessary to manage dynamic network interface
|
# Try necessary to manage dynamic network interface
|
||||||
|
@ -98,7 +92,7 @@ class Plugin(GlancesPlugin):
|
||||||
netstat['cumulative_cx'] = (netstat['cumulative_rx'] +
|
netstat['cumulative_cx'] = (netstat['cumulative_rx'] +
|
||||||
netstat['cumulative_tx'])
|
netstat['cumulative_tx'])
|
||||||
netstat['cx'] = netstat['rx'] + netstat['tx']
|
netstat['cx'] = netstat['rx'] + netstat['tx']
|
||||||
except Exception:
|
except KeyError:
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
network.append(netstat)
|
network.append(netstat)
|
||||||
|
@ -106,10 +100,16 @@ class Plugin(GlancesPlugin):
|
||||||
|
|
||||||
self.stats = network
|
self.stats = network
|
||||||
|
|
||||||
|
return self.stats
|
||||||
|
|
||||||
def msg_curse(self, args=None):
|
def msg_curse(self, args=None):
|
||||||
"""
|
"""
|
||||||
Return the dict to display in the curse interface
|
Return the dict to display in the curse interface
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
#!!! TODO: Add alert on network interface bitrate
|
||||||
|
#!!! TODO: Manage the hide tag to hide a list of net interface
|
||||||
|
|
||||||
# Init the return message
|
# Init the return message
|
||||||
ret = []
|
ret = []
|
||||||
|
|
||||||
|
@ -117,6 +117,14 @@ class Plugin(GlancesPlugin):
|
||||||
# Header
|
# Header
|
||||||
msg = "{0:8}".format(_("NETWORK"))
|
msg = "{0:8}".format(_("NETWORK"))
|
||||||
ret.append(self.curse_add_line(msg, "TITLE"))
|
ret.append(self.curse_add_line(msg, "TITLE"))
|
||||||
|
if (args.network_stats_cumulative):
|
||||||
|
# Cumulative stats
|
||||||
|
msg = " {0:>6}".format(_("Rx"))
|
||||||
|
ret.append(self.curse_add_line(msg))
|
||||||
|
msg = " {0:>6}".format(_("Tx"))
|
||||||
|
ret.append(self.curse_add_line(msg))
|
||||||
|
else:
|
||||||
|
# Bitrate stats
|
||||||
msg = " {0:>6}".format(_("Rx/s"))
|
msg = " {0:>6}".format(_("Rx/s"))
|
||||||
ret.append(self.curse_add_line(msg))
|
ret.append(self.curse_add_line(msg))
|
||||||
msg = " {0:>6}".format(_("Tx/s"))
|
msg = " {0:>6}".format(_("Tx/s"))
|
||||||
|
@ -126,12 +134,19 @@ class Plugin(GlancesPlugin):
|
||||||
# Format stats
|
# Format stats
|
||||||
ifname = i['interface_name'].split(':')[0]
|
ifname = i['interface_name'].split(':')[0]
|
||||||
if (args.byte):
|
if (args.byte):
|
||||||
|
if (args.network_stats_cumulative):
|
||||||
|
rxps = self.auto_unit(int(i['cumulative_rx']))
|
||||||
|
txps = self.auto_unit(int(i['cumulative_tx']))
|
||||||
|
else:
|
||||||
rxps = self.auto_unit(int(i['rx'] // i['time_since_update']))
|
rxps = self.auto_unit(int(i['rx'] // i['time_since_update']))
|
||||||
txps = self.auto_unit(int(i['tx'] // i['time_since_update']))
|
txps = self.auto_unit(int(i['tx'] // i['time_since_update']))
|
||||||
|
else:
|
||||||
|
if (args.network_stats_cumulative):
|
||||||
|
rxps = self.auto_unit(int(i['cumulative_rx'] * 8)) + "b"
|
||||||
|
txps = self.auto_unit(int(i['cumulative_tx'] * 8)) + "b"
|
||||||
else:
|
else:
|
||||||
rxps = self.auto_unit(int(i['rx'] // i['time_since_update'] * 8)) + "b"
|
rxps = self.auto_unit(int(i['rx'] // i['time_since_update'] * 8)) + "b"
|
||||||
txps = self.auto_unit(int(i['tx'] // i['time_since_update'] * 8)) + "b"
|
txps = self.auto_unit(int(i['tx'] // i['time_since_update'] * 8)) + "b"
|
||||||
# !!! TODO: manage the hide tag
|
|
||||||
# New line
|
# New line
|
||||||
ret.append(self.curse_new_line())
|
ret.append(self.curse_new_line())
|
||||||
msg = "{0:8}".format(ifname)
|
msg = "{0:8}".format(ifname)
|
||||||
|
|
Loading…
Reference in New Issue