Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[System logs]: Dynamically calculate file rotation thresholds based on /var/log/ partition size #851

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions files/build_templates/sonic_debian_extension.j2
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,13 @@ sudo cp $IMAGE_CONFIGS/rsyslog/rsyslog-config.sh $FILESYSTEM_ROOT/usr/bin/
sudo cp $IMAGE_CONFIGS/rsyslog/rsyslog.conf.j2 $FILESYSTEM_ROOT/usr/share/sonic/templates/
sudo cp $IMAGE_CONFIGS/rsyslog/rsyslog.d/* $FILESYSTEM_ROOT/etc/rsyslog.d/

# Copy logrotate.d configuration files
sudo cp -f $IMAGE_CONFIGS/logrotate.d/rsyslog $FILESYSTEM_ROOT/etc/logrotate.d/
# Copy logrotate configuration files and templates
sudo cp $IMAGE_CONFIGS/logrotate/logrotate-config.service $FILESYSTEM_ROOT/etc/systemd/system/
sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable logrotate-config.service
sudo cp $IMAGE_CONFIGS/logrotate/logrotate-config.sh $FILESYSTEM_ROOT/usr/bin/
sudo cp $IMAGE_CONFIGS/logrotate/logrotate.conf $FILESYSTEM_ROOT/etc/
sudo mkdir -p $FILESYSTEM_ROOT/usr/share/sonic/templates/logrotate.d/
sudo cp $IMAGE_CONFIGS/logrotate/rsyslog.j2 $FILESYSTEM_ROOT/usr/share/sonic/templates/logrotate.d/

# Copy systemd-journald configuration files
sudo cp -f $IMAGE_CONFIGS/systemd/journald.conf $FILESYSTEM_ROOT/etc/systemd/
Expand Down
2 changes: 1 addition & 1 deletion files/image_config/cron.d/logrotate
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Attempt to rotate system logs once per minute
* * * * * root /usr/sbin/logrotate /etc/logrotate.d/rsyslog
* * * * * root /usr/sbin/logrotate /etc/logrotate.conf >/dev/null 2>&1
10 changes: 10 additions & 0 deletions files/image_config/logrotate/logrotate-config.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Unit]
Description=Update logrotate configuration
After=basic.target

[Service]
Type=oneshot
ExecStart=/usr/bin/logrotate-config.sh

[Install]
WantedBy=multi-user.target
35 changes: 35 additions & 0 deletions files/image_config/logrotate/logrotate-config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

# Reserve space for btmp and wtmp, auth.log and cron.log, as well as logs that
# should be disabled, just in case they get created and rotated
RESERVED_SPACE_KB=1024

# Number of rotated archive files to keep per log file
NUM_ARCHIVES_PER_LOG_FILE=7

# Number of files to rotate in each block within rsyslog.j2 template
NUM_LOG_FILES_SYSLOG=1
NUM_LOG_FILES_QUAGGA_TEAMD=3
NUM_LOG_FILES_SWSS=2

# Percentage of usable /var/log/ space to allocate to each block in template
# NOTE: These must sum to 100!
PERCENT_ALLOCATED_SYSLOG=75
PERCENT_ALLOCATED_QUAGGA_TEAMD=15
PERCENT_ALLOCATED_SWSS=10

VAR_LOG_SIZE_KB=$(df /var/log | sed -n 2p | awk '{print $2}')
USABLE_SPACE_KB=$((VAR_LOG_SIZE_KB - RESERVED_SPACE_KB))

SIZE_LIMIT_SYSLOG_FILES_KB=$(((USABLE_SPACE_KB * PERCENT_ALLOCATED_SYSLOG / 100) / (NUM_LOG_FILES_SYSLOG * (NUM_ARCHIVES_PER_LOG_FILE + 1))))
SIZE_LIMIT_QUAGGA_TEAMD_FILES_KB=$(((USABLE_SPACE_KB * PERCENT_ALLOCATED_QUAGGA_TEAMD / 100) / (NUM_LOG_FILES_QUAGGA_TEAMD * (NUM_ARCHIVES_PER_LOG_FILE + 1))))
SIZE_LIMIT_SWSS_FILES_KB=$(((USABLE_SPACE_KB * PERCENT_ALLOCATED_SWSS / 100) / (NUM_LOG_FILES_SWSS * (NUM_ARCHIVES_PER_LOG_FILE + 1))))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what are the actual sizes when the log partition size is 100M?

for example syslog log size limit is 102400 * 75 / 100 / (1 * ( 7 + 1) = 9600KB.
swss size limit is 10240010/100/(2(7+1)) = 640KB

since, the gzipped archive is much smaller than the actual log size. we might end up with not fully utilizing the /var/log partition.

Ideally, if we can rotate based on the free disk size, we can utilize the /var/log partition. however, we need to modify logrotate for that.

there are some discussion in here. https://serverfault.com/questions/372809/free-space-driven-log-rotation-on-linux


ADDITIONAL_DATA_JSON="{"
ADDITIONAL_DATA_JSON+="\"num_archive_files\":$NUM_ARCHIVES_PER_LOG_FILE,"
ADDITIONAL_DATA_JSON+="\"size_limit_syslog_kb\":$SIZE_LIMIT_SYSLOG_FILES_KB,"
ADDITIONAL_DATA_JSON+="\"size_limit_quagga_teamd_kb\":$SIZE_LIMIT_QUAGGA_TEAMD_FILES_KB,"
ADDITIONAL_DATA_JSON+="\"size_limit_swss_kb\":$SIZE_LIMIT_SWSS_FILES_KB"
ADDITIONAL_DATA_JSON+="}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have a lot of variables, another way is to save data into a json file and load in sonic-cfggen with -j. Not a big difference with -a, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the info. I think this solution is fine for the three variables. If it grows more, I'll keep that idea in mind.


sonic-cfggen -a "$ADDITIONAL_DATA_JSON" -t /usr/share/sonic/templates/logrotate.d/rsyslog.j2 > /etc/logrotate.d/rsyslog
32 changes: 32 additions & 0 deletions files/image_config/logrotate/logrotate.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# see "man logrotate" for details
# Rotate log files daily by default
daily

# Keep 4 days worth of backlogs by default
rotate 4

# create new (empty) log files after rotating old ones
create

# uncomment this if you want your log files compressed
#compress

# packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp, or btmp -- we'll rotate them here
/var/log/wtmp {
missingok
size 100k
create 0664 root utmp
rotate 1
}

/var/log/btmp {
missingok
size 100k
create 0660 root utmp
rotate 1
}

# system-specific logs may be configured here
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
{% block banner %}
#
# =========== Managed by SONiC Config Engine - DO NOT EDIT MANUALLY! ===========
# Generated from:
# /usr/share/sonic/templates/logrotate.d/rsyslog.j2 using sonic-cfggen
#
# file: /etc/logrotate.d/rsyslog
# Logrotate configuration file for SONiC
#
{% endblock banner %}

/var/log/syslog
{
rotate {{ num_archive_files }}
size {{ size_limit_syslog_kb }}k
missingok
notifempty
compress
delaycompress
sharedscripts
postrotate
# Calling kill directly instead of 'service rsyslog rotate >/dev/null' due
# to bug in init-system-helpers. Bug has apparently been fixed in v1.47.
# However, Debian Jessie is still using v1.22.
# See https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=672218
kill -HUP $(cat /var/run/rsyslogd.pid)
endscript
}

/var/log/quagga/*.log
/var/log/teamd.log
{
rotate 7
daily
maxsize 100M
rotate {{ num_archive_files }}
size {{ size_limit_quagga_teamd_kb }}k
missingok
notifempty
compress
delaycompress
sharedscripts
postrotate
# Calling kill directly instead of 'service rsyslog rotate >/dev/null' due
# to bug in init-system-helpers. Bug has apparently been fixed in v1.47.
Expand All @@ -17,11 +45,12 @@
kill -HUP $(cat /var/run/rsyslogd.pid)
endscript
}

# SwSS logs (written by orchagent, not rsyslog)
/var/log/swss/*.rec
{
rotate 7
daily
maxsize 20M
rotate {{ num_archive_files }}
size {{ size_limit_swss_kb }}k
missingok
notifempty
compress
Expand All @@ -31,6 +60,10 @@
pgrep -x orchagent | xargs /bin/kill -HUP 2>/dev/null || true
endscript
}

# Of these rsyslog-written log files, only auth.log and cron.log should still
# be created. However, we should attempt to rotate all of them anyway, just
# in case others happen to get created.
/var/log/mail.info
/var/log/mail.warn
/var/log/mail.err
Expand All @@ -44,9 +77,8 @@
/var/log/debug
/var/log/messages
{
rotate 4
daily
maxsize 50M
rotate 1
size 10k
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10k [](start = 9, length = 3)

is 10k a good value for auth.log?

missingok
notifempty
compress
Expand Down
13 changes: 7 additions & 6 deletions files/image_config/rsyslog/rsyslog.conf.j2
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
###############################################################################
# Managed by Ansible
# file: ansible/roles/acs/templates/rsyslog.conf.j2
###############################################################################
{% block banner %}
#
# /etc/rsyslog.conf Configuration file for rsyslog.
# =========== Managed by SONiC Config Engine - DO NOT EDIT MANUALLY! ===========
# Generated from /usr/share/sonic/templates/rsyslog.conf.j2 using sonic-cfggen
#
# file: /etc/rsyslog.conf Configuration file for rsyslog.
#
# For more information see
# /usr/share/doc/rsyslog-doc/html/rsyslog_conf.html
{% endblock banner %}


#################
Expand Down Expand Up @@ -50,7 +51,7 @@ $ActionFileDefaultTemplate SONiCFileFormat
#
$FileOwner root
$FileGroup adm
$FileCreateMode 0640
$FileCreateMode 0644
$DirCreateMode 0755
$Umask 0022

Expand Down
51 changes: 4 additions & 47 deletions files/image_config/rsyslog/rsyslog.d/99-default.conf
Original file line number Diff line number Diff line change
@@ -1,61 +1,18 @@
#
# First some standard log files. Log by facility.
#

# Log all facilities to /var/log/syslog except cron, auth
# and authpriv. They are noisy - log them to their own files
*.*;cron,auth,authpriv.none -/var/log/syslog
auth,authpriv.* /var/log/auth.log
*.*;auth,authpriv.none -/var/log/syslog
cron.* /var/log/cron.log
# Do not redirect daemon, kernel or lpr logs to
# their own files. Let them log to /var/log/syslog
#daemon.* -/var/log/daemon.log
#kern.* -/var/log/kern.log
#kern.* -/var/persist/log/kern.log
#lpr.* -/var/log/lpr.log
mail.* -/var/log/mail.log
user.* -/var/log/user.log

#
# Logging for the mail system. Split it up so that
# it is easy to write scripts to parse these files.
#
mail.info -/var/log/mail.info
mail.warn -/var/log/mail.warn
mail.err /var/log/mail.err

#
# Logging for INN news system.
#
news.crit /var/log/news/news.crit
news.err /var/log/news/news.err
news.notice -/var/log/news/news.notice

#
# Some "catch-all" log files.
#
*.=debug;\
auth,authpriv.none;\
news.none;mail.none -/var/log/debug
#
# Removed as duplicates:
#*.=info;*.=notice;*.=warn;\
# auth,authpriv.none;\
# cron,daemon.none;\
# mail,news.none -/var/log/messages
#
*.=crit;*.=alert;*.=emerg -/var/persist/log/alarms
#
# Emergencies are sent to everybody logged in.
#
*.emerg :omusrmsg:*

#
# I like to have messages displayed on the console, but only on a virtual
# console I usually leave idle.
#
#daemon,mail.*;\
# news.=crit;news.=err;news.=notice;\
# *.=debug;*.=info;\
# *.=notice;*.=warn /dev/tty8

# The named pipe /dev/xconsole is for the `xconsole' utility. To use it,
# you must invoke `xconsole' with the `-file' option:
#
Expand Down