diff --git a/conf/glances.conf b/conf/glances.conf index 7c953a7f26..a69a119273 100644 --- a/conf/glances.conf +++ b/conf/glances.conf @@ -244,6 +244,10 @@ battery_critical=95 [processlist] disable=False +# Sort key: if not defined, the sort is automatically done by Glances (recommended) +# Should be one of the following: +# cpu_percent, memory_percent, io_counters, name, cpu_times, username +#sort_key=memory_percent # Define CPU/MEM (per process) thresholds in % # Default values if not defined: 50/70/90 cpu_careful=50 diff --git a/glances/events.py b/glances/events.py index 5035625501..6cdec9df81 100644 --- a/glances/events.py +++ b/glances/events.py @@ -92,12 +92,12 @@ def get_event_sort_key(self, event_type): def set_process_sort(self, event_type): """Define the process auto sort key from the alert type.""" if glances_processes.auto_sort: - glances_processes.sort_key = self.get_event_sort_key(event_type) + glances_processes.set_sort_key(self.get_event_sort_key(event_type)) def reset_process_sort(self): """Reset the process auto sort key.""" if glances_processes.auto_sort: - glances_processes.sort_key = 'cpu_percent' + glances_processes.set_sort_key('auto') def add(self, event_state, event_type, event_value, proc_list=None, proc_desc="", peak_time=6): diff --git a/glances/outputs/glances_curses.py b/glances/outputs/glances_curses.py index 6e05d9f12c..ec1afdf43d 100644 --- a/glances/outputs/glances_curses.py +++ b/glances/outputs/glances_curses.py @@ -80,13 +80,13 @@ class _GlancesCurses(object): 'U': {'switch': 'network_cumul'}, 'W': {'switch': 'disable_wifi'}, # Processes sort hotkeys - 'a': {'auto_sort': True, 'sort_key': 'cpu_percent'}, - 'c': {'auto_sort': False, 'sort_key': 'cpu_percent'}, - 'i': {'auto_sort': False, 'sort_key': 'io_counters'}, - 'm': {'auto_sort': False, 'sort_key': 'memory_percent'}, - 'p': {'auto_sort': False, 'sort_key': 'name'}, - 't': {'auto_sort': False, 'sort_key': 'cpu_times'}, - 'u': {'auto_sort': False, 'sort_key': 'username'}, + 'a': {'sort_key': 'auto'}, + 'c': {'sort_key': 'cpu_percent'}, + 'i': {'sort_key': 'io_counters'}, + 'm': {'sort_key': 'memory_percent'}, + 'p': {'sort_key': 'name'}, + 't': {'sort_key': 'cpu_times'}, + 'u': {'sort_key': 'username'}, } _sort_loop = ['cpu_percent', 'memory_percent', 'username', @@ -276,9 +276,9 @@ def _init_colors(self): 'DEFAULT': self.no_color, 'UNDERLINE': curses.A_UNDERLINE, 'BOLD': A_BOLD, - 'SORT': A_BOLD, + 'SORT': curses.A_UNDERLINE | A_BOLD, 'OK': self.default_color2, - 'MAX': self.default_color2 | curses.A_BOLD, + 'MAX': self.default_color2 | A_BOLD, 'FILTER': self.filter_color, 'TITLE': self.title_color, 'PROCESS': self.default_color2, @@ -308,21 +308,6 @@ def set_cursor(self, value): except Exception: pass - # def get_key(self, window): - # # Catch ESC key AND numlock key (issue #163) - # keycode = [0, 0] - # keycode[0] = window.getch() - # keycode[1] = window.getch() - # - # if keycode != [-1, -1]: - # logger.debug("Keypressed (code: %s)" % keycode) - # - # if keycode[0] == 27 and keycode[1] != -1: - # # Do not escape on specials keys - # return -1 - # else: - # return keycode[0] - def get_key(self, window): # @TODO: Check issue #163 ret = window.getch() @@ -340,14 +325,9 @@ def __catch_key(self, return_to_browser=False): self._hotkeys[hotkey]['switch'], not getattr(self.args, self._hotkeys[hotkey]['switch'])) - if self.pressedkey == ord(hotkey) and 'auto_sort' in self._hotkeys[hotkey]: - setattr(glances_processes, - 'auto_sort', - self._hotkeys[hotkey]['auto_sort']) if self.pressedkey == ord(hotkey) and 'sort_key' in self._hotkeys[hotkey]: - setattr(glances_processes, - 'sort_key', - self._hotkeys[hotkey]['sort_key']) + glances_processes.set_sort_key(self._hotkeys[hotkey]['sort_key'], + self._hotkeys[hotkey]['sort_key'] == 'auto') # Other actions... if self.pressedkey == ord('\x1b') or self.pressedkey == ord('q'): @@ -400,14 +380,12 @@ def __catch_key(self, return_to_browser=False): glances_processes.enable() elif self.pressedkey == curses.KEY_LEFT: # "<" (left arrow) navigation through process sort - setattr(glances_processes, 'auto_sort', False) next_sort = (self.loop_position() - 1) % len(self._sort_loop) - glances_processes.sort_key = self._sort_loop[next_sort] + glances_processes.set_sort_key(self._sort_loop[next_sort], False) elif self.pressedkey == curses.KEY_RIGHT: # ">" (right arrow) navigation through process sort - setattr(glances_processes, 'auto_sort', False) next_sort = (self.loop_position() + 1) % len(self._sort_loop) - glances_processes.sort_key = self._sort_loop[next_sort] + glances_processes.set_sort_key(self._sort_loop[next_sort], False) # Return the key code return self.pressedkey diff --git a/glances/plugins/glances_processlist.py b/glances/plugins/glances_processlist.py index ede75415ef..7c1f80fd18 100644 --- a/glances/plugins/glances_processlist.py +++ b/glances/plugins/glances_processlist.py @@ -116,6 +116,11 @@ def __init__(self, args=None, config=None): # Use to optimize space (see https://github.com/nicolargo/glances/issues/959) self.pid_max = glances_processes.pid_max + # Set the default sort key if it is defined in the configuration file + if 'processlist' in config.as_dict() and 'sort_key' in config.as_dict()['processlist']: + logger.debug('Configuration overwrites processes sort key by {}'.format(config.as_dict()['processlist']['sort_key'])) + glances_processes.set_sort_key(config.as_dict()['processlist']['sort_key'], False) + # Note: 'glances_processes' is already init in the processes.py script def get_key(self): diff --git a/glances/processes.py b/glances/processes.py index 2fcef2596f..29024bf52c 100644 --- a/glances/processes.py +++ b/glances/processes.py @@ -30,7 +30,6 @@ class GlancesProcesses(object): - """Get processed stats using the psutil library.""" def __init__(self, cache_timeout=60): @@ -50,8 +49,11 @@ def __init__(self, cache_timeout=60): self.io_old = {} # Init stats - self.auto_sort = True - self._sort_key = 'cpu_percent' + self.auto_sort = None + self._sort_key = None + # Default processes sort key is 'auto' + # Can be overwrite from the configuration file (issue#1536) => See glances_processlist.py init + self.set_sort_key('auto', auto=True) self.processlist = [] self.reset_processcount() @@ -368,10 +370,14 @@ def sort_key(self): """Get the current sort key.""" return self._sort_key - @sort_key.setter - def sort_key(self, key): + def set_sort_key(self, key, auto=True): """Set the current sort key.""" - self._sort_key = key + if key == 'auto': + self.auto_sort = True + self._sort_key = 'cpu_percent' + else: + self.auto_sort = auto + self._sort_key = key def weighted(value):