From 803b4154977f66fbdc700afc5beaf65578f14f45 Mon Sep 17 00:00:00 2001 From: Nicolas Hennion Date: Sat, 22 Mar 2014 09:08:23 +0100 Subject: [PATCH] Client/Server works but no process are displayed. Also rename In/Out by Read/Write for DiskIO according to #339 --- glances/__init__.py | 6 ++-- glances/core/glances_client.py | 50 +++++++++++++++++---------- glances/core/glances_stats.py | 56 +++++++++++-------------------- glances/plugins/glances_diskio.py | 10 +++--- glances/plugins/glances_load.py | 3 ++ 5 files changed, 62 insertions(+), 63 deletions(-) diff --git a/glances/__init__.py b/glances/__init__.py index 4364e94c..1b76f313 100644 --- a/glances/__init__.py +++ b/glances/__init__.py @@ -53,15 +53,13 @@ def main(argv=None): # Init the client client = GlancesClient(args=core.get_args(), server_address=core.server_ip, server_port=int(core.server_port), - username=core.username, password=core.password) + username=core.username, password=core.password, config=core.get_config()) # Test if client and server are in the same major version if (not client.login()): - print(_("Error: The server version is not compatible")) + print(_("Error: The server version is not compatible with the client")) sys.exit(2) - print("{} {}:{}".format(_("Glances client connected"), core.server_ip, core.server_port)) - # Start the client loop client.serve_forever() diff --git a/glances/core/glances_client.py b/glances/core/glances_client.py index 686c31b3..687d7217 100644 --- a/glances/core/glances_client.py +++ b/glances/core/glances_client.py @@ -47,7 +47,8 @@ class GlancesClient(): def __init__(self, args=None, server_address="localhost", server_port=61209, - username="glances", password=""): + username="glances", password="", + config=None): # Build the URI if (password != ""): uri = 'http://%s:%s@%s:%d' % (username, password, server_address, server_port) @@ -61,11 +62,9 @@ class GlancesClient(): print(_("Error: creating client socket") + " %s" % uri) pass - # Init stats - self.stats = GlancesStatsClient() - - # Init screen - self.screen = glancesCurses(args=args) + # Store the arg/config + self.args = args + self.config = config def login(self): """ @@ -82,21 +81,38 @@ class GlancesClient(): else: print("{} ({})".format(_("Error: Connection to server failed"), err)) sys.exit(2) - # Debug - # print "Server version: {}\nClient version: {}\n".format(__version__, client_version) - return __version__[:3] == client_version[:3] + + if (__version__[:3] == client_version[:3]): + # Init stats and limits + self.stats = GlancesStatsClient() + self.stats.set_plugins(json.loads(self.client.getAllPlugins())) + self.stats.load_limits(self.config) + + # Init screen + self.screen = glancesCurses(args=self.args) + + # Debug + # print "Server version: {}\nClient version: {}\n".format(__version__, client_version) + return True + else: + return False def update(self): """ Get stats from server - """ + Return the client/server connection status: + - Connected: Connection OK + - Disconnected: Connection NOK + """ try: server_stats = json.loads(self.client.getAll()) - except Exception: - server_stats = {} - - # Put it in the internal dict - self.stats.update(server_stats) + except socket.error as e: + # Client can not get server stats + return "Disconnected" + else: + # Put it in the internal dict + self.stats.update(server_stats) + return "Connected" def serve_forever(self): """ @@ -104,10 +120,10 @@ class GlancesClient(): """ while True: # Update the stats - self.update() + cs_status = self.update() # Update the screen - self.screen.update(self.stats, cs_status="Connected") + self.screen.update(self.stats, cs_status=cs_status) def close(self): """ diff --git a/glances/core/glances_stats.py b/glances/core/glances_stats.py index 52bda645..efec1fed 100644 --- a/glances/core/glances_stats.py +++ b/glances/core/glances_stats.py @@ -74,6 +74,7 @@ class GlancesStats(object): Load all plugins in the "plugins" folder """ + # Set the plugins' path plug_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../plugins") sys.path.insert(0, plug_dir) @@ -172,51 +173,32 @@ class GlancesStatsServer(GlancesStats): class GlancesStatsClient(GlancesStats): - def __init__(self): - + def __init__(self): + # Init the plugin list dict + self._plugins = collections.defaultdict(dict) + # Init the all_stats dict used by the server # all_stats is a dict of dicts filled by the server self.all_stats = collections.defaultdict(dict) - # Load the plugins - self.load_plugins() - - # Load the limits - self.load_limits() - - - def load_plugins(self): + def set_plugins(self, input_plugins): """ - Load all plugins serve by the Glances' server + Set the plugin list accoring to the Glances' server """ - #!!! TODO: call the getAllPlugins() server method - #!!! Build the plugins list - # plug_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../plugins") - # sys.path.insert(0, plug_dir) + # Set the plugins' path + plug_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../plugins") + sys.path.insert(0, plug_dir) - # header = "glances_" - # for plug in os.listdir(plug_dir): - # if (plug.startswith(header) and plug.endswith(".py") and - # plug != (header + "plugin.py")): - # # Import the plugin - # m = __import__(os.path.basename(plug)[:-3]) - # # Add the plugin to the dictionnary - # # The key is the plugin name - # # for example, the file glances_xxx.py - # # generate self._plugins_list["xxx"] = ... - # plugname = os.path.basename(plug)[len(header):-3].lower() - # self._plugins[plugname] = m.Plugin() - pass - - def load_limits(self, config=None): - """ - Load the limits serve by the Glances' server - """ - # For each plugins, call the init_limits method - # for p in self._plugins: - # self._plugins[p].load_limits(config) - pass + header = "glances_" + for plug in input_plugins: + # Import the plugin + m = __import__(header + plug) + # Add the plugin to the dictionnary + # The key is the plugin name + # for example, the file glances_xxx.py + # generate self._plugins_list["xxx"] = ... + self._plugins[plug] = m.Plugin() def update(self, input_stats={}): """ diff --git a/glances/plugins/glances_diskio.py b/glances/plugins/glances_diskio.py index 83b30816..e25b725e 100644 --- a/glances/plugins/glances_diskio.py +++ b/glances/plugins/glances_diskio.py @@ -123,9 +123,9 @@ class Plugin(GlancesPlugin): # Header msg = "{0:8}".format(_("DISK I/O")) ret.append(self.curse_add_line(msg, "TITLE")) - msg = " {0:>6}".format(_("In/s")) + msg = " {0:>6}".format(_("R/s")) ret.append(self.curse_add_line(msg)) - msg = " {0:>6}".format(_("Out/s")) + msg = " {0:>6}".format(_("W/s")) ret.append(self.curse_add_line(msg)) # Disk list (sorted by name) for i in sorted(self.stats, key=lambda diskio: diskio['disk_name']): @@ -133,11 +133,11 @@ class Plugin(GlancesPlugin): ret.append(self.curse_new_line()) msg = "{0:8}".format(i['disk_name']) ret.append(self.curse_add_line(msg)) - rxps = self.auto_unit(int(i['write_bytes'] // i['time_since_update'])) txps = self.auto_unit(int(i['read_bytes'] // i['time_since_update'])) - msg = " {0:>6}".format(rxps) - ret.append(self.curse_add_line(msg)) + rxps = self.auto_unit(int(i['write_bytes'] // i['time_since_update'])) msg = " {0:>6}".format(txps) ret.append(self.curse_add_line(msg)) + msg = " {0:>6}".format(rxps) + ret.append(self.curse_add_line(msg)) return ret diff --git a/glances/plugins/glances_load.py b/glances/plugins/glances_load.py index 9e90b33f..7840d1d8 100644 --- a/glances/plugins/glances_load.py +++ b/glances/plugins/glances_load.py @@ -51,6 +51,9 @@ class Plugin(GlancesPlugin): # Enter -1 to diplay bottom self.line_curse = 1 + # Init stats + self.stats = {} + def update(self): """ Update load stats