#!/bin/bash ################################################################################################################# <<"COMMENT" Author: Anil Parvatam Revision Date Author Notes 1.0.0 03/19/2019 Anil Parvatam Script creation How to run this script ./tams_store_scores.sh <<-S> 7DigitStoreNumber <<-r> ResultsDir [ <<-l> Loglevel <<-L> LogFileSuffix ] <<-d> TargetDirectory [<<-t> TempDirectory ] <<-l> Logging Level - Optional <<-r> ResultsDir - Required <<-d> TargetDirectoryToCopyScorecardFile - Required e.g. ./tams_store_scores.sh -S 0502192 -r `pwd`/Results_inventory_scorecard -l N -d /mnt/dropbox/tibco/inventory-scorecard/tams/ FileDependency common/esb.env common/errorhandler.env ${__filename} common/archive.env common/dircheck.env scorecard/ftp.nfo scorecard/inventory_scorecard_exec.sh scorecard/store_score.sql scorecard/nfop.tmp scorecard/server_all.csv scorecard/sql.nfo COMMENT ################################################################################################################# usage () { echo " Usage: $0 <<-s> 7DigitStoreNumber <<-r> ResultsDir <<-d> TargetDirectory [ <<-l> Loglevel <<-L> LogFileSuffix ] [<<-t> TempDirectory ] " >&2 exit 1 } #ftp_get_inventory_scorecard and ftp_put_inventory_scorecard do_ping() { edebug "Performing Ping on ${IP} Ip" PingOK="N" case "$KERNEL_NAME" in Linux) ping -c 1 -w 5 ${IP} 1>/dev/null 2>&1 ;; CYGWIN_NT) ping -n 1 -w 5000 ${IP} 1>/dev/null 2>&1 ;; esac if [ $? -eq 0 ] then PingOK="Y" fi } createcmdfile() { einfo "Generating FTP Command file for ${operation} operation" if [ "${operation}" = "ftp_get" ] then ( echo "open "$IP echo "user "$User" "$Password echo "lcd ${RESDIR}" echo "cd /usr/tmp" echo "bin" echo "get esb_inventory_scorecard.out inventory_scorecard.${IP}" echo "get esb_inventory_scorecard_exec.op.gz inventory_scorecard_exec.${IP}.op.gz" echo "bye" ) > $CmdFile else ( echo "open "$IP echo "user "$User" "$Password echo "cd /usr/tmp" echo "bin" echo "lcd ${SCOREDIR}" echo "put nfop.tmp" echo "put ${SQLFILE} esb_inventory_scorecard.sql" echo "put inventory_scorecard_exec.sh esb_inventory_scorecard_exec.scr" echo "bye" ) > $CmdFile fi } #ftp_put_inventory_scorecard do_ftp() { User=$1 Password=$2 OutFile=${RESDIR}/${operation}_inventory_scorecard.${LOGNAME}.${ProcessNumber}.out CmdFile=$TMP_DIR/${operation}_inventory_scorecard.${LOGNAME}.${ProcessNumber}.tmp einfo "Running ${operation} operation for ${IP} Ip" createcmdfile ftp -n < $CmdFile 1>$OutFile 2>&1 rm $CmdFile ExitCode=0 grep -q 'USER and PASS' $OutFile if [ $? = 0 ]; then ExitCode=1 fi return ${ExitCode} } credentialloop() { operation=$1 ProcessNumber=$2 CheckFile=${RESDIR}/${operation}_inventory_scorecard.${IP} AlreadyHaveResults="N" edebug "Checking if the Results already exists ${CheckFile}" if [ -f "${CheckFile}" ] then AlreadyHaveResults="Y" PingOK="Y" else do_ping fi #Looping the Credentials from ftp.nfo file for successful loging if [ "${AlreadyHaveResults}" = "N" -a ${PingOK} = "Y" ] then Attempt_Num=0 Done_Trying=0 while [ ${Done_Trying} -eq 0 ] do Attempt_Num=`expr ${Attempt_Num} + 1` einfo "Attemp ${Attempt_Num}, trying the credentials from ${INFO_FILE} file" nfo=`cat $INFO_FILE | tail --lines=+${Attempt_Num} | head -1` if [ -z "$nfo" ] then Done_Trying=1 else do_ftp $nfo if [ $? -eq 0 ] then Done_Trying=1 fi fi done fi } #background_batch background_batch() { CommandToRun=$1 einfo "Encrypting SQL Password" cat ${SQLINFO} | gzip -c | dd conv=swab 2>/dev/null > ${SQLINFOTMP} errorcheck "Error Occured while Encrypting SQL Password" chmod 600 ${SQLINFOTMP} runCounter=0 STORE=${STORENUM} IP=$(grep ${STORENUM} ${SERVERLIST}| cut -f2 -d',' | sed 's/"//g') if [ ! -z ${IP} ] then enotify "Working on ${CommandToRun} operation on ${STORE} Store with ${IP} Ip Address " runCounter=$(expr ${runCounter} + 1) credentialloop ${CommandToRun} ${runCounter} & else eerror "IP Address not found in ${SERVERLIST} for store ${STORE}" exit 1 fi } ################################################################################################################# #SCRIPT STARTS HERE ################################################################################################################# #Input Parameter Intialization #Exit when your script tries to use undeclared variables set -o nounset #To trace what gets executed. Useful for debugging #set -o xtrace #Fails if the argument before pipe fails set -o pipefail # Set magic variables for current file & dir #Directory Of the Script __dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" #Absolute Script Name __absfilename="${__dir}/$(basename "${BASH_SOURCE[0]}")" #Script Name __filename="$(basename ${__absfilename} .sh)" #__root="$(cd "$(dirname "${__dir}")" && __commonloc=${__dir}/common . ${__commonloc}/esb.env . ${__commonloc}/errorhandler.env ${__filename} . ${__commonloc}/archive.env . ${__commonloc}/dircheck.env date=`date +%Y%m%d` RESDIR="" LLVL="N" LSFX="taminvscore" TMP_DIR="/usr/tmp" DRCTRY="" STORENUM="" SCOREDIR=${__dir}/scorecard SQLINFOTMP=${SCOREDIR}/nfop.tmp SQLINFO=${SCOREDIR}/sql.nfo #TAMS Database query to retrieve scores #SQLFILE=${SCOREDIR}/inventory_scorecard.sql SQLFILE=${SCOREDIR}/inventory_scorecard_store.sql #List Of Stores and their IPs SERVERLIST=${SCOREDIR}/server_all.csv #To Perform Ping KERNEL_NAME=`uname -s | cut -f1 -d'-'` #Credentials to TAMS Servers INFO_FILE=${SCOREDIR}/ftp.nfo #Used to loop through number of stores from the server list at a time STORECOUNT=30 while [ $# -gt 0 ] do case "$1" in -r) RESDIR="$2" ; shift ;; -l) LLVL="$2" ; shift ;; -L) LSFX="$2" ; shift ;; -t) TMP_DIR="$2" ; shift ;; -s) STORENUM="$2" ; shift ;; -d) DRCTRY="$2" ; shift ;; --) shift; break ;; -*) usage ;; *) break ;; esac shift done #Checking the Input Parameters [ "$RESDIR" = "" ] && usage [ "$STORENUM" = "" ] && usage [ "$DRCTRY" = "" ] && usage #__logfilesfx=${LSFX} . ${__commonloc}/logger.env -${LLVL} enotify "Job ${__filename} started" cd $__dir #Naming the file by storenumber when it is provided SCORECARDFILE=inventory_scorecard.${STORENUM} einfo "Removing Content under Results Directory if exists" mkdir -p $RESDIR rm -f "${RESDIR}"/* einfo "Triggering batch process for ftp put operation" #Trigger Put Operation background_batch ftp_put einfo "Done Triggering batch process for ftp put operation" enotify "Waiting 60 seconds before proceeding" sleep 60 einfo "Triggering batch process for ftp get operation" #Trigger Get Operation background_batch ftp_get einfo "Done Triggering batch process for ftp get operation" enotify "Waiting 5 seconds before proceeding" sleep 5 enotify "Consolidating the results from the Stores into one" edebug "find ${RESDIR} -type f -name 'inventory_scorecard.*' -exec cat {} >> ${RESDIR}/consolidate_inventory_scorecard.out \;" find ${RESDIR} -type f -name 'inventory_scorecard.*' -exec cat {} >> ${RESDIR}/consolidate_inventory_scorecard.out \; errorcheck "Error Occured while Consolidating the results from the Stores into one" edebug "Sorting the results and writing to ${RESDIR}/${SCORECARDFILE}" sort ${RESDIR}/consolidate_inventory_scorecard.out -o ${RESDIR}/${SCORECARDFILE} errorcheck "Error Occured while Sorting the results and writing to ${SCORECARDFILE}" enotify "Pushing the Scorecard ${SCORECARDFILE} to ${DRCTRY}" cp ${RESDIR}/${SCORECARDFILE} ${DRCTRY} eok "Job ${__filename} completed" exit 0