Setting up Centralized Logging to S3 with logrotated

What we are trying to do here

Get all logs to be backup to S3 whenever logrotated runs or whenever the machine is shutdown/restarted (so that if you have AWS auto scaling enabled, you won't loose any logs).

Basic Steps

  • Install s3cmd
  • Setup a script to upload logs to S3 (that'll be called via logrotated)
  • Setup a init.d script to run logrotated -f (forced) on shutdown/restart
  • Setup logrotated scripts to call your S3 upload script

Install s3cmd

sudo apt-get install s3cmd  

setup s3cmd by running

sudo s3cmd --configure  

Just enter your key and the secret. Everything else can be left empty/with default values. This will create a '.s3cfg' in your root.

Script to upload logs to S3

The following script is largly based on whats found at: http://www.raghuramanb.com/2012/12/aws-log-archive-amazon-s3-glacier-part-3.html

/usr/local/admintools/upload.sh

if [ -z $1 ]; then  
echo "Error: no filename passed in... Exiting..."  
exit;  
fi

echo 'Starting S3 Log Upload...'  
# Perform Rotated Log File Compression
tar -czPf "$1".gz "$1"

BUCKET_NAME="literacyplanet-logs"  
# Fetch the instance id from the instance
EC2_INSTANCE_ID="`wget -q -O - http://169.254.169.254/latest/meta-data/instance-id`"  
if [ -z $EC2_INSTANCE_ID ]; then  
echo "Error: Couldn't fetch Instance ID .. Exiting .."  
exit;  
else

# Upload log file to Amazon S3 bucket
/usr/bin/s3cmd -c /.s3cfg put "$1" s3://$BUCKET_NAME/staging/lp-public/$EC2_INSTANCE_ID$1-$(date +%H%M%S).gz
fi

# Removing Rotated Compressed Log File
rm -f "$1”.gz  

Setup init.d script

This is from s3logarchiver at https://eautils.googlecode.com/svn/trunk/s3logarchiver/s3logrotate

/etc/init.d/s3logrotate

#!/bin/sh
### BEGIN INIT INFO
# Provides:          s3logrotate
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# X-Interactive:     true
# Short-Description: rotates the log files before shutdown
### END INIT INFO
# chkconfig: 345 79 20
# description: rotates the log files before shutdown

# Copyright 2010 Benjie Gillam. All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without modification, are
# permitted provided that the following conditions are met:
# 
#    1. Redistributions of source code must retain the above copyright notice, this list of
#       conditions and the following disclaimer.
# 
#    2. Redistributions in binary form must reproduce the above copyright notice, this list
#       of conditions and the following disclaimer in the documentation and/or other materials
#       provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY Benjie Gillam ``AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Benjie Gillam OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# 
# The views and conclusions contained in the software and documentation are those of the
# authors and should not be interpreted as representing official policies, either expressed
# or implied, of Benjie Gillam.

start () {  
        true
        RETVAL=$?
        [ $RETVAL = 0 ] && touch /var/run/s3logrotate
        return $RETVAL
}

stop () {  
          #We do this 3 times to make sure everything goes through .2.gz and once for luck...
        logrotate -f /etc/logrotate.conf
        logrotate -f /etc/logrotate.conf
        logrotate -f /etc/logrotate.conf
        RETVAL=$?
        [ $RETVAL = 0 ] && rm -rf /var/run/s3logrotate
}

case "$1" in  
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        stop
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart}"
esac  

Make sure to add the initd script to all runlevels.

sudo update-rc.d s3logrotate defaults  

To test if its working properly.

/etc/init.d/s3logrotate start
/etc/init.d/s3logrotate stop 

Your logs, if any, should be uploaded to S3 now.

Logrotate

Now all the base work is done. It's time to setup your logrotate scripts. You will be using your regular existing logrotate scripts and adding a postrotate call to the script that we setup above.

For example consider the following logrotate config.

/var/log/nginx/*.log {
    weekly
    missingok
    rotate 10
#    compress
#    delaycompress
    copytruncate
    notifempty
    create 0640 www-data adm
    nosharedscripts
    prerotate
        if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
            run-parts /etc/logrotate.d/httpd-prerotate; \
        fi; \
    endscript
    postrotate
        [ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid`
        /usr/local/admintools/s3_upload.sh $1 &> /var/log/custom/nginx_logrotate.log
    endscript
}

Note that we are not compressing our logs via logrotated in the above script. This is because we are handling this in the S3 upload script. However there is nothing stopping you from doing it via logrotated and not in your script.

Also note that we are using nosharedscripts. I had to use this so that logrotated pass in the currently processed filename to the postrotate script.

References

This post wouldn't exist if it weren't for the following posts :)