@@ -1728,56 +1728,58 @@ class Memory_StatisticsCfg:
17281728 It listens to ConfigDB changes and applies them by restarting, shutting down, or reloading
17291729 the MemoryStatisticsDaemon.
17301730 """
1731-
1731+
17321732 def __init__ (self , config_db ):
17331733 self .cache = {
17341734 "enabled" : "false" ,
17351735 "retention" : "15" ,
17361736 "sampling" : "5"
17371737 }
17381738 self .config_db = config_db # Store config_db instance for further use
1739-
1739+
17401740 def load (self , memory_statistics_config : dict ):
17411741 """Load initial memory statistics configuration."""
17421742 syslog .syslog (syslog .LOG_INFO , 'Memory_StatisticsCfg: Load initial configuration' )
1743+
17431744 if not memory_statistics_config :
17441745 memory_statistics_config = {}
1745-
1746- self .memory_statistics_message ("enabled" , memory_statistics_config .get ("enabled" , {}))
1747- self .memory_statistics_message ("retention" , memory_statistics_config .get ("retention" , {}))
1748- self .memory_statistics_message ("sampling" , memory_statistics_config .get ("sampling" , {}))
1749-
1750- def memory_statistics_message (self , key , data ):
1746+
1747+ # Call memory_statistics_message to handle the initial config load for each key
1748+ self .memory_statistics_message ("enabled" , memory_statistics_config .get ("enabled" , "false" ))
1749+ self .memory_statistics_message ("retention" , memory_statistics_config .get ("retention" , "15" ))
1750+ self .memory_statistics_message ("sampling" , memory_statistics_config .get ("sampling" , "5" ))
1751+
1752+ def memory_statistics_update (self , key , data ):
17511753 """
17521754 Apply memory statistics settings handler.
17531755 Args:
17541756 key: DB table's key that triggered the change.
17551757 data: New table data to process.
17561758 """
1757- if not isinstance (data , dict ):
1758- return # Nothing to handle if data is not a dictionary
1759-
1760- update_required = any (data .get (k ) != self .cache .get (k ) for k in data )
1761- if not update_required :
1762- return
1763-
1764- try :
1765- if key == "enabled" :
1766- enabled = data .get ("enabled" , "false" ).lower () == "true"
1767- if enabled :
1768- self .restart_memory_statistics ()
1759+ # Ensure data is a string or convertible to the required value
1760+ if not isinstance (data , str ):
1761+ data = str (data )
1762+
1763+ # Check if any value has changed
1764+ if data != self .cache .get (key ):
1765+ syslog .syslog (syslog .LOG_INFO , f"Memory_StatisticsCfg: Detected change in '{ key } '" )
1766+
1767+ try :
1768+ if key == "enabled" :
1769+ enabled = data .lower () == "true"
1770+ if enabled :
1771+ self .restart_memory_statistics () # Start or restart the daemon
1772+ else :
1773+ self .shutdown_memory_statistics () # Stop the daemon if disabled
17691774 else :
1770- self .shutdown_memory_statistics ()
1771- else :
1772- # If other keys (like sampling/retention) are changed, just reload the daemon config
1773- self .reload_memory_statistics ()
1774- except Exception as e :
1775- syslog .syslog (syslog .LOG_ERR , f'Memory_StatisticsCfg: Failed to manage MemoryStatisticsDaemon: { e } ' )
1776- return
1777-
1778- # Update cache with the new values
1779- self .cache .update (data )
1780-
1775+ # If other keys (like sampling/retention) are changed, just reload the daemon config
1776+ self .reload_memory_statistics ()
1777+
1778+ # Update cache with the new value
1779+ self .cache [key ] = data
1780+ except Exception as e :
1781+ syslog .syslog (syslog .LOG_ERR , f'Memory_StatisticsCfg: Failed to manage MemoryStatisticsDaemon: { e } ' )
1782+
17811783 def restart_memory_statistics (self ):
17821784 """Restart the memory statistics daemon."""
17831785 self .shutdown_memory_statistics () # Ensure the daemon is stopped before restarting
@@ -1787,21 +1789,21 @@ class Memory_StatisticsCfg:
17871789 subprocess .Popen (['/usr/bin/memorystatsd' ])
17881790 except Exception as e :
17891791 syslog .syslog (syslog .LOG_ERR , f"Memory_StatisticsCfg: Failed to start MemoryStatisticsDaemon: { e } " )
1790-
1792+
17911793 def reload_memory_statistics (self ):
17921794 """Send SIGHUP to the MemoryStatisticsDaemon to reload its configuration."""
17931795 pid = self .get_memory_statistics_pid ()
17941796 if pid :
17951797 os .kill (pid , signal .SIGHUP ) # Notify daemon to reload its configuration
17961798 syslog .syslog (syslog .LOG_INFO , "Memory_StatisticsCfg: Sent SIGHUP to reload daemon configuration" )
1797-
1799+
17981800 def shutdown_memory_statistics (self ):
17991801 """Send SIGTERM to stop the MemoryStatisticsDaemon gracefully."""
18001802 pid = self .get_memory_statistics_pid ()
18011803 if pid :
18021804 os .kill (pid , signal .SIGTERM ) # Graceful shutdown
18031805 syslog .syslog (syslog .LOG_INFO , "Memory_StatisticsCfg: Sent SIGTERM to stop MemoryStatisticsDaemon" )
1804-
1806+
18051807 def get_memory_statistics_pid (self ):
18061808 """Retrieve the PID of the running MemoryStatisticsDaemon."""
18071809 try :
@@ -1812,16 +1814,6 @@ class Memory_StatisticsCfg:
18121814 syslog .syslog (syslog .LOG_ERR , f"Memory_StatisticsCfg: Failed to retrieve MemoryStatisticsDaemon PID: { e } " )
18131815 return None
18141816
1815- def on_config_change (self , memory_statistics_config ):
1816- """Callback function to handle configuration changes from ConfigDB."""
1817- syslog .syslog (syslog .LOG_INFO , "Memory_StatisticsCfg: Configuration change detected" )
1818- enabled = memory_statistics_config .get ("enabled" , "false" )
1819-
1820- if enabled == "true" :
1821- self .restart_memory_statistics () # Start or restart the daemon
1822- else :
1823- self .shutdown_memory_statistics () # Stop the daemon if disabled
1824-
18251817class SerialConsoleCfg :
18261818
18271819 def __init__ (self ):
0 commit comments