From 92986f6bcf2c959a355cb41908c1883dd132bdd3 Mon Sep 17 00:00:00 2001 From: Tacitor Date: Tue, 26 Nov 2024 23:17:52 -0500 Subject: [PATCH 1/9] Test to see if the colour will change as per the decoration --- glances/plugins/processlist/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/glances/plugins/processlist/__init__.py b/glances/plugins/processlist/__init__.py index 8298e1b316..e5effb8e85 100644 --- a/glances/plugins/processlist/__init__.py +++ b/glances/plugins/processlist/__init__.py @@ -317,7 +317,9 @@ def _get_process_curses_username(self, p, selected, args): # docker internal users are displayed as ints only, therefore str() # Correct issue #886 on Windows OS msg = self.layout_stat['user'].format(str(p['username'])[:9]) - ret = self.curse_add_line(msg) + + # Test to see if the colour will change as per the decoration + ret = self.curse_add_line(msg, decoration='CRITICAL') else: msg = self.layout_header['user'].format('?') ret = self.curse_add_line(msg) From c3185f7c1fd540e77d6b60145e844f1bc7be0607 Mon Sep 17 00:00:00 2001 From: Tacitor Date: Tue, 26 Nov 2024 23:26:14 -0500 Subject: [PATCH 2/9] hard code the root user as critical --- glances/plugins/processlist/__init__.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/glances/plugins/processlist/__init__.py b/glances/plugins/processlist/__init__.py index e5effb8e85..f1faaa6d66 100644 --- a/glances/plugins/processlist/__init__.py +++ b/glances/plugins/processlist/__init__.py @@ -317,9 +317,14 @@ def _get_process_curses_username(self, p, selected, args): # docker internal users are displayed as ints only, therefore str() # Correct issue #886 on Windows OS msg = self.layout_stat['user'].format(str(p['username'])[:9]) - - # Test to see if the colour will change as per the decoration - ret = self.curse_add_line(msg, decoration='CRITICAL') + + # feature for #2995 adding the test to see if a user is root level + if (msg == "root"): + # Set the decoration colour to be critical if the user is root + ret = self.curse_add_line(msg, decoration='CRITICAL') + else: + # Set the decoration colour to be the default for all other users + ret = self.curse_add_line(msg, decoration='DEFAULT') else: msg = self.layout_header['user'].format('?') ret = self.curse_add_line(msg) From cbe0b0bb9a8221a8034d22a91f5df020521293ab Mon Sep 17 00:00:00 2001 From: Tacitor Date: Tue, 26 Nov 2024 23:45:05 -0500 Subject: [PATCH 3/9] hard code the root user as critical but for real this time --- glances/plugins/processlist/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/glances/plugins/processlist/__init__.py b/glances/plugins/processlist/__init__.py index f1faaa6d66..d9be21fad1 100644 --- a/glances/plugins/processlist/__init__.py +++ b/glances/plugins/processlist/__init__.py @@ -319,7 +319,7 @@ def _get_process_curses_username(self, p, selected, args): msg = self.layout_stat['user'].format(str(p['username'])[:9]) # feature for #2995 adding the test to see if a user is root level - if (msg == "root"): + if (msg == "root "): # Set the decoration colour to be critical if the user is root ret = self.curse_add_line(msg, decoration='CRITICAL') else: From a45da2db8b95f8a3e45654e35ce43f61e4b7c327 Mon Sep 17 00:00:00 2001 From: Tacitor Date: Wed, 27 Nov 2024 00:03:58 -0500 Subject: [PATCH 4/9] Prep the hardcode username dectection for the config file --- glances/plugins/processlist/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/glances/plugins/processlist/__init__.py b/glances/plugins/processlist/__init__.py index d9be21fad1..9bf0e02024 100644 --- a/glances/plugins/processlist/__init__.py +++ b/glances/plugins/processlist/__init__.py @@ -319,7 +319,7 @@ def _get_process_curses_username(self, p, selected, args): msg = self.layout_stat['user'].format(str(p['username'])[:9]) # feature for #2995 adding the test to see if a user is root level - if (msg == "root "): + if (p['username'] == "root"): # Set the decoration colour to be critical if the user is root ret = self.curse_add_line(msg, decoration='CRITICAL') else: From 30afa7c84488f53529bbc1695a1c2f49344b62b2 Mon Sep 17 00:00:00 2001 From: Tacitor Date: Wed, 27 Nov 2024 18:12:43 -0500 Subject: [PATCH 5/9] add username dectection from the config file --- conf/glances.conf | 3 +++ glances/plugins/processlist/__init__.py | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/conf/glances.conf b/conf/glances.conf index 0a73282de9..c98f81e108 100644 --- a/conf/glances.conf +++ b/conf/glances.conf @@ -422,6 +422,9 @@ nice_warning=-20,-19,-18,-17,-16,-15,-14,-13,-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2 # Define the list of processes to export using: # a comma-separated list of Glances filter #export=.*firefox.*,pid:1234 +# +# A username to display with critical colours. Example the 'root' user +username_warning=root [ports] disable=False diff --git a/glances/plugins/processlist/__init__.py b/glances/plugins/processlist/__init__.py index 9bf0e02024..a2eb844365 100644 --- a/glances/plugins/processlist/__init__.py +++ b/glances/plugins/processlist/__init__.py @@ -190,6 +190,12 @@ def __init__(self, args=None, config=None): if args.export: logger.info("Export process filter is set to: {}".format(config.as_dict()['processlist']['export'])) + # For #2995. Load the username of a process to decorate with waning colours as from the config file. + if config is not None: + self.username_warning = config.get_value(self.plugin_name, 'username_warning') + else: + self.username_warning = "" + # The default sort key could also be overwrite by command line (see #1903) if args and args.sort_processes_key is not None: glances_processes.set_sort_key(args.sort_processes_key, False) @@ -319,7 +325,7 @@ def _get_process_curses_username(self, p, selected, args): msg = self.layout_stat['user'].format(str(p['username'])[:9]) # feature for #2995 adding the test to see if a user is root level - if (p['username'] == "root"): + if (p['username'] == self.username_warning): # Set the decoration colour to be critical if the user is root ret = self.curse_add_line(msg, decoration='CRITICAL') else: From 3a705a201f1c0b3459c5b19e257b1558be23056f Mon Sep 17 00:00:00 2001 From: Tacitor Date: Wed, 27 Nov 2024 18:21:20 -0500 Subject: [PATCH 6/9] add a guard for an empty username and hide the config option by default --- conf/glances.conf | 2 +- glances/plugins/processlist/__init__.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/conf/glances.conf b/conf/glances.conf index c98f81e108..1c1388642a 100644 --- a/conf/glances.conf +++ b/conf/glances.conf @@ -424,7 +424,7 @@ nice_warning=-20,-19,-18,-17,-16,-15,-14,-13,-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2 #export=.*firefox.*,pid:1234 # # A username to display with critical colours. Example the 'root' user -username_warning=root +#username_warning=root [ports] disable=False diff --git a/glances/plugins/processlist/__init__.py b/glances/plugins/processlist/__init__.py index a2eb844365..746a65b403 100644 --- a/glances/plugins/processlist/__init__.py +++ b/glances/plugins/processlist/__init__.py @@ -325,7 +325,7 @@ def _get_process_curses_username(self, p, selected, args): msg = self.layout_stat['user'].format(str(p['username'])[:9]) # feature for #2995 adding the test to see if a user is root level - if (p['username'] == self.username_warning): + if (not (self.username_warning == "") and p['username'] == self.username_warning): # Set the decoration colour to be critical if the user is root ret = self.curse_add_line(msg, decoration='CRITICAL') else: From c99aa315044bc1bde9ecc63449f42755b78a3b43 Mon Sep 17 00:00:00 2001 From: Tacitor Date: Wed, 27 Nov 2024 22:06:08 -0500 Subject: [PATCH 7/9] bypass the get_alert() function as it seems not to properly be able to load waning and carful thresholds. --- glances/plugins/processlist/__init__.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/glances/plugins/processlist/__init__.py b/glances/plugins/processlist/__init__.py index 746a65b403..372a0ce825 100644 --- a/glances/plugins/processlist/__init__.py +++ b/glances/plugins/processlist/__init__.py @@ -193,6 +193,9 @@ def __init__(self, args=None, config=None): # For #2995. Load the username of a process to decorate with waning colours as from the config file. if config is not None: self.username_warning = config.get_value(self.plugin_name, 'username_warning') + self.cpu_critical = config.get_float_value(self.plugin_name, 'cpu_critical') + self.cpu_warning = config.get_float_value(self.plugin_name, 'cpu_warning') + self.cpu_careful = config.get_float_value(self.plugin_name, 'cpu_careful') else: self.username_warning = "" @@ -260,6 +263,17 @@ def get_nice_alert(self, value): except KeyError: pass return 'DEFAULT' + + def get_cpu_decoration(self, value): + """Return the level of decoration needed for the CPU percentage based on the config file""" + if value >= self.cpu_critical: + return 'CRITICAL' + elif value >= self.cpu_warning: + return 'WARNING' + elif value >= self.cpu_warning: + return 'CAREFUL' + else: + return 'DEFAULT' def _get_process_curses_cpu(self, p, selected, args): """Return process CPU curses""" @@ -269,13 +283,7 @@ def _get_process_curses_cpu(self, p, selected, args): msg = cpu_layout.format(p['cpu_percent'] / float(self.nb_log_core)) else: msg = cpu_layout.format(p['cpu_percent']) - alert = self.get_alert( - p['cpu_percent'], - highlight_zero=False, - is_max=(p['cpu_percent'] == self.max_values['cpu_percent']), - header="cpu", - ) - ret = self.curse_add_line(msg, alert) + ret = self.curse_add_line(msg, self.get_cpu_decoration(p['cpu_percent'])) else: msg = self.layout_header['cpu'].format('?') ret = self.curse_add_line(msg) From a42acba97ebe8d0f203f56e54d2d26bf5a8545a9 Mon Sep 17 00:00:00 2001 From: Tacitor Date: Wed, 27 Nov 2024 22:38:35 -0500 Subject: [PATCH 8/9] quick fix for cpu_careful --- glances/plugins/processlist/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/glances/plugins/processlist/__init__.py b/glances/plugins/processlist/__init__.py index 372a0ce825..4dc7846271 100644 --- a/glances/plugins/processlist/__init__.py +++ b/glances/plugins/processlist/__init__.py @@ -263,14 +263,14 @@ def get_nice_alert(self, value): except KeyError: pass return 'DEFAULT' - + def get_cpu_decoration(self, value): """Return the level of decoration needed for the CPU percentage based on the config file""" if value >= self.cpu_critical: return 'CRITICAL' elif value >= self.cpu_warning: return 'WARNING' - elif value >= self.cpu_warning: + elif value >= self.cpu_careful: return 'CAREFUL' else: return 'DEFAULT' From 979fbb7011711580434b0e76fed3de81056414cc Mon Sep 17 00:00:00 2001 From: Tacitor Date: Fri, 29 Nov 2024 18:27:57 -0500 Subject: [PATCH 9/9] quick fix after updating dev branch to reflect changes since branching --- glances/plugins/processlist/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/glances/plugins/processlist/__init__.py b/glances/plugins/processlist/__init__.py index 18f0799833..95688eade6 100644 --- a/glances/plugins/processlist/__init__.py +++ b/glances/plugins/processlist/__init__.py @@ -370,9 +370,10 @@ def _get_process_curses_username(self, p, selected, args): else: # Set the decoration colour to be the default for all other users ret = self.curse_add_line(msg, decoration='DEFAULT') + return ret else: msg = self.layout_header['user'].format('?') - return self.curse_add_line(msg) + return self.curse_add_line(msg) def _get_process_curses_cpu_times(self, p, selected, args): """Return process time curses"""