-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
[System logs]: Dynamically calculate file rotation thresholds based on /var/log/ partition size #851
Changes from all commits
a09b5fe
f8c2d41
3cc5d15
ce58980
fe53df3
63c734a
ebdda8e
4bac664
aef25cf
e46090e
b121c16
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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 |
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)))) | ||
|
||
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+="}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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. | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -44,9 +77,8 @@ | |
/var/log/debug | ||
/var/log/messages | ||
{ | ||
rotate 4 | ||
daily | ||
maxsize 50M | ||
rotate 1 | ||
size 10k | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
is 10k a good value for auth.log? |
||
missingok | ||
notifempty | ||
compress | ||
|
There was a problem hiding this comment.
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