Refactor limits for process list

This commit is contained in:
Nicolas Hennion 2014-02-22 22:45:47 +01:00
parent de47a8920c
commit 22d5e4c982
5 changed files with 113 additions and 73 deletions

View File

@ -62,14 +62,17 @@ careful=45
warning=52
critical=60
[process]
# Limit values for CPU per process in %
[processlist]
# Limit values for CPU/MEM per process in %
# Default values if not defined: 50/70/90
#careful=50
#warning=70
#critical=90
# Limit values for MEM per process in %
# Default values if not defined: 50/70/90
cpu_careful=50
cpu_warning=70
cpu_critical=90
# Limit values for MEM per process in %
# Default values if not defined: 50/70/90
mem_careful=50
mem_warning=70
mem_critical=90

View File

@ -50,6 +50,7 @@ class Config:
self.parser = RawConfigParser()
self.load()
def load(self):
"""
Load a config file from the list of paths, if it exists
@ -68,12 +69,14 @@ class Config:
sys.exit(1)
break
def get_config_path(self):
"""
Return the readed configuration file path
"""
return self.config_path
def get_paths_list(self):
"""
Get a list of config file paths, taking into account of the OS,
@ -123,12 +126,21 @@ class Config:
return paths
def items(self, section):
"""
Return the items list of a section
"""
return self.parser.items(section)
def has_section(self, section):
"""
Return info about the existence of a section
"""
return self.parser.has_section(section)
def get_option(self, section, option):
"""
Get the float value of an option, if it exists
@ -140,6 +152,7 @@ class Config:
else:
return value
def get_raw_option(self, section, option):
"""
Get the raw value of an option, if it exists

View File

@ -20,7 +20,7 @@
# Import system libs
# Check for PSUtil already done in the glances_core script
from psutil import cpu_times
from psutil import cpu_times, cpu_times_percent
# from ..plugins.glances_plugin import GlancesPlugin
from glances_plugin import GlancesPlugin
@ -50,6 +50,24 @@ class Plugin(GlancesPlugin):
Update CPU stats
"""
# Grab CPU using the PSUtil cpu_times_percent method (PSUtil 0.7 or higher)
cputimespercent = cpu_times_percent(interval=0, percpu=False)
self.stats = {}
for cpu in ['user', 'system', 'idle', 'nice',
'iowait', 'irq', 'softirq', 'steal']:
if hasattr(cputimespercent, cpu):
self.stats[cpu] = getattr(cputimespercent, cpu)
return self.stats
def update_deprecated(self):
"""
!!! Not used anymore...
Update CPU stats
"""
# Grab CPU using the PSUtil cpu_times method
cputime = cpu_times(percpu=False)
cputime_total = cputime.user + cputime.system + cputime.idle
@ -133,20 +151,6 @@ class Plugin(GlancesPlugin):
ret.append(self.curse_add_line(msg))
msg = "{0}".format(format(self.stats['user'] / 100, '>6.1%'))
ret.append(self.curse_add_line(msg, self.get_alert_log(self.stats['user'])))
# Nice CPU
if ('nice' in self.stats):
msg = " {0:7} {1}".format(
_("nice:"),
format(self.stats['nice'] / 100, '>6.1%'))
ret.append(self.curse_add_line(msg, optional=True))
# New line
ret.append(self.curse_new_line())
# System CPU
if ('system' in self.stats):
msg = "{0:8}".format(_("system:"))
ret.append(self.curse_add_line(msg))
msg = "{0}".format(format(self.stats['system'] / 100, '>6.1%'))
ret.append(self.curse_add_line(msg, self.get_alert_log(self.stats['system'])))
# IOWait CPU
if ('iowait' in self.stats):
msg = " {0:8}".format(_("iowait:"))
@ -155,18 +159,32 @@ class Plugin(GlancesPlugin):
ret.append(self.curse_add_line(msg, self.get_alert_log(self.stats['iowait']), optional=True))
# New line
ret.append(self.curse_new_line())
# Idles CPU
if ('idle' in self.stats):
msg = "{0:7} {1}".format(
_("idle:"),
format(self.stats['idle'] / 100, '>6.1%'))
# System CPU
if ('system' in self.stats):
msg = "{0:8}".format(_("system:"))
ret.append(self.curse_add_line(msg))
msg = "{0}".format(format(self.stats['system'] / 100, '>6.1%'))
ret.append(self.curse_add_line(msg, self.get_alert_log(self.stats['system'])))
# IRQ CPU
if ('irq' in self.stats):
msg = " {0:7} {1}".format(
_("irq:"),
format(self.stats['irq'] / 100, '>6.1%'))
ret.append(self.curse_add_line(msg, optional=True))
# New line
ret.append(self.curse_new_line())
# Nice CPU
if ('nice' in self.stats):
msg = "{0:7} {1}".format(
_("nice:"),
format(self.stats['nice'] / 100, '>6.1%'))
ret.append(self.curse_add_line(msg, optional=True))
# Idles CPU
if ('idle' in self.stats):
msg = " {0:7} {1}".format(
_("idle:"),
format(self.stats['idle'] / 100, '>6.1%'))
ret.append(self.curse_add_line(msg))
# Return the message with decoration
return ret

View File

@ -60,16 +60,11 @@ class GlancesPlugin(object):
"""
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)
# print "Load limits for %s" % self.plugin_name
for s, v in config.items(self.plugin_name):
# Read limits
# print "\t%s = %s" % (self.plugin_name + '_' + s, v)
self.limits[self.plugin_name + '_' + s] = config.get_option(self.plugin_name, s)
def __repr__(self):
@ -97,7 +92,7 @@ class GlancesPlugin(object):
return self.limits
def get_alert(self, current=0, min=0, max=100):
def get_alert(self, current=0, min=0, max=100, header="", log=False):
# Return the alert status relative to a current value
# Use this function for minor stat
# If current < CAREFUL of max then alert = OK
@ -105,53 +100,64 @@ class GlancesPlugin(object):
# If current > WARNING of max then alert = WARNING
# If current > CRITICAL of max then alert = CRITICAL
# stat is USER, SYSTEM, IOWAIT or STEAL
#
# If defined 'header' is added between the plugin name and the status
# Only usefull for stats with several alert status
#
# If log=True than return the logged status
# Compute the %
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'
# If log is enable than add _LOG to the return string
if (log):
log_str = "_LOG"
else:
log_str = ""
return 'OK'
# if (self.plugin_name == "processlist"):
# print "*"*300
# print self.limits
# sys.exit(0)
# Manage limits
if (value > self.get_limit_critical(header=header)):
return 'CRITICAL'+log_str
elif (value > self.get_limit_warning(header=header)):
return 'WARNING'+log_str
elif (value > self.get_limit_careful(header=header)):
return 'CAREFUL'+log_str
# Default is ok
return 'OK'+log_str
def get_alert_log(self, current=0, min=0, max=100):
# Return the alert status relative to a current value
# Use this function for major stat
# If current < CAREFUL of max then alert = OK_LOG
# If current > CAREFUL of max then alert = CAREFUL_LOG
# If current > WARNING of max then alert = WARNING_LOG
# If current > CRITICAL of max then alert = CRITICAL_LOG
# stat is USER, SYSTEM, IOWAIT or STEAL
try:
value = (current * 100) / max
except ZeroDivisionError:
return 'DEFAULT'
if (value > self.get_limit_critical()):
return 'CRITICAL_LOG'
elif (value > self.get_limit_warning()):
return 'WARNING_LOG'
elif (value > self.get_limit_careful()):
return 'CAREFUL_LOG'
return 'OK_LOG'
def get_alert_log(self, current=0, min=0, max=100, header=""):
return self.get_alert(current, min, max, header, log=True)
def get_limit_critical(self):
return self.limits[self.plugin_name + '_' + 'critical']
def get_limit_critical(self, header=""):
if (header == ""):
return self.limits[self.plugin_name + '_' + 'critical']
else:
return self.limits[self.plugin_name + '_' + header + '_' + 'critical']
def get_limit_warning(self):
return self.limits[self.plugin_name + '_' + 'warning']
def get_limit_warning(self, header=""):
if (header == ""):
return self.limits[self.plugin_name + '_' + 'warning']
else:
return self.limits[self.plugin_name + '_' + header + '_' + 'warning']
def get_limit_careful(self):
return self.limits[self.plugin_name + '_' + 'careful']
def get_limit_careful(self, header=""):
if (header == ""):
return self.limits[self.plugin_name + '_' + 'careful']
else:
return self.limits[self.plugin_name + '_' + header + '_' + 'careful']
def msg_curse(self, args):
@ -201,8 +207,8 @@ class GlancesPlugin(object):
OK_LOG: Value is OK and logged
CAREFUL: Value is CAREFUL and non logged
CAREFUL_LOG: Value is CAREFUL and logged
WARINING: Value is WARINING and non logged
WARINING_LOG: Value is WARINING and logged
WARNING: Value is WARINING and non logged
WARNING_LOG: Value is WARINING and logged
CRITICAL: Value is CRITICAL and non logged
CRITICAL_LOG: Value is CRITICAL and logged
optional: True if the stat is optional (display only if space is available)

View File

@ -103,10 +103,10 @@ class Plugin(GlancesPlugin):
ret.append(self.curse_add_line(msg))
# CPU
msg = "{0:>6}".format(format(p['cpu_percent'], '>5.1f'))
ret.append(self.curse_add_line(msg))
ret.append(self.curse_add_line(msg, self.get_alert(p['cpu_percent'], header="cpu")))
# MEM
msg = "{0:>6}".format(format(p['memory_percent'], '>5.1f'))
ret.append(self.curse_add_line(msg))
ret.append(self.curse_add_line(msg, self.get_alert(p['memory_percent'], header="mem")))
# VMS
msg = "{0:>6}".format(self.auto_unit(p['memory_info'][1], low_precision=False))
ret.append(self.curse_add_line(msg, optional=True))