From 6ed1b4cfc070ebdbccbf509d45fcb04a27f40e4a Mon Sep 17 00:00:00 2001 From: skafandri Date: Sat, 13 Sep 2025 10:37:18 +0100 Subject: [PATCH 1/2] Update plugin config with interval --- agent360/plugins/plugins-installer.py | 50 ++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/agent360/plugins/plugins-installer.py b/agent360/plugins/plugins-installer.py index 61f1d01..398da10 100644 --- a/agent360/plugins/plugins-installer.py +++ b/agent360/plugins/plugins-installer.py @@ -8,6 +8,8 @@ import certifi import logging import json +import time +import threading from pprint import pprint if sys.version_info >= (3,): @@ -30,13 +32,42 @@ class Plugin(plugins.BasePlugin): __name__ = 'plugins-installer' + # guard flags to prevent multiple updater threads + _updater_started = False + _lock = threading.Lock() + def run(self, config): self.config = config - updated = self._update_plugins_from_backend() - if updated: - self._restart_agent() - results = self._get_plugins(config) - return results + + # ensure only one background updater thread + with Plugin._lock: + if not Plugin._updater_started: + Plugin._updater_started = True + t = threading.Thread( + target=self._periodic_updater, + name="PluginUpdaterThread", + daemon=True + ) + t.start() + + return self._get_plugins(config) + + def _periodic_updater(self): + while True: + try: + updated = self._update_plugins_from_backend() + if updated: + logging.info("Plugins updated, restarting agent") + self._restart_agent() + except Exception as e: + logging.error("Error in periodic plugin update: %s", e) + + try: + interval = self.config.getint('agent', 'plugin_update_interval') + except Exception: + interval = 1800 # default 30 minutes + time.sleep(interval) + def _restart_agent(self): pid = os.fork() @@ -45,7 +76,7 @@ def _restart_agent(self): try: subprocess.run(['systemctl', 'restart', 'agent360'], check=True) except Exception as e: - logging.error('Failed to restart agent360: %s' % e) + logging.error('Failed to restart agent360: %s', e) os._exit(0) def _update_plugins_from_backend(self, proto='https'): @@ -71,7 +102,8 @@ def _update_plugins_from_backend(self, proto='https'): updated = True self._set_plugin_configuration(plugin['id'], c, plugin['config'][c]) except Exception as e: - logging.error('Failed to get plugins state: %s' % e) + logging.error('Failed to get plugins state: %s', e) + return False return updated def _get_connection(self, proto='https'): @@ -120,8 +152,8 @@ def _config_section_create(self, section): def _get_config_section_properties(self, config, section_name): # Exclude parent configuration excluded_keys = ['api_host', 'api_path', 'interval', 'log_file', 'log_file_mode', - 'logging_level','max_cached_collections', 'max_data_age', 'max_data_span', - 'plugins', 'server', 'subprocess', 'threads', 'ttl', 'user'] + 'logging_level', 'max_cached_collections', 'max_data_age', 'max_data_span', + 'plugins', 'server', 'subprocess', 'threads', 'ttl', 'user'] if section_name in config: properties = { key: config[section_name][key] From aee4e6b3bf8974542a063ac047a4c8e8da7b9490 Mon Sep 17 00:00:00 2001 From: ilyes kooli Date: Tue, 30 Sep 2025 09:04:20 +0100 Subject: [PATCH 2/2] Add subprocess import to plugins-installer.py --- agent360/plugins/plugins-installer.py | 1 + 1 file changed, 1 insertion(+) diff --git a/agent360/plugins/plugins-installer.py b/agent360/plugins/plugins-installer.py index 398da10..d7278ff 100644 --- a/agent360/plugins/plugins-installer.py +++ b/agent360/plugins/plugins-installer.py @@ -10,6 +10,7 @@ import json import time import threading +import subprocess from pprint import pprint if sys.version_info >= (3,):