Log transfer shell script for Linux


 logtransfer.sh


#! /bin/ksh -p
#####
# Check to ensure that no current log transfer is running before starting.

loggather=`basename $0`
ZIP="/usr/bin/zip"
HOSTNAME=`/bin/hostname`
if [ $# -eq 0 ];then
    print "Please provide destination log dir name as command option"
    print "For example\t$loggather ALL\n\t\tor $loggather <LOB dir name>"
    print "\t\tor $loggather <LOB dir name> checkpoint"
    print "\t\t\t#if .checkpoint has been setup on AMS log server.\n\n\n"
    cat << README
###### `basename $0` v0.3a

Three steps are required on the initial run of the script.
A. Create a filelist file under working dir, ex. /logtransfer, which contains the list of all logs need to be sent to AMS log server.
        Multiple filelist files can be created to send multiple log.zip to AMS log server.
        The file names will be filelist.<LOB1 dir name>, ... filelist.<LOBn dir name>.
        The resulting logs will be sent to AMS log under sub dir as ./<LOB1 dir name>, ... ./<LOBn dir name>.
        Or for multi group logs under the same LOB, filelist.<LOB>.<Group1 name>, ... filelist.<LOB>.<Groupn name>.
        The resulting logs will be sent to AMS log under sub dir as ./<LOB>/<Group1 file or dir name>, ... ./<LOB>/<Groupn file or dir name>.
B. Setup SSH key pairs between system and AMS log server for user sftpuser.
C. Add cronjob entry and put LOB dir name as command option.
        Example,
        0,15,30,45 * * * * /logtransfer/logtransfer.sh IL> /logtransfer/logtransfer.log 2>&1

README
    exit 0
fi

if [ -f /tmp/$loggather.running ] ; then
  # If the lock file is present, the script will check if process running or not.
  if [ "$(ps -p `cat /tmp/$loggather.running` | wc -l)" -gt 1 ]; then
    echo "$0: Log Transfer abandoned.  Currently running under process id `cat /tmp/$loggather.running`"
mailx -s "Log Gather Issue" "pascal.yu@bmo.com" <<EOT
The log transfer on $HOSTNAME is not functioning correctly
EOT
    exit 0
  else
    echo " $0: Orphaned lock file present but no Log Transfer running. Lock file deleted."
    rm /tmp/$loggather.running
  fi
fi
echo $$ > /tmp/$loggather.running
function prepare {
    filelist="/tmp/$$.$1"
    if [ "$2" == "checkpoint" ]; then
        for i in 1 2 3
        do
            scp -p "sftpuser@10.225.3.73:/opt/IBM/HTTPServer/logdir/logfiles$LOB/.checkpoint/$HOSTNAME.logfile.zip" .
            [ $? -eq 0 ] && break
            i="NF"
        done
        if [ "$i" == "NF" ];then
            RECENT="-mtime -1"
        else
            RECENT="-newer $HOSTNAME.logfile.zip"
        fi
        if [ $? -eq 0 ]; then
            while read
            do
                [[ "$REPLY" == *+(\`*\`)* ]] && REPLY="$(eval print $REPLY)"
                [[ "$REPLY" == *+(\$\(*\))* ]] && REPLY="$(eval print $REPLY)"
                if [[ "$REPLY" == *@(/) ]]; then
                    find $REPLY -type f $RECENT
                elif [[ -d "$REPLY" ]]; then
                    print "$REPLY"
                else
                    find $REPLY -prune -type f $RECENT
                fi
            done < $1 > "$filelist"
            return
        fi
    fi
   
    while read
    do
        eval ls -1d $REPLY
    done < $1 > "$filelist"
}
##############################################################
#Log Gathering Steps
echo "####################################################"
date "+%D %T - running log Gathering"
cd /usr/local/scripts/logtransfer
[[ "$1" == "ALL" ]] || LOB=".$1"
for filelist in `ls filelist$LOB*`
do
    LOB="${filelist#*filelist}"
    LOB="$(print "$LOB"|sed 's/[.]/\//g')"
    prepare $filelist $2
    rm $HOSTNAME.logfile.zip
    if [ -s $filelist ]; then
        cat $filelist | $ZIP -r -y $HOSTNAME.logfile.zip -@
        scp -p $HOSTNAME.logfile.zip "sftpuser@10.225.3.73:/opt/IBM/HTTPServer/logdir/logfiles$LOB"
    fi
    rm $filelist
done
##############################################################
#Removal of Lock File
rm /tmp/$loggather.running

Post a Comment

Previous Post Next Post