From 13e4652bf11f8c1e13dfb397608c3eca1c3f4fc4 Mon Sep 17 00:00:00 2001 From: nicolargo Date: Thu, 28 Dec 2017 11:32:12 +0100 Subject: [PATCH] Add support for the --modules-list --- glances/exports/glances_csv.py | 2 +- glances/exports/graph.py | 4 +-- glances/main.py | 4 +++ glances/outputs/glances_bottle.py | 2 +- glances/outputs/glances_curses.py | 2 +- glances/server.py | 2 +- glances/standalone.py | 14 ++++++++ glances/stats.py | 53 +++++++++++++++++++++---------- 8 files changed, 61 insertions(+), 22 deletions(-) diff --git a/glances/exports/glances_csv.py b/glances/exports/glances_csv.py index 2348881f..a412e550 100644 --- a/glances/exports/glances_csv.py +++ b/glances/exports/glances_csv.py @@ -65,7 +65,7 @@ class Export(GlancesExport): """Update stats in the CSV output file.""" # Get the stats all_stats = stats.getAllExports() - plugins = stats.getAllPlugins() + plugins = stats.getPluginsList() # Init data with timestamp (issue#708) if self.first_line: diff --git a/glances/exports/graph.py b/glances/exports/graph.py index baa3401a..b6e31ddf 100644 --- a/glances/exports/graph.py +++ b/glances/exports/graph.py @@ -54,7 +54,7 @@ class GlancesGraph(object): """Reset all the history.""" if not self.graph_enabled(): return False - for p in stats.getAllPlugins(): + for p in stats.getPluginsList(): h = stats.get_plugin(p).get_stats_history() if h is not None: stats.get_plugin(p).reset_stats_history() @@ -94,7 +94,7 @@ class GlancesGraph(object): return 0 index_all = 0 - for p in stats.getAllPlugins(): + for p in stats.getPluginsList(): # History h = stats.get_plugin(p).get_export_history() # Current plugin item history list diff --git a/glances/main.py b/glances/main.py index e37e8927..dd6abc8e 100644 --- a/glances/main.py +++ b/glances/main.py @@ -114,6 +114,10 @@ Examples of use: parser.add_argument('-C', '--config', dest='conf_file', help='path to the configuration file') # Disable plugin + parser.add_argument('--modules-list', '--module-list', + action='store_true', default=False, + dest='modules_list', + help='display modules (plugins & exports) list and exit') parser.add_argument('--disable-plugin', dest='disable_plugin', help='disable plugin (comma separed list)') parser.add_argument('--disable-process', action='store_true', default=False, diff --git a/glances/outputs/glances_bottle.py b/glances/outputs/glances_bottle.py index 2746d0e4..1c691a6a 100644 --- a/glances/outputs/glances_bottle.py +++ b/glances/outputs/glances_bottle.py @@ -168,7 +168,7 @@ class GlancesBottle(object): self.stats = stats # Init plugin list - self.plugins_list = self.stats.getAllPlugins() + self.plugins_list = self.stats.getPluginsList() # Bind the Bottle TCP address/port if self.args.open_web_browser: diff --git a/glances/outputs/glances_curses.py b/glances/outputs/glances_curses.py index 0d3f2d15..48ecff62 100644 --- a/glances/outputs/glances_curses.py +++ b/glances/outputs/glances_curses.py @@ -494,7 +494,7 @@ class _GlancesCurses(object): """ ret = {} - for p in stats.getAllPlugins(enable=False): + for p in stats.getPluginsList(enable=False): if p == 'quicklook' or p == 'processlist': # processlist is done later # because we need to know how many processes could be displayed diff --git a/glances/server.py b/glances/server.py index 5c5036e9..83fa0aa7 100644 --- a/glances/server.py +++ b/glances/server.py @@ -162,7 +162,7 @@ class GlancesInstance(object): def getAllPlugins(self): # Return the plugins list - return json.dumps(self.stats.getAllPlugins()) + return json.dumps(self.stats.getPluginsList()) def getAllLimits(self): # Return all the plugins limits diff --git a/glances/standalone.py b/glances/standalone.py index 52121157..354e8217 100644 --- a/glances/standalone.py +++ b/glances/standalone.py @@ -19,6 +19,7 @@ """Manage the Glances standalone session.""" +import sys from glances.globals import WINDOWS from glances.logger import logger @@ -41,6 +42,12 @@ class GlancesStandalone(object): # Init stats self.stats = GlancesStats(config=config, args=args) + # Modules (plugins and exporters) are loaded at this point + # Glances can display the list if asked... + if args.modules_list: + self.display_modules_list() + sys.exit(0) + # If process extended stats is disabled by user if not args.enable_process_extended: logger.debug("Extended stats for top process are disabled") @@ -85,6 +92,13 @@ class GlancesStandalone(object): def quiet(self): return self._quiet + def display_modules_list(self): + """Display modules list""" + print("Plugins list: {}".format( + ', '.join(sorted(self.stats.getPluginsList(enable=False))))) + print("Exporters list: {}".format( + ', '.join(sorted(self.stats.getExportsList(enable=False))))) + def __serve_forever(self): """Main loop for the CLI.""" # Start a counter used to compute the time needed for diff --git a/glances/stats.py b/glances/stats.py index 28e9287d..e88cd815 100644 --- a/glances/stats.py +++ b/glances/stats.py @@ -87,14 +87,16 @@ class GlancesStats(object): """Wrapper to load: plugins and export modules.""" # Init the plugins dict + # Active plugins dictionnary self._plugins = collections.defaultdict(dict) - # Load the plugins self.load_plugins(args=args) # Init the export modules dict + # Active exporters dictionnary self._exports = collections.defaultdict(dict) - + # All available exporters dictionnary + self._exports_all = collections.defaultdict(dict) # Load the export modules self.load_exports(args=args) @@ -102,7 +104,7 @@ class GlancesStats(object): sys.path = sys_path def _load_plugin(self, plugin_script, args=None, config=None): - """Load the plugin (script), init it and add to the _plugin dict""" + """Load the plugin (script), init it and add to the _plugin dict.""" # The key is the plugin name # for example, the file glances_xxx.py # generate self._plugins_list["xxx"] = ... @@ -136,23 +138,26 @@ class GlancesStats(object): args=args, config=self.config) # Log plugins list - logger.debug("Available plugins list: {}".format(self.getAllPlugins())) + logger.debug("Active plugins list: {}".format(self.getPluginsList())) def load_exports(self, args=None): """Load all export modules in the 'exports' folder.""" if args is None: return False header = "glances_" - # Transform the arguments list into a dict - # The aim is to chec if the export module should be loaded + # Build the export module available list args_var = vars(locals()['args']) for item in os.listdir(exports_path): export_name = os.path.basename(item)[len(header):-3].lower() if (item.startswith(header) and item.endswith(".py") and item != (header + "export.py") and - item != (header + "history.py") and - args_var['export_' + export_name] is not None and + item != (header + "history.py")): + self._exports_all[export_name] = None + + # Aim is to check if the export module should be loaded + for export_name in self._exports_all: + if (args_var['export_' + export_name] is not None and args_var['export_' + export_name] is not False): # Import the export module export_module = __import__(os.path.basename(item)[:-3]) @@ -160,22 +165,38 @@ class GlancesStats(object): # The key is the module name # for example, the file glances_xxx.py # generate self._exports_list["xxx"] = ... - self._exports[export_name] = export_module.Export(args=args, config=self.config) + self._exports[export_name] = export_module.Export(args=args, + config=self.config) + self._exports_all[export_name] = self._exports[export_name] # Log plugins list - logger.debug("Available exports modules list: {}".format(self.getExportList())) + logger.debug("Active exports modules list: {}".format(self.getExportsList())) return True - def getAllPlugins(self, enable=True): - """Return the enable plugins list. - if enable is False, return the list of all the plugins""" + def getPluginsList(self, enable=True): + """Return the plugins list. + + if enable is True, only return the active plugins (default) + if enable is False, return all the plugins + + Return: list of plugin name + """ if enable: return [p for p in self._plugins if self._plugins[p].is_enable()] else: return [p for p in self._plugins] - def getExportList(self): - """Return the exports modules list.""" - return [e for e in self._exports] + def getExportsList(self, enable=True): + """Return the export module list. + + if enable is True, only return the active exporters (default) + if enable is False, return all the exporters + + Return: list of export module name + """ + if enable: + return [e for e in self._exports] + else: + return [e for e in self._exports_all] def load_limits(self, config=None): """Load the stats limits (except the one in the exclude list)."""