Commit 696f4678 authored by Maciej Lipinski's avatar Maciej Lipinski

[issue #20] output syslog to /tmp with rotation of files

rsyslog outputs syslog to /tmp/syslog until the file is ~1MB, then
the file is moved/renamed to a "rotate file" (name includes date &
time) while the /tmp/syslog is filled by rsyslog from zero. Max
number of 10 "rotate files" are kept. The oldest is removed when
new file from rsyslog comes. Next step is to make this a default
option in kconfig
parent 86f990a7
#!/bin/sh
#############################################################
# Maciej Lipinski @CERN
#
# Script to rotate files written by rsyslogd, it is called
# by rsyslog as per configuration in /etc/rsyslog.conf
# (more info in this config file)
#
# Parameters:
# 1. Path and name of the output file that is written by
# rsyslogd
# 2. Number of rotate files (copies of the file written
# by rsyslogd
#
# Example usage:
# log_rotate.sh /tmp/log_rotation.log 3
#############################################################
# Set log file name, from input or default
if [ -z $1 ]; then
log_file=/tmp/log_rotation.log
else
log_file=$1
fi
# Set number of rotated logs, fron input or default
if [ -z $2 ]; then
log_numb=10
else
log_numb=$2
fi
echo "Rotate log file $log_file $log_numb times"
# Just in case, check whether the log file exists
if [ ! -f "$log_file" ]; then
echo "No logfile $log_file"
exit 1;
fi
# Move the log file from rsyslogd into the rotate file
# The date is an integer so that it is easy to recongize
# the oldest file.
new_rotate_file=${log_file}-$(date +%Y%m%d%H%M%S)
mv -f $log_file $new_rotate_file
echo "Move $log_file to $new_rotate_file"
# Remove excess rotate file. Just in case, there are
# more than a single excess rotate file, remove excess
# files until happy with its number.
# Print the expected and current number of rotate files (just for info)
log_cnt=$(/bin/ls ${log_file}-* | /usr/bin/wc -l)
echo "Number of rotate files is $log_cnt and should be max $log_numb"
# Remove any excess files
while [ 1 ]; do
# Check the number of rotate files.
log_cnt=$(/bin/ls ${log_file}-* | /usr/bin/wc -l)
if [ $log_cnt -gt $log_numb ]; then
# Remove the oldest file if there are too many rotate files.
oldest_rotate_file=$(/bin/ls ${log_file}-* | /usr/bin/head -1)
echo "Remove oldest file: $oldest_rotate_file"
rm $oldest_rotate_file
else
break
fi
done
......@@ -48,9 +48,27 @@ $PreserveFQDN on
#$ActionQueueType LinkedList # run asynchronously
#$ActionResumeRetryCount -1 # infinite retries if host is down
# remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional
##*.* @@remote-host:514
##*.* @remote-host:514
# ######### Output channel to log syslog locally ###########
# The rsyslog feature of output channel is used, see more at:
# https://www.rsyslog.com/doc/v8-stable/configuration/output_channels.html
#
# WARNING: It is most likely that this feature will be replaced by something
# different in the future.
#
# Using this feature, syslog is written locally to file /tmp/syslog, once
# the output file reaches configured size (i.e. ~1MB), /wr/bin/log_rotate.sh
# script is executed. This script moves the output file giving it a different
# name (with date in the name) and removes the oldest of such files. Once
# the script is executed, the /tmp/syslog file is written from zero.
# The script keeps maximum 10 of old syslog files (with a date).
#
# In total, syslog uses ~11MB (1MB for current output and 10MB for old
# logs) of /tmp/ space, (out of ~30MB available).
$outchannel log_rotation,/tmp/syslog 1048576,/wr/bin/log_rotate.sh /tmp/syslog
*.* :omfile:$log_rotation
# ######### Receiving Messages from Remote Hosts ##########
# TCP Syslog Server:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment