Skip to content

Instantly share code, notes, and snippets.

@tbl3rd
Last active August 29, 2015 14:18
Show Gist options
  • Save tbl3rd/917b30a00b42bb210f0b to your computer and use it in GitHub Desktop.
Save tbl3rd/917b30a00b42bb210f0b to your computer and use it in GitHub Desktop.
manage logs without a framework
# Fnord
description "Manage Fnord remote logs from fnorders"
start on stopped rc RUNLEVEL=[2345]
stop on runlevel [016S]
task
console none
chdir /var/log/fnord
script
######################################################################
# Put an Fnord log rotation script into "$1" that examines a directory
# of log files every "$2" seconds. The script compresses .log files
# older than "$3" days and deletes compressed .log.gz files older than
# "$4" days.
#
startlogrotate() {
cat - > "$1" << 'EOF'
#!/bin/sh
#
# /etc/rc.local writes and starts this log rotation script at boot
# time. Any changes to the script file will not survive a restart.
#
ok=0
test $ok -eq 0 && test "$#" -eq 3 && ok=1
test $ok -eq 1 && test "$1" -eq "$1" && checkseconds="$1" && ok=2
test $ok -eq 2 && test "$2" -eq "$2" && zipdays="$2" && ok=3
test $ok -eq 3 && test "$3" -eq "$3" && rmdays="$3" && ok=4
checkseconds=${checkseconds:-10800} # to record errors
if test $ok -eq 4
then
cd /var/log/fnord
for d in consoles fnorder-r???-c??-n??
do
if cd "$d" 2>/dev/null
then
finder='find . -maxdepth 1 -type f'
zip=$($finder -mtime +$zipdays -name '*.log' -print)
rm=$($finder -mtime +$rmdays -name '*.log.gz' -print)
logger -t "$0" $$ In $d after $zipdays days: gzip $zip
logger -t "$0" $$ In $d after $rmdays days: rm $rm
test -n "$zip" && gzip -9v $zip
test -n "$rm" && rm $rm
cd - >/dev/null
fi
sleep "$checkseconds"
done
else
logger -t "$0" $$ Bad arguments to $0: "$@"
sleep "$checkseconds"
fi
exec "$0" $checkseconds $zipdays $rmdays
EOF
chmod 744 "$1"
nohup "$@" </dev/null >/dev/null 2>/dev/null &
}
# Check one fnorder- directory every 6 minutes (360 seconds).
#
startlogrotate /var/log/fnord/rotate.sh 360 1 10
end script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment