Monitoring process list refactor into AMPs

pull/863/head
nicolargo 2016-05-01 18:36:52 +02:00
parent 92cb6e3407
commit 9999f558df
6 changed files with 83 additions and 69 deletions

View File

@ -191,3 +191,13 @@ class GlancesAmp(object):
if ret is not None: if ret is not None:
ret = u(ret) ret = u(ret)
return 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()

View File

@ -75,15 +75,13 @@ class Amp(GlancesAmp):
def update(self, process_list): def update(self, process_list):
"""Update the AMP""" """Update the AMP"""
# Get the Nginx status
if self.should_update(): logger.debug('{0}: Update stats using status URL {1}'.format(self.NAME, self.get('status_url')))
# Get the Nginx status res = requests.get(self.get('status_url'))
logger.debug('{0}: Update stats using status URL {1}'.format(self.NAME, self.get('status_url'))) if res.ok:
res = requests.get(self.get('status_url')) # u'Active connections: 1 \nserver accepts handled requests\n 1 1 1 \nReading: 0 Writing: 1 Waiting: 0 \n'
if res.ok: self.set_result(res.text.rstrip())
# u'Active connections: 1 \nserver accepts handled requests\n 1 1 1 \nReading: 0 Writing: 1 Waiting: 0 \n' else:
self.set_result(res.text.rstrip()) logger.debug('{0}: Can not grab status URL {1} ({2})'.format(self.NAME, self.get('status_url'), res.reason))
else:
logger.debug('{0}: Can not grab status URL {1} ({2})'.format(self.NAME, self.get('status_url'), res.reason))
return self.result() return self.result()

View File

@ -67,31 +67,29 @@ class Amp(GlancesAmp):
def update(self, process_list): def update(self, process_list):
"""Update the AMP""" """Update the AMP"""
# Get the systemctl status
if self.should_update(): logger.debug('{0}: Update stats using systemctl {1}'.format(self.NAME, self.get('systemctl_cmd')))
# Get the systemctl status try:
logger.debug('{0}: Update stats using systemctl {1}'.format(self.NAME, self.get('systemctl_cmd'))) res = check_output(self.get('systemctl_cmd').split())
try: except OSError as e:
res = check_output(self.get('systemctl_cmd').split()) logger.debug('{0}: Error while executing systemctl ({1})'.format(self.NAME, e))
except OSError as e: else:
logger.debug('{0}: Error while executing systemctl ({1})'.format(self.NAME, e)) status = {}
else: # For each line
status = {} for r in res.split('\n')[1:-8]:
# For each line # Split per space .*
for r in res.split('\n')[1:-8]: l = r.split()
# Split per space .* if len(l) > 3:
l = r.split() # load column
if len(l) > 3: for c in range(1, 3):
# load column try:
for c in range(1, 3): status[l[c]] += 1
try: except KeyError:
status[l[c]] += 1 status[l[c]] = 1
except KeyError: # Build the output (string) message
status[l[c]] = 1 output = 'Services\n'
# Build the output (string) message for k, v in iteritems(status):
output = 'Services\n' output += '{0}: {1}\n'.format(k, v)
for k, v in iteritems(status): self.set_result(output, separator=' ')
output += '{0}: {1}\n'.format(k, v)
self.set_result(output, separator=' ')
return self.result() return self.result()

View File

@ -66,32 +66,30 @@ class Amp(GlancesAmp):
def update(self, process_list): def update(self, process_list):
"""Update the AMP""" """Update the AMP"""
# Get the systemctl status
if self.should_update(): logger.debug('{0}: Update stats using service {1}'.format(self.NAME, self.get('service_cmd')))
# Get the systemctl status try:
logger.debug('{0}: Update stats using service {1}'.format(self.NAME, self.get('service_cmd'))) res = check_output(self.get('service_cmd').split(), stderr=STDOUT)
try: except OSError as e:
res = check_output(self.get('service_cmd').split(), stderr=STDOUT) logger.debug('{0}: Error while executing service ({1})'.format(self.NAME, e))
except OSError as e: else:
logger.debug('{0}: Error while executing service ({1})'.format(self.NAME, e)) status = {'running': 0, 'stopped': 0, 'upstart': 0}
else: # For each line
status = {'running': 0, 'stopped': 0, 'upstart': 0} for r in res.split('\n'):
# For each line # Split per space .*
for r in res.split('\n'): l = r.split()
# Split per space .* if len(l) < 4:
l = r.split() continue
if len(l) < 4: if l[1] == '+':
continue status['running'] += 1
if l[1] == '+': elif l[1] == '-':
status['running'] += 1 status['stopped'] += 1
elif l[1] == '-': elif l[1] == '?':
status['stopped'] += 1 status['upstart'] += 1
elif l[1] == '?': # Build the output (string) message
status['upstart'] += 1 output = 'Services\n'
# Build the output (string) message for k, v in iteritems(status):
output = 'Services\n' output += '{0}: {1}\n'.format(k, v)
for k, v in iteritems(status): self.set_result(output, separator=' ')
output += '{0}: {1}\n'.format(k, v)
self.set_result(output, separator=' ')
return self.result() return self.result()

View File

@ -113,8 +113,14 @@ class AmpsList(object):
# At least one process is matching the regex # At least one process is matching the regex
logger.debug("AMPS: {} process detected (PID={})".format(k, amps_list[0]['pid'])) logger.debug("AMPS: {} process detected (PID={})".format(k, amps_list[0]['pid']))
# Call the AMP update method # 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() 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 return self.__amps_dict

View File

@ -107,14 +107,18 @@ class Plugin(GlancesPlugin):
continue continue
# Display AMP # Display AMP
first_column = '{0}'.format(m['name']) 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'): for l in m['result'].split('\n'):
# Display first column with the process name... # Display first column with the process name...
msg = '{0:<20} '.format(first_column) msg = '{0:<16} '.format(first_column)
ret.append(self.curse_add_line(msg, self.get_alert(m['count'], m['countmin'], m['countmax']))) 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 # ... only on the first line
first_column = '' first_column = second_column = ''
# Display AMP result in the second column # Display AMP result in the third column
ret.append(self.curse_add_line(l, splittable=True)) ret.append(self.curse_add_line(l, splittable=True))
ret.append(self.curse_new_line()) ret.append(self.curse_new_line())