diff options
author | nicolargo <nicolas@nicolargo.com> | 2023-05-19 21:22:31 +0200 |
---|---|---|
committer | nicolargo <nicolas@nicolargo.com> | 2023-05-19 21:22:31 +0200 |
commit | 8d5d993d4588533acf00e8831ee68d79409d5772 (patch) | |
tree | fd0f5cbac9f70c9c8fa91a4c0e88ddffc083a36f | |
parent | d3077ffba9a485aff7c50557ad6a9affdeeb4b12 (diff) |
Refactor core and load plugins
-rw-r--r-- | glances/plugins/core/model.py | 33 | ||||
-rw-r--r-- | glances/plugins/load/model.py | 15 | ||||
-rw-r--r-- | glances/plugins/mem/model.py | 2 | ||||
-rw-r--r-- | glances/plugins/plugin/model.py | 23 |
4 files changed, 50 insertions, 23 deletions
diff --git a/glances/plugins/core/model.py b/glances/plugins/core/model.py index c4a8816b..579817f3 100644 --- a/glances/plugins/core/model.py +++ b/glances/plugins/core/model.py @@ -15,8 +15,13 @@ import psutil # Fields description fields_description = { - 'phys': {'description': 'Number of physical cores (hyper thread CPUs are excluded).', 'unit': 'number'}, + 'phys': { + 'getter': 'self.cpu_count', + 'description': 'Number of physical cores (hyper thread CPUs are excluded).', + 'unit': 'number' + }, 'log': { + 'getter': 'self.cpu_count', 'description': 'Number of logical CPUs. A logical CPU is the number of \ physical cores multiplied by the number of threads that can run on each core.', 'unit': 'number', @@ -40,6 +45,20 @@ class PluginModel(GlancesPluginModel): # The core number is displayed by the load plugin self.display_curse = False + def cpu_count(self): + """Return the CPU core number.""" + stats = self.get_init_value() + try: + # The psutil 2.0 include psutil.cpu_count() and psutil.cpu_count(logical=False) + # Return a dict with: + # - phys: physical cores only (hyper thread CPUs are excluded) + # - log: logical CPUs in the system + stats['phys'] = psutil.cpu_count(logical=False) + stats['log'] = psutil.cpu_count() + except NameError: + pass + return stats + # Do *NOT* uncomment the following line # @GlancesPluginModel._check_decorator @GlancesPluginModel._log_result_decorator @@ -53,17 +72,7 @@ class PluginModel(GlancesPluginModel): if self.input_method == 'local': # Update stats using the standard system lib - - # The psutil 2.0 include psutil.cpu_count() and psutil.cpu_count(logical=False) - # Return a dict with: - # - phys: physical cores only (hyper thread CPUs are excluded) - # - log: logical CPUs in the system - # Return None if undefined - try: - stats["phys"] = psutil.cpu_count(logical=False) - stats["log"] = psutil.cpu_count() - except NameError: - self.reset() + stats = self.update_local(stats) elif self.input_method == 'snmp': # Update stats using SNMP diff --git a/glances/plugins/load/model.py b/glances/plugins/load/model.py index c803b202..4ae7bb25 100644 --- a/glances/plugins/load/model.py +++ b/glances/plugins/load/model.py @@ -37,7 +37,11 @@ waiting in the run-queue plus the number currently executing \ over 15 minutes.', 'unit': 'float', }, - 'cpucore': {'description': 'Total number of CPU core.', 'unit': 'number'}, + 'cpucore': { + 'description': + 'Total number of CPU core.', + 'unit': 'number' + }, } # SNMP OID @@ -68,7 +72,10 @@ class PluginModel(GlancesPluginModel): def __init__(self, args=None, config=None): """Init the plugin.""" super(PluginModel, self).__init__( - args=args, config=config, items_history_list=items_history_list, fields_description=fields_description + args=args, + config=config, + items_history_list=items_history_list, + fields_description=fields_description ) # We want to display the stat in the curse interface @@ -104,9 +111,7 @@ class PluginModel(GlancesPluginModel): # Get the load using the os standard lib load = self._getloadavg() - if load is None: - stats = self.get_init_value() - else: + if load: stats = {'min1': load[0], 'min5': load[1], 'min15': load[2], 'cpucore': self.nb_log_core} elif self.input_method == 'snmp': diff --git a/glances/plugins/mem/model.py b/glances/plugins/mem/model.py index c3d6b5c5..5d5684bb 100644 --- a/glances/plugins/mem/model.py +++ b/glances/plugins/mem/model.py @@ -2,7 +2,7 @@ # # This file is part of Glances. # -# SPDX-FileCopyrightText: 2022 Nicolas Hennion <nicolas@nicolargo.com> +# SPDX-FileCopyrightText: 2023 Nicolas Hennion <nicolas@nicolargo.com> # # SPDX-License-Identifier: LGPL-3.0-only # diff --git a/glances/plugins/plugin/model.py b/glances/plugins/plugin/model.py index 3ba2e5fe..ba33acff 100644 --- a/glances/plugins/plugin/model.py +++ b/glances/plugins/plugin/model.py @@ -2,7 +2,7 @@ # # This file is part of Glances. # -# SPDX-FileCopyrightText: 2022 Nicolas Hennion <nicolas@nicolargo.com> +# SPDX-FileCopyrightText: 2023 Nicolas Hennion <nicolas@nicolargo.com> # # SPDX-License-Identifier: LGPL-3.0-only # @@ -114,13 +114,15 @@ class GlancesPluginModel(object): # Init stats description self.fields_description = fields_description if fields_description: - # Return a list of getter (external functions to run to get the stats) + # Return a list of getter (internal or external functions to run to get the stats) + # Note: It should return a dict self.getters = list(set([fields_description[i]['getter'] for i in fields_description if (i in fields_description and 'getter' in fields_description[i] and fields_description[i]['getter'] not in ('compute'))])) # Return a list of internal functions to run to compute the stats # Computation is done after the getters + # Note It could return what ever you want self.computes = [i for i in fields_description if (i in fields_description and 'getter' in fields_description[i] and @@ -196,11 +198,22 @@ class GlancesPluginModel(object): """Return the stats updated from the getters functions.""" for g in self.getters: # For each "getter", the Python function is called - g_call = getattr(globals()[g.split('.')[0]], g.split('.')[1])() + g_lib = g.split('.')[0] + g_func = g.split('.')[1] + if g_lib == 'self': + g_call = getattr(self, g_func)() + else: + g_call = getattr(globals()[g_lib], g_func)() # The result is stored in the stats dict # (only if the field is in the self.fields_description) - for name in [f for f in g_call._fields if f in self.fields_description]: - stats[name] = getattr(g_call, name) + if hasattr(g_call, '_fields'): + # It's a psutil object + for name in [f for f in g_call._fields if f in self.fields_description]: + stats[name] = getattr(g_call, name) + else: + # It's a Dict object + for name in [f for f in g_call if f in self.fields_description]: + stats[name] = g_call.get(name, None) return stats def update_computes(self, stats): |