Create the Glances threshold dict

This commit is contained in:
nicolargo 2017-04-17 19:24:00 +02:00
parent a2f97d56d3
commit 14d07ae5d1
3 changed files with 59 additions and 2 deletions

View File

@ -32,6 +32,7 @@ from glances.actions import GlancesActions
from glances.history import GlancesHistory
from glances.logger import logger
from glances.logs import glances_logs
from glances.thresholds import glances_thresholds
class GlancesPlugin(object):
@ -545,12 +546,22 @@ class GlancesPlugin(object):
# Add the log to the list
glances_logs.add(ret, stat_name.upper(), value)
# Manage threshold
self.manage_threshold(stat_name, ret)
# Manage action
self.manage_action(stat_name, ret.lower(), header, action_key)
# Default is 'OK'
return ret + log_str
def manage_threshold(self,
stat_name,
trigger):
"""Manage the threshold for the current stat"""
glances_thresholds.add(stat_name, trigger)
# logger.info(glances_thresholds.get())
def manage_action(self,
stat_name,
trigger,

View File

@ -21,6 +21,47 @@
Thresholds classes: OK, CAREFUL, WARNING, CRITICAL
"""
import sys
class GlancesThresholds(object):
"""Class to manage thresholds dict for all Glances plugins:
key: Glances stats (example: cpu_user)
value: Threasold* instance
"""
threshold_list = ['OK', 'CAREFUL', 'WARNING', 'CRITICAL']
def __init__(self):
self.current_module = sys.modules[__name__]
self._thresholds = {}
def get(self, stat_name=None):
"""Return the threshold dict.
If stat_name is None, return the threshold for all plugins (dict of Threshold*)
Else return the Threshold* instance for the given plugin
"""
if stat_name is None:
return self._thresholds
if stat_name in self._thresholds:
return self._thresholds[stat_name]
else:
return {}
def add(self, stat_name, threshold_description):
"""Add a new threshold to the dict (key = stat_name)"""
if threshold_description not in self.threshold_list:
return False
else:
self._thresholds[stat_name] = getattr(self.current_module,
'GlancesThreshold' + threshold_description.capitalize())()
return True
# Global variable uses to share thresholds between Glances componants
glances_thresholds = GlancesThresholds()
class _GlancesThreshold(object):
@ -33,7 +74,7 @@ class _GlancesThreshold(object):
return self._threshold['value']
def __repr__(self):
return self._threshold
return str(self._threshold)
def __str__(self):
return self.description()

View File

@ -32,6 +32,7 @@ from glances.thresholds import GlancesThresholdOk
from glances.thresholds import GlancesThresholdCareful
from glances.thresholds import GlancesThresholdWarning
from glances.thresholds import GlancesThresholdCritical
from glances.thresholds import GlancesThresholds
# Global variables
# =================
@ -211,7 +212,7 @@ class TestGlances(unittest.TestCase):
def test_094_thresholds(self):
"""Test thresholds classes"""
print('INFO: [TEST_094] Mandatories methods')
print('INFO: [TEST_094] Thresholds')
ok = GlancesThresholdOk()
careful = GlancesThresholdCareful()
warning = GlancesThresholdWarning()
@ -222,6 +223,9 @@ class TestGlances(unittest.TestCase):
self.assertFalse(ok > careful)
self.assertTrue(ok == ok)
self.assertTrue(str(ok) == 'OK')
thresholds = GlancesThresholds()
thresholds.add('cpu_percent', 'OK')
self.assertTrue(thresholds.get(stat_name='cpu_percent').description() == 'OK')
def test_095_methods(self):
"""Test mandatories methods"""
@ -311,5 +315,6 @@ class TestGlances(unittest.TestCase):
stats.end()
self.assertTrue(True)
if __name__ == '__main__':
unittest.main()