Skip to content

Commit

Permalink
Added logrotate config and show commands
Browse files Browse the repository at this point in the history
Signed-off-by: Yevhen Fastiuk <yfastiuk@nvidia.com>
  • Loading branch information
fastiuk committed Apr 27, 2024
1 parent 7670609 commit e6aafd4
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 1 deletion.
71 changes: 71 additions & 0 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7375,5 +7375,76 @@ def date(date, time):
clicommon.run_command(['timedatectl', 'set-time', date_time])


#
# 'logrotate' group ('config logrotate ...')
#
@config.group()
def logrotate():
"""Configuring logrotate"""
pass


@logrotate.command()
@click.argument('file', metavar='<syslog|debug>', required=True,
type=click.Choice(['syslog', 'debug']))
@click.argument('disk_percentage', metavar='<disk-percentage>',
required=True, type=float)
def disk_percentage(file, disk_percentage):
"""Configuring logrotate disk-precentage file <syslog|debug> <disk_percentage>"""

if 0 > disk_percentage > 100:
click.echo(f'Disk percentage {disk_percentage} is not in range [0 - 100]')
pass

config_db = ConfigDBConnector()
config_db.connect()
config_db.mod_entry(swsscommon.CFG_LOGGING_TABLE_NAME, file,
{'disk_percentage': disk_percentage})


@logrotate.command()
@click.argument('file', metavar='<syslog|debug>', required=True,
type=click.Choice(['syslog', 'debug']))
@click.argument('frequency', metavar='<daily|weekly|monthly|yearly>',
required=True,
type=click.Choice(['daily', 'weekly', 'monthly', 'yearly']))
def frequency(file, frequency):
"""Configuring logrotate frequency file <syslog|debug> <frequency>"""
config_db = ConfigDBConnector()
config_db.connect()
config_db.mod_entry(swsscommon.CFG_LOGGING_TABLE_NAME, file,
{'frequency': frequency})


@logrotate.command()
@click.argument('file', metavar='<syslog|debug>', required=True,
type=click.Choice(['syslog', 'debug']))
@click.argument('max_number', metavar='<max-number>',
type=click.IntRange(0, 999999), required=True)
def max_number(file, max_number):
"""Configuring logrotate max-number file <syslog|debug> <max_number>"""
config_db = ConfigDBConnector()
config_db.connect()
config_db.mod_entry(swsscommon.CFG_LOGGING_TABLE_NAME, file,
{'max_number': max_number})


@logrotate.command()
@click.argument('file', metavar='<syslog|debug>', required=True,
type=click.Choice(['syslog', 'debug']))
@click.argument('size', metavar='<size>', type=float, required=True)
def size(file, size):
"""Configuring logrotate size file <syslog|debug> <size>"""

if 0.001 > size > 3500.0:
click.echo(f'Size {disk_percentage} is not in range [0.001 - 3500.0]')
pass

config_db = ConfigDBConnector()
config_db.connect()
config_db.mod_entry(swsscommon.CFG_LOGGING_TABLE_NAME, file,
{'size': size})


if __name__ == '__main__':
config()
69 changes: 68 additions & 1 deletion doc/Command-Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@
* [Reloading Configuration](#reloading-configuration)
* [Loading Management Configuration](#loading-management-configuration)
* [Saving Configuration to a File for Persistence](#saving-configuration-to-a-file-for-persistence)
* [Loopback Interfaces](#loopback-interfaces)
* [Logrotate](#logrotate)
* [Logrotate config commands](#logrotate-config-commands)
* [Logrotate show command](#logrotate-show-command)
* [Loopback Interfaces](#loopback-interfaces)
* [Loopback show commands](#loopback-show-commands)
* [Loopback config commands](#loopback-config-commands)
* [VRF Configuration](#vrf-configuration)
Expand Down Expand Up @@ -13206,3 +13209,67 @@ Sending 3 magic packet to 11:33:55:77:99:bb via interface Vlan1000
```
For the 4th example, it specifise 2 target MAC addresses and `count` is 3. So it'll send 6 magic packets in total.
# Logrotate Commands
This sub-section explains the list of the configuration options available for logrotate feature.
## Logrotate config commands
- Rotate logs when they surpass a specified percentage of disk
```
admin@sonic:~$ config logrotate disk-percentage <> <>
Usage: config logrotate disk-precentage [OPTIONS] <syslog|debug> <disk-percentage>
Configuring logrotate disk-precentage file <syslog|debug> <disk_percentage>
Options:
-?, -h, --help Show this message and exit.
```
- Log files rotation frequency
```
admin@sonic:~$ config logrotate frequency <syslog|debug> <daily|weekly|monthly|yearly>
Usage: config logrotate frequency [OPTIONS] <syslog|debug> <daily|weekly|monthly|yearly>
Configuring logrotate frequency file <syslog|debug> <frequency>
Options:
-?, -h, --help Show this message and exit.
```
- Max number of log files to keep
```
admin@sonic:~$ config logrotate max-number <syslog|debug> <max-number>
Usage: config logrotate max-number [OPTIONS] <syslog|debug> <max-number>
Configuring logrotate max-number file <syslog|debug> <max_number>
Options:
-?, -h, --help Show this message and exit.
```
- Rotate logs if they grow bigger then size in Mebibytes
```
admin@sonic:~$ config logrotate size <syslog|debug> <size>
Usage: config logrotate size [OPTIONS] <syslog|debug> <size>
Configuring logrotate size file <syslog|debug> <size>
Options:
-h, -?, --help Show this message and exit.
```
## Logrotate show command
- Show logrotate configuration
```
admin@sonic:~$ show logrotate
Usage: show logrotate [OPTIONS] COMMAND [ARGS]...
Show logrotate configuration
Options:
-?, -h, --help Show this message and exit.
```
```
admin@sonic:~$ show logrotate
file disk-percentage frequency max-number size
------ ----------------- ----------- ------------ ------
syslog 10.2 daily 10 20.0
debug 50.5 weekly 20 10.0
```
24 changes: 24 additions & 0 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2156,6 +2156,30 @@ def suppress_pending_fib(db):
click.echo(state)



#
# 'logrorare' command group ("show logrotate ...")
#
@cli.group('logrotate', invoke_without_command=True)
@clicommon.pass_db
def logrotate(db):
"""Show logrotate configuration"""

table = []

logging_table = db.cfgdb.get_table('LOGGING')
for key, value in logging_table.items():
disk_percentage = value.get('disk_percentage', '')
frequency = value.get('frequency', '')
max_number = value.get('max_number', '')
size = value.get('size', '')

table.append((key, disk_percentage, frequency, max_number, size))

hdrs = ['file', 'disk-percentage', 'frequency', 'max-number', 'size']
click.echo(tabulate(table, headers=hdrs, tablefmt='simple', missingval=''))


# Load plugins and register them
helper = util_base.UtilHelper()
helper.load_and_register_plugins(plugins, cli)
Expand Down
60 changes: 60 additions & 0 deletions tests/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2635,3 +2635,63 @@ def test_date_bad(self):
@classmethod
def teardown_class(cls):
print('TEARDOWN')


class TestConfigLogrotate(object):
@classmethod
def setup_class(cls):
print('SETUP')
import config.main
importlib.reload(config.main)

@patch('utilities_common.cli.run_command',
mock.MagicMock(side_effect=mock_run_command_side_effect))
def test_logrotate_disk_percentage(self):
runner = CliRunner()
obj = {'db': Db().cfgdb}

result = runner.invoke(
config.config.commands['logrotate'].commands['disk-percentage'],
['debug', '24.25'], obj=obj)

assert result.exit_code == 0

@patch('utilities_common.cli.run_command',
mock.MagicMock(side_effect=mock_run_command_side_effect))
def test_logrotate_frequency(self):
runner = CliRunner()
obj = {'db': Db().cfgdb}

result = runner.invoke(
config.config.commands['logrotate'].commands['frequency'],
['debug', 'daily'], obj=obj)

assert result.exit_code == 0

@patch('utilities_common.cli.run_command',
mock.MagicMock(side_effect=mock_run_command_side_effect))
def test_logrotate_max_number(self):
runner = CliRunner()
obj = {'db': Db().cfgdb}

result = runner.invoke(
config.config.commands['logrotate'].commands['max-number'],
['debug', '2024'], obj=obj)

assert result.exit_code == 0

@patch('utilities_common.cli.run_command',
mock.MagicMock(side_effect=mock_run_command_side_effect))
def test_logrotate_size(self):
runner = CliRunner()
obj = {'db': Db().cfgdb}

result = runner.invoke(
config.config.commands['logrotate'].commands['size'],
['debug', '30.0'], obj=obj)

assert result.exit_code == 0

@classmethod
def teardown_class(cls):
print('TEARDOWN')
6 changes: 6 additions & 0 deletions tests/show_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,12 @@ def test_show_ztp(self, mock_run_command):
assert result.exit_code == 0
mock_run_command.assert_called_with(['ztp', 'status', '--verbose'], display_cmd=True)

@patch('show.main.run_command')
def test_show_logrotate(self, mock_run_command):
runner = CliRunner()
result = runner.invoke(show.cli.commands['logrotate'])
assert result.exit_code == 0

def teardown(self):
print('TEAR DOWN')

Expand Down

0 comments on commit e6aafd4

Please sign in to comment.