Skip to content

Commit

Permalink
resolved error
Browse files Browse the repository at this point in the history
  • Loading branch information
kanza-latif committed Sep 11, 2024
1 parent c8aa989 commit 3fb4958
Showing 1 changed file with 69 additions and 23 deletions.
92 changes: 69 additions & 23 deletions scripts/hostcfgd
Original file line number Diff line number Diff line change
Expand Up @@ -1695,37 +1695,83 @@ class FipsCfg(object):
loader.set_fips(image, self.enforce)

class Memory_StatisticsCfg(object):
def __init__(self, config_db):
self.config_db = config_db
pass
"""
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 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")
def __init__(self):
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)

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"])
else:
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)
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)

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, 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)}")

syslog.syslog(syslog.LOG_ERR, e.output.decode('utf-8'))

class HostConfigDaemon:
def __init__(self):
self.state_db_conn = DBConnector(STATE_DB, 0)
Expand Down

0 comments on commit 3fb4958

Please sign in to comment.