Skip to content

Commit

Permalink
Update log rotation size dynamically of 10% tmpfs
Browse files Browse the repository at this point in the history
  • Loading branch information
stephdl committed Oct 21, 2024
1 parent 6449aa4 commit 36e5a14
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
5 changes: 4 additions & 1 deletion files/usr/sbin/ns-log-size
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ get_log_rotation_size() {

set_log_rotation_size() {
local new_size=$1
#The value 9223372036854775807 bytes is the maximum value that can be set (8589.93 GB)

# Check if the new size is a positive integer
if [[ ! $new_size =~ ^[0-9]+$ ]]; then
echo "Error: Size must be a positive integer."
exit 1
fi

# Check if the new size is less than the minimum required size
if (( new_size < MIN_SIZE )); then
echo "Error: The input size is too low. The minimum value is $MIN_SIZE."
Expand Down
21 changes: 18 additions & 3 deletions packages/ns-storage/files/30_ns-storage
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

[ "$(uci -q get rsyslog.ns_memory)" == "selector" ] && exit 0

echo '# Rotate in-memory log if size is bigger than 50MB' >> /etc/rsyslog.conf
echo '$outchannel log_rotation,/var/log/messages, 52428800, /usr/sbin/rotate-messages' >> /etc/rsyslog.conf

uci -q import rsyslog << EOI
config syslog 'syslog'
option tcp_input_port '514'
Expand All @@ -21,3 +18,21 @@ config selector ns_memory
EOI

crontab -l | grep -q '/usr/sbin/logrotate /etc/logrotate.conf' || echo '5 1 * * * /usr/sbin/logrotate /etc/logrotate.conf' >> /etc/crontabs/root

# Default log_size in case of error
log_size=52428800

# Try to extract tmpfs size, fall back to default log_size if an error occurs
tmpfs_size=$(df -k /tmp | awk 'NR==2 {print $2}')
if [ -z "$tmpfs_size" ] || ! echo "$tmpfs_size" | grep -qE '^[0-9]+$'; then
echo "Error: Could not retrieve tmpfs size, defaulting log_size to 52428800" >&2
else
# Calculate 10% of tmpfs size in bytes
ten_percent_bytes=$(( tmpfs_size * 1024 / 10 ))

# Use default log_size if the calculated value is less than 52428800
if [ "$ten_percent_bytes" -gt "$log_size" ]; then
log_size=$ten_percent_bytes
fi
fi
echo '$outchannel log_rotation,/var/log/messages, '$log_size', /usr/sbin/rotate-messages' >> /etc/rsyslog.conf

0 comments on commit 36e5a14

Please sign in to comment.