Skip to content

Commit

Permalink
syslog: T2778: fix invalid handling of logrotate and default values
Browse files Browse the repository at this point in the history
There was no /var/log/messages handler for logrotate making rsyslog
crash after a few days.

In addition we had some JIna2 templating errors for hosts, user and file
CLI nodes

jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'facility'

Looks like therey are used rarely ;) - lucky me!
  • Loading branch information
c-po committed Jul 16, 2023
1 parent a7d2639 commit 751d326
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 38 deletions.
11 changes: 11 additions & 0 deletions data/templates/rsyslog/logrotate.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
### Autogenerated by system-syslog.py ###
/var/log/messages {
missingok
notifempty
create
rotate 5
size=256k
postrotate
invoke-rc.d rsyslog rotate > /dev/null
endscript
}

{% if file is vyos_defined %}
{% for file_name, file_options in file.items() %}
/var/log/user/{{ file_name }} {
Expand Down
27 changes: 17 additions & 10 deletions data/templates/rsyslog/rsyslog.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ $outchannel global,/var/log/messages,262144,/usr/sbin/logrotate {{ logrotate }}
{% if file is vyos_defined %}
# File based configuration section
{% for file_name, file_options in file.items() %}
$outchannel {{ file_name }},/var/log/user/{{ file_name }},{{ file_options.archive.size }},/usr/sbin/logrotate {{ logrotate }}
{% set tmp = [] %}
{% for facility, facility_options in file_options.facility.items() %}
{% set _ = tmp.append(facility.replace('all', '*') + '.' + facility_options.level) %}
{% endfor %}
$outchannel {{ file_name }},/var/log/user/{{ file_name }},{{ file_options.archive.size }},/usr/sbin/logrotate {{ logrotate }}
{% if file_options.facility is vyos_defined %}
{% for facility, facility_options in file_options.facility.items() %}
{% set _ = tmp.append(facility.replace('all', '*') + '.' + facility_options.level) %}
{% endfor %}
{% endif %}
{{ tmp | join(';') }} :omfile:${{ file }}
{% endfor %}
{% endif %}
Expand All @@ -45,9 +47,11 @@ $outchannel {{ file_name }},/var/log/user/{{ file_name }},{{ file_options.archiv
# Remote logging
{% for host_name, host_options in host.items() %}
{% set tmp = [] %}
{% for facility, facility_options in host_options.facility.items() %}
{% set _ = tmp.append(facility.replace('all', '*') + '.' + facility_options.level) %}
{% endfor %}
{% if host_options.facility is vyos_defined %}
{% for facility, facility_options in host_options.facility.items() %}
{% set _ = tmp.append(facility.replace('all', '*') + '.' + facility_options.level) %}
{% endfor %}
{% endif %}
{% if host_options.protocol is vyos_defined('tcp') %}
{% if host_options.format.octet_counted is vyos_defined %}
{{ tmp | join(';') }} @@(o){{ host_name | bracketize_ipv6 }}:{{ host_options.port }};RSYSLOG_SyslogProtocol23Format
Expand All @@ -63,9 +67,12 @@ $outchannel {{ file_name }},/var/log/user/{{ file_name }},{{ file_options.archiv
{% if user is defined and user is not none %}
# Log to user terminal
{% for username, user_options in user.items() %}
{% for facility, facility_options in user_options.facility.items() %}
{% set _ = tmp.append(facility.replace('all', '*') + '.' + facility_options.level) %}
{% endfor %}
{% set tmp = [] %}
{% if user_options.facility is vyos_defined %}
{% for facility, facility_options in user_options.facility.items() %}
{% set _ = tmp.append(facility.replace('all', '*') + '.' + facility_options.level) %}
{% endfor %}
{% endif %}
{{ tmp | join(';') }} :omusrmsg:{{ username }}
{% endfor %}
{% endif %}
42 changes: 14 additions & 28 deletions src/conf_mode/system-syslog.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def get_config(config=None):
syslog = conf.get_config_dict(base, key_mangling=('-', '_'),
get_first_key=True, no_tag_node_value_mangle=True)

syslog.update({ 'logrotate' : logrotate_conf })
tmp = is_node_changed(conf, base + ['vrf'])
if tmp: syslog.update({'restart_required': {}})

Expand All @@ -70,35 +69,22 @@ def get_config(config=None):
syslog['console']['facility'][facility])

# XXX: add defaults for "host" tree
if 'host' in syslog:
default_values_host = defaults(base + ['host'])
for syslog_type in ['host', 'user', 'file']:
# Bail out early if there is nothing to do
if syslog_type not in syslog:
continue

default_values_host = defaults(base + [syslog_type])
if 'facility' in default_values_host:
del default_values_host['facility']
default_values_facility = defaults(base + ['host', 'facility'])

for host, host_config in syslog['host'].items():
syslog['host'][host] = dict_merge(default_values_host, syslog['host'][host])
if 'facility' in host_config:
for facility in host_config['facility']:
syslog['host'][host]['facility'][facility] = dict_merge(default_values_facility,
syslog['host'][host]['facility'][facility])

# XXX: add defaults for "user" tree
if 'user' in syslog:
default_values = defaults(base + ['user', 'facility'])
for user, user_config in syslog['user'].items():
if 'facility' in user_config:
for facility in user_config['facility']:
syslog['user'][user]['facility'][facility] = dict_merge(default_values,
syslog['user'][user]['facility'][facility])

# XXX: add defaults for "file" tree
if 'file' in syslog:
default_values = defaults(base + ['file'])
for file, file_config in syslog['file'].items():
for facility in file_config['facility']:
syslog['file'][file]['facility'][facility] = dict_merge(default_values,
syslog['file'][file]['facility'][facility])

for tmp, tmp_config in syslog[syslog_type].items():
syslog[syslog_type][tmp] = dict_merge(default_values_host, syslog[syslog_type][tmp])
if 'facility' in tmp_config:
default_values_facility = defaults(base + [syslog_type, 'facility'])
for facility in tmp_config['facility']:
syslog[tmp_config][tmp]['facility'][facility] = dict_merge(default_values_facility,
syslog[tmp_config][tmp]['facility'][facility])

return syslog

Expand Down

0 comments on commit 751d326

Please sign in to comment.