diff options
-rw-r--r-- | data/templates/logs/logrotate/vyos-atop.tmpl | 2 | ||||
-rw-r--r-- | data/templates/logs/logrotate/vyos-rsyslog.tmpl | 13 | ||||
-rw-r--r-- | interface-definitions/system-logs.xml.in | 37 | ||||
-rwxr-xr-x | smoketest/scripts/cli/test_system_logs.py | 36 | ||||
-rwxr-xr-x | src/conf_mode/system-logs.py | 15 |
5 files changed, 94 insertions, 9 deletions
diff --git a/data/templates/logs/logrotate/vyos-atop.tmpl b/data/templates/logs/logrotate/vyos-atop.tmpl index 797bf237f..444d1da36 100644 --- a/data/templates/logs/logrotate/vyos-atop.tmpl +++ b/data/templates/logs/logrotate/vyos-atop.tmpl @@ -2,7 +2,7 @@ daily dateext dateformat _%Y-%m-%d_%H-%M-%S - maxsize {{ maxsize|default('10') }}M + maxsize {{ max_size|default('10') }}M missingok nocompress nocreate diff --git a/data/templates/logs/logrotate/vyos-rsyslog.tmpl b/data/templates/logs/logrotate/vyos-rsyslog.tmpl new file mode 100644 index 000000000..3af1bfd8e --- /dev/null +++ b/data/templates/logs/logrotate/vyos-rsyslog.tmpl @@ -0,0 +1,13 @@ +/var/log/messages { + create + missingok + nomail + notifempty + rotate {{ rotate|default('10') }} + size {{ max_size|default('1') }}M + postrotate + # restart rsyslog service + systemctl restart rsyslog.service + endscript +} + diff --git a/interface-definitions/system-logs.xml.in b/interface-definitions/system-logs.xml.in index 5f81b8f00..dc5751372 100644 --- a/interface-definitions/system-logs.xml.in +++ b/interface-definitions/system-logs.xml.in @@ -18,7 +18,7 @@ <help>Atop logs options (system resources usage)</help> </properties> <children> - <leafNode name="maxsize"> + <leafNode name="max-size"> <properties> <help>Size of a single log file that triggers rotation</help> <valueHelp> @@ -48,6 +48,41 @@ </leafNode> </children> </node> + <node name="messages"> + <properties> + <help>The /var/log/messages file rotation</help> + </properties> + <children> + <leafNode name="max-size"> + <properties> + <help>Size of a single log file that triggers rotation</help> + <valueHelp> + <format>u32:1-1024</format> + <description>Size in MB (default: 1)</description> + </valueHelp> + <constraint> + <validator name="numeric" argument="--range 1-1024" /> + </constraint> + <constraintErrorMessage>The size must be between 1 and 1024 MB</constraintErrorMessage> + </properties> + <defaultValue>10</defaultValue> + </leafNode> + <leafNode name="rotate"> + <properties> + <help>Count of rotations before old logs will be deleted</help> + <valueHelp> + <format>u32:1-100</format> + <description>Rotations (default: 10)</description> + </valueHelp> + <constraint> + <validator name="numeric" argument="--range 1-100" /> + </constraint> + <constraintErrorMessage>The count must be between 1 and 100</constraintErrorMessage> + </properties> + <defaultValue>10</defaultValue> + </leafNode> + </children> + </node> </children> </node> </children> diff --git a/smoketest/scripts/cli/test_system_logs.py b/smoketest/scripts/cli/test_system_logs.py index bb23dcb1d..0c11c4663 100755 --- a/smoketest/scripts/cli/test_system_logs.py +++ b/smoketest/scripts/cli/test_system_logs.py @@ -19,11 +19,14 @@ import unittest from base_vyostest_shim import VyOSUnitTestSHIM from vyos.util import read_file -# path to logrotate config for atop +# path to logrotate configs logrotate_atop_file = '/etc/logrotate.d/vyos-atop' +logrotate_rsyslog_file = '/etc/logrotate.d/vyos-rsyslog' # default values default_atop_maxsize = '10M' default_atop_rotate = '10' +default_rsyslog_size = '1M' +default_rsyslog_rotate = '10' base_path = ['system', 'logs'] @@ -64,13 +67,18 @@ class TestSystemLogs(VyOSUnitTestSHIM.TestCase): self.cli_commit() # read the config file and check content - logrotate_config = logrotate_config_parse(logrotate_atop_file) - self.assertEqual(logrotate_config['maxsize'], default_atop_maxsize) - self.assertEqual(logrotate_config['rotate'], default_atop_rotate) + logrotate_config_atop = logrotate_config_parse(logrotate_atop_file) + logrotate_config_rsyslog = logrotate_config_parse( + logrotate_rsyslog_file) + self.assertEqual(logrotate_config_atop['maxsize'], default_atop_maxsize) + self.assertEqual(logrotate_config_atop['rotate'], default_atop_rotate) + self.assertEqual(logrotate_config_rsyslog['size'], default_rsyslog_size) + self.assertEqual(logrotate_config_rsyslog['rotate'], + default_rsyslog_rotate) def test_logs_atop_maxsize(self): # test for maxsize option - self.cli_set(base_path + ['logrotate', 'atop', 'maxsize', '50']) + self.cli_set(base_path + ['logrotate', 'atop', 'max-size', '50']) self.cli_commit() # read the config file and check content @@ -86,6 +94,24 @@ class TestSystemLogs(VyOSUnitTestSHIM.TestCase): logrotate_config = logrotate_config_parse(logrotate_atop_file) self.assertEqual(logrotate_config['rotate'], '50') + def test_logs_rsyslog_size(self): + # test for size option + self.cli_set(base_path + ['logrotate', 'messages', 'max-size', '50']) + self.cli_commit() + + # read the config file and check content + logrotate_config = logrotate_config_parse(logrotate_rsyslog_file) + self.assertEqual(logrotate_config['size'], '50M') + + def test_logs_rsyslog_rotate(self): + # test for rotate option + self.cli_set(base_path + ['logrotate', 'messages', 'rotate', '50']) + self.cli_commit() + + # read the config file and check content + logrotate_config = logrotate_config_parse(logrotate_rsyslog_file) + self.assertEqual(logrotate_config['rotate'], '50') + if __name__ == '__main__': unittest.main(verbosity=2, failfast=True) diff --git a/src/conf_mode/system-logs.py b/src/conf_mode/system-logs.py index 7b5af240f..a9f2da476 100755 --- a/src/conf_mode/system-logs.py +++ b/src/conf_mode/system-logs.py @@ -24,8 +24,9 @@ from vyos.template import render from vyos.util import dict_search airbag.enable() -# path to logrotate config for atop +# path to logrotate configs logrotate_atop_file = '/etc/logrotate.d/vyos-atop' +logrotate_rsyslog_file = '/etc/logrotate.d/vyos-rsyslog' def get_config(config=None): @@ -35,7 +36,7 @@ def get_config(config=None): conf = Config() base = ['system', 'logs'] - logs_config = conf.get_config_dict(base) + logs_config = conf.get_config_dict(base, key_mangling=('-', '_')) return logs_config @@ -55,6 +56,16 @@ def generate(logs_config): syslog.debug('Adding logrotate config for atop') render(logrotate_atop_file, 'logs/logrotate/vyos-atop.tmpl', logrotate_atop) + # get configuration for logrotate rsyslog + logrotate_rsyslog = dict_search('logs.logrotate.messages', logs_config) + # provide an empty dictionary if there is no config + if not logrotate_rsyslog: + logrotate_rsyslog = {} + # generate new config file for rsyslog + syslog.debug('Adding logrotate config for rsyslog') + render(logrotate_rsyslog_file, 'logs/logrotate/vyos-rsyslog.tmpl', + logrotate_rsyslog) + def apply(logs_config): # No further actions needed |