Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
logrotate.sh
#!/bin/sh
#
# Kenneth Finnegan, 2010
# kennethfinnegan.blogspot.com
#
# Counts service requests for the day and adds count to smaller log file
#
# To have run by cron at 23:59 daily, add the following to your crontab
# 59 23 * * * /mnt/storage/lighty/logrotate.sh
# 2010 11 23: Initial revision
# 2010 11 24: Added awk script to count unique IP addr in logs
# Removed ACC_TODAY path from output to ACC_TOTAL
# 2010 11 25: Fixed daily rotation of logs, send HUP signal to lighttpd
# Added date-stamped compressed copies for later review
LOGDIR=/mnt/storage/logs
# Todays log file being fed by lighttpd
ACC_TODAY=lighttpd.access.log
# Backup of yesterdays log, before being overwritten at midnight
ACC_YESTERDAY=lighttpd.access.log.yesterday
# Compressed copy of today's logs
ACC_TODAY_GZ=lighttpd.access.$(date +%F).log
# Total count of traffic, daily with DATE UNIQUE TOTAL per line
ACC_TOTAL=lighttpd.access.log.total
# File storing the current PID for the daemon
PID=lighttpd.pid
touch $LOGDIR/$ACC_TODAY
DAILYUNIQUE="$(awk '{print $1}' $LOGDIR/$ACC_TODAY | sort -n -u | wc -l )"
DAILYHANDLED="$(wc -l < $LOGDIR/$ACC_TODAY)"
DAILYLOG="$(date +%F) \t $DAILYUNIQUE \t $DAILYHANDLED"
echo -e $DAILYLOG >> $LOGDIR/$ACC_TOTAL
cp -p $LOGDIR/$ACC_TODAY $LOGDIR/$ACC_YESTERDAY
cp -p $LOGDIR/$ACC_TODAY $LOGDIR/$ACC_TODAY_GZ
gzip $LOGDIR/$ACC_TODAY_GZ
# Clear the daily log file and notify lighttpd
cat /dev/null > $LOGDIR/$ACC_TODAY
kill -s HUP `cat $LOGDIR/$PID`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment