From 8ee0a83d7781da919c2ded4bc28d3c1395765606 Mon Sep 17 00:00:00 2001 From: nicolargo Date: Sat, 7 Oct 2023 09:49:13 +0200 Subject: [PATCH] Make the alerts number configurable (related to #2558) --- conf/glances.conf | 29 +++++++++++++++++------------ glances/events.py | 12 ++++++++---- glances/plugins/alert/model.py | 9 ++++++++- glances/plugins/mem/model.py | 5 ++++- 4 files changed, 37 insertions(+), 18 deletions(-) diff --git a/conf/glances.conf b/conf/glances.conf index 19fbbee6..ef71a4c6 100644 --- a/conf/glances.conf +++ b/conf/glances.conf @@ -396,35 +396,40 @@ port_default_gateway=True disable=False # Only show specific containers (comma separated list of container name or regular expression) # Comment this line to display all containers (default configuration) -#show=telegraf +; show=telegraf # Hide some containers (comma separated list of container name or regular expression) # Comment this line to display all containers (default configuration) -#hide=telegraf +; hide=telegraf # Define the maximum docker size name (default is 20 chars) max_name_size=20 -#cpu_careful=50 +; cpu_careful=50 # Thresholds for CPU and MEM (in %) -#cpu_warning=70 -#cpu_critical=90 -#mem_careful=20 -#mem_warning=50 -#mem_critical=70 +; cpu_warning=70 +; cpu_critical=90 +; mem_careful=20 +; mem_warning=50 +; mem_critical=70 # # Per container thresholds -#containername_cpu_careful=10 -#containername_cpu_warning=20 -#containername_cpu_critical=30 +; containername_cpu_careful=10 +; containername_cpu_warning=20 +; containername_cpu_critical=30 # # By default, Glances only display running containers # Set the following key to True to display all containers all=False # Define Podman sock -#podman_sock=unix:///run/user/1000/podman/podman.sock +; podman_sock=unix:///run/user/1000/podman/podman.sock [amps] # AMPs configuration are defined in the bottom of this file disable=False +[alert] +disable=True +# Maximum number of alerts to display (default is 10) +; max_events=10 + ############################################################################## # Client/server ############################################################################## diff --git a/glances/events.py b/glances/events.py index b107afa6..b1a9ba3b 100644 --- a/glances/events.py +++ b/glances/events.py @@ -35,14 +35,18 @@ class GlancesEvents(object): "top sort key"] """ - def __init__(self): + def __init__(self, max_events=10): """Init the events class.""" # Maximum size of the events list - self.events_max = 10 + self.set_max_events(max_events) # Init the logs list self.events_list = [] + def set_max_events(self, max_events): + """Set the maximum size of the events list.""" + self.max_events = max_events + def get(self): """Return the raw events list.""" return self.events_list @@ -138,8 +142,8 @@ class GlancesEvents(object): # Add the item to the list self.events_list.insert(0, item) - # Limit the list to 'events_max' items - if self.len() > self.events_max: + # Limit the list to 'max_events' items + if self.len() > self.max_events: self.events_list.pop() return True diff --git a/glances/plugins/alert/model.py b/glances/plugins/alert/model.py index 3ccdf36a..950acec5 100644 --- a/glances/plugins/alert/model.py +++ b/glances/plugins/alert/model.py @@ -11,6 +11,7 @@ from datetime import datetime +from glances.logger import logger from glances.events import glances_events from glances.thresholds import glances_thresholds @@ -170,7 +171,9 @@ class PluginModel(GlancesPluginModel): def __init__(self, args=None, config=None): """Init the plugin.""" - super(PluginModel, self).__init__(args=args, config=config, stats_init_value=[]) + super(PluginModel, self).__init__(args=args, + config=config, + stats_init_value=[]) # We want to display the stat in the curse interface self.display_curse = True @@ -178,6 +181,10 @@ class PluginModel(GlancesPluginModel): # Set the message position self.align = 'bottom' + # Set the maximum number of events to display + if config is not None and (config.has_section('alert') or config.has_section('alerts')): + glances_events.set_max_events(config.get_int_value('alert', 'max_events')) + def update(self): """Nothing to do here. Just return the global glances_log.""" # Set the stats to the glances_events diff --git a/glances/plugins/mem/model.py b/glances/plugins/mem/model.py index 52ceed0c..fb000b2d 100644 --- a/glances/plugins/mem/model.py +++ b/glances/plugins/mem/model.py @@ -115,7 +115,10 @@ class PluginModel(GlancesPluginModel): def __init__(self, args=None, config=None): """Init the plugin.""" super(PluginModel, self).__init__( - args=args, config=config, items_history_list=items_history_list, fields_description=fields_description + args=args, + config=config, + items_history_list=items_history_list, + fields_description=fields_description ) # We want to display the stat in the curse interface