mirror of
https://github.com/nicolargo/glances.git
synced 2024-12-27 19:25:27 +03:00
Trigger OK
This commit is contained in:
parent
6f18c3fd7b
commit
7f34a6d07a
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user