#!/bin/bash
#===============================================================================
#
#          FILE: disk_space_monitor
#
#         USAGE: disk_space_monitor
#
#   DESCRIPTION: Monitoring the disk space and send disk space information to 188.
#
#                Please add this schedule to crontab:
#                # Disk Space Monitor
#                * * * * * ~/bin/disk_space_monitor >> ~/.disk_space_monitor.log 2>&1
#
#
#  REQUIREMENTS: 
#
#          BUGS:  ---
#        AUTHOR: rickz (Rick Zhang), xlrickz@gmail.com
#       COMPANY: X-LEGEND Entertainment Corp.
#       CREATED: Tue Mar  5 17:18:46 KST 2013
#      REVISION: 1.0
#
#          TODO:
#
#===============================================================================

#set -o nounset                              # Treat unset variables as an error
#set -m                                       # Enable job control


if ! [ -f ~/.gamerc ] ; then
   ################
   # Set manually #
   ################
   # Game Type (JS, DJ, FN, GD, ...)
   GAME_TYPE="FN"

   # Country Code(ISO 3166-1, UPPER CASE)for example: "TW"
   COUNTRY_CODE="US"

else

   #################################
   # Automatically if you use Ctrl #
   #################################
   source ~/.gamerc

fi

DATE="$(date +%s)"

# LIVE
sendscript allall -p nosave nohostname <<< '

    [ -r ~/.gamerc ] && source ~/.gamerc || HOST_NAME="$HOSTNAME"

    eval $(
        cat /etc/init.d/postgre* 2> /dev/null|grep ^PGDATA=
    )

    df -Plm|grep "% /" \
    |egrep -v "shm|/boot" \
    |awk "{print \"$HOST_NAME,\"\$NF\",\"\$(NF-1)\",\"\$(NF-3)\",\"\$(NF-2)\",\"(\$(NF-2)+\$(NF-3))*0.95-\$(NF-3)}"
' 2>&1|\
grep "%"\
|sed "s/^/$GAME_TYPE,$COUNTRY_CODE,$DATE,/g" > "diskspace-$GAME_TYPE-$COUNTRY_CODE-$DATE.tmp"


if grep -q CTRL <<< $HOST_NAME ; then

    # WITH CTRL

    # TEST
    ssh TEST "cat | sh" <<< '
        [ -r ~/.gamerc ] && source ~/.gamerc || HOST_NAME="$HOSTNAME"

        eval $(
            cat /etc/init.d/postgre* 2> /dev/null|grep ^PGDATA=
        )

        df -Plm|grep "% /" \
        |egrep -v "shm|/boot" \
        |awk "{print \"$HOST_NAME,\"\$NF\",\"\$(NF-1)\",\"\$(NF-3)\",\"\$(NF-2)\",\"(\$(NF-2)+\$(NF-3))*0.95-\$(NF-3)}"
    ' 2>&1|\
    grep "%"\
    |sed "s/^/$GAME_TYPE,$COUNTRY_CODE,$DATE,/g" >> "diskspace-$GAME_TYPE-$COUNTRY_CODE-$DATE.tmp"

    # CTRL
    [ -r ~/.gamerc ] && source ~/.gamerc || HOST_NAME="$HOSTNAME"
    eval $(
        cat /etc/init.d/postgre* 2> /dev/null|grep ^PGDATA=
    )
    df -Plm|grep "% /" \
    |egrep -v "shm|/boot" \
    |awk "{print \"$HOST_NAME,\"\$NF\",\"\$(NF-1)\",\"\$(NF-3)\",\"\$(NF-2)\",\"(\$(NF-2)+\$(NF-3))*0.95-\$(NF-3)}"\
    |grep "%"|sed "s/^/$GAME_TYPE,$COUNTRY_CODE,$DATE,/g" >> "diskspace-$GAME_TYPE-$COUNTRY_CODE-$DATE.tmp"
 
    # Clean Data   
    cat "diskspace-$GAME_TYPE-$COUNTRY_CODE-$DATE.tmp" | grep -v ",/dev" | sort | uniq > "diskspace-$GAME_TYPE-$COUNTRY_CODE-$DATE"
    rm -f "diskspace-$GAME_TYPE-$COUNTRY_CODE-$DATE.tmp"

    # Send to 188
    scp diskspace-$GAME_TYPE-$COUNTRY_CODE-$DATE test:~/
    ssh test "curl --progress-bar --form upload=@\"diskspace-$GAME_TYPE-$COUNTRY_CODE-$DATE\" http://60.199.246.188/upload/ 2>&1 ; rm -f \"diskspace-$GAME_TYPE-$COUNTRY_CODE-$DATE\""
    rm -f diskspace-$GAME_TYPE-$COUNTRY_CODE-$DATE

else
    
    # WITHOUT CTRL
    [ -r ~/.gamerc ] && source ~/.gamerc || HOST_NAME="$HOSTNAME"
    eval $(
        cat /etc/init.d/postgre* 2> /dev/null|grep ^PGDATA=
    )
    df -Plm|grep "% /" \
    |egrep -v "shm|/boot" \
    |awk "{print \"$HOST_NAME,\"\$NF\",\"\$(NF-1)\",\"\$(NF-3)\",\"\$(NF-2)\",\"(\$(NF-2)+\$(NF-3))*0.95-\$(NF-3)}"\
    |grep "%"|sed "s/^/$GAME_TYPE,$COUNTRY_CODE,$DATE,/g" >> "diskspace-$GAME_TYPE-$COUNTRY_CODE-$DATE.tmp"

    # Clean Data
    cat "diskspace-$GAME_TYPE-$COUNTRY_CODE-$DATE.tmp" | grep -v ",/dev" | sort | uniq > "diskspace-$GAME_TYPE-$COUNTRY_CODE-$DATE"
    rm -f "diskspace-$GAME_TYPE-$COUNTRY_CODE-$DATE.tmp"
    
    # Send to 188
    curl --progress-bar --form upload=@"diskspace-$GAME_TYPE-$COUNTRY_CODE-$DATE" http://60.199.246.188/upload/
    rm -f diskspace-$GAME_TYPE-$COUNTRY_CODE-$DATE

fi

