From 92cb6e34074573cbeabe70fe0654bfb56da5aa2a Mon Sep 17 00:00:00 2001 From: nicolargo Date: Sat, 30 Apr 2016 23:29:05 +0200 Subject: [PATCH] First step for countmin / countmax --- conf/glances.conf | 20 +++++++++++++++++++- glances/amps/glances_amp.py | 19 +++++++++++++++++++ glances/plugins/glances_amps.py | 27 +++++++++++++++++++++++++-- 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/conf/glances.conf b/conf/glances.conf index 7c090651..1726f8e4 100644 --- a/conf/glances.conf +++ b/conf/glances.conf @@ -253,10 +253,20 @@ queue=glances_queue ###### # AMPS +# * enable: Enable (true) or disable (false) the AMP +# * regex: Regular expression to filter the process(es) +# * refresh: The AMP is executed every refresh seconds +# * one_line: (optional) Force (if true) the AMP to be displayed in one line +* * command: (optional) command to execute when the process is detected (thk to the regex) +# * countmin: (optional) minimal number of processes +# A warning will be displayed if number of process < count +# * countmax: (optional) maximum number of processes +# A warning will be displayed if number of process > count +# * : Others variables can be defined and used in the AMP script ###### [amp_dropbox] -# Use the default AMP which execute the command +# Use the default AMP (no dedicated AMP Python script) enable=true regex=.*dropbox.* refresh=3 @@ -264,6 +274,14 @@ one_line=false command=dropbox status countmin=1 +[amp_xeyes] +# Use the default AMP (no dedicated AMP Python script) +enable=true +regex=.*xeyes.* +refresh=3 +countmin=1 +countmax=2 + [amp_nginx] # Nginx status page should be enable (https://easyengine.io/tutorials/nginx/status-page/) enable=true diff --git a/glances/amps/glances_amp.py b/glances/amps/glances_amp.py index 2094070d..4a16885e 100644 --- a/glances/amps/glances_amp.py +++ b/glances/amps/glances_amp.py @@ -109,6 +109,9 @@ class GlancesAmp(object): else: logger.warning("{0} is disabled".format(self.NAME)) + # Init the count to 0 + self.configs['count'] = 0 + return self.enable() def get(self, key): @@ -157,6 +160,22 @@ class GlancesAmp(object): return self.enable() return False + def set_count(self, count): + """Set the number of processes matching the regex""" + self.configs['count'] = count + + def count(self): + """Get the number of processes matching the regex""" + return self.get('count') + + def count_min(self): + """Get the minimum number of processes""" + return self.get('countmin') + + def count_max(self): + """Get the maximum number of processes""" + return self.get('countmax') + def set_result(self, result, separator=''): """Store the result (string) into the result key of the AMP if one_line is true then replace \n by separator diff --git a/glances/plugins/glances_amps.py b/glances/plugins/glances_amps.py index fe0b4ade..0ed4bb49 100644 --- a/glances/plugins/glances_amps.py +++ b/glances/plugins/glances_amps.py @@ -61,13 +61,36 @@ class Plugin(GlancesPlugin): 'name': v.NAME, 'result': v.result(), 'refresh': v.refresh(), - 'timer': v.time_until_refresh()}) + 'timer': v.time_until_refresh(), + 'count': v.count(), + 'countmin': v.count_min(), + 'countmax': v.count_max(), + }) else: # Not available in SNMP mode pass return self.stats + def get_alert(self, nbprocess=0, countmin=None, countmax=None, header="", log=False): + """Return the alert status relative to the process number.""" + if nbprocess is None: + return 'OK' + if countmin is None: + countmin = nbprocess + if countmax is None: + countmax = nbprocess + if nbprocess > 0: + if int(countmin) <= int(nbprocess) <= int(countmax): + return 'OK' + else: + return 'WARNING' + else: + if int(countmin) == 0: + return 'OK' + else: + return 'CRITICAL' + def msg_curse(self, args=None): """Return the dict to display in the curse interface.""" # Init the return message @@ -88,7 +111,7 @@ class Plugin(GlancesPlugin): for l in m['result'].split('\n'): # Display first column with the process name... msg = '{0:<20} '.format(first_column) - ret.append(self.curse_add_line(msg, "TITLE")) + ret.append(self.curse_add_line(msg, self.get_alert(m['count'], m['countmin'], m['countmax']))) # ... only on the first line first_column = '' # Display AMP result in the second column