From 697110ea46e4c625c181d26289d848e1a97a9ac7 Mon Sep 17 00:00:00 2001 From: Nicolas Hennion Date: Sun, 13 Apr 2014 10:34:07 +0200 Subject: [PATCH] Refactor arguments --- glances/__init__.py | 32 +++++++-------- glances/core/glances_client.py | 20 +++++---- glances/core/glances_main.py | 56 ++++++++++++++++---------- glances/core/glances_server.py | 11 ++--- glances/core/glances_standalone.py | 2 +- glances/outputs/glances_bottle.py | 4 +- glances/plugins/glances_alert.py | 5 ++- glances/plugins/glances_help.py | 4 +- glances/plugins/glances_processlist.py | 5 ++- 9 files changed, 77 insertions(+), 62 deletions(-) diff --git a/glances/__init__.py b/glances/__init__.py index b4a6b0f1..6ecef7b4 100644 --- a/glances/__init__.py +++ b/glances/__init__.py @@ -80,9 +80,7 @@ def main(): # Init the standalone mode standalone = GlancesStandalone(config=core.get_config(), - args=core.get_args(), - refresh_time=core.refresh_time, - use_bold=core.use_bold) + args=core.get_args()) # Start the standalone (CLI) loop standalone.serve_forever() @@ -93,9 +91,8 @@ def main(): from glances.core.glances_client import GlancesClient # 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, config=core.get_config()) + client = GlancesClient(config=core.get_config(), + args=core.get_args()) # Test if client and server are in the same major version if (not client.login()): @@ -106,8 +103,6 @@ def main(): client.serve_forever() # Shutdown the client - # !!! How to close the server with CTRL-C - # !!! Call core.end() with parameters ? client.close() elif (core.is_server()): @@ -116,16 +111,21 @@ def main(): from glances.core.glances_server import GlancesServer # Init the server - server = GlancesServer(bind_address=core.bind_ip, - bind_port=int(core.server_port), - cached_time=core.cached_time, - config=core.get_config()) - # print(_("DEBUG: Glances server is running on %s:%s with config file %s") % (core.bind_ip, core.server_port, core.config.get_config_path())) - print("{} {}:{}".format(_("Glances server is running on"), core.bind_ip, core.server_port)) + # server = GlancesServer(bind_address=core.bind_ip, + # bind_port=int(core.server_port), + # cached_time=core.cached_time, + # config=core.get_config()) + + args = core.get_args() + + server = GlancesServer(cached_time=core.cached_time, + config=core.get_config(), + args=args) + print("{} {}:{}".format(_("Glances server is running on"), args.bind, args.port)) # Set the server login/password (if -P/--password tag) - if (core.password != ""): - server.add_user(core.username, core.password) + if (args.password != ""): + server.add_user(args.username, args.password) # Start the server loop server.serve_forever() diff --git a/glances/core/glances_client.py b/glances/core/glances_client.py index 565f589f..1a31290f 100644 --- a/glances/core/glances_client.py +++ b/glances/core/glances_client.py @@ -45,15 +45,17 @@ class GlancesClient(): """ def __init__(self, - args=None, - server_address="localhost", server_port=61209, - username="glances", password="", - config=None): + config=None, + args=None): + # Store the arg/config + self.args = args + self.config = config + # Build the URI - if (password != ""): - uri = 'http://%s:%s@%s:%d' % (username, password, server_address, server_port) + if (args.password != ""): + uri = 'http://%s:%s@%s:%d' % (args.username, args.password, args.bind, args.port) else: - uri = 'http://%s:%d' % (server_address, server_port) + uri = 'http://%s:%d' % (args.bind, args.port) # Try to connect to the URI try: @@ -62,10 +64,6 @@ class GlancesClient(): print("{} {} ({})".format(_("Error: creating client socket"), uri, e)) sys.exit(2) - # Store the arg/config - self.args = args - self.config = config - def login(self): """ Logon to the server diff --git a/glances/core/glances_main.py b/glances/core/glances_main.py index c7ef3052..89df5e87 100644 --- a/glances/core/glances_main.py +++ b/glances/core/glances_main.py @@ -67,6 +67,8 @@ class GlancesMain(object): server_ip = None # Server TCP port number (default is 61209) server_port = 61209 + # Web Server TCP port number (default is 61208) + web_server_port = 61208 # Default username/password for client/server mode username = "glances" password = "" @@ -206,6 +208,8 @@ class GlancesMain(object): args.time = 5 else: args.time = 3 + # !!! Usefull ? Default refresh time + if (args.time is not None): self.refresh_time = args.time # By default Help is hidden args.help_tag = False @@ -214,38 +218,46 @@ class GlancesMain(object): args.network_sum = False args.network_cumul = False + # Bind address/port + if (args.bind is None): + args.bind = self.bind_ip + if (args.port is None): + if (args.webserver): + args.port = self.web_server_port + else: + args.port = self.server_port + else: + args.port = int(args.port) + + # Server or client login/password + args.username = self.username + if (args.password_arg is not None): + # Password is passed as an argument + args.password = args.password_arg + elif (args.password): + # Interactive password + if (args.server): + args.password = self.__get_password( + description=_("Define the password for the Glances server"), + confirm=True) + elif (args.client): + args.password = self.__get_password( + description=_("Enter the Glances server password"), + confirm=False) + else: + # Default is no password + args.password = self.password + # !!! Change global variables regarding to user args # !!! To be refactor to use directly the args list in the code - if (args.time is not None): self.refresh_time = args.time - # self.network_bytepersec_tag = args.byte - # self.diskio_tag = args.disable_diskio - # self.fs_tag = args.disable_mount - # self.hddtemp_flag = args.disable_hddtemp - # self.network_tag = args.disable_network - # self.process_tag = args.disable_process - # self.sensors_tag = args.disable_sensors and is_Linux # and sensors_lib_tag - self.use_bold = args.no_bold - self.percpu_tag = args.percpu if (args.config is not None): self.conf_file_tag = True self.conf_file = args.config - if (args.bind is not None): self.bind_ip = args.bind if (args.client is not None): self.client_tag = True self.server_ip = args.client self.webserver_tag = args.webserver self.server_tag = args.server - if (args.port is not None): self.server_port = args.port - if (args.password_arg is not None): self.password = args.password_arg - if (args.password): - if (self.server_tag): - self.password = self.__get_password( - description=_("Define the password for the Glances server"), - confirm=True) - elif (self.client_tag): - self.password = self.__get_password( - description=_("Enter the Glances server password"), - confirm=False) if (args.output is not None): setattr(self, args.output+"_tag", True) if (args.file is not None): diff --git a/glances/core/glances_server.py b/glances/core/glances_server.py index 8eb8322e..52683d85 100644 --- a/glances/core/glances_server.py +++ b/glances/core/glances_server.py @@ -22,6 +22,8 @@ import sys import socket import json +from base64 import b64decode +from hashlib import md5 # Import Glances libs from glances.core.glances_globals import __version__ @@ -201,14 +203,13 @@ class GlancesServer(): This class creates and manages the TCP server """ - def __init__(self, bind_address="0.0.0.0", - bind_port=61209, - requestHandler=GlancesXMLRPCHandler, + def __init__(self, requestHandler=GlancesXMLRPCHandler, cached_time=1, - config=None): + config=None, + args=None): # Init the XML RPC server try: - self.server = GlancesXMLRPCServer(bind_address, bind_port, requestHandler) + self.server = GlancesXMLRPCServer(args.bind, args.port, requestHandler) except Exception as err: print(_("Error: Cannot start Glances server (%s)") % err) sys.exit(2) diff --git a/glances/core/glances_standalone.py b/glances/core/glances_standalone.py index 28a2baa9..b25b99bf 100644 --- a/glances/core/glances_standalone.py +++ b/glances/core/glances_standalone.py @@ -28,7 +28,7 @@ class GlancesStandalone(): This class creates and manages the Glances standalone session """ - def __init__(self, config=None, args=None, refresh_time=3, use_bold=True): + def __init__(self, config=None, args=None): # Init stats self.stats = GlancesStats(config) diff --git a/glances/outputs/glances_bottle.py b/glances/outputs/glances_bottle.py index ed60bada..d0d1b4de 100644 --- a/glances/outputs/glances_bottle.py +++ b/glances/outputs/glances_bottle.py @@ -47,8 +47,6 @@ class glancesBottle: self.stats = None # Init Bottle - self._host = 'localhost' - self._port = 8080 self._app = Bottle() self._route() @@ -90,7 +88,7 @@ class glancesBottle: self.stats = stats # Start the Bottle - self._app.run(host=self._host, port=self._port) + self._app.run(host=self.args.bind, port=self.args.port) def end(self): # End the Bottle diff --git a/glances/plugins/glances_alert.py b/glances/plugins/glances_alert.py index fb4bfedd..320008a5 100644 --- a/glances/plugins/glances_alert.py +++ b/glances/plugins/glances_alert.py @@ -103,7 +103,10 @@ class Plugin(GlancesPlugin): msg = "{0}".format(alert[3]) ret.append(self.curse_add_line(msg, decoration=alert[2])) # Min / Mean / Max - msg = " ({0:.1f}/{1:.1f}/{2:.1f})".format(alert[6], alert[5], alert[4]) + if (alert[6] == alert[4]): + msg = " ({0:.1f})".format(alert[5]) + else: + msg = " (Min:{0:.1f} Mean:{1:.1f} Max:{2:.1f})".format(alert[6], alert[5], alert[4]) ret.append(self.curse_add_line(msg)) # else: diff --git a/glances/plugins/glances_help.py b/glances/plugins/glances_help.py index 4bd396fb..6f0df395 100644 --- a/glances/plugins/glances_help.py +++ b/glances/plugins/glances_help.py @@ -73,12 +73,12 @@ class Plugin(GlancesPlugin): ret.append(self.curse_new_line()) msg = msg_col.format(_("a"), _("Sort processes automatically")) ret.append(self.curse_add_line(msg)) - msg = msg_col2.format(_("l"), _("Show/hide logs (alerts)")) + msg = msg_col2.format(_("b"), _("Bytes or bits for network I/O")) ret.append(self.curse_add_line(msg)) ret.append(self.curse_new_line()) msg = msg_col.format(_("c"), _("Sort processes by CPU%")) ret.append(self.curse_add_line(msg)) - msg = msg_col2.format(_("b"), _("Bytes or bits for network I/O")) + msg = msg_col2.format(_("l"), _("Show/hide logs (alerts)")) ret.append(self.curse_add_line(msg)) ret.append(self.curse_new_line()) msg = msg_col.format(_("m"), _("Sort processes by MEM%")) diff --git a/glances/plugins/glances_processlist.py b/glances/plugins/glances_processlist.py index 868c483c..c6a98b62 100644 --- a/glances/plugins/glances_processlist.py +++ b/glances/plugins/glances_processlist.py @@ -182,7 +182,10 @@ class Plugin(GlancesPlugin): ret.append(self.curse_add_line(msg, optional=True)) ret.append(self.curse_add_line(msg, optional=True)) # Command line - msg = " {0}".format(p['cmdline']) + try: + msg = " {0}".format(p['cmdline']) + except UnicodeEncodeError: + msg = "" ret.append(self.curse_add_line(msg, optional=True, splittable=True)) # Return the message with decoration