diff options
author | nicolargo <nicolas@nicolargo.com> | 2023-05-19 17:53:55 +0200 |
---|---|---|
committer | nicolargo <nicolas@nicolargo.com> | 2023-05-19 17:53:55 +0200 |
commit | d3077ffba9a485aff7c50557ad6a9affdeeb4b12 (patch) | |
tree | ef8c421c88c2728aa15847ad76c9c43723d5107d | |
parent | a549b08107f600a73e67589afc80bad3c0f01a0d (diff) |
Done form memswap
-rw-r--r-- | glances/plugins/memswap/model.py | 69 | ||||
-rw-r--r-- | glances/plugins/plugin/model.py | 7 |
2 files changed, 48 insertions, 28 deletions
diff --git a/glances/plugins/memswap/model.py b/glances/plugins/memswap/model.py index 13cc88e1..eb04314e 100644 --- a/glances/plugins/memswap/model.py +++ b/glances/plugins/memswap/model.py @@ -10,28 +10,52 @@ """Swap memory plugin.""" from glances.globals import iterkeys -from glances.timer import getTimeSinceLastUpdate from glances.plugins.plugin.model import GlancesPluginModel import psutil # Fields description fields_description = { - 'total': {'description': 'Total swap memory.', 'unit': 'bytes', 'min_symbol': 'K'}, - 'used': {'description': 'Used swap memory.', 'unit': 'bytes', 'min_symbol': 'K'}, - 'free': {'description': 'Free swap memory.', 'unit': 'bytes', 'min_symbol': 'K'}, - 'percent': {'description': 'Used swap memory in percentage.', 'unit': 'percent'}, + 'total': { + 'getter': 'psutil.swap_memory', + 'description': 'Total swap memory.', + 'unit': 'bytes', + 'min_symbol': 'K' + }, + 'used': { + 'getter': 'psutil.swap_memory', + 'description': 'Used swap memory.', + 'unit': 'bytes', + 'min_symbol': 'K' + }, + 'free': { + 'getter': 'psutil.swap_memory', + 'description': 'Free swap memory.', + 'unit': 'bytes', + 'min_symbol': 'K' + }, + 'percent': { + 'getter': 'psutil.swap_memory', + 'description': 'Used swap memory in percentage.', + 'unit': 'percent' + }, 'sin': { + 'getter': 'psutil.swap_memory', 'description': 'The number of bytes the system has swapped in from disk (cumulative).', 'unit': 'bytes', 'min_symbol': 'K', }, 'sout': { + 'getter': 'psutil.swap_memory', 'description': 'The number of bytes the system has swapped out from disk (cumulative).', 'unit': 'bytes', 'min_symbol': 'K', }, - 'time_since_update': {'description': 'Number of seconds since last update.', 'unit': 'seconds'}, + 'time_since_update': { + 'getter': 'compute', + 'description': 'Number of seconds since last update.', + 'unit': 'seconds' + }, } # SNMP OID @@ -61,7 +85,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 @@ -75,29 +102,19 @@ class PluginModel(GlancesPluginModel): stats = self.get_init_value() if self.input_method == 'local': - # Update stats using the standard system lib - # Grab SWAP using the psutil swap_memory method + # Update stats using the standard PsUtil system lib + # Get all the swap stats (copy/paste of the psutil documentation) + # total: total swap memory in bytes + # used: used swap memory in bytes + # free: free swap memory in bytes + # percent: the percentage usage + # sin: the number of bytes the system has swapped in from disk (cumulative) + # sout: the number of bytes the system has swapped out from disk (cumulative) try: - sm_stats = psutil.swap_memory() + stats = self.update_local(stats) except RuntimeError: # Crash on startup on Illumos when no swap is configured #1767 pass - else: - # Get all the swap stats (copy/paste of the psutil documentation) - # total: total swap memory in bytes - # used: used swap memory in bytes - # free: free swap memory in bytes - # percent: the percentage usage - # sin: the number of bytes the system has swapped in from disk (cumulative) - # sout: the number of bytes the system has swapped out from disk (cumulative) - for swap in ['total', 'used', 'free', 'percent', 'sin', 'sout']: - if hasattr(sm_stats, swap): - stats[swap] = getattr(sm_stats, swap) - - # By storing time data we enable sin/s and sout/s calculations in the - # XML/RPC API, which would otherwise be overly difficult work - # for users of the API - stats['time_since_update'] = getTimeSinceLastUpdate('memswap') elif self.input_method == 'snmp': # Update stats using SNMP if self.short_system_name == 'windows': diff --git a/glances/plugins/plugin/model.py b/glances/plugins/plugin/model.py index af6993b5..3ba2e5fe 100644 --- a/glances/plugins/plugin/model.py +++ b/glances/plugins/plugin/model.py @@ -25,10 +25,9 @@ from glances.history import GlancesHistory from glances.logger import logger from glances.events import glances_events from glances.thresholds import glances_thresholds -from glances.timer import Counter, Timer +from glances.timer import Counter, Timer, getTimeSinceLastUpdate from glances.outputs.glances_unicode import unicode_message - fields_unit_short = {'percent': '%'} fields_unit_type = { @@ -210,6 +209,10 @@ class GlancesPluginModel(object): stats[c] = getattr(self, c)(stats) return stats + def time_since_update(self, stats): + """Memorized the time since last update.""" + return getTimeSinceLastUpdate(self.plugin_name) + def update_local(self, stats): """Return the stats updated by getters and computes.""" # Update the stats from the getters |