diff --git a/glances/core/glances_actions.py b/glances/core/glances_actions.py index 9ee947b2..0624fd13 100644 --- a/glances/core/glances_actions.py +++ b/glances/core/glances_actions.py @@ -30,11 +30,44 @@ class GlancesActions(object): """This class manage action if an alert is reached""" - def run(self, commands): - """Run the commands (in background) - - commands: a list of command line""" + def __init__(self): + """Init GlancesActions class""" + # Dict with the criticity status + # - key: stat_name + # - value: criticity + # Goal: avoid to execute the same command twice + self.status = {} + + def get(self, stat_name): + """Get the stat_name criticity""" + try: + return self.status[stat_name] + except KeyError: + return None + + def set(self, stat_name, criticity): + """Set the stat_name to criticity""" + self.status[stat_name] = criticity + + def run(self, stat_name, criticity, commands): + """Run the commands (in background) + - stats_name: plugin_name (+ header) + - criticity: criticity of the trigger + - commands: a list of command line + + Return True if the commands have been ran""" + + if self.get(stat_name) == criticity: + # Action already executed => Exit + return False + + # Ran all actions in background for cmd in commands: - logger.info("Action triggered: {0}".format(cmd)) + logger.info("Action triggered for {0} ({1}): {2}".format(stat_name, criticity, cmd)) splitted_cmd = cmd.split() Popen(splitted_cmd) + + self.set(stat_name, criticity) + + return True diff --git a/glances/plugins/glances_plugin.py b/glances/plugins/glances_plugin.py index 9954cf20..ecc2d4ca 100644 --- a/glances/plugins/glances_plugin.py +++ b/glances/plugins/glances_plugin.py @@ -316,27 +316,29 @@ class GlancesPlugin(object): except KeyError: return 'DEFAULT' - # Init the return post string - log_str = "" + # Get the stat_name = plugin_name (+ header) + if header == "": + stat_name = self.plugin_name + else: + stat_name = self.plugin_name + '_' + header # Manage log + log_str = "" if log: # Add _LOG to the return string # So stats will be highlited with a specific color log_str = "_LOG" - # Get the stat_name = plugin_name (+ header) - if header == "": - stat_name = self.plugin_name - else: - stat_name = self.plugin_name + '_' + header # Add the log to the list glances_logs.add(ret, stat_name.upper(), value, []) # Manage action - action = self.__get_limit_action(ret.lower(), header=header) - if action is not None: - # An action is available for the current alert, run it - self.actions.run(action) + # Here is a command line for the current trigger ? + command = self.__get_limit_action(ret.lower(), header=header) + if command is not None: + # Acommand line is available for the current alert, run it + self.actions.run(stat_name, ret.lower(), command) + else: + self.actions.set(stat_name, ret.lower()) # Default is ok return ret + log_str