Subversion Repositories SmartDukaan

Rev

Rev 4904 | Rev 11452 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/bin/bash

# resolve links - $0 may be a softlink
PRG="$0"

while [ -h "$PRG" ]; do
  ls=`ls -ld "$PRG"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '/.*' > /dev/null; then
    PRG="$link"
  else
    PRG=`dirname "$PRG"`/"$link"
  fi
done


echo "======================================================="
echo "Get the scp/rsync snapshot and mysql table migration snapshot."
echo "======================================================="

# Get standard environment variables
PRGDIR=`dirname $(readlink -f "$PRG")`

hinit() {
    mv ${PRGDIR}/hashmap.new ${PRGDIR}/hashmap.old
}

hput() {
    echo "$1 $2" >> ${PRGDIR}/hashmap.new
}

hget() {
    grep "^$2 " ${PRGDIR}/hashmap.$1 | awk '{ print $2 };'
}

hiterate() {
        awk '{ print $1 };' ${PRGDIR}/hashmap.$1
}

cd ${PRGDIR}
CATALOG_FILE=`ls -t partial-catalog-*.sql | head -n 1`
MYSQL_DUMP="${PRGDIR}/${CATALOG_FILE}"
EXPORT_PATH="/var/lib/tomcat6/webapps/export"
ALL_RSYNC_DIRS="${EXPORT_PATH}/media ${EXPORT_PATH}/media/website ${EXPORT_PATH}/media/static ${EXPORT_PATH}/documents"
ALL_SCP_FILES="${EXPORT_PATH}/javascripts.tgz ${EXPORT_PATH}/html/helpdocs.tgz ${EXPORT_PATH}/html/entities-shop2020.tgz ${EXPORT_PATH}/html/entities-saholic.tgz ${EXPORT_PATH}/partners.tgz ${EXPORT_PATH}/solr.tgz ${MYSQL_DUMP}"
PROJECT_DIR="`dirname ${PRGDIR}`"
OUT_FILE="${PRGDIR}/catalog_info.txt"
OUT_FILE_OLD="${PRGDIR}/catalog_info_old.txt"
COMP_FILES=1
ALERT=0
ALERT_FILE=${PRGDIR}/alert.txt

#Empty the alert file
echo > ${ALERT_FILE}

#Before getting started move OUT_FILE to OUT_FILE_OLD
# also init the hashmap
`hinit`

if [ -f ${OUT_FILE} ];
then
        cat ${OUT_FILE} > ${OUT_FILE_OLD}
else 
        echo "Could not find the yesterday's validation file. Comapsions cant be performed"
        COMP_FILES=0
fi
        
echo -e "Resource name\tSize\tDir Count\tFile Count" > ${OUT_FILE}

for DIR in ${ALL_RSYNC_DIRS}
do
        cd ${DIR}
        FILE_COUNT="$(find ${DIR} -maxdepth 1 -type f | wc -l)"
        DIR_COUNT="$(find ${DIR} -maxdepth 1 -type d | wc -l)"
        DIR_SIZE="$(du -k --max-depth 0| awk {'print $1'})"
        hput "${DIR}|FILE_COUNT" ${FILE_COUNT}
        hput "${DIR}|DIR_COUNT" ${DIR_COUNT}
        hput "${DIR}|DIR_SIZE" ${DIR_SIZE}
        echo -e "${DIR}\t${DIR_SIZE}\t${DIR_COUNT}\t${FILE_COUNT}" >> ${OUT_FILE}
done

for FILE in ${ALL_SCP_FILES}
do
        FILE_SIZE=$(ls -la ${FILE} | awk {'print $5'})
        echo -e "${FILE}\t${FILE_SIZE}" >> ${OUT_FILE}
        FILE=${FILE/\-[0-9][0-9][0-9][0-9]*/}
        hput "${FILE}|FILE_SIZE" ${FILE_SIZE}
done

#get the items count from the mysql server
PAUSED_ITEMS=$(mysql -s --skip-column-names -uroot -pshop2020 catalog -e "select count(*) from item where status = 2")
ACTIVE_ITEMS=$(mysql -s --skip-column-names -uroot -pshop2020 catalog -e "select count(*) from item where status = 3")
echo "Active items=${ACTIVE_ITEMS}" >> ${OUT_FILE}
echo "Paused items=${PAUSED_ITEMS}" >> ${OUT_FILE}
hput "ACTIVE_ITEMS_COUNT|NUMBER" ${ACTIVE_ITEMS}
hput "PAUSED_ITEMS_COUNT|NUMBER" ${PAUSED_ITEMS}

if [ ${COMP_FILES} -ne 0 ]; then
        for KEY in `hiterate old`
        do
                NEW_VAL=`hget "new" ${KEY}`
                OLD_VAL=`hget "old" ${KEY}`
                if [ ${OLD_VAL} -gt 0 ]; then
                        DIFF=$(echo "${NEW_VAL} - ${OLD_VAL}"|bc|awk '{ print ($1 >= 0) ? $1 : 0 - $1}')
                        DIFF_PERC=$(echo "scale=2;(${DIFF}*100)/${OLD_VAL}" |bc|awk '{printf "%.2f", $0}')
                        if [ $(echo ${DIFF_PERC} | tr "." " "| awk {'print $1'}) -ge 5 ]; then
                                ALERT=1
                                NAME=$(echo ${KEY} | tr "|" " "| awk {'print $1'})
                                VALUE=$(echo ${KEY} | tr "|" " "| awk {'print $2'})
                                echo "${VALUE} for ${NAME} is varied by ${DIFF_PERC}%">>${ALERT_FILE}
                        fi
                fi
        done
        if [ $ALERT -ne 0 ]; then
                sendEmail -f build@shop2020.in -t pankaj.kankar@shop2020.in anupam.singh@shop2020.in mandeep.dhir@shop2020.in rajveer.singh@shop2020.in amit.gupta@shop2020.in -u "Alert: At least one parameter exceeded their threshold value" -s smtp.gmail.com:587  -xu build@shop2020.in  -xp cafe@nes -a ${OUT_FILE} ${OUT_FILE_OLD} -o tls=yes message-file=${ALERT_FILE}
                exit 1
        fi
fi