Created
January 23, 2013 18:52
-
-
Save trak3r/4611540 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
MINUTES=10 | |
# | |
# this script pulls the last $MINUTES minutes out of the apache logs | |
# so cron it to run every $MINUTES minutes | |
# | |
SERVER_ALIAS=$(hostname -s) | |
NAMESPACE="Apache" | |
METRIC_NAME="RequestsPerMinute" | |
WORKING_FOLDER="/var/log/apache2" | |
FILE_NAME="other_vhosts_access.log" | |
FULL_PATH="$WORKING_FOLDER/$FILE_NAME" | |
# | |
# because cron is grossly inconsistent about which environment variables it persists | |
# | |
source $HOME/.rvm/scripts/rvm | |
PATH=$PATH:$HOME/.rvm/bin | |
export JAVA_HOME=/usr/lib/jvm/java-6-sun | |
export JRE_HOME=$JAVA_HOME | |
export AWS_CREDENTIAL_FILE=$HOME/.aws-credentials | |
export AWS_CLOUDWATCH_HOME=$HOME/CloudWatch-1.0.13.4 | |
export PATH=$PATH:$AWS_CLOUDWATCH_HOME/bin | |
# | |
# awk: the date out of the log file | |
# cut: the year, month, day, hour, minute | |
# uniq: count up the uniq occurences | |
# awk: join the dates and their counts so the loop doesn't treat them as separate items in the list | |
# tail: just the last 10 items (latest 10 minutes) | |
# | |
SNIPPETS=$(awk '{print $5}' $FULL_PATH | cut -d: -f1-3 | uniq -c | awk '{print $1 $2}' | tail -n $MINUTES) | |
# | |
# this will return lines that look like this: | |
# | |
# 35[19/Jan/2013:06:29 | |
# | |
for snippet in $SNIPPETS; do | |
# | |
# split the date and the count | |
# | |
JUNK=($(echo $snippet | awk -F[ '{print $1, $2}')) | |
COUNT=${JUNK[0]} | |
RAW_DATE=${JUNK[1]} | |
# | |
# convert the date format to what cloudwatch wants | |
# 2013-01-21T13:00:00.000Z | |
# | |
CONVERTED_DATE=$(ruby -rtime -e "puts Time.strptime('$RAW_DATE', '%d/%b/%Y:%H:%M').strftime('%Y-%m-%dT%H:%M:00.000Z')") | |
# | |
# submit the count to cloudwatch | |
# | |
CMD="mon-put-data --namespace $NAMESPACE --metric-name $METRIC_NAME --timestamp $CONVERTED_DATE --value $COUNT --dimensions VolumeId=$SERVER_ALIAS" | |
echo $CMD | |
$($CMD) | |
# | |
# FIXME: ignore the last item (minute) in the list as it could be partial data | |
# | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment