Skip to content

Commit

Permalink
update testcases
Browse files Browse the repository at this point in the history
Signed-off-by: Arham-Nasir <[email protected]>
  • Loading branch information
Arham-Nasir committed Nov 12, 2024
1 parent 89075f8 commit 91e3fa5
Showing 1 changed file with 0 additions and 258 deletions.
258 changes: 0 additions & 258 deletions scripts/hostcfgd
Original file line number Diff line number Diff line change
Expand Up @@ -1718,264 +1718,6 @@ class FipsCfg(object):
syslog.syslog(syslog.LOG_INFO, f'FipsCfg: update the FIPS enforce option {self.enforce}.')
loader.set_fips(image, self.enforce)

# class MemoryStatisticsCfg:
# """
# MemoryStatisticsCfg class handles the configuration updates for the MemoryStatisticsDaemon.
# It listens to ConfigDB changes and applies them by restarting, shutting down, or reloading
# the MemoryStatisticsDaemon.
# """

# VALID_KEYS = ["enabled", "sampling_interval", "retention_period"] # Valid keys for configuration

# def __init__(self, config_db):
# """Initialize MemoryStatisticsCfg with a configuration database."""
# self.cache = {
# "enabled": "false",
# "sampling_interval": "5",
# "retention_period": "15"
# }
# self.config_db = config_db # Store config_db instance for further use

# # def load(self, memory_statistics_config: dict):
# # """Load initial memory statistics configuration."""
# # syslog.syslog(syslog.LOG_INFO, 'MemoryStatisticsCfg: Load initial configuration')

# # # Use default values if no config provided
# # memory_statistics_config = memory_statistics_config or {}

# # # Load configuration for each valid key
# # for key in self.VALID_KEYS:
# # self.memory_statistics_update(key, memory_statistics_config.get(key, self.cache[key]))
# def load(self, memory_statistics_config: dict):
# """Load initial memory statistics configuration."""
# syslog.syslog(syslog.LOG_INFO, 'Memory_StatisticsCfg: Load initial configuration')

# if not memory_statistics_config:
# memory_statistics_config = {}

# # Handle the initial config load for each key with validation
# self.memory_statistics_update("enabled", memory_statistics_config.get("enabled", "false"))
# self.memory_statistics_update("retention_period", memory_statistics_config.get("retention_period", "15"))
# self.memory_statistics_update("sampling_interval", memory_statistics_config.get("sampling_interval", "5"))

# def memory_statistics_update(self, key, data):
# """
# Apply memory statistics settings handler.

# Args:
# key: DB table's key that triggered the change.
# data: New table data to process.
# """
# if key not in self.VALID_KEYS:
# syslog.syslog(syslog.LOG_ERR, f"MemoryStatisticsCfg: Invalid key '{key}' received.")
# return

# # Convert data to string if it's not already
# data = str(data)

# # Validate numeric keys (retention_period and sampling_interval)
# if key in ["retention_period", "sampling_interval"] and (not data.isdigit() or int(data) <= 0):
# syslog.syslog(syslog.LOG_ERR, f"MemoryStatisticsCfg: Invalid value '{data}' for key '{key}'. Must be a positive integer.")
# return

# # Check if any value has changed
# if data != self.cache.get(key):
# syslog.syslog(syslog.LOG_INFO, f"MemoryStatisticsCfg: Detected change in '{key}' to '{data}'")

# try:
# self.apply_setting(key, data)
# self.cache[key] = data # Update cache with the new value
# except Exception as e:
# syslog.syslog(syslog.LOG_ERR, f'MemoryStatisticsCfg: Failed to manage MemoryStatisticsDaemon: {e}')

# def apply_setting(self, key, data):
# """Apply the setting based on the key."""
# if key == "enabled":
# if data.lower() == "true":
# self.restart_memory_statistics()
# else:
# self.shutdown_memory_statistics()
# else:
# self.reload_memory_statistics()

# def restart_memory_statistics(self):
# """Restart the memory statistics daemon."""
# self.shutdown_memory_statistics() # Stop the daemon before restarting
# time.sleep(1) # Optional delay to ensure proper restart
# syslog.syslog(syslog.LOG_INFO, "MemoryStatisticsCfg: Starting MemoryStatisticsDaemon")

# try:
# subprocess.Popen(['/usr/bin/memorystatsd']) # Replace with the correct daemon path
# except Exception as e:
# syslog.syslog(syslog.LOG_ERR, f"MemoryStatisticsCfg: Failed to start MemoryStatisticsDaemon: {e}")

# def reload_memory_statistics(self):
# """Send SIGHUP to the MemoryStatisticsDaemon to reload its configuration."""
# pid = self.get_memory_statistics_pid()
# if pid:
# try:
# os.kill(pid, signal.SIGHUP) # Notify daemon to reload its configuration
# syslog.syslog(syslog.LOG_INFO, "MemoryStatisticsCfg: Sent SIGHUP to reload daemon configuration")
# except Exception as e:
# syslog.syslog(syslog.LOG_ERR, f"MemoryStatisticsCfg: Failed to reload MemoryStatisticsDaemon: {e}")

# def shutdown_memory_statistics(self):
# """Send SIGTERM to stop the MemoryStatisticsDaemon gracefully."""
# pid = self.get_memory_statistics_pid()
# if pid:
# try:
# os.kill(pid, signal.SIGTERM) # Graceful shutdown
# syslog.syslog(syslog.LOG_INFO, "MemoryStatisticsCfg: Sent SIGTERM to stop MemoryStatisticsDaemon")
# except Exception as e:
# syslog.syslog(syslog.LOG_ERR, f"MemoryStatisticsCfg: Failed to shutdown MemoryStatisticsDaemon: {e}")

# def get_memory_statistics_pid(self):
# """Retrieve the PID of the running MemoryStatisticsDaemon."""
# try:
# with open('/var/run/memorystatsd.pid', 'r') as pid_file:
# pid = int(pid_file.read().strip())
# return pid
# except FileNotFoundError:
# syslog.syslog(syslog.LOG_WARNING, "MemoryStatisticsCfg: PID file not found.")
# except Exception as e:
# syslog.syslog(syslog.LOG_ERR, f"MemoryStatisticsCfg: Failed to retrieve MemoryStatisticsDaemon PID: {e}")
# return None

# class MemoryStatisticsCfg:
# """
# MemoryStatisticsCfg class handles the configuration updates for the MemoryStatisticsDaemon.
# It listens to ConfigDB changes and applies them by restarting, shutting down, or reloading
# the MemoryStatisticsDaemon.
# """

# VALID_KEYS = ["enabled", "sampling_interval", "retention_period"] # Valid keys for configuration

# def __init__(self, config_db):
# """Initialize MemoryStatisticsCfg with a configuration database."""
# self.cache = {
# "enabled": "false",
# "sampling_interval": "5",
# "retention_period": "15"
# }
# self.config_db = config_db # Store config_db instance for further use

# def load(self, memory_statistics_config: dict):
# """Load initial memory statistics configuration."""
# syslog.syslog(syslog.LOG_INFO, 'MemoryStatisticsCfg: Load initial configuration')

# if not memory_statistics_config:
# memory_statistics_config = {}
# for key, value in memory_statistics_config.items():
# if key not in self.VALID_KEYS:
# syslog.syslog(syslog.LOG_ERR, f"MemoryStatisticsCfg: Invalid key '{key}' in initial configuration.")
# continue # Skip invalid keys
# self.memory_statistics_update(key, value) # Valid key, apply the setting

# def memory_statistics_update(self, key, data):
# """
# Apply memory statistics settings handler.

# Args:
# key: DB table's key that triggered the change.
# data: New table data to process.
# """
# if key not in self.VALID_KEYS:
# syslog.syslog(syslog.LOG_ERR, f"MemoryStatisticsCfg: Invalid key '{key}' received.")
# return

# # Convert data to string if it's not already
# data = str(data)

# # Validate numeric keys (retention_period and sampling_interval)
# if key in ["retention_period", "sampling_interval"] and (not data.isdigit() or int(data) <= 0):
# syslog.syslog(syslog.LOG_ERR, f"MemoryStatisticsCfg: Invalid value '{data}' for key '{key}'. Must be a positive integer.")
# return

# # Check if any value has changed
# if data != self.cache.get(key):
# syslog.syslog(syslog.LOG_INFO, f"MemoryStatisticsCfg: Detected change in '{key}' to '{data}'")

# try:
# self.apply_setting(key, data)
# self.cache[key] = data # Update cache with the new value
# except Exception as e:
# syslog.syslog(syslog.LOG_ERR, f'MemoryStatisticsCfg: Failed to manage MemoryStatisticsDaemon: {e}')

# def apply_setting(self, key, data):
# """Apply the setting based on the key."""
# if key == "enabled":
# if data.lower() == "true":
# self.restart_memory_statistics()
# else:
# self.shutdown_memory_statistics()
# else:
# self.reload_memory_statistics()

# def restart_memory_statistics(self):
# """Restart the memory statistics daemon."""
# self.shutdown_memory_statistics() # Stop the daemon before restarting
# time.sleep(1) # Optional delay to ensure proper restart
# syslog.syslog(syslog.LOG_INFO, "MemoryStatisticsCfg: Starting MemoryStatisticsDaemon")

# try:
# subprocess.Popen(['/usr/bin/memorystatsd']) # Replace with the correct daemon path
# except Exception as e:
# syslog.syslog(syslog.LOG_ERR, f"MemoryStatisticsCfg: Failed to start MemoryStatisticsDaemon: {e}")

# def reload_memory_statistics(self):
# """Send SIGHUP to the MemoryStatisticsDaemon to reload its configuration."""
# pid = self.get_memory_statistics_pid()
# if pid:
# try:
# os.kill(pid, signal.SIGHUP) # Notify daemon to reload its configuration
# syslog.syslog(syslog.LOG_INFO, "MemoryStatisticsCfg: Sent SIGHUP to reload daemon configuration")
# except Exception as e:
# syslog.syslog(syslog.LOG_ERR, f"MemoryStatisticsCfg: Failed to reload MemoryStatisticsDaemon: {e}")

# def shutdown_memory_statistics(self):
# """Send SIGTERM to stop the MemoryStatisticsDaemon gracefully."""
# pid = self.get_memory_statistics_pid()
# if pid:
# try:
# os.kill(pid, signal.SIGTERM) # Graceful shutdown
# syslog.syslog(syslog.LOG_INFO, "MemoryStatisticsCfg: Sent SIGTERM to stop MemoryStatisticsDaemon")
# self.wait_for_shutdown(pid)
# except Exception as e:
# syslog.syslog(syslog.LOG_ERR, f"MemoryStatisticsCfg: Failed to shutdown MemoryStatisticsDaemon: {e}")

# def wait_for_shutdown(self, pid, timeout=10):
# """Wait for the daemon to shut down gracefully."""
# try:
# process = psutil.Process(pid)
# process.wait(timeout=timeout) # Wait for the process to terminate
# syslog.syslog(syslog.LOG_INFO, "MemoryStatisticsCfg: MemoryStatisticsDaemon stopped gracefully")
# except psutil.TimeoutExpired:
# syslog.syslog(syslog.LOG_WARNING, "MemoryStatisticsCfg: Timed out while waiting for daemon shutdown.")
# except psutil.NoSuchProcess:
# syslog.syslog(syslog.LOG_WARNING, "MemoryStatisticsCfg: MemoryStatisticsDaemon process not found.")

# def get_memory_statistics_pid(self):
# """Retrieve the PID of the running MemoryStatisticsDaemon."""
# try:
# with open('/var/run/memorystatsd.pid', 'r') as pid_file:
# pid = int(pid_file.read().strip())
# # Verify if the PID is for memorystatsd
# if psutil.pid_exists(pid):
# process = psutil.Process(pid)
# if process.name() == "memorystatsd":
# return pid
# else:
# syslog.syslog(syslog.LOG_WARNING, f"MemoryStatisticsCfg: PID {pid} does not correspond to memorystatsd.")
# else:
# syslog.syslog(syslog.LOG_WARNING, "MemoryStatisticsCfg: PID does not exist.")
# except FileNotFoundError:
# syslog.syslog(syslog.LOG_WARNING, "MemoryStatisticsCfg: PID file not found. Daemon might not be running.")
# except ValueError:
# syslog.syslog(syslog.LOG_ERR, "MemoryStatisticsCfg: PID file contents invalid.")
# except Exception as e:
# syslog.syslog(syslog.LOG_ERR, f"MemoryStatisticsCfg: Failed to retrieve MemoryStatisticsDaemon PID: {e}")
# return None

class MemoryStatisticsCfg:
"""
MemoryStatisticsCfg class handles the configuration updates for the MemoryStatisticsDaemon.
Expand Down

0 comments on commit 91e3fa5

Please sign in to comment.