From da593949124bbf8b08027da0a24f760d0fe89394 Mon Sep 17 00:00:00 2001 From: "kanza.latif" Date: Wed, 11 Sep 2024 11:20:52 +0500 Subject: [PATCH] errors resolving --- scripts/hostcfgd | 94 ++++++++++++------------------------------------ 1 file changed, 23 insertions(+), 71 deletions(-) diff --git a/scripts/hostcfgd b/scripts/hostcfgd index de3a5eed..95f40469 100644 --- a/scripts/hostcfgd +++ b/scripts/hostcfgd @@ -1694,85 +1694,37 @@ class FipsCfg(object): syslog.syslog(syslog.LOG_INFO, f'FipsCfg: update the FIPS enforce option {self.enforce}.') loader.set_fips(image, self.enforce) -class Memory_StatisticsCfg(object): - """ - Memory Stats Config Daemon - Handles changes in MEMORY_STATS table. - 1) Handle enabling or disabling the feature - 2) Handle change of retention period - 3) Handle change of sampling interval - """ - - def __init__(self, config_db): - self.config_db = config_db - self.cache = {} - self.memory_statistics_defaults = { - "enabled": "false", - "retention_time": "15 days", - "sampling_interval": "5 minutes" - } - - def load(self, memory_stats_config: dict): - """ - Load memory statistics configuration when the daemon starts. - Args: - memory_stats_config: Configured memory statistics settings. - """ - syslog.syslog(syslog.LOG_INFO, "Memory_StatisticsCfg init ...") - memory_statistics_conf = memory_stats_config.get("config", {}) - - # Apply default configurations if not present - for row, value in self.memory_statistics_defaults.items(): - if not memory_statistics_conf.get(row): - self.config_db.mod_entry("MEMORY_STATISTICS", "config", {row: value}) - - # Apply configurations to ensure they are set correctly on startup - self.apply_configuration(memory_statistics_conf) +class Memory_StatisticsCfg: + def __init__(self): + pass - def apply_configuration(self, config): - """ - Apply the memory statistics configuration settings. - Args: - config: Configuration data for memory statistics. - """ - # Determine if the feature is enabled or disabled - enabled = config.get("enabled", self.memory_statistics_defaults["enabled"]).lower() == "true" - retention_time = config.get("retention_time", self.memory_statistics_defaults["retention_time"]) - sampling_interval = config.get("sampling_interval", self.memory_statistics_defaults["sampling_interval"]) - - # Enable or disable memory statistics - if enabled: - self.run_cmd(["sonic-memory_statistics-config", "--enable"]) + def apply_configuration(self, config_data): + enabled = config_data.get('enabled', 'false') + retention_time = config_data.get('retention_time', '30 days') + sampling_interval = config_data.get('sampling_interval', '10 minutes') + + cmd = ["sonic-memory_statistics-config"] + if enabled.lower() == 'true': + cmd.append("--enable") else: - self.run_cmd(["sonic-memory_statistics-config", "--disable"]) - - # Set retention time and sampling interval - self.run_cmd(["sonic-memory_statistics-config", "--retention_time", retention_time]) - self.run_cmd(["sonic-memory_statistics-config", "--sampling_interval", sampling_interval]) - - def memory_statistics_update(self, key, data): - """ - Handle updates to the memory statistics configuration. - Args: - key: Key identifying the config type. - data: Updated configuration data. - """ - syslog.syslog(syslog.LOG_INFO, "Memory_Statistics global configuration update") - if key == "config": - self.apply_configuration(data) + cmd.append("--disable") + + if retention_time: + cmd.extend(["--retention-time", retention_time]) + if sampling_interval: + cmd.extend(["--sampling-interval", sampling_interval]) + + self.run_cmd(cmd) def run_cmd(self, cmd): - """ - Execute a shell command and return the output. - Args: - cmd: List of command arguments. - """ try: output = subprocess.check_output(cmd, stderr=subprocess.STDOUT) syslog.syslog(syslog.LOG_INFO, output.decode('utf-8')) except subprocess.CalledProcessError as e: - syslog.syslog(syslog.LOG_ERR, e.output.decode('utf-8')) - + syslog.syslog(syslog.LOG_ERR, f"Command failed with error: {e.output.decode('utf-8')}") + except Exception as e: + syslog.syslog(syslog.LOG_ERR, f"An unexpected error occurred: {str(e)}") + class HostConfigDaemon: def __init__(self): self.state_db_conn = DBConnector(STATE_DB, 0)