-
Notifications
You must be signed in to change notification settings - Fork 667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memory Statistics Config and Show Commands #3575
Open
kanza-latif
wants to merge
28
commits into
sonic-net:master
Choose a base branch
from
kanza-latif:show-config
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,993
−0
Open
Changes from 27 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
ac16864
added show and config commands
kanza-latif f4811d1
resolved the errors
kanza-latif b8d7274
resolved the errors
kanza-latif 636f30a
added test filw
kanza-latif 81df5f0
updated test cases
kanza-latif 1ad1222
updated test cases
kanza-latif 29afad4
updated the mock db
kanza-latif aecbc72
removed pre-commit errors
kanza-latif 2d066ee
resolivng pre-commit errors
kanza-latif 0cf9043
resolivng pre-commit errors
kanza-latif 52d7c0a
added mock db in config file
kanza-latif 43f1f9c
removed incomaptibilities from the config and test files
kanza-latif b4716c3
removed incomaptibilities from the config and test files
kanza-latif b17f226
updated the test file
kanza-latif a6680d9
updated the test file
kanza-latif 732612f
corrected the failing test case
kanza-latif 7b17295
added testfile for show commands
kanza-latif 5e72f6b
added testfile for show commands
kanza-latif a51c84d
removed the commented import
kanza-latif 07fbf15
Add show memory-stats CLI command with Dict2Obj handling, clean outpu…
Arham-Nasir 9ddaf96
updated config and test files
kanza-latif 5c6b013
updated config and test files
kanza-latif 81be860
update CLI commands for memory statistics configuration and management.
Arham-Nasir 02848eb
Add show commands for memory statistics and configuration
Arham-Nasir 4aab773
Add memory statistics monitoring commands to documentation.
Arham-Nasir bc8c240
Merge branch 'master' into show-config
Arham-Nasir c47b61b
Implement single DB connection for config commands
Arham-Nasir cf7f6d0
add closelog to ensure that connection is closed
Arham-Nasir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
import syslog | ||
|
||
import click | ||
from swsscommon.swsscommon import ConfigDBConnector | ||
|
||
# Constants | ||
MEMORY_STATISTICS_TABLE = "MEMORY_STATISTICS" | ||
MEMORY_STATISTICS_KEY = "memory_statistics" | ||
SAMPLING_INTERVAL_MIN = 3 | ||
SAMPLING_INTERVAL_MAX = 15 | ||
RETENTION_PERIOD_MIN = 1 | ||
RETENTION_PERIOD_MAX = 30 | ||
DEFAULT_SAMPLING_INTERVAL = 5 # minutes | ||
DEFAULT_RETENTION_PERIOD = 15 # days | ||
|
||
|
||
def log_to_syslog(message, level=syslog.LOG_INFO): | ||
"""Log a message to syslog. | ||
|
||
This function logs the provided message to syslog at the specified level. | ||
It opens the syslog with the application name 'memory_statistics' and the | ||
appropriate log level. | ||
|
||
Args: | ||
message (str): The message to log. | ||
level (int): The syslog log level. | ||
""" | ||
syslog.openlog("memory_statistics", syslog.LOG_PID | syslog.LOG_CONS, syslog.LOG_USER) | ||
syslog.syslog(level, message) | ||
|
||
|
||
class MemoryStatisticsDB: | ||
"""Singleton class to handle memory statistics database connection. | ||
|
||
This class ensures only one instance of the database connection exists using | ||
the Singleton pattern. It provides access to the database connection and | ||
ensures that it is created only once during the application's lifetime. | ||
""" | ||
_instance = None | ||
_db = None | ||
|
||
def __new__(cls): | ||
"""Ensure only one instance of MemoryStatisticsDB is created. | ||
|
||
This method implements the Singleton pattern to guarantee that only one | ||
instance of the MemoryStatisticsDB class exists. If no instance exists, | ||
it creates one and connects to the database. | ||
|
||
Returns: | ||
MemoryStatisticsDB: The singleton instance of the class. | ||
""" | ||
if cls._instance is None: | ||
cls._instance = super(MemoryStatisticsDB, cls).__new__(cls) | ||
cls._db = ConfigDBConnector() | ||
cls._db.connect() | ||
return cls._instance | ||
|
||
@classmethod | ||
def get_db(cls): | ||
"""Get the singleton database connection instance. | ||
|
||
Returns the existing database connection instance. If it doesn't exist, | ||
a new instance is created by calling the __new__ method. | ||
|
||
Returns: | ||
ConfigDBConnector: The database connection instance. | ||
""" | ||
if cls._instance is None: | ||
cls._instance = cls() | ||
return cls._db | ||
|
||
|
||
def update_memory_statistics_status(status): | ||
""" | ||
Update the status of the memory statistics feature in the config DB. | ||
|
||
This function modifies the configuration database to enable or disable | ||
memory statistics collection based on the provided status. It also logs | ||
the action and returns a tuple indicating whether the operation was successful. | ||
|
||
Args: | ||
status (str): The status to set for memory statistics ("true" or "false"). | ||
|
||
Returns: | ||
tuple: A tuple (success, error_message) where `success` is a boolean | ||
indicating whether the operation was successful, and | ||
`error_message` contains any error details if unsuccessful. | ||
""" | ||
try: | ||
db = MemoryStatisticsDB.get_db() | ||
db.mod_entry(MEMORY_STATISTICS_TABLE, MEMORY_STATISTICS_KEY, {"enabled": status}) | ||
msg = f"Memory statistics feature {'enabled' if status == 'true' else 'disabled'} successfully." | ||
click.echo(msg) | ||
log_to_syslog(msg) | ||
return True, None | ||
except Exception as e: | ||
error_msg = f"Error updating memory statistics status: {e}" | ||
click.echo(error_msg, err=True) | ||
log_to_syslog(error_msg, syslog.LOG_ERR) | ||
return False, error_msg | ||
|
||
|
||
@click.group() | ||
def cli(): | ||
"""Memory statistics configuration tool. | ||
|
||
This command-line interface (CLI) allows users to configure and manage | ||
memory statistics settings such as enabling/disabling the feature and | ||
modifying parameters like the sampling interval and retention period. | ||
""" | ||
pass | ||
|
||
|
||
@cli.group() | ||
def config(): | ||
"""Configuration commands for managing memory statistics.""" | ||
pass | ||
|
||
|
||
@config.group(name='memory-stats') | ||
def memory_stats(): | ||
"""Configure memory statistics collection and settings.""" | ||
pass | ||
|
||
|
||
@memory_stats.command(name='enable') | ||
def memory_stats_enable(): | ||
"""Enable memory statistics collection. | ||
|
||
This command enables the collection of memory statistics on the device. | ||
It updates the configuration and reminds the user to run 'config save' | ||
to persist changes. | ||
""" | ||
success, error = update_memory_statistics_status("true") | ||
if success: | ||
click.echo("Reminder: Please run 'config save' to persist changes.") | ||
log_to_syslog("Memory statistics enabled. Reminder to run 'config save'") | ||
|
||
|
||
@memory_stats.command(name='disable') | ||
def memory_stats_disable(): | ||
"""Disable memory statistics collection. | ||
|
||
This command disables the collection of memory statistics on the device. | ||
It updates the configuration and reminds the user to run 'config save' | ||
to persist changes. | ||
""" | ||
success, error = update_memory_statistics_status("false") | ||
if success: | ||
click.echo("Reminder: Please run 'config save' to persist changes.") | ||
log_to_syslog("Memory statistics disabled. Reminder to run 'config save'") | ||
|
||
|
||
@memory_stats.command(name='sampling-interval') | ||
@click.argument("interval", type=int) | ||
def memory_stats_sampling_interval(interval): | ||
""" | ||
Set the sampling interval for memory statistics. | ||
|
||
This command allows users to configure the frequency at which memory statistics | ||
are collected. The interval must be between 3 and 15 minutes. | ||
|
||
Args: | ||
interval (int): The sampling interval in minutes (must be between 3 and 15). | ||
""" | ||
if not (SAMPLING_INTERVAL_MIN <= interval <= SAMPLING_INTERVAL_MAX): | ||
error_msg = ( | ||
f"Error: Sampling interval must be between {SAMPLING_INTERVAL_MIN} " | ||
f"and {SAMPLING_INTERVAL_MAX} minutes." | ||
) | ||
click.echo(error_msg, err=True) | ||
log_to_syslog(error_msg, syslog.LOG_ERR) | ||
return | ||
|
||
try: | ||
db = MemoryStatisticsDB.get_db() | ||
db.mod_entry(MEMORY_STATISTICS_TABLE, MEMORY_STATISTICS_KEY, {"sampling_interval": str(interval)}) | ||
success_msg = f"Sampling interval set to {interval} minutes successfully." | ||
click.echo(success_msg) | ||
log_to_syslog(success_msg) | ||
click.echo("Reminder: Please run 'config save' to persist changes.") | ||
except Exception as e: | ||
error_msg = f"Error setting sampling interval: {e}" | ||
click.echo(error_msg, err=True) | ||
log_to_syslog(error_msg, syslog.LOG_ERR) | ||
|
||
|
||
@memory_stats.command(name='retention-period') | ||
@click.argument("period", type=int) | ||
def memory_stats_retention_period(period): | ||
""" | ||
Set the retention period for memory statistics. | ||
|
||
This command allows users to configure how long memory statistics are retained | ||
before being purged. The retention period must be between 1 and 30 days. | ||
|
||
Args: | ||
period (int): The retention period in days (must be between 1 and 30). | ||
""" | ||
if not (RETENTION_PERIOD_MIN <= period <= RETENTION_PERIOD_MAX): | ||
error_msg = f"Error: Retention period must be between {RETENTION_PERIOD_MIN} and {RETENTION_PERIOD_MAX} days." | ||
click.echo(error_msg, err=True) | ||
log_to_syslog(error_msg, syslog.LOG_ERR) | ||
return | ||
|
||
try: | ||
db = MemoryStatisticsDB.get_db() | ||
db.mod_entry(MEMORY_STATISTICS_TABLE, MEMORY_STATISTICS_KEY, {"retention_period": str(period)}) | ||
success_msg = f"Retention period set to {period} days successfully." | ||
click.echo(success_msg) | ||
log_to_syslog(success_msg) | ||
click.echo("Reminder: Please run 'config save' to persist changes.") | ||
except Exception as e: | ||
error_msg = f"Error setting retention period: {e}" | ||
click.echo(error_msg, err=True) | ||
log_to_syslog(error_msg, syslog.LOG_ERR) | ||
|
||
|
||
if __name__ == "__main__": | ||
cli() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest to close the log after msg being logged.
def log_message(...):
try:
syslog.openlog(...)
syslog.syslog(...)
finally:
syslog.closelog()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the suggestion. I have added a try-finally block to ensure syslog.closelog() is always called after logging the message. Please let me know if there is anything else you’d like me to adjust.