Add sort information on Docker plugin (console mode). Related to #2138

This commit is contained in:
nicolargo 2022-10-02 12:46:23 +02:00
parent 16832111ee
commit 94d1e00692
3 changed files with 50 additions and 24 deletions

View File

@ -58,6 +58,16 @@ items_history_list = [{'name': 'cpu_percent', 'description': 'Container CPU cons
# List of key to remove before export
export_exclude_list = ['cpu', 'io', 'memory', 'network']
# Sort dictionary for human
sort_for_human = {
'io_counters': 'disk IO',
'cpu_percent': 'CPU consumption',
'memory_usage': 'memory consumption',
'cpu_times': 'uptime',
'name': 'container name',
None: 'None',
}
class Plugin(GlancesPlugin):
"""Glances Docker plugin.
@ -96,6 +106,9 @@ class Plugin(GlancesPlugin):
# value: network stats dict
self.io_old = {}
# Sort key
self.sort_key = None
# Force a first update because we need two update to have the first stat
self.update()
self.refresh_timer.set(0)
@ -303,7 +316,7 @@ class Plugin(GlancesPlugin):
pass
# Sort and update the stats
self.stats = sort_stats(stats)
self.sort_key, self.stats = sort_docker_stats(stats)
return self.stats
@ -533,8 +546,10 @@ class Plugin(GlancesPlugin):
ret.append(self.curse_add_line(msg, "TITLE"))
msg = ' {}'.format(len(self.stats['containers']))
ret.append(self.curse_add_line(msg))
msg = ' (served by Docker {})'.format(self.stats['version']["Version"])
msg = ' sorted by {}'.format(sort_for_human[self.sort_key])
ret.append(self.curse_add_line(msg))
# msg = ' (served by Docker {})'.format(self.stats['version']["Version"])
# ret.append(self.curse_add_line(msg))
ret.append(self.curse_new_line())
# Header
ret.append(self.curse_new_line())
@ -545,15 +560,15 @@ class Plugin(GlancesPlugin):
len(max(self.stats['containers'], key=lambda x: len(x['name']))['name']),
)
msg = ' {:{width}}'.format('Name', width=name_max_width)
ret.append(self.curse_add_line(msg))
ret.append(self.curse_add_line(msg, 'SORT' if self.sort_key == 'name' else 'DEFAULT'))
msg = '{:>10}'.format('Status')
ret.append(self.curse_add_line(msg))
msg = '{:>10}'.format('Uptime')
ret.append(self.curse_add_line(msg))
msg = '{:>6}'.format('CPU%')
ret.append(self.curse_add_line(msg))
ret.append(self.curse_add_line(msg, 'SORT' if self.sort_key == 'cpu_percent' else 'DEFAULT'))
msg = '{:>7}'.format('MEM')
ret.append(self.curse_add_line(msg))
ret.append(self.curse_add_line(msg, 'SORT' if self.sort_key == 'memory_usage' else 'DEFAULT'))
msg = '/{:<7}'.format('MAX')
ret.append(self.curse_add_line(msg))
msg = '{:>7}'.format('IOR/s')
@ -700,8 +715,8 @@ class ThreadDockerGrabber(threading.Thread):
time.sleep(0.1)
if self.stopped():
break
except:
logger.debug("docker plugin - Exception thrown during run")
except Exception as e:
logger.debug("docker plugin - Exception thrown during run ({})".format(e))
self.stop()
@property
@ -724,12 +739,22 @@ class ThreadDockerGrabber(threading.Thread):
return self._stopper.is_set()
def sort_stats(stats):
def sort_docker_stats(stats):
# Sort Docker stats using the same function than processes
sort_by = 'cpu_percent'
sort_by = glances_processes.sort_key
sort_by_secondary = 'memory_usage'
if glances_processes.sort_key.startswith('memory'):
if sort_by == 'memory_percent':
sort_by = 'memory_usage'
sort_by_secondary = 'cpu_percent'
sort_stats_processes(stats['containers'], sorted_by=sort_by, sorted_by_secondary=sort_by_secondary)
return stats
elif sort_by in ['username', 'io_counters', 'cpu_times']:
sort_by = 'cpu_percent'
# Sort docker stats
sort_stats_processes(stats['containers'],
sorted_by=sort_by,
sorted_by_secondary=sort_by_secondary,
# Reverse for all but name
reverse=glances_processes.sort_key != 'name')
# Return the main sort key and the sorted stats
return sort_by, stats

View File

@ -9,7 +9,7 @@
"""Process count plugin."""
from glances.processes import glances_processes
from glances.processes import glances_processes, sort_for_human
from glances.plugins.glances_plugin import GlancesPlugin
# Define the history items list
@ -27,16 +27,6 @@ class Plugin(GlancesPlugin):
stats is a list
"""
sort_for_human = {
'io_counters': 'disk IO',
'cpu_percent': 'CPU consumption',
'memory_percent': 'memory consumption',
'cpu_times': 'process time',
'username': 'user name',
'name': 'process name',
None: 'None',
}
def __init__(self, args=None, config=None):
"""Init the plugin."""
super(Plugin, self).__init__(args=args, config=config, items_history_list=items_history_list)
@ -133,7 +123,7 @@ class Plugin(GlancesPlugin):
# Display sort information
msg = 'Programs' if self.args.programs else 'Threads'
try:
sort_human = self.sort_for_human[glances_processes.sort_key]
sort_human = sort_for_human[glances_processes.sort_key]
except KeyError:
sort_human = glances_processes.sort_key
if glances_processes.auto_sort:

View File

@ -20,6 +20,17 @@ import psutil
# This constant defines the list of available processes sort key
sort_processes_key_list = ['cpu_percent', 'memory_percent', 'username', 'cpu_times', 'io_counters', 'name']
# Sort dictionary for human
sort_for_human = {
'io_counters': 'disk IO',
'cpu_percent': 'CPU consumption',
'memory_percent': 'memory consumption',
'cpu_times': 'process time',
'username': 'user name',
'name': 'processs name',
None: 'None',
}
class GlancesProcesses(object):
"""Get processed stats using the psutil library."""