diff --git a/data/templates/rsyslog/rsyslog.conf.j2 b/data/templates/rsyslog/rsyslog.conf.j2 index c6eb6430c..e2ff334ff 100644 --- a/data/templates/rsyslog/rsyslog.conf.j2 +++ b/data/templates/rsyslog/rsyslog.conf.j2 @@ -1,122 +1,122 @@ ### Autogenerated by system_syslog.py ### #### MODULES #### # Load input modules for local logging and kernel logging # Old-style log file format with low-precision timestamps # A modern-style logfile format with high-precision timestamps and timezone info # RSYSLOG_FileFormat module(load="builtin:omfile" Template="RSYSLOG_TraditionalFileFormat") module(load="imuxsock") # provides support for local system logging module(load="imklog") # provides kernel logging support # Import logs from journald module( load="imjournal" StateFile="/var/spool/rsyslog/imjournal.state" # Persistent state file to track the journal cursor Ratelimit.Interval="0" # Disable rate limiting (set to "0" for no limit) RateLimit.Burst="0" ) ########################### #### GLOBAL DIRECTIVES #### ########################### # Log specific programs to auth.log, then stop further processing if ( $programname == "CRON" or $programname == "sudo" or $programname == "su" ) then { action(type="omfile" file="/var/log/auth.log") stop } global(workDirectory="/var/spool/rsyslog") ############### #### RULES #### ############### # Send emergency messages to all logged-in users *.emerg action(type="omusrmsg" users="*") -{% if marker is vyos_defined %} +{% if marker is vyos_defined and marker.disable is not vyos_defined %} # Load the immark module for periodic --MARK-- message capability module(load="immark" interval="{{ marker.interval }}") {% endif %} {% if preserve_fqdn is vyos_defined %} # Preserve the fully qualified domain name (FQDN) in log messages global(preserveFQDN="on") {% if preserve_fqdn.host_name is vyos_defined and preserve_fqdn.domain_name is vyos_defined %} # Set the local hostname for log messages global(localHostname="{{ preserve_fqdn.host_name }}.{{ preserve_fqdn.domain_name }}") {% endif %} {% endif %} #### GLOBAL LOGGING #### {% if local.facility is vyos_defined %} {% set tmp = [] %} {% if local.facility is vyos_defined %} {% for facility, facility_options in local.facility.items() %} {% set _ = tmp.append(facility.replace('all', '*') ~ "." ~ facility_options.level.replace('all', 'debug')) %} {% endfor %} if prifilt("{{ tmp | join(',') }}") then { action( type="omfile" file="/var/log/messages" rotation.sizeLimit="524288" # 512Kib - maximum filesize before rotation rotation.sizeLimitCommand="/usr/sbin/logrotate {{ logrotate }}" ) } {% endif %} {% endif %} #### CONSOLE LOGGING #### {% if console.facility is vyos_defined %} {% set tmp = [] %} {% if console.facility is vyos_defined %} {% for facility, facility_options in console.facility.items() %} {% set _ = tmp.append(facility.replace('all', '*') ~ "." ~ facility_options.level.replace('all', 'debug')) %} {% endfor %} if prifilt("{{ tmp | join(',') }}") then { action(type="omfile" file="/dev/console") } {% endif %} {% endif %} #### REMOTE LOGGING #### {% if remote is vyos_defined %} {% for remote_name, remote_options in remote.items() %} {% set tmp = [] %} {% if remote_options.facility is vyos_defined %} {% for facility, facility_options in remote_options.facility.items() %} {% set _ = tmp.append(facility.replace('all', '*') ~ "." ~ facility_options.level.replace('all', 'debug')) %} {% endfor %} {% set _ = tmp.sort() %} # Remote syslog to {{ remote_name }} if prifilt("{{ tmp | join(',') }}") then { action( type="omfwd" # Remote syslog server where we send our logs to target="{{ remote_name | bracketize_ipv6 }}" # Port on the remote syslog server port="{{ remote_options.port }}" protocol="{{ remote_options.protocol }}" {% if remote_options.format.include_timezone is vyos_defined %} template="SyslogProtocol23Format" {% endif %} TCP_Framing="{{ 'octed-counted' if remote_options.format.octet_counted is vyos_defined else 'traditional' }}" {% if remote_options.source_address is vyos_defined %} Address="{{ remote_options.source_address }}" {% endif %} {% if remote_options.vrf is vyos_defined %} Device="{{ remote_options.vrf }}" {% endif %} ) } {% endif %} {% endfor %} {% endif %} # Include all configuration files in /etc/rsyslog.d/ include(file="/etc/rsyslog.d/*.conf") diff --git a/interface-definitions/system_syslog.xml.in b/interface-definitions/system_syslog.xml.in index 32dcf65ff..8b2d9cab7 100644 --- a/interface-definitions/system_syslog.xml.in +++ b/interface-definitions/system_syslog.xml.in @@ -1,109 +1,110 @@ <?xml version="1.0"?> <interfaceDefinition> <node name="system"> <children> <node name="syslog" owner="${vyos_conf_scripts_dir}/system_syslog.py"> <properties> <help>System logging</help> <priority>400</priority> </properties> <children> <node name="console"> <properties> <help>Log to system console (/dev/console)</help> </properties> <children> #include <include/syslog-facility.xml.i> </children> </node> <tagNode name="remote"> <properties> <help>Log to remote host</help> <constraint> <validator name="ip-address"/> <validator name="fqdn"/> </constraint> <constraintErrorMessage>Invalid host (FQDN or IP address)</constraintErrorMessage> <valueHelp> <format>ipv4</format> <description>Remote syslog server IPv4 address</description> </valueHelp> <valueHelp> <format>ipv6</format> <description>Remote syslog server IPv6 address</description> </valueHelp> <valueHelp> <format>hostname</format> <description>Remote syslog server FQDN</description> </valueHelp> </properties> <children> #include <include/syslog-facility.xml.i> <node name="format"> <properties> <help>Logging format</help> </properties> <children> <leafNode name="octet-counted"> <properties> <help>Allows for the transmission of all characters inside a syslog message</help> <valueless/> </properties> </leafNode> <leafNode name="include-timezone"> <properties> <help>Include system timezone in syslog message</help> <valueless/> </properties> </leafNode> </children> </node> #include <include/port-number.xml.i> <leafNode name="port"> <defaultValue>514</defaultValue> </leafNode> #include <include/protocol-tcp-udp.xml.i> #include <include/source-address-ipv4-ipv6.xml.i> #include <include/interface/vrf.xml.i> </children> </tagNode> <node name="local"> <properties> <help>Log to standard system location /var/log/messages</help> </properties> <children> #include <include/syslog-facility.xml.i> </children> </node> <node name="marker"> <properties> <help>Mark messages sent to syslog</help> </properties> <children> + #include <include/generic-disable-node.xml.i> <leafNode name="interval"> <properties> <help>Mark message interval</help> <valueHelp> <format>u32:1-65535</format> <description>Time in seconds</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 1-65535"/> </constraint> <constraintErrorMessage>Port number must be in range 1 to 65535</constraintErrorMessage> </properties> <defaultValue>1200</defaultValue> </leafNode> </children> </node> <leafNode name="preserve-fqdn"> <properties> <help>Always include domain portion in hostname</help> <valueless/> </properties> </leafNode> </children> </node> </children> </node> </interfaceDefinition> diff --git a/smoketest/scripts/cli/test_system_syslog.py b/smoketest/scripts/cli/test_system_syslog.py index c3b14e1c0..e642b5660 100755 --- a/smoketest/scripts/cli/test_system_syslog.py +++ b/smoketest/scripts/cli/test_system_syslog.py @@ -1,275 +1,281 @@ #!/usr/bin/env python3 # # Copyright (C) 2019-2025 VyOS maintainers and contributors # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 or later as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import unittest from base_vyostest_shim import VyOSUnitTestSHIM from vyos.utils.file import read_file from vyos.utils.process import cmd from vyos.utils.process import process_named_running from vyos.xml_ref import default_value PROCESS_NAME = 'rsyslogd' RSYSLOG_CONF = '/run/rsyslog/rsyslog.conf' base_path = ['system', 'syslog'] def get_config(string=''): """ Retrieve current "running configuration" from FRR string: search for a specific start string in the configuration """ command = 'cat /run/rsyslog/rsyslog.conf' if string: command += f' | sed -n "/^{string}$/,/}}/p"' # }} required to escape } in f-string return cmd(command) class TestRSYSLOGService(VyOSUnitTestSHIM.TestCase): @classmethod def setUpClass(cls): super(TestRSYSLOGService, cls).setUpClass() # ensure we can also run this test on a live system - so lets clean # out the current configuration :) cls.cli_delete(cls, base_path) cls.cli_delete(cls, ['vrf']) def tearDown(self): # Check for running process self.assertTrue(process_named_running(PROCESS_NAME)) # delete testing SYSLOG config self.cli_delete(base_path) self.cli_commit() # Check for running process self.assertFalse(process_named_running(PROCESS_NAME)) def test_console(self): level = 'warning' self.cli_set(base_path + ['console', 'facility', 'all', 'level'], value=level) self.cli_commit() rsyslog_conf = get_config() config = [ f'if prifilt("*.{level}") then {{', # {{ required to escape { in f-string 'action(type="omfile" file="/dev/console")', ] for tmp in config: self.assertIn(tmp, rsyslog_conf) def test_basic(self): hostname = 'vyos123' domain_name = 'example.local' default_marker_interval = default_value(base_path + ['marker', 'interval']) facility = { 'auth': {'level': 'info'}, 'kern': {'level': 'debug'}, 'all': {'level': 'notice'}, } self.cli_set(['system', 'host-name'], value=hostname) self.cli_set(['system', 'domain-name'], value=domain_name) self.cli_set(base_path + ['preserve-fqdn']) for tmp, tmp_options in facility.items(): level = tmp_options['level'] self.cli_set(base_path + ['local', 'facility', tmp, 'level'], value=level) self.cli_commit() config = get_config('') expected = [ f'module(load="immark" interval="{default_marker_interval}")', 'global(preserveFQDN="on")', f'global(localHostname="{hostname}.{domain_name}")', ] for e in expected: self.assertIn(e, config) config = get_config('#### GLOBAL LOGGING ####') prifilt = [] for tmp, tmp_options in facility.items(): if tmp == 'all': tmp = '*' level = tmp_options['level'] prifilt.append(f'{tmp}.{level}') prifilt.sort() prifilt = ','.join(prifilt) self.assertIn(f'if prifilt("{prifilt}") then {{', config) self.assertIn( ' action(', config) self.assertIn( ' type="omfile"', config) self.assertIn( ' file="/var/log/messages"', config) self.assertIn( ' rotation.sizeLimit="524288"', config) self.assertIn( ' rotation.sizeLimitCommand="/usr/sbin/logrotate /etc/logrotate.d/vyos-rsyslog"', config) + self.cli_set(base_path + ['marker', 'disable']) + self.cli_commit() + + config = get_config('') + self.assertNotIn('module(load="immark"', config) + def test_remote(self): rhosts = { '169.254.0.1': { 'facility': {'auth' : {'level': 'info'}}, 'protocol': 'udp', }, '169.254.0.2': { 'port': '1514', 'protocol': 'udp', }, '169.254.0.3': { 'facility': {'auth' : {'level': 'info'}, 'kern' : {'level': 'debug'}, 'all' : {'level': 'notice'}, }, 'format': ['include-timezone', 'octet-counted'], 'protocol': 'tcp', 'port': '10514', }, } default_port = default_value(base_path + ['remote', next(iter(rhosts)), 'port']) default_protocol = default_value(base_path + ['remote', next(iter(rhosts)), 'protocol']) for remote, remote_options in rhosts.items(): remote_base = base_path + ['remote', remote] if 'port' in remote_options: self.cli_set(remote_base + ['port'], value=remote_options['port']) if 'facility' in remote_options: for facility, facility_options in remote_options['facility'].items(): level = facility_options['level'] self.cli_set(remote_base + ['facility', facility, 'level'], value=level) if 'format' in remote_options: for format in remote_options['format']: self.cli_set(remote_base + ['format'], value=format) if 'protocol' in remote_options: protocol = remote_options['protocol'] self.cli_set(remote_base + ['protocol'], value=protocol) self.cli_commit() config = read_file(RSYSLOG_CONF) for remote, remote_options in rhosts.items(): config = get_config(f'# Remote syslog to {remote}') prifilt = [] if 'facility' in remote_options: for facility, facility_options in remote_options['facility'].items(): level = facility_options['level'] if facility == 'all': facility = '*' prifilt.append(f'{facility}.{level}') prifilt.sort() prifilt = ','.join(prifilt) if not prifilt: # Skip test - as we do not render anything if no facility is set continue self.assertIn(f'if prifilt("{prifilt}") then {{', config) self.assertIn( ' type="omfwd"', config) self.assertIn(f' target="{remote}"', config) port = default_port if 'port' in remote_options: port = remote_options['port'] self.assertIn(f'port="{port}"', config) protocol = default_protocol if 'protocol' in remote_options: protocol = remote_options['protocol'] self.assertIn(f'protocol="{protocol}"', config) if 'format' in remote_options: if 'include-timezone' in remote_options['format']: self.assertIn( ' template="SyslogProtocol23Format"', config) if 'octet-counted' in remote_options['format']: self.assertIn( ' TCP_Framing="octed-counted"', config) else: self.assertIn( ' TCP_Framing="traditional"', config) def test_vrf_source_address(self): rhosts = { '169.254.0.10': { }, '169.254.0.11': { 'vrf': {'name' : 'red', 'table' : '12321'}, 'source_address' : '169.254.0.11', }, '169.254.0.12': { 'vrf': {'name' : 'green', 'table' : '12322'}, 'source_address' : '169.254.0.12', }, '169.254.0.13': { 'vrf': {'name' : 'blue', 'table' : '12323'}, 'source_address' : '169.254.0.13', }, } for remote, remote_options in rhosts.items(): remote_base = base_path + ['remote', remote] self.cli_set(remote_base + ['facility', 'all']) vrf = None if 'vrf' in remote_options: vrf = remote_options['vrf']['name'] self.cli_set(['vrf', 'name', vrf, 'table'], value=remote_options['vrf']['table']) self.cli_set(remote_base + ['vrf'], value=vrf) if 'source_address' in remote_options: source_address = remote_options['source_address'] self.cli_set(remote_base + ['source-address'], value=source_address) idx = source_address.split('.')[-1] self.cli_set(['interfaces', 'dummy', f'dum{idx}', 'address'], value=f'{source_address}/32') if vrf: self.cli_set(['interfaces', 'dummy', f'dum{idx}', 'vrf'], value=vrf) self.cli_commit() config = read_file(RSYSLOG_CONF) for remote, remote_options in rhosts.items(): config = get_config(f'# Remote syslog to {remote}') self.assertIn(f'target="{remote}"', config) if 'vrf' in remote_options: vrf = remote_options['vrf']['name'] self.assertIn(f'Device="{vrf}"', config) if 'source_address' in remote_options: source_address = remote_options['source_address'] self.assertIn(f'Address="{source_address}"', config) # Cleanup VRF/Dummy interfaces for remote, remote_options in rhosts.items(): if 'vrf' in remote_options: vrf = remote_options['vrf']['name'] self.cli_delete(['vrf', 'name', vrf]) if 'source_address' in remote_options: source_address = remote_options['source_address'] idx = source_address.split('.')[-1] self.cli_delete(['interfaces', 'dummy', f'dum{idx}']) if __name__ == '__main__': unittest.main(verbosity=2)