summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornicolargo <nicolas@nicolargo.com>2023-05-20 12:03:38 +0200
committernicolargo <nicolas@nicolargo.com>2023-05-20 12:03:38 +0200
commit94daf17e904cc8925b674eb0a9d7d1a28210aadc (patch)
tree9ee808e615495e033ab48df7b89dce50558e5bb0
parente98cf3c0a4a8fa999e168facf71f213734611d12 (diff)
CPU ok, whith gauge management
-rw-r--r--glances/plugins/cpu/model.py31
-rw-r--r--glances/plugins/plugin/model.py31
2 files changed, 42 insertions, 20 deletions
diff --git a/glances/plugins/cpu/model.py b/glances/plugins/cpu/model.py
index b8fc2734..b15ff08d 100644
--- a/glances/plugins/cpu/model.py
+++ b/glances/plugins/cpu/model.py
@@ -115,8 +115,16 @@ another while ensuring that the tasks do not conflict.',
'min_symbol': 'K',
'short_name': 'sys_call',
},
- 'cpucore': {'description': 'Total number of CPU core.', 'unit': 'number'},
- 'time_since_update': {'description': 'Number of seconds since last update.', 'unit': 'seconds'},
+ 'cpucore': {
+ # 'getter': 'compute',
+ 'description': 'Total number of CPU core.',
+ 'unit': 'number'
+ },
+ 'time_since_update': {
+ 'getter': 'compute',
+ 'description': 'Number of seconds since last update.',
+ 'unit': 'seconds'
+ },
}
# SNMP OID
@@ -232,26 +240,11 @@ class PluginModel(GlancesPluginModel):
# By storing time data we enable Rx/s and Tx/s calculations in the
# XML/RPC API, which would otherwise be overly difficult work
# for users of the API
- stats['time_since_update'] = getTimeSinceLastUpdate('cpu')
+ # stats['time_since_update'] = getTimeSinceLastUpdate('cpu')
# Core number is needed to compute the CTX switch limit
stats['cpucore'] = self.nb_log_core
- # # Previous CPU stats are stored in the cpu_stats_old variable
- # if not hasattr(self, 'stats_old'):
- # # Init the stats (needed to have the key name for export)
- # for stat in stats:
- # # @TODO: better to set it to None but should refactor views and UI...
- # stats[stat] = 0
- # else:
- # # Others calls...
- # for stat in stats:
- # if stat in stats:
- # stats[stat] = stats[stat] - self.stats_old[stat]
-
- # # Save stats to compute next step
- # self.stats_old = stats
-
return stats
def update_cpu_snmp(self):
@@ -316,7 +309,7 @@ class PluginModel(GlancesPluginModel):
self.views[key]['decoration'] = self.get_alert(self.stats[key], header=key)
# Alert only but depend on Core number
for key in ['ctx_switches']:
- if key in self.stats:
+ if key in self.stats and key + '_gauge' in self.stats:
self.views[key]['decoration'] = self.get_alert(
self.stats[key], maximum=100 * self.stats['cpucore'], header=key
)
diff --git a/glances/plugins/plugin/model.py b/glances/plugins/plugin/model.py
index 30013a55..a3c58757 100644
--- a/glances/plugins/plugin/model.py
+++ b/glances/plugins/plugin/model.py
@@ -135,6 +135,7 @@ class GlancesPluginModel(object):
# Init the stats
self.stats_init_value = stats_init_value
self.stats = None
+ self.stats_old = None
self.reset()
def __repr__(self):
@@ -227,6 +228,23 @@ class GlancesPluginModel(object):
stats[c] = getattr(self, c)(stats)
return stats
+ def update_gauges(self, stats):
+ """Manage gauge (field with rate=True).
+ Create a new field with the gauge value (cumulative).
+ Set the field to stats_gauge current - old (to compute the rate)
+ """
+ if self.stats_old is None:
+ # First call
+ return stats
+ for c in self.fields_description:
+ if 'rate' in self.fields_description[c] and self.fields_description[c]:
+ stats[c + '_gauge'] = stats[c]
+ if c + '_gauge' in self.stats_old:
+ stats[c] = stats[c] - self.stats_old[c + '_gauge']
+ else:
+ stats[c] = 0
+ return stats
+
def time_since_update(self, stats):
"""Memorized the time since last update."""
return getTimeSinceLastUpdate(self.plugin_name)
@@ -235,8 +253,16 @@ class GlancesPluginModel(object):
"""Return the stats updated by getters and computes."""
# Update the stats from the getters
stats = self.update_getters(stats)
+
# Update the stats from the computes
stats = self.update_computes(stats)
+
+ # Update gauge (if rate=True)
+ stats = self.update_gauges(stats)
+
+ # Record current stats
+ self.stats_old = stats
+
return stats
def update_stats_history(self):
@@ -1052,7 +1078,10 @@ class GlancesPluginModel(object):
'rate' in self.fields_description[key] and
self.fields_description[key]['rate'] is True
):
- value = self.stats[key] // self.stats['time_since_update']
+ if key + '_gauge' in self.stats:
+ value = self.stats[key] // self.stats['time_since_update']
+ else:
+ value = 0
else:
value = self.stats[key]