提交 6d0fe0f7 编写于 作者: N nicolargo

Add new repeat function to the action (issue #952)

上级 e9260c3d
......@@ -7,9 +7,10 @@ Version 2.9.2
Enhancements and new features:
* New plugin to scan remote Web sites (URL) (issue #981)
* New plugin to scan remote Web sites (URL) (issue #981)
* Use -> and <- arrows keys to switch between processing sort (issue #1075)
* Add trends in the Curses interface (issue #1077)
* Add new repeat function to the action (issue #952)
Bugs corrected:
......
......@@ -87,6 +87,7 @@ mem_critical=90
# Define RAM thresholds in %
# Default values if not defined: 50/70/90
careful=50
#careful_action_repeat=echo {{percent}} >> /tmp/memory.alert
warning=70
critical=90
......
......@@ -31,4 +31,14 @@ reached:
https://github.com/nicolargo/glances/wiki/The-Glances-2.x-API-How-to
for the stats list.
It is also possible to repeat action until the end of the alert.
Keep in mind that the command line is executed every refresh time so
use with caution:
.. code-block:: ini
[load]
critical=5.0
critical_action_repeat=/home/myhome/bin/bipper.sh
.. _{{mustache}}: https://mustache.github.io/
.\" Man page generated from reStructuredText.
.
.TH "GLANCES" "1" "Apr 03, 2017" "2.9.2_DEVELOP" "Glances"
.TH "GLANCES" "1" "Apr 10, 2017" "2.9.2_DEVELOP" "Glances"
.SH NAME
glances \- An eye on your system
.
......
......@@ -63,21 +63,24 @@ class GlancesActions(object):
"""Set the stat_name to criticity."""
self.status[stat_name] = criticity
def run(self, stat_name, criticity, commands, mustache_dict=None):
def run(self, stat_name, criticity, commands, repeat, mustache_dict=None):
"""Run the commands (in background).
- stats_name: plugin_name (+ header)
- criticity: criticity of the trigger
- commands: a list of command line with optional {{mustache}}
- If True, then repeat the action
- mustache_dict: Plugin stats (can be use within {{mustache}})
Return True if the commands have been ran.
"""
if self.get(stat_name) == criticity or not self.start_timer.finished():
if (self.get(stat_name) == criticity and not repeat) or \
not self.start_timer.finished():
# Action already executed => Exit
return False
logger.debug("Run action {} for {} ({}) with stats {}".format(
logger.debug("{} action {} for {} ({}) with stats {}".format(
"Repeat" if repeat else "Run",
commands, stat_name, criticity, mustache_dict))
# Run all actions in background
......
......@@ -559,7 +559,7 @@ class GlancesPlugin(object):
"""Manage the action for the current stat"""
# Here is a command line for the current trigger ?
try:
command = self.get_limit_action(trigger, stat_name=stat_name)
command, repeat = self.get_limit_action(trigger, stat_name=stat_name)
except KeyError:
# Reset the trigger
self.actions.set(stat_name, trigger)
......@@ -584,7 +584,8 @@ class GlancesPlugin(object):
mustache_dict = self.get_stats_action()
# 2) Run the action
self.actions.run(
stat_name, trigger, command, mustache_dict=mustache_dict)
stat_name, trigger,
command, repeat, mustache_dict=mustache_dict)
def get_alert_log(self,
current=0,
......@@ -617,18 +618,22 @@ class GlancesPlugin(object):
return limit
def get_limit_action(self, criticity, stat_name=""):
"""Return the action for the alert."""
"""Return the tuple (action, repeat) for the alert.
- action is a command line
- repeat is a bool"""
# Get the action for stat + header
# Exemple: network_wlan0_rx_careful_action
try:
ret = self._limits[stat_name + '_' + criticity + '_action']
except KeyError:
# Try fallback to plugin default limit
# Exemple: network_careful_action
ret = self._limits[self.plugin_name + '_' + criticity + '_action']
# Return the action list
return ret
# Action key available ?
ret = [(stat_name + '_' + criticity + '_action', False),
(stat_name + '_' + criticity + '_action_repeat', True),
(self.plugin_name + '_' + criticity + '_action', False),
(self.plugin_name + '_' + criticity + '_action_repeat', True)]
for r in ret:
if r[0] in self._limits:
return self._limits[r[0]], r[1]
# No key found, the raise an error
raise KeyError
def get_limit_log(self, stat_name, default_action=False):
"""Return the log tag for the alert."""
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册