Skip to content

Instantly share code, notes, and snippets.

@meconlin
Last active April 19, 2016 17:42
Show Gist options
  • Save meconlin/a5c902942d37f1c4247f967f46584198 to your computer and use it in GitHub Desktop.
Save meconlin/a5c902942d37f1c4247f967f46584198 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# USAGE:
# <script>.sh <bucketname> <level> <appname>
# ./logs.sh mybucket stage myapp
#
# Use DEBUG flag to run locally and see debug print lines
# ~/.s3cfg should be set correctly for this to work in non DEBUG mode
# date -d doesnt work on mac osx so watch out for that
#
DEBUG="TRUE"
S3COMMAND_PART=" "
# log things and use local keys for testing, not all versions of s3cmd support --access_key
#
if [ "$DEBUG" == "TRUE" ]; then
KEY=iamakey
SECRET=yoursecretheremineisthatilovewafflehouse
S3COMMAND_PART = " --access_key=$KEY --secret_key=$SECRET "
fi
BUCKETNAME=$1
LEVEL=$2
S3APPNAME=$3
TODAY=$(date +"y=%Y/m=%m/d=%d")
YESTERDAY=$(date --date="1 day ago" +"y=%Y/m=%m/d=%d")
BUCKET_TODAY="s3://$BUCKETNAME/$LEVEL/$S3APPNAME/$TODAY/"
BUCKET_YESTERDAY="s3://$BUCKETNAME/$LEVEL/$S3APPNAME/$YESTERDAY/"
log() {
if [ "$DEBUG" == "TRUE" ]; then
echo $1
fi
}
log " $BUCKETNAME : $LEVEL : $S3APPNAME "
log $TODAY
log $YESTERDAY
log $BUCKET_TODAY
log $BUCKET_YESTERDAY
# if we dont have any from today, check yesterday, we might be on a date boundry
#
LARGEST=$(s3cmd ls $BUCKET_TODAY $S3COMMAND_PART -v | awk 'BEGIN {max = 0} {if ($1$2>max) max=$1" "$2} END {print max}')
if [ LARGEST = "0" ]; then
LARGEST=$(s3cmd ls $BUCKET_YESTERDAY $S3COMMAND_PART -v | awk 'BEGIN {max = 0} {if ($1$2>max) max=$1" "$2} END {print max}')
fi
log $LARGEST
# date -d doesnt work on OSX
LARGEST_S=$(date -d"$LARGEST" +%s)
NOW_S=$(date +%s)
log $LARGEST_S
log $NOW_S
# ANSWER
echo $(( $NOW_S - $LARGEST_S ))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment