@@ -1694,88 +1694,43 @@ class FipsCfg(object):
16941694 syslog .syslog (syslog .LOG_INFO , f'FipsCfg: update the FIPS enforce option { self .enforce } .' )
16951695 loader .set_fips (image , self .enforce )
16961696
1697- class Memory_StatisticsCfg (object ):
1698- """
1699- Memory Stats Config Daemon
1700- Handles changes in MEMORY_STATS table.
1701- 1) Handle enabling or disabling the feature
1702- 2) Handle change of retention period
1703- 3) Handle change of sampling interval
1704- """
1705-
1706- def __init__ (self , config_db ):
1707- self .config_db = config_db
1708- self .cache = {}
1709- self .memory_statistics_defaults = {
1710- "enabled" : "false" ,
1711- "retention_time" : "15 days" ,
1712- "sampling_interval" : "5 minutes"
1713- }
1714-
1715- def load (self , memory_stats_config : dict ):
1716- """
1717- Load memory statistics configuration when the daemon starts.
1718- Args:
1719- memory_stats_config: Configured memory statistics settings.
1720- """
1721- syslog .syslog (syslog .LOG_INFO , "Memory_StatisticsCfg init ..." )
1722- memory_statistics_conf = memory_stats_config .get ("config" , {})
1723-
1724- # Apply default configurations if not present
1725- for row , value in self .memory_statistics_defaults .items ():
1726- if not memory_statistics_conf .get (row ):
1727- self .config_db .mod_entry ("MEMORY_STATISTICS" , "config" , {row : value })
1728-
1729- # Apply configurations to ensure they are set correctly on startup
1730- self .apply_configuration (memory_statistics_conf )
1697+ class Memory_StatisticsCfg :
1698+ def __init__ (self ):
1699+ self .enabled = False
1700+ self .retention_time = 0
1701+ self .sampling_interval = 0
17311702
1732- def apply_configuration (self , config ):
1733- """
1734- Apply the memory statistics configuration settings.
1735- Args:
1736- config: Configuration data for memory statistics.
1737- """
1738- # Determine if the feature is enabled or disabled
1739- enabled = config .get ("enabled" , self .memory_statistics_defaults ["enabled" ]).lower () == "true"
1740- retention_time = config .get ("retention_time" , self .memory_statistics_defaults ["retention_time" ])
1741- sampling_interval = config .get ("sampling_interval" , self .memory_statistics_defaults ["sampling_interval" ])
1742-
1743- # Enable or disable memory statistics
1744- if enabled :
1745- self .run_cmd (["sonic-memory_statistics-config" , "--enable" ])
1703+ def load_config (self , config_data ):
1704+ try :
1705+ config = json .loads (config_data )
1706+ self .enabled = config .get ("enabled" , self .enabled )
1707+ self .retention_time = config .get ("retention_time" , self .retention_time )
1708+ self .sampling_interval = config .get ("sampling_interval" , self .sampling_interval )
1709+ except json .JSONDecodeError :
1710+ raise ValueError ("Invalid JSON format for configuration data" )
1711+
1712+ def apply_config (self ):
1713+ if self .enabled :
1714+ self ._run_shell_command (f"set_memory_statistics --enable --retention-time { self .retention_time } --sampling-interval { self .sampling_interval } " )
17461715 else :
1747- self .run_cmd (["sonic-memory_statistics-config" , "--disable" ])
1748-
1749- # Set retention time and sampling interval
1750- self .run_cmd (["sonic-memory_statistics-config" , "--retention-time" , retention_time ])
1751- self .run_cmd (["sonic-memory_statistics-config" , "--sampling-interval" , sampling_interval ])
1716+ self ._run_shell_command ("set_memory_statistics --disable" )
1717+
1718+ def update_config (self , key , value ):
1719+ if key == "enabled" :
1720+ self .enabled = value
1721+ elif key == "retention_time" :
1722+ self .retention_time = value
1723+ elif key == "sampling_interval" :
1724+ self .sampling_interval = value
1725+ else :
1726+ raise ValueError (f"Unknown configuration key: { key } " )
17521727
1753- def memory_statistics_update (self , key , data ):
1754- """
1755- Handle updates to the memory statistics configuration.
1756- Args:
1757- key: Key identifying the config type.
1758- data: Updated configuration data.
1759- """
1760- syslog .syslog (syslog .LOG_INFO , "Memory_Statistics global configuration update" )
1761- if key == "config" :
1762- self .apply_configuration (data )
1728+ self .apply_config ()
17631729
1764- def run_cmd (self , cmd ):
1765- """
1766- Execute a shell command and return the output.
1767- Args:
1768- cmd: List of command arguments.
1769- """
1770- try :
1771- output = subprocess .check_output (cmd , stderr = subprocess .STDOUT )
1772- syslog .syslog (syslog .LOG_INFO , output .decode ('utf-8' ))
1773- except subprocess .CalledProcessError as e :
1774- syslog .syslog (syslog .LOG_ERR , e .output .decode ('utf-8' ))
1775- except FileNotFoundError :
1776- syslog .syslog (syslog .LOG_ERR , f"Command not found: { ' ' .join (cmd )} " )
1777- except Exception as e :
1778- syslog .syslog (syslog .LOG_ERR , f"An unexpected error occurred: { str (e )} " )
1730+ def _run_shell_command (self , command ):
1731+ result = subprocess .run (command , shell = True , text = True , capture_output = True )
1732+ if result .returncode != 0 :
1733+ raise RuntimeError (f"Shell command failed with error: { result .stderr } " )
17791734
17801735class HostConfigDaemon :
17811736 def __init__ (self ):
0 commit comments