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"=== 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