Tiny Core Relay - Deploy Log Rotation
Log In or Register to download the BES file, and more.

0 Votes

Description

Deploys a script that will rotate specified logs once they reach a certain size.  This will run once per day.

Log Size Threshold: 200KB

Logs Rotated (configured in external file):

 







 

Note: LogRotate.tcz will be available in TCL v13. 


Property Details

ID26988
StatusProduction - Fully Tested and Ready for Production
TitleTiny Core Relay - Deploy Log Rotation
SourceInternal
Source Release Date12/7/2022 12:00:00 AM
KeywordsTiny Core Linux Relay, Log Rotation
Added by on 12/12/2022 8:08:54 AM
Last Modified by on 12/12/2022 8:08:54 AM
Counters 142 Views / 1 Download
User Rating 1 star 2 star 3 star 4 star 5 star * Average over 0 ratings. ** Log In or Register to add your rating.

Relevance

Used in 5 fixlets   * Results in a true/false
Show indented relevance
(exists relay service)
Used in 2 fixlets   * Results in a true/false
Show indented relevance
((if exists property "in proxy agent context" then ( not in proxy agent context ) else true ))
Used in 4 fixlets   * Results in a true/false
Show indented relevance
name of operating system contains "Linux TinyCore"
Used in 1 fixlet   * Results in a true/false
Show indented relevance
not exists file "/home/tc/scripts/log_rotation.sh"

Actions

Action 1 (default)

Action Link Click here to deploy this action.
Script Type BigFix Action Script
//create /home/tc/scripts/ folder if it doesn't exist

if {not exists folder "/home/tc/scripts/"}
wait mkdir /home/tc/scripts
endif

//Create the log rotation script
delete __createfile
createfile until endofscript1
#!/bin/sh

#Create log list variable - conf file that lists absolute log file paths to be rotated (one per line)
LOG_LIST=/home/tc/scripts/loglist.conf

#Exit if conf file does not exist
if [ ! -r "$LOG_LIST" ]; then
logger "$(date) - ERROR - Log Rotation Failed. "$LOG_LIST" missing!"
exit
fi

#Filter out blank lines and comments from LOG_LIST
LOGS=$(grep -v '^[[:space:]]*$' ${{LOG_LIST} | grep -v '#')

#TMP file that the validated log list will be written to
TMPLIST=/tmp/loglist.conf
> "$TMPLIST"
#validate logs in loglist.conf
for F in $LOGS
do
    if [ -r "$F" ]; then
        echo "$F" >> $TMPLIST
    else
        logger "$(date) - ERROR - Log Rotation Failed for "$F". File Not Found"
    fi
done

#Store validated logs line by line
VALIDATED_LOGS=$(cat "$TMPLIST")

#rotate_logfile function (max= max rotations. The actual number of rotations will be one higher than specified)
rotate_logfile()
{
max=4
#seq command - count backwards from max by an increment of 1 until it reaches 1
seq="$(seq "${{max}" -1 1)"
for s in $seq
do
#add one to the current seq value
d=$(($s +1))
     OLDFILE="$LOG_BASE".$s.log
     NEWFILE="$LOG_BASE".$d.log
#rotate existing 'rotated' logs - .1.log will become .2.log etc...
quiet mv -f $OLDFILE $NEWFILE
done
#rotate the active log
mv -f $FILE ${{LOG_BASE}.1.log
}

#output suppression function
quiet()
{
"$@" > /dev/null 2>&1
}

#Rotate Logs

for FILE in $VALIDATED_LOGS
do
#LOG_BASE removes .log from file
LOG_BASE=$(expr ${{FILE} | awk -F '.' '{{ print $1 }')
#SIZE pulls file size in bytes
SIZE=$(ls -l ${{FILE} | awk '{{ print $5}')
#file exists and is greater than or equal to 200KB
if [ -f "$FILE" ] && [ $SIZE -ge 200000 ]; then
rotate_logfile
> "$FILE"
fi
done

endofscript1

delete /home/tc/scripts/log_rotation.sh
move __createfile /home/tc/scripts/log_rotation.sh
wait chmod 750 /home/tc/scripts/log_rotation.sh

//Create log list conf file
delete __createfile
createfile until endofscript2
# DO NOT DELETE FILE!
# Conf file that defines what logs are to be rotated. This file is used by /home/tc/scripts/log_rotation.sh.
# This file can be updated on all relays using the fixlet: PM - Tiny Core Relay - Update Log Rotation Settings
# Insert one log file per line using the absolute path. The file must end in .log

//INSERT LOGS TO BE ROTATED HERE
endofscript2

delete /home/tc/scripts/loglist.conf
move __createfile /home/tc/scripts/loglist.conf
wait chmod 660 /home/tc/scripts/loglist.conf

//create file to use for crontab

if {not exists file "/var/spool/cron/crontabs/root"}

delete __appendfile
appendfile 12 0 * * * /bin/sh /home/tc/scripts/log_rotation.sh

if {exists file "/home/tc/newcrontab"}
delete "/home/tc/newcrontab"
endif

continue if {not exists file "/home/tc/newcrontab"}

move __appendfile /home/tc/newcrontab

continue if {exists file "/home/tc/newcrontab"}

endif

//create script to add cronjob to crontab, add /var/spool/cron/crontabs directory to filetool.lst, add starting the cron service to bootlocal, and backup

delete __appendfile
appendfile #!/bin/sh

if {exists file "/var/spool/cron/crontabs/root"}
appendfile echo "12 0 * * * /bin/sh /home/tc/scripts/log_rotation.sh" >> "/var/spool/cron/crontabs/root"
else
appendfile sudo crontab /home/tc/newcrontab
endif

if {not exists lines whose (it contains "var/spool/cron/crontabs/") of file "/opt/.filetool.lst"}
appendfile echo "var/spool/cron/crontabs/" >> /opt/.filetool.lst
endif

if {not exists lines whose (it contains "/sbin/syslogd") of file "/opt/bootlocal.sh"}
appendfile echo "/sbin/syslogd" >> /opt/bootlocal.sh
endif

if {not exists lines whose (it contains "/etc/init.d/services/crond start") of file "/opt/bootlocal.sh"}
appendfile echo "/etc/init.d/services/crond start" >> /opt/bootlocal.sh
endif

appendfile sudo filetool.sh -b
appendfile sudo reboot

delete /var/opt/BESClient/logrotate_config.sh
move __appendfile /var/opt/BESClient/logrotate_config.sh
wait chmod 750 /var/opt/BESClient/logrotate_config.sh
run /bin/sh /var/opt/BESClient/logrotate_config.sh
Success Criteria

This action will be considered successful when the applicability relevance evaluates to false.


Sharing

Social Media:
Share this page on Yammer

Comments

Log In or Register to leave comments!