Skip to content

Commit

Permalink
added apply_configurations function
Browse files Browse the repository at this point in the history
  • Loading branch information
kanza-latif committed Sep 5, 2024
1 parent 017b959 commit 4dac17f
Showing 1 changed file with 48 additions and 33 deletions.
81 changes: 48 additions & 33 deletions scripts/hostcfgd
Original file line number Diff line number Diff line change
Expand Up @@ -1702,55 +1702,70 @@ class MemoryStatsCfg(object):
2) Handle change of retention period
3) Handle change of sampling interval
"""

def __init__(self):
self.cache = {}
self.memory_statistics_defaults = {
"enabled": "false",
"retention_time": "24h",
"sampling_interval": "5m"
}

def load(self, memory_stats_config: dict):
"""Memory stats configuration
Force load memory statistics configuration.
"""
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_statistics_table.get("config", {})
for row in self.memory_statistics_defaults:
value = self.memory_statistics_defaults.get(row)
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:
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":
# Admin mode
memory_statistics_enabled = self.memory_statistics_defaults["enabled"]
if data.get("enabled") is not None:
memory_statistics_enabled = data.get("enabled")
if memory_statistics_enabled.lower() == "true":
enabled = True
else:
enabled = False
if enabled:
run_cmd(["sonic-memory_statistics-conifg", "--enable"])
else:
run_cmd(["sonic-memory_statistics-config", "--disable"])

# Memory configuration
retention_time = self.memory_statistics_defaults["retention_time"]
if data.get("retention_time") is not None:
retention_time = data.get("retention_time")
run_cmd(["sonic-memory_statistics-config", "--retention_time", retention_time])

# Sampling interval
sampling_interval = self.memory_statistics_defaults["sampling_interval"]
if data.get("sampling_interval") is not None:
sampling_interval = data.get("sampling_interval")
run_cmd(["sonic-memory_statistics-config", "--sampling_interval", sampling_interval])

def run_cmd(cmd):
self.apply_configuration(data)

def run_cmd(self, cmd):
"""
Execute a shell command and return the output
Execute a shell command and return the output.
Args:
cmd: List of command arguments.
"""
import subprocess
try:
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
syslog.syslog(syslog.LOG_INFO, output.decode('utf-8'))
Expand Down

0 comments on commit 4dac17f

Please sign in to comment.