From 12d9ac1dff05ac74ba304cbf9186aa159fde1d61 Mon Sep 17 00:00:00 2001 From: nicolargo Date: Sun, 31 Mar 2024 12:21:01 +0200 Subject: First try, stdout JSON work ok but not the CSV export --- conf/glances.conf | 6 ++++++ glances/exports/export.py | 2 +- glances/exports/glances_csv/__init__.py | 4 +++- glances/filter.py | 4 +++- glances/plugins/plugin/model.py | 5 ++++- glances/plugins/processlist/__init__.py | 15 +++++++++++++-- glances/processes.py | 32 ++++++++++++++++++++++++++++---- glances/stats.py | 4 ++-- 8 files changed, 60 insertions(+), 12 deletions(-) diff --git a/conf/glances.conf b/conf/glances.conf index a9986280..bfdd9c4a 100644 --- a/conf/glances.conf +++ b/conf/glances.conf @@ -357,6 +357,12 @@ nice_warning=-20,-19,-18,-17,-16,-15,-14,-13,-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2 #nice_careful=1,2,3,4,5,6,7,8,9 #nice_warning=10,11,12,13,14 #nice_critical=15,16,17,18,19 +# +# Define the list of processes to export using: +# a comma-separated list of regular expression (apply on name and cmdline) +export=.*firefox.*,.*python.* +# or an uniq key:value filter +#export=pid:1234 [ports] disable=False diff --git a/glances/exports/export.py b/glances/exports/export.py index dbcabe62..8522f9dd 100644 --- a/glances/exports/export.py +++ b/glances/exports/export.py @@ -33,7 +33,7 @@ class GlancesExport(object): 'now', 'plugin', 'ports', - 'processlist', + # 'processlist', 'psutilversion', 'quicklook', 'version', diff --git a/glances/exports/glances_csv/__init__.py b/glances/exports/glances_csv/__init__.py index 88e0615a..82497d47 100644 --- a/glances/exports/glances_csv/__init__.py +++ b/glances/exports/glances_csv/__init__.py @@ -69,7 +69,9 @@ class Export(GlancesExport): self.csv_file.close() def update(self, stats): - """Update stats in the CSV output file.""" + """Update stats in the CSV output file. + This class overwrite the one in the parent class. + """ # Get the stats all_stats = stats.getAllExportsAsDict(plugin_list=self.plugins_to_export(stats)) diff --git a/glances/filter.py b/glances/filter.py index 55950060..449fa619 100644 --- a/glances/filter.py +++ b/glances/filter.py @@ -79,7 +79,9 @@ class GlancesFilter(object): self._filter_re = None if self.filter is not None: - logger.info("Set filter to {} on key {}".format(self.filter, self.filter_key)) + logger.info("Set filter to {} on {}".format( + self.filter, + self.filter_key if self.filter_key else 'name or cmdline')) # Compute the regular expression try: self._filter_re = re.compile(self.filter) diff --git a/glances/plugins/plugin/model.py b/glances/plugins/plugin/model.py index 8e3a4643..d6b84e09 100644 --- a/glances/plugins/plugin/model.py +++ b/glances/plugins/plugin/model.py @@ -392,7 +392,10 @@ class GlancesPluginModel(object): return self.stats def get_export(self): - """Return the stats object to export.""" + """Return the stats object to export. + By default, return the raw stats. + Note: this method could be overwritten by the plugin if a specific format is needed (ex: processlist) + """ return self.get_raw() def get_stats(self): diff --git a/glances/plugins/processlist/__init__.py b/glances/plugins/processlist/__init__.py index d780af02..17c4a041 100644 --- a/glances/plugins/processlist/__init__.py +++ b/glances/plugins/processlist/__init__.py @@ -179,14 +179,18 @@ class PluginModel(GlancesPluginModel): self.pid_max = glances_processes.pid_max # Set the default sort key if it is defined in the configuration file - if config is not None: - if 'processlist' in config.as_dict() and 'sort_key' in config.as_dict()['processlist']: + if config is not None and 'processlist' in config.as_dict(): + if 'sort_key' in config.as_dict()['processlist']: logger.debug( 'Configuration overwrites processes sort key by {}'.format( config.as_dict()['processlist']['sort_key'] ) ) glances_processes.set_sort_key(config.as_dict()['processlist']['sort_key'], False) + if 'export' in config.as_dict()['processlist']: + logger.debug('Processlist will export processes matching following regexp: {}'.format( + config.as_dict()['processlist']['export'])) + glances_processes.set_export(config.as_dict()['processlist']['export']) # The default sort key could also be overwrite by command line (see #1903) if args.sort_processes_key is not None: @@ -226,6 +230,13 @@ class PluginModel(GlancesPluginModel): return stats + def get_export(self): + """Return the processes list to export. + Not all the processeses are exported. + Only the one defined in the Glances configuration file (see #794 for details). + """ + return glances_processes.get_export() + def get_nice_alert(self, value): """Return the alert relative to the Nice configuration list""" value = str(value) diff --git a/glances/processes.py b/glances/processes.py index ec8962af..1575e81a 100644 --- a/glances/processes.py +++ b/glances/processes.py @@ -69,6 +69,10 @@ class GlancesProcesses(object): # Cache is a dict with key=pid and value = dict of cached value self.processlist_cache = {} + # List of processes stats to export (filtred by the _filter_export) + self._filter_export = GlancesFilter() + self.processlist_export = [] + # Tag to enable/disable the processes stats (to reduce the Glances CPU consumption) # Default is to enable the processes stats self.disable_tag = False @@ -119,6 +123,10 @@ class GlancesProcesses(object): """Set args.""" self.args = args + def set_export(self, export): + """Set the process export list of regexp.""" + self._filter_export.filter = '|'.join(export.split(',')) + def reset_processcount(self): """Reset the global process count""" self.processcount = {'total': 0, 'running': 0, 'sleeping': 0, 'thread': 0, 'pid_max': None} @@ -482,11 +490,11 @@ class GlancesProcesses(object): except KeyError: pass - # Apply user filter - processlist = list(filter(lambda p: not self._filter.is_filtered(p), processlist)) + # Filter and transform process list + processlist = self.update_export_list(processlist) - # Save the new processlist and transform all namedtuples to dict - processlist = list_of_namedtuple_to_list_of_dict(processlist) + # Filter and transform process export list + self.processlist_export = self.update_export_list(processlist) # Compute the maximum value for keys in self._max_values_list: CPU, MEM # Useful to highlight the processes with maximum values @@ -500,6 +508,18 @@ class GlancesProcesses(object): return self.processlist + def update_list(self, processlist): + """Return the process list after filtering and transformation (namedtuple to dict).""" + ret = list(filter(lambda p: not self._filter.is_filtered(p), processlist)) + return list_of_namedtuple_to_list_of_dict(ret) + + def update_export_list(self, processlist): + """Return the process export list after filtering and transformation (namedtuple to dict).""" + if self._filter_export.filter is None: + return [] + ret = list(filter(lambda p: not self._filter_export.is_filtered(p), processlist)) + return list_of_namedtuple_to_list_of_dict(ret) + def get_count(self): """Get the number of processes.""" return self.processcount @@ -513,6 +533,10 @@ class GlancesProcesses(object): else: return self.processlist + def get_export(self): + """Return the processlist for export.""" + return self.processlist_export + @property def sort_key(self): """Get the current sort key.""" diff --git a/glances/stats.py b/glances/stats.py index 3929f337..9b875a63 100644 --- a/glances/stats.py +++ b/glances/stats.py @@ -315,10 +315,10 @@ class GlancesStats(object): return [self._plugins[p].get_export() for p in self._plugins] def getAllExportsAsDict(self, plugin_list=None): - """Return all the stats to be exported (list). + """Return all the stats to be exported (dict). Default behavior is to export all the stat - if plugin_list is provided, only export stats of given plugin (list) + if plugin_list is provided (list), only export stats of given plugins """ if plugin_list is None: # All enabled plugins should be exported -- cgit v1.2.3