diff options
Diffstat (limited to 'glances/plugins/glances_alert.py')
-rw-r--r-- | glances/plugins/glances_alert.py | 76 |
1 files changed, 64 insertions, 12 deletions
diff --git a/glances/plugins/glances_alert.py b/glances/plugins/glances_alert.py index 16a88b88..1bafda9e 100644 --- a/glances/plugins/glances_alert.py +++ b/glances/plugins/glances_alert.py @@ -22,8 +22,58 @@ from datetime import datetime from glances.logs import glances_logs +from glances.thresholds import glances_thresholds +from glances.logger import logger from glances.plugins.glances_plugin import GlancesPlugin +# Static decision tree for the global alert message +# - msg: Message to be displayed (result of the decision tree) +# - threasholds: a list of stats to take into account +# - thresholds_min: minimal value of the threasholds sum +tree = [{'msg': 'No warning or critical alert detected', + 'thresholds': [], + 'thresholds_min': 0}, + {'msg': 'High CPU user mode', + 'thresholds': ['cpu_user'], + 'thresholds_min': 2}, + {'msg': 'High CPU kernel usage', + 'thresholds': ['cpu_system'], + 'thresholds_min': 2}, + {'msg': 'High CPU I/O waiting', + 'thresholds': ['cpu_iowait'], + 'thresholds_min': 2}, + {'msg': 'Large CPU stolen time. System running the hypervisor is too busy.', + 'thresholds': ['cpu_steal'], + 'thresholds_min': 2}, + {'msg': 'High CPU niced value', + 'thresholds': ['cpu_niced'], + 'thresholds_min': 2}, + {'msg': 'System overloaded in the last 5 minutes', + 'thresholds': ['load'], + 'thresholds_min': 2}, + {'msg': 'High swap (paging) usage', + 'thresholds': ['memswap'], + 'thresholds_min': 2}, + {'msg': 'High memory consumption', + 'thresholds': ['mem'], + 'thresholds_min': 2}, + ] + + +def global_message(): + """Parse the decision tree and return the message + corresponding to the current threasholds values""" + # Compute the weight for each item in the tree + current_thresholds = glances_thresholds.get() + for i in tree: + i['weight'] = sum([current_thresholds[t].value() for t in i['thresholds'] if t in current_thresholds]) + themax = max(tree, key=lambda d: d['weight']) + if themax['weight'] >= themax['thresholds_min']: + # Check if the weight is > to the minimal threashold value + return themax['msg'] + else: + return tree[0]['msg'] + class Plugin(GlancesPlugin): @@ -53,6 +103,10 @@ class Plugin(GlancesPlugin): """Nothing to do here. Just return the global glances_log.""" # Set the stats to the glances_logs self.stats = glances_logs.get() + # Define the global message thanks to the current thresholds + # and the decision tree + # !!! Call directly in the msg_curse function + # global_message() def msg_curse(self, args=None): """Return the dict to display in the curse interface.""" @@ -65,19 +119,17 @@ class Plugin(GlancesPlugin): # Build the string message # Header - if not self.stats: - msg = 'No warning or critical alert detected' - ret.append(self.curse_add_line(msg, "TITLE")) - else: + ret.append(self.curse_add_line(global_message(), "TITLE")) + if self.stats: # Header - msg = 'Warning or critical alerts' - ret.append(self.curse_add_line(msg, "TITLE")) - logs_len = glances_logs.len() - if logs_len > 1: - msg = ' (last {} entries)'.format(logs_len) - else: - msg = ' (one entry)' - ret.append(self.curse_add_line(msg, "TITLE")) + # msg = 'Warning or critical alerts' + # ret.append(self.curse_add_line(msg, "TITLE")) + # logs_len = glances_logs.len() + # if logs_len > 1: + # msg = ' (last {} entries)'.format(logs_len) + # else: + # msg = ' (one entry)' + # ret.append(self.curse_add_line(msg, "TITLE")) # Loop over alerts for alert in self.stats: # New line |