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

Draft/Feature/memory statistics/show commands #3629

Draft
wants to merge 101 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
101 commits
Select commit Hold shift + click to select a range
0a7e894
update the code file
Arham-Nasir Nov 26, 2024
613b21e
add memory_statistics code and test file
Arham-Nasir Nov 26, 2024
ac8a85a
update test file
Arham-Nasir Nov 27, 2024
8b33545
update test file
Arham-Nasir Nov 27, 2024
d52d7bf
update test file
Arham-Nasir Nov 27, 2024
0d530af
update test file
Arham-Nasir Nov 27, 2024
7f1fe11
update test file
Arham-Nasir Nov 27, 2024
f0a75b1
update test file
Arham-Nasir Nov 27, 2024
7ad4e56
update test file
Arham-Nasir Nov 27, 2024
9c13b70
update test file
Arham-Nasir Nov 27, 2024
8aa8d9e
update test file
Arham-Nasir Nov 27, 2024
0adf282
update test file
Arham-Nasir Nov 27, 2024
4cb0e54
update test file
Arham-Nasir Nov 27, 2024
f2a7ea6
update test file
Arham-Nasir Nov 27, 2024
8dab713
update test file
Arham-Nasir Nov 28, 2024
6dbed98
update test file
Arham-Nasir Nov 28, 2024
a757a6e
update test file
Arham-Nasir Nov 29, 2024
8bb993f
update test file
Arham-Nasir Nov 29, 2024
0313df2
update test file
Arham-Nasir Nov 29, 2024
c893d71
update test file
Arham-Nasir Nov 29, 2024
d5102b3
update test file
Arham-Nasir Nov 29, 2024
c67fe93
added more test cases
kanza-latif Dec 3, 2024
2773cd6
added more test cases
kanza-latif Dec 3, 2024
f1ca08e
added more test cases
kanza-latif Dec 3, 2024
999b6d3
added more test cases
kanza-latif Dec 3, 2024
1e82d70
added more test cases
kanza-latif Dec 4, 2024
ecef889
added more test cases
kanza-latif Dec 4, 2024
464c91c
added more test cases
kanza-latif Dec 4, 2024
c8025f7
Merge branch 'master' into feature/memory_statistics/show_commands
kanza-latif Dec 4, 2024
1bb2904
added more test cases
kanza-latif Dec 6, 2024
91141b5
added more test cases
kanza-latif Dec 9, 2024
81d7698
added more test cases
kanza-latif Dec 9, 2024
77a74b0
added more test cases
kanza-latif Dec 9, 2024
2ed5ebd
added more test cases
kanza-latif Dec 9, 2024
c3b34e9
added more test cases
kanza-latif Dec 9, 2024
50e73d8
added more test cases
kanza-latif Dec 9, 2024
ff4de69
added more test cases
kanza-latif Dec 9, 2024
98250ff
added more test cases
kanza-latif Dec 9, 2024
f275eda
added more test cases
kanza-latif Dec 9, 2024
e1b6396
added more test cases
kanza-latif Dec 9, 2024
7e18e3e
added more test cases
kanza-latif Dec 9, 2024
1791ed3
added more test cases
kanza-latif Dec 9, 2024
31c13b3
added more test cases
kanza-latif Dec 9, 2024
7fab62a
added more test cases
kanza-latif Dec 9, 2024
fa98ab3
added more test cases
kanza-latif Dec 9, 2024
1e5be1c
added more test cases
kanza-latif Dec 9, 2024
1d08b01
added more test cases
kanza-latif Dec 10, 2024
34e1d63
added more test cases
kanza-latif Dec 10, 2024
cbb946b
added more test cases
kanza-latif Dec 10, 2024
698c0a1
added more test cases
kanza-latif Dec 10, 2024
3392a86
added more test cases
kanza-latif Dec 10, 2024
402d974
added more test cases
kanza-latif Dec 10, 2024
ea64a86
added more test cases
kanza-latif Dec 10, 2024
db52cee
added more test cases
kanza-latif Dec 10, 2024
18505ea
added more test cases
kanza-latif Dec 10, 2024
39ce9ff
added more test cases
kanza-latif Dec 10, 2024
7c80bf3
added more test cases
kanza-latif Dec 10, 2024
8cbacda
added more test cases
kanza-latif Dec 10, 2024
09fa6c6
update memory_statistics file
Arham-Nasir Dec 17, 2024
25b91e7
update memory_statistics file
Arham-Nasir Dec 17, 2024
454b7af
update memory_statistics test file
Arham-Nasir Dec 17, 2024
b0cd253
update memory_statistics test file
Arham-Nasir Dec 17, 2024
9bc8b36
update memory_statistics test file
Arham-Nasir Dec 17, 2024
2aed876
update memory_statistics test file
Arham-Nasir Dec 17, 2024
eee4d99
update memory_statistics test file
Arham-Nasir Dec 17, 2024
5f29990
update memory_statistics test file
Arham-Nasir Dec 17, 2024
a72144a
update memory_statistics test file
Arham-Nasir Dec 17, 2024
d6703bd
update memory_statistics test file
Arham-Nasir Dec 17, 2024
b38360f
update memory_statistics test file
Arham-Nasir Dec 17, 2024
361546a
update memory_statistics test file
Arham-Nasir Dec 17, 2024
60eccdf
update memory_statistics test file
Arham-Nasir Dec 17, 2024
68bd055
update memory_statistics test file
Arham-Nasir Dec 17, 2024
842f647
update memory_statistics test file
Arham-Nasir Dec 17, 2024
6abb029
update memory_statistics test file
Arham-Nasir Dec 17, 2024
c1196ad
update memory_statistics test file
Arham-Nasir Dec 17, 2024
9974766
update memory_statistics test file
Arham-Nasir Dec 17, 2024
54dfcc8
add config memory_statistics test file
Arham-Nasir Dec 19, 2024
52b2df5
add config memory_statistics test file
Arham-Nasir Dec 19, 2024
921dfb3
add config memory_statistics test file
Arham-Nasir Dec 19, 2024
fe9bb5d
update memory_statistics config and test file
Arham-Nasir Dec 21, 2024
b643ba7
update memory_statistics config and test file
Arham-Nasir Dec 21, 2024
6c2e837
update memory_statistics config and test file
Arham-Nasir Dec 21, 2024
4d4fb1f
update memory_statistics config and test file
Arham-Nasir Dec 21, 2024
7433c9e
update memory_statistics config and test file
Arham-Nasir Dec 21, 2024
476b0df
update memory_statistics config and test file
Arham-Nasir Dec 22, 2024
7db3c31
update memory_statistics config and test file
Arham-Nasir Dec 22, 2024
f21a349
update test file
Arham-Nasir Dec 22, 2024
d36ee47
update test file
Arham-Nasir Dec 22, 2024
63c5bcf
add the show commands and their test cases
Arham-Nasir Dec 24, 2024
15ababa
resolved pretest issues
Arham-Nasir Dec 24, 2024
e469e9c
resolved pretest issues
Arham-Nasir Dec 24, 2024
a77aa37
resolved pretest issues
Arham-Nasir Dec 24, 2024
5790917
add description of commands in command line reference
Arham-Nasir Dec 24, 2024
5d3057f
update description of commands in command line reference
Arham-Nasir Dec 24, 2024
1798042
update description of commands in command line reference
Arham-Nasir Dec 24, 2024
4899c12
update description of commands in command line reference
Arham-Nasir Dec 24, 2024
fa85737
update the code
Arham-Nasir Dec 30, 2024
a2931d7
update the code
Arham-Nasir Dec 30, 2024
682e3c6
resolve the pretest issues
Arham-Nasir Dec 30, 2024
1f223d7
update the file
Arham-Nasir Jan 7, 2025
3854bb3
update the file
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
272 changes: 272 additions & 0 deletions config/memory_statistics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
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, ensuring the connection is closed after logging.

Args:
message (str): The message to log.
level (int): The syslog log level.
"""
try:
syslog.openlog("memory_statistics", syslog.LOG_PID | syslog.LOG_CONS, syslog.LOG_USER)
syslog.syslog(level, message)
finally:
syslog.closelog()


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.

Example:
$ config memory-stats enable
$ config memory-stats sampling-interval 5
"""
pass


@config.group(name='memory-stats')
def memory_stats():
"""Configure memory statistics collection and settings.

This group contains commands to enable/disable memory statistics collection
and configure related parameters.

Examples:
Enable memory statistics:
$ config memory-stats enable

Set sampling interval to 5 minutes:
$ config memory-stats sampling-interval 5

Set retention period to 7 days:
$ config memory-stats retention-period 7

Disable memory statistics:
$ config memory-stats disable
"""
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.

Example:
$ config memory-stats enable
Memory statistics feature enabled successfully.
Reminder: Please 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.

Example:
$ config memory-stats disable
Memory statistics feature disabled successfully.
Reminder: Please 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).

Examples:
Set sampling interval to 5 minutes:
$ config memory-stats sampling-interval 5
Sampling interval set to 5 minutes successfully.
Reminder: Please run 'config save' to persist changes.

Invalid interval example:
$ config memory-stats sampling-interval 20
Error: Sampling interval must be between 3 and 15 minutes.
"""
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).

Examples:
Set retention period to 7 days:
$ config memory-stats retention-period 7
Retention period set to 7 days successfully.
Reminder: Please run 'config save' to persist changes.

Invalid period example:
$ config memory-stats retention-period 45
Error: Retention period must be between 1 and 30 days.
"""
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