From d90032467ed2f71415851e072d19a75be2f96098 Mon Sep 17 00:00:00 2001 From: Alessio Sergi Date: Sat, 13 Jul 2013 01:13:00 +0200 Subject: [PATCH] Add psutil >= 1.0.0 support Add support to the new net_io_counters() API. Of course, still preserved backward compatibility with the now deprecated network_io_counters(). --- glances/glances.py | 23 ++++++++++++++++++++--- requirements.txt | 2 +- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/glances/glances.py b/glances/glances.py index 877311b4..caf64e12 100644 --- a/glances/glances.py +++ b/glances/glances.py @@ -102,13 +102,21 @@ if psutil_version < (0, 4, 1): sys.exit(1) try: - # virtual_memory() only available from psutil >= 0.6 + # psutil.virtual_memory() only available from psutil >= 0.6 psutil.virtual_memory() except Exception: psutil_mem_vm = False else: psutil_mem_vm = True +try: + # psutil.net_io_counters() only available from psutil >= 1.0.0 + psutil.net_io_counters() +except Exception: + psutil_net_io_counters = False +else: + psutil_net_io_counters = True + if not is_Mac: psutil_get_io_counter_tag = True else: @@ -1203,17 +1211,26 @@ class GlancesStats: # NET if network_tag and not self.network_error_tag: self.network = [] + # 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') + + if psutil_net_io_counters: + # psutil >= 1.0.0 + get_net_io_counters = psutil.net_io_counters(pernic=True) + else: + # psutil < 1.0.0 + get_net_io_counters = psutil.network_io_counters(pernic=True) + if not hasattr(self, 'network_old'): try: - self.network_old = psutil.network_io_counters(pernic=True) + self.network_old = get_net_io_counters except IOError: self.network_error_tag = True else: - self.network_new = psutil.network_io_counters(pernic=True) + self.network_new = get_net_io_counters for net in self.network_new: try: # Try necessary to manage dynamic network interface diff --git a/requirements.txt b/requirements.txt index 37829f3d..bca35be9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -psutil==0.7.1 +psutil==1.0.1 -- GitLab