
#!/bin/sh
# File	: dfm_restart_and_clean-up.sh
# By	: Maarten.deBoer@Atos.net, 150901
# Subject	: Script to restart & clean-up DFM 5.x
#
PGM="`basename $0|cut -d\. -f1`"
TMP="/tmp/${PGM}.$$"
LOG="${HOME}/log/${PGM}.log"
ASC="/tmp/${PGM}.asc"
HOSTNAME="`hostname|cut -d\. -f1`"

MAILTO="maarten.deboer@atos.net"

echo "`date` ${PGM} v${VER} started."|tee -a ${LOG}
touch ${TMP}

##${HOME}/bin/create_sdm-ticket.sh -i -s 3 -t Incident -m 'DFM restart due to missing events'
 
sleep 5
##echo "Please be aware DFM @${HOSTNAME} will be restarted. Due to due to missing events."|mailx -s "DFM restart" dlnlo-fsod@atos.net

echo "Save last appl/dfm status"|tee -a ${LOG}
dfm service list >> ${TMP}
ps -ef | grep 'appl/dfm' >> ${TMP}

/usr/bin/logger stop host `uname -n` 1 "Restart of DFM application"

echo "`date` Stopping DFM ..."|tee -a ${LOG}
sudo /usr/bin/dfm service stop

sleep 5
echo "`date` DFM-procs:" |tee -a ${TMP}
ps -ef|grep 'appl/dfm' | tee -a ${TMP}
echo "`date` Kill them ..." |tee -a ${TMP}
ps -ef |grep 'appl/dfm'|grep -v grep|awk '{print $2}'| while read PID
do
  sudo kill ${PID}
done  # PID

sleep 5
echo "`date` Hanging DFM-procs:" |tee -a ${TMP}
ps -ef|grep 'appl/dfm' | tee -a ${TMP}
echo "`date` Kill them ..." |tee -a ${TMP}
ps -ef |grep 'appl/dfm'|grep -v grep|awk '{print $2}'| while read PID
do
  sudo kill -9 ${PID}
done  # PID

sleep 5
ls -ld /tmp/*SQL* >>  ${TMP}
echo "`date` Clean-up DFM"|tee -a ${TMP} ${LOG}
cd /tmp
sudo rm -rf /tmp/*SQL*
ls -ld /tmp/*SQL* >>  ${TMP}

sleep 5
echo "`date` Starting DFM-sql ..."|tee -a ${LOG}
sudo /usr/bin/dfm service start sql
echo "Waiting 5 min."
sleep 300
echo "`date` Starting DFM ..."|tee -a ${LOG}
sudo /usr/bin/dfm service start 

sleep 5
sudo /usr/bin/dfm service list |tee -a ${TMP}

sudo cat /var/log/messages grep FSOD|tail -2|tee -a ${TMP} ${LOG}

cp ${TMP} ${ASC}
date|mailx -a ${ASC} -s "Restarted DFM @${HOSTNAME} [${PGM} v${VER}]" ${MAILTO}

rm ${TMP} ${ASC}
echo "`date` ${PGM} finished."|tee -a ${LOG}
exit 0

