From bdd557437a238140f09810a2438f138d63b04623 Mon Sep 17 00:00:00 2001 From: nicolargo Date: Fri, 31 May 2019 16:35:24 +0200 Subject: [PATCH] Allow comma separated commands in AMP --- NEWS | 3 ++- conf/glances.conf | 8 +++++++ glances/amps/glances_default.py | 39 +++++++++++++++++++-------------- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/NEWS b/NEWS index c41cc5ff..8ce253ee 100644 --- a/NEWS +++ b/NEWS @@ -22,12 +22,13 @@ Bugs corrected: * Too less data using prometheus exporter #1462 * Getting an error when running with prometheus exporter #1469 * Stack trace when starts Glances on CentOS #1470 - * UnicodeEncodeError: 'ascii' codec can't encode character u'\u25cf' - Raspbian stretch #1483 + * UnicodeEncodeError: 'ascii' codec can't encode character u'\u25cf' - Raspbian stretch #1483 Others: * Documentation is unclear how to get Docker information #1386 * Add 'all' target to the Pip install (install all dependencies) + * Allow comma separated commands in AMP Version 3.1 =========== diff --git a/conf/glances.conf b/conf/glances.conf index 840c6899..1a6880e1 100644 --- a/conf/glances.conf +++ b/conf/glances.conf @@ -510,6 +510,14 @@ regex=.*python.* refresh=3 countmax=20 +[amp_conntrack] +# Use comma separated for multiple commands (no space around the comma) +enable=false +regex=\/sbin\/init +refresh=30 +one_line=false +command=sysctl net.netfilter.nf_conntrack_count;sysctl net.netfilter.nf_conntrack_max + [amp_nginx] # Use the NGinx AMP # Nginx status page should be enable (https://easyengine.io/tutorials/nginx/status-page/) diff --git a/glances/amps/glances_default.py b/glances/amps/glances_default.py index fe9919e4..f57865a4 100644 --- a/glances/amps/glances_default.py +++ b/glances/amps/glances_default.py @@ -46,7 +46,7 @@ class Amp(GlancesAmp): """Glances' Default AMP.""" NAME = '' - VERSION = '1.0' + VERSION = '1.1' DESCRIPTION = '' AUTHOR = 'Nicolargo' EMAIL = 'contact@nicolargo.com' @@ -59,23 +59,28 @@ class Amp(GlancesAmp): def update(self, process_list): """Update the AMP""" # Get the systemctl status - logger.debug('{}: Update AMP stats using service {}'.format(self.NAME, self.get('service_cmd'))) + logger.debug('{}: Update AMP stats using command {}'.format(self.NAME, self.get('service_cmd'))) + # Get command to execute try: res = self.get('command') except OSError as e: - logger.debug('{}: Error while executing service ({})'.format(self.NAME, e)) - else: - if res is not None: - try: - msg = u(check_output(res.split(), stderr=STDOUT)) - self.set_result(to_ascii(msg.rstrip())) - except CalledProcessError as e: - self.set_result(e.output) - else: - # Set the default message if command return None - # Default sum of CPU and MEM for the matching regex - self.set_result('CPU: {:.1f}% | MEM: {:.1f}%'.format( - sum([p['cpu_percent'] for p in process_list]), - sum([p['memory_percent'] for p in process_list]))) - + logger.debug('{}: Error while executing command ({})'.format(self.NAME, e)) + return self.result() + # No command found, use default message + if res is None: + # Set the default message if command return None + # Default sum of CPU and MEM for the matching regex + self.set_result('CPU: {:.1f}% | MEM: {:.1f}%'.format( + sum([p['cpu_percent'] for p in process_list]), + sum([p['memory_percent'] for p in process_list]))) + return self.result() + # Run command(s) + # Comman separated commands can be executed + try: + msg = '' + for cmd in res.split(';'): + msg += u(check_output(cmd.split(), stderr=STDOUT)) + self.set_result(to_ascii(msg.rstrip())) + except CalledProcessError as e: + self.set_result(e.output) return self.result()