Created
December 25, 2017 09:07
-
-
Save jibing57/fd241ab78d0243252a4b19ba19f69fe8 to your computer and use it in GitHub Desktop.
shell to get entry including http 5XX code from access logs of AWS ELB
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 | |
############################################# | |
###### Global Variable ######### | |
############################################# | |
MAIL_TO="" | |
S3_BASE_PREFIX="" | |
BASE_WORK_DIR=`cd $(dirname $0); pwd ` | |
############################################# | |
###### Common Function ######### | |
############################################# | |
function log() | |
{ | |
timer=`date "+%Y-%m-%d %H:%M:%S"` | |
echo "$timer -- $1" | |
} | |
function err_log() | |
{ | |
log "[Error]: $1" | |
} | |
function create_dir() | |
{ | |
dir_name=$1 | |
if [ ! -d $dir_name ]; then | |
log "dir [$dir_name] is not existed, should create it" | |
mkdir -p $dir_name | |
fi | |
if [ ! -d $dir_name ]; then | |
err_log "dir [$dir_name] is not existed, and can't create it" | |
exit -1 | |
fi | |
return 0 | |
} | |
############################################# | |
###### Main Process ######### | |
############################################# | |
if [ "$MAIL_TO" == "" ]; then | |
log "Error: MAIL_TO is empty" | |
exit 1 | |
fi | |
if [ "$S3_BASE_PREFIX" == "" ]; then | |
log "Error: S3_BASE_PREFIX is empty" | |
exit 1 | |
fi | |
is_mac=false | |
os_name=$(uname -s) | |
if [[ "$os_name" == "Linux" ]]; then | |
#statements | |
is_mac=false | |
elif [[ "$os_name" == "Darwin" ]]; then | |
is_mac=true | |
fi | |
if [[ $is_mac == true ]]; then | |
YEAR=`date -v -1d +%Y` | |
MONTH=`date -v -1d +%m` | |
DAY=`date -v -1d +%d` | |
else | |
YEAR=`date +"%Y" -d "1 day ago"` | |
MONTH=`date +"%m" -d "1 day ago"` | |
DAY=`date +"%d" -d "1 day ago"` | |
fi | |
log "=== Try to process access log of ELB on YEAR=$YEAR, MONTH=$MONTH, DAY=$DAY" | |
DATE_PATH="$YEAR/$MONTH/$DAY" | |
cd $BASE_WORK_DIR | |
RESULT_DIR=$BASE_WORK_DIR/result/ | |
WORK_DIR="$BASE_WORK_DIR/$DATE_PATH/" | |
log "=== try to create dir $WORK_DIR" | |
create_dir $WORK_DIR | |
log "=== try to create dir $RESULT_DIR" | |
create_dir $RESULT_DIR | |
cd $WORK_DIR | |
log "=== process work dir is $WORK_DIR, current dir is `pwd`" | |
log "===== Download s3 file start =======" | |
# download s3 file to local dir | |
log "aws s3 sync $S3_BASE_PREFIX/$DATE_PATH/ ./" | |
aws s3 sync $S3_BASE_PREFIX/$DATE_PATH/ ./ | |
log "===== Download s3 file end =======" | |
log "===== grep 5XX start =======" | |
DATE_LOG_NAME="${YEAR}_${MONTH}_${DAY}.log" | |
RESULT_FILE="$RESULT_DIR/${DATE_LOG_NAME}" | |
grep "5[0-9]\{2\} 5[0-9]\{2\}" *.log > $RESULT_FILE | |
cd $BASE_WORK_DIR | |
rm -r $WORK_DIR/*.log | |
log "===== grep 5XX end =======" | |
log "=== File on result_dir[${RESULT_DIR}] is $(ls ${RESULT_DIR}/*${DATE_LOG_NAME})" | |
############################################# | |
###### Send Mail ######### | |
############################################# | |
subject="Report of Http 5XX in ELB Access log on ${YEAR}/${MONTH}/${DAY}" | |
MAIL_ATTACHED_OPTIONS="" | |
for result_file in `ls ${RESULT_DIR}/${DATE_LOG_NAME}` | |
do | |
MAIL_ATTACHED_OPTIONS=" ${MAIL_ATTACHED_OPTIONS} -a ${result_file}" | |
done | |
log "MAIL_ATTACHED_OPTIONS is [${MAIL_ATTACHED_OPTIONS}]" | |
log "=== send report to email $MAIL_TO" | |
if [[ $is_mac == true ]]; then | |
echo -e "Attached is the http 5XX log on ${YEAR}/${MONTH}/${DAY}" | mail -s "${subject}" $MAIL_TO | |
else | |
echo -e "Attached is the http 5XX log on ${YEAR}/${MONTH}/${DAY}" | mail -s "${subject}" $MAIL_ATTACHED_OPTIONS $MAIL_TO | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment