mirror of https://github.com/nicolargo/glances.git
Start working on limits and alerts. Huge action list...
This commit is contained in:
parent
59f2af119e
commit
2dad74de14
|
|
@ -46,7 +46,7 @@ class GlancesStandalone():
|
|||
self.monitors = monitorList(config)
|
||||
|
||||
# Init stats
|
||||
self.stats = GlancesStats()
|
||||
self.stats = GlancesStats(config)
|
||||
|
||||
# Initial update
|
||||
self.stats.update()
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ import collections
|
|||
# Import Glances libs
|
||||
from ..core.glances_globals import *
|
||||
|
||||
class GlancesStats:
|
||||
class GlancesStats(object):
|
||||
"""
|
||||
This class store, update and give stats
|
||||
"""
|
||||
|
|
@ -35,7 +35,7 @@ class GlancesStats:
|
|||
_plugins = {}
|
||||
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, config=None):
|
||||
"""
|
||||
Init the stats
|
||||
"""
|
||||
|
|
@ -43,6 +43,9 @@ class GlancesStats:
|
|||
# Load the plugins
|
||||
self.load_plugins()
|
||||
|
||||
# Load the limits
|
||||
self.load_limits(config)
|
||||
|
||||
|
||||
def __getattr__(self, item):
|
||||
"""
|
||||
|
|
@ -102,6 +105,16 @@ class GlancesStats:
|
|||
self._plugins[plugname] = m.Plugin()
|
||||
|
||||
|
||||
def load_limits(self, config):
|
||||
"""
|
||||
Load the stats limits
|
||||
"""
|
||||
|
||||
# For each plugins, call the init_limits method
|
||||
for p in self._plugins:
|
||||
self._plugins[p].load_limits(config)
|
||||
|
||||
|
||||
def __update__(self, input_stats):
|
||||
"""
|
||||
Update the stats
|
||||
|
|
|
|||
|
|
@ -82,9 +82,13 @@ class Plugin(GlancesPlugin):
|
|||
# New line
|
||||
ret.append(self.curse_new_line())
|
||||
# 1min load
|
||||
msg = "{0:7} {1}".format(
|
||||
# msg = "{0:7} {1}".format(
|
||||
# _("1 min:"),
|
||||
# format(self.stats['min1'], '>5.2f'))
|
||||
msg = "{0:7} {1} {2}".format(
|
||||
_("1 min:"),
|
||||
format(self.stats['min1'], '>5.2f'))
|
||||
format(self.stats['min1'], '>5.2f'),
|
||||
str(self.get_alert(self.stats['min1'], max=100*self.core_plugin.update())))
|
||||
ret.append(self.curse_add_line(msg, "NORMAL"))
|
||||
# New line
|
||||
ret.append(self.curse_new_line())
|
||||
|
|
@ -100,5 +104,10 @@ class Plugin(GlancesPlugin):
|
|||
_("15 min:"),
|
||||
format(self.stats['min15'], '>5.2f'))
|
||||
ret.append(self.curse_add_line(msg, "NORMAL"))
|
||||
|
||||
|
||||
# !!! Limits (debug only)
|
||||
ret.append(self.curse_new_line())
|
||||
msg = str(self.get_limits())
|
||||
ret.append(self.curse_add_line(msg, "NORMAL"))
|
||||
|
||||
return ret
|
||||
|
|
@ -44,9 +44,33 @@ class GlancesPlugin(object):
|
|||
"""
|
||||
|
||||
def __init__(self):
|
||||
# Init the stat list
|
||||
# Plugin name (= module name without glances_)
|
||||
self.plugin_name = self.__class__.__module__[len('glances_'):]
|
||||
|
||||
# Init the stats list
|
||||
self.stats = None
|
||||
|
||||
|
||||
# Init the limits dictionnary
|
||||
self.limits = dict()
|
||||
|
||||
|
||||
def load_limits(self, config):
|
||||
"""
|
||||
Load the limits from the configuration file
|
||||
"""
|
||||
|
||||
if (config.has_section(self.plugin_name)):
|
||||
# print ">>> Load limits for %s" % self.plugin_name
|
||||
# Read LOAD limits
|
||||
for s in [ 'careful', 'warning', 'critical' ]:
|
||||
try:
|
||||
value = config.get_option(self.plugin_name, s)
|
||||
except:
|
||||
pass
|
||||
else:
|
||||
self.limits[self.plugin_name + '_' + s] = value
|
||||
# print ">>> %s = %s" % (self.plugin_name + '_' + s, value)
|
||||
|
||||
|
||||
def __repr__(self):
|
||||
# Return the raw stats
|
||||
|
|
@ -68,6 +92,45 @@ class GlancesPlugin(object):
|
|||
return json.dumps(self.stats)
|
||||
|
||||
|
||||
def get_limits(self):
|
||||
# Return the limits object
|
||||
return self.limits
|
||||
|
||||
|
||||
def get_alert(self, current=0, min=0, max=100):
|
||||
# Return the alert status relative to a current value
|
||||
# If current < CAREFUL of max then alert = OK
|
||||
# If current > CAREFUL of max then alert = CAREFUL
|
||||
# If current > WARNING of max then alert = WARNING
|
||||
# If current > CRITICAL of max then alert = CRITICAL
|
||||
# stat is USER, SYSTEM, IOWAIT or STEAL
|
||||
try:
|
||||
value = (current * 100) / max
|
||||
except ZeroDivisionError:
|
||||
return 'DEFAULT'
|
||||
|
||||
if (value > self.get_limit_critical()):
|
||||
return 'CRITICAL'
|
||||
elif (value > self.get_limit_warning()):
|
||||
return 'WARNING'
|
||||
elif (value > self.get_limit_careful()):
|
||||
return 'CAREFUL'
|
||||
|
||||
return 'OK'
|
||||
|
||||
|
||||
def get_limit_critical(self):
|
||||
return self.limits[self.plugin_name + '_' + 'critical']
|
||||
|
||||
|
||||
def get_limit_warning(self):
|
||||
return self.limits[self.plugin_name + '_' + 'warning']
|
||||
|
||||
|
||||
def get_limit_careful(self):
|
||||
return self.limits[self.plugin_name + '_' + 'careful']
|
||||
|
||||
|
||||
def msg_curse(self):
|
||||
"""
|
||||
Return default string to display in the curse interface
|
||||
|
|
|
|||
Loading…
Reference in New Issue