summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornicolargo <nicolas@nicolargo.com>2019-11-02 10:34:48 +0100
committernicolargo <nicolas@nicolargo.com>2019-11-02 10:34:48 +0100
commita1c63f4003020394b84e59882ad7fbaaf1cece87 (patch)
tree7fb5cae2a167ff21b9b28a7dd3b4472ea4b9e578
parent9943c3071bc137c2611f661fc2bdc5e85743a83b (diff)
parent83bc9deb2a8f3ad8865a9c0c31196df7f2976603 (diff)
Merge branch 'issue1536' into develop
-rw-r--r--conf/glances.conf4
-rw-r--r--glances/events.py4
-rw-r--r--glances/outputs/glances_curses.py48
-rw-r--r--glances/plugins/glances_processlist.py5
-rw-r--r--glances/processes.py18
5 files changed, 36 insertions, 43 deletions
diff --git a/conf/glances.conf b/conf/glances.conf
index 7c953a7f..a69a1192 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 50356255..6cdec9df 100644
--- a/glances/events.py
+++ b/glances/events.py
@@ -92,12 +92,12 @@ class GlancesEvents(object):
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 6e05d9f1..ec1afdf4 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 @@ class _GlancesCurses(object):
'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 @@ class _GlancesCurses(object):
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 @@ class _GlancesCurses(object):
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 @@ class _GlancesCurses(object):
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 ede75415..7c1f80fd 100644
--- a/glances/plugins/glances_processlist.py
+++ b/glances/plugins/glances_processlist.py
@@ -116,6 +116,11 @@ class Plugin(GlancesPlugin):
# 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 2fcef259..29024bf5 100644
--- a/glances/processes.py
+++ b/glances/processes.py
@@ -30,7 +30,6 @@ import psutil
class GlancesProcesses(object):
-
"""Get processed stats using the psutil library."""
def __init__(self, cache_timeout=60):
@@ -50,8 +49,11 @@ class GlancesProcesses(object):
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 @@ class GlancesProcesses(object):
"""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):