Skip to content
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
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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 Oct 14, 2024
f4811d1
resolved the errors
kanza-latif Oct 14, 2024
b8d7274
resolved the errors
kanza-latif Oct 14, 2024
636f30a
added test filw
kanza-latif Oct 21, 2024
81df5f0
updated test cases
kanza-latif Oct 23, 2024
1ad1222
updated test cases
kanza-latif Oct 23, 2024
29afad4
updated the mock db
kanza-latif Oct 28, 2024
aecbc72
removed pre-commit errors
kanza-latif Oct 28, 2024
2d066ee
resolivng pre-commit errors
kanza-latif Oct 28, 2024
0cf9043
resolivng pre-commit errors
kanza-latif Oct 28, 2024
52d7c0a
added mock db in config file
kanza-latif Oct 28, 2024
43f1f9c
removed incomaptibilities from the config and test files
kanza-latif Oct 28, 2024
b4716c3
removed incomaptibilities from the config and test files
kanza-latif Oct 28, 2024
b17f226
updated the test file
kanza-latif Oct 28, 2024
a6680d9
updated the test file
kanza-latif Oct 28, 2024
732612f
corrected the failing test case
kanza-latif Oct 28, 2024
7b17295
added testfile for show commands
kanza-latif Nov 5, 2024
5e72f6b
added testfile for show commands
kanza-latif Nov 5, 2024
a51c84d
removed the commented import
kanza-latif Nov 6, 2024
07fbf15
Add show memory-stats CLI command with Dict2Obj handling, clean outpu…
Arham-Nasir Nov 13, 2024
9ddaf96
updated config and test files
kanza-latif Nov 25, 2024
5c6b013
updated config and test files
kanza-latif Nov 25, 2024
81be860
update CLI commands for memory statistics configuration and management.
Arham-Nasir Dec 26, 2024
02848eb
Add show commands for memory statistics and configuration
Arham-Nasir Dec 26, 2024
4aab773
Add memory statistics monitoring commands to documentation.
Arham-Nasir Dec 26, 2024
bc8c240
Merge branch 'master' into show-config
Arham-Nasir Dec 26, 2024
c47b61b
Implement single DB connection for config commands
Arham-Nasir Dec 30, 2024
cf7f6d0
add closelog to ensure that connection is closed
Arham-Nasir Jan 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
220 changes: 220 additions & 0 deletions config/memory_statistics.py
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syslog

Suggest to close the log after msg being logged.

def log_message(...):
try:
syslog.openlog(...)
syslog.syslog(...)
finally:
syslog.closelog()

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.



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()
Loading
Loading