From 9999f558df0289a1bffa244bba1d24f692cacc4c Mon Sep 17 00:00:00 2001 From: nicolargo Date: Sun, 1 May 2016 18:36:52 +0200 Subject: [PATCH] Monitoring process list refactor into AMPs --- glances/amps/glances_amp.py | 10 +++++++ glances/amps/glances_nginx.py | 18 +++++------- glances/amps/glances_systemd.py | 50 +++++++++++++++---------------- glances/amps/glances_systemv.py | 52 ++++++++++++++++----------------- glances/amps_list.py | 8 ++++- glances/plugins/glances_amps.py | 14 +++++---- 6 files changed, 83 insertions(+), 69 deletions(-) diff --git a/glances/amps/glances_amp.py b/glances/amps/glances_amp.py index 4a16885e..ae4c7d4d 100644 --- a/glances/amps/glances_amp.py +++ b/glances/amps/glances_amp.py @@ -191,3 +191,13 @@ class GlancesAmp(object): if ret is not None: ret = u(ret) return ret + + def update_wrapper(self, process_list): + """Wrapper for the children update""" + # Set the number of running process + self.set_count(len(process_list)) + # Call the children update method + if self.should_update(): + return self.update(process_list) + else: + return self.result() diff --git a/glances/amps/glances_nginx.py b/glances/amps/glances_nginx.py index 7b5cedf7..a26c2044 100644 --- a/glances/amps/glances_nginx.py +++ b/glances/amps/glances_nginx.py @@ -75,15 +75,13 @@ class Amp(GlancesAmp): def update(self, process_list): """Update the AMP""" - - if self.should_update(): - # Get the Nginx status - logger.debug('{0}: Update stats using status URL {1}'.format(self.NAME, self.get('status_url'))) - res = requests.get(self.get('status_url')) - if res.ok: - # u'Active connections: 1 \nserver accepts handled requests\n 1 1 1 \nReading: 0 Writing: 1 Waiting: 0 \n' - self.set_result(res.text.rstrip()) - else: - logger.debug('{0}: Can not grab status URL {1} ({2})'.format(self.NAME, self.get('status_url'), res.reason)) + # Get the Nginx status + logger.debug('{0}: Update stats using status URL {1}'.format(self.NAME, self.get('status_url'))) + res = requests.get(self.get('status_url')) + if res.ok: + # u'Active connections: 1 \nserver accepts handled requests\n 1 1 1 \nReading: 0 Writing: 1 Waiting: 0 \n' + self.set_result(res.text.rstrip()) + else: + logger.debug('{0}: Can not grab status URL {1} ({2})'.format(self.NAME, self.get('status_url'), res.reason)) return self.result() diff --git a/glances/amps/glances_systemd.py b/glances/amps/glances_systemd.py index 4ee6feb6..073dbc00 100644 --- a/glances/amps/glances_systemd.py +++ b/glances/amps/glances_systemd.py @@ -67,31 +67,29 @@ class Amp(GlancesAmp): def update(self, process_list): """Update the AMP""" - - if self.should_update(): - # Get the systemctl status - logger.debug('{0}: Update stats using systemctl {1}'.format(self.NAME, self.get('systemctl_cmd'))) - try: - res = check_output(self.get('systemctl_cmd').split()) - except OSError as e: - logger.debug('{0}: Error while executing systemctl ({1})'.format(self.NAME, e)) - else: - status = {} - # For each line - for r in res.split('\n')[1:-8]: - # Split per space .* - l = r.split() - if len(l) > 3: - # load column - for c in range(1, 3): - try: - status[l[c]] += 1 - except KeyError: - status[l[c]] = 1 - # Build the output (string) message - output = 'Services\n' - for k, v in iteritems(status): - output += '{0}: {1}\n'.format(k, v) - self.set_result(output, separator=' ') + # Get the systemctl status + logger.debug('{0}: Update stats using systemctl {1}'.format(self.NAME, self.get('systemctl_cmd'))) + try: + res = check_output(self.get('systemctl_cmd').split()) + except OSError as e: + logger.debug('{0}: Error while executing systemctl ({1})'.format(self.NAME, e)) + else: + status = {} + # For each line + for r in res.split('\n')[1:-8]: + # Split per space .* + l = r.split() + if len(l) > 3: + # load column + for c in range(1, 3): + try: + status[l[c]] += 1 + except KeyError: + status[l[c]] = 1 + # Build the output (string) message + output = 'Services\n' + for k, v in iteritems(status): + output += '{0}: {1}\n'.format(k, v) + self.set_result(output, separator=' ') return self.result() diff --git a/glances/amps/glances_systemv.py b/glances/amps/glances_systemv.py index 583f6b82..79e93e84 100644 --- a/glances/amps/glances_systemv.py +++ b/glances/amps/glances_systemv.py @@ -66,32 +66,30 @@ class Amp(GlancesAmp): def update(self, process_list): """Update the AMP""" - - if self.should_update(): - # Get the systemctl status - logger.debug('{0}: Update stats using service {1}'.format(self.NAME, self.get('service_cmd'))) - try: - res = check_output(self.get('service_cmd').split(), stderr=STDOUT) - except OSError as e: - logger.debug('{0}: Error while executing service ({1})'.format(self.NAME, e)) - else: - status = {'running': 0, 'stopped': 0, 'upstart': 0} - # For each line - for r in res.split('\n'): - # Split per space .* - l = r.split() - if len(l) < 4: - continue - if l[1] == '+': - status['running'] += 1 - elif l[1] == '-': - status['stopped'] += 1 - elif l[1] == '?': - status['upstart'] += 1 - # Build the output (string) message - output = 'Services\n' - for k, v in iteritems(status): - output += '{0}: {1}\n'.format(k, v) - self.set_result(output, separator=' ') + # Get the systemctl status + logger.debug('{0}: Update stats using service {1}'.format(self.NAME, self.get('service_cmd'))) + try: + res = check_output(self.get('service_cmd').split(), stderr=STDOUT) + except OSError as e: + logger.debug('{0}: Error while executing service ({1})'.format(self.NAME, e)) + else: + status = {'running': 0, 'stopped': 0, 'upstart': 0} + # For each line + for r in res.split('\n'): + # Split per space .* + l = r.split() + if len(l) < 4: + continue + if l[1] == '+': + status['running'] += 1 + elif l[1] == '-': + status['stopped'] += 1 + elif l[1] == '?': + status['upstart'] += 1 + # Build the output (string) message + output = 'Services\n' + for k, v in iteritems(status): + output += '{0}: {1}\n'.format(k, v) + self.set_result(output, separator=' ') return self.result() diff --git a/glances/amps_list.py b/glances/amps_list.py index 06eaee6b..faa7d1e3 100644 --- a/glances/amps_list.py +++ b/glances/amps_list.py @@ -113,8 +113,14 @@ class AmpsList(object): # At least one process is matching the regex logger.debug("AMPS: {} process detected (PID={})".format(k, amps_list[0]['pid'])) # Call the AMP update method - thread = threading.Thread(target=v.update, args=[amps_list]) + thread = threading.Thread(target=v.update_wrapper, args=[amps_list]) thread.start() + else: + # Set the process number to 0 + v.set_count(0) + if v.count_min() > 0: + # Only display the "No running process message" is countmin is defined + v.set_result("No running process") return self.__amps_dict diff --git a/glances/plugins/glances_amps.py b/glances/plugins/glances_amps.py index 0ed4bb49..08d06363 100644 --- a/glances/plugins/glances_amps.py +++ b/glances/plugins/glances_amps.py @@ -107,14 +107,18 @@ class Plugin(GlancesPlugin): continue # Display AMP first_column = '{0}'.format(m['name']) - # first_column = '{0} {1}/{2}'.format(m['key'], int(m['timer']), int(m['refresh'])) + first_column_style = self.get_alert(m['count'], m['countmin'], m['countmax']) + second_column = '{0}'.format(m['count']) 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, self.get_alert(m['count'], m['countmin'], m['countmax']))) + msg = '{0:<16} '.format(first_column) + ret.append(self.curse_add_line(msg, first_column_style)) + # ... and second column with the number of matching processes... + msg = '{0:<4} '.format(second_column) + ret.append(self.curse_add_line(msg)) # ... only on the first line - first_column = '' - # Display AMP result in the second column + first_column = second_column = '' + # Display AMP result in the third column ret.append(self.curse_add_line(l, splittable=True)) ret.append(self.curse_new_line())