Skip to content

Commit

Permalink
update CLI commands for memory statistics configuration and management.
Browse files Browse the repository at this point in the history
Signed-off-by: Arham-Nasir <arqamnasir719@gmail.com>
  • Loading branch information
Arham-Nasir committed Dec 26, 2024
1 parent 5c6b013 commit 81be860
Show file tree
Hide file tree
Showing 2 changed files with 317 additions and 182 deletions.
180 changes: 104 additions & 76 deletions config/memory_statistics.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import click
import syslog

import click
from swsscommon.swsscommon import ConfigDBConnector

# Default values
DEFAULT_SAMPLING_INTERVAL = 5
DEFAULT_RETENTION_PERIOD = 15
# 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):
Expand All @@ -13,106 +20,127 @@ def log_to_syslog(message, level=syslog.LOG_INFO):
syslog.syslog(level, message)


def get_db_connection():
"""Create and return a database connection."""
db = ConfigDBConnector()
db.connect()
return db


def update_memory_statistics_status(status, db):
"""Updates the status of the memory statistics feature in the config DB."""
"""
Updates the status of the memory statistics feature in the config DB.
Returns a tuple of (success, error_message).
"""
try:
db.mod_entry("MEMORY_STATISTICS", "memory_statistics", {"enabled": status})
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) # Log to syslog
return True, None # Success: return True and no error
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) # Log error to syslog
return False, error_msg # Failure: return False and the error message
log_to_syslog(error_msg, syslog.LOG_ERR)
return False, error_msg


@click.command()
def memory_statistics_enable():
"""Enable memory statistics."""
db = ConfigDBConnector()
db.connect()
@click.group()
def cli():
"""Memory statistics configuration tool."""
pass


@cli.group()
def config():
"""Configuration commands."""
pass


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


@memory_stats.command(name='enable')
def memory_stats_enable():
"""Enable memory statistics collection."""
db = get_db_connection()
success, error = update_memory_statistics_status("true", db)
if not success:
click.echo(error, err=True) # Handle error if unsuccessful
else:
success_msg = "Memory statistics enabled successfully."
click.echo(success_msg)
log_to_syslog(success_msg) # Log to syslog
reminder_msg = "Reminder: Please run 'config save' to persist changes."
click.echo(reminder_msg)
log_to_syslog(reminder_msg) # Log to syslog
if success:
click.echo("Reminder: Please run 'config save' to persist changes.")
log_to_syslog("Memory statistics enabled. Reminder to run 'config save'")


@click.command()
def memory_statistics_disable():
"""Disable memory statistics."""
db = ConfigDBConnector()
db.connect()
@memory_stats.command(name='disable')
def memory_stats_disable():
"""Disable memory statistics collection."""
db = get_db_connection()
success, error = update_memory_statistics_status("false", db)
if not success:
click.echo(error, err=True) # Handle error if unsuccessful
else:
success_msg = "Memory statistics disabled successfully."
click.echo(success_msg)
log_to_syslog(success_msg) # Log to syslog
reminder_msg = "Reminder: Please run 'config save' to persist changes."
click.echo(reminder_msg)
log_to_syslog(reminder_msg) # Log to syslog


@click.command()
@click.argument("retention_period", type=int, required=False, default=DEFAULT_RETENTION_PERIOD)
def memory_statistics_retention_period(retention_period):
"""Set retention period for memory statistics."""
if not (1 <= retention_period <= 30):
error_msg = "Error: Retention period must be between 1 and 30."
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 sampling interval for memory statistics.
The sampling interval must be between 3 and 15 minutes.
This determines how frequently memory usage data is collected.
"""
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) # Log error to syslog
log_to_syslog(error_msg, syslog.LOG_ERR)
return

db = ConfigDBConnector()
db.connect()
db = get_db_connection()
try:
db.mod_entry("MEMORY_STATISTICS", "memory_statistics", {"retention_period": retention_period})
success_msg = f"Retention period set to {retention_period} successfully."
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) # Log to syslog
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}"
error_msg = f"Error setting sampling interval: {e}"
click.echo(error_msg, err=True)
log_to_syslog(error_msg, syslog.LOG_ERR) # Log error to syslog
log_to_syslog(error_msg, syslog.LOG_ERR)


@click.command()
@click.argument("sampling_interval", type=int, required=False, default=DEFAULT_SAMPLING_INTERVAL)
def memory_statistics_sampling_interval(sampling_interval):
"""Set sampling interval for memory statistics."""
if not (3 <= sampling_interval <= 15):
error_msg = "Error: Sampling interval must be between 3 and 15."
@memory_stats.command(name='retention-period')
@click.argument("period", type=int)
def memory_stats_retention_period(period):
"""
Set retention period for memory statistics.
The retention period must be between 1 and 30 days.
This determines how long memory usage data is stored before being purged.
"""
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) # Log error to syslog
log_to_syslog(error_msg, syslog.LOG_ERR)
return

db = ConfigDBConnector()
db.connect()
db = get_db_connection()
try:
db.mod_entry("MEMORY_STATISTICS", "memory_statistics", {"sampling_interval": sampling_interval})
success_msg = f"Sampling interval set to {sampling_interval} successfully."
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) # Log to syslog
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}"
error_msg = f"Error setting retention period: {e}"
click.echo(error_msg, err=True)
log_to_syslog(error_msg, syslog.LOG_ERR) # Log error to syslog


def get_memory_statistics_table(db):
"""Retrieve MEMORY_STATISTICS table from config DB."""
return db.get_table("MEMORY_STATISTICS")
log_to_syslog(error_msg, syslog.LOG_ERR)


def check_memory_statistics_table_existence(table):
"""Check if MEMORY_STATISTICS table exists in the given table."""
return "memory_statistics" in table
if __name__ == "__main__":
cli()
Loading

0 comments on commit 81be860

Please sign in to comment.