#!/bin/sh   
###This script will be placed at /flash/apps/ #######
### When system comes up it will be automatically placed at /opt/appstop.d/ ######
#Revision history 
#Date                MailId                     Change Reson
#12May2015      nagi.reddy@nokia.com          Commenting the removal of log to retain the postmortem dump files.      

rm -rf /flash/process_list_shutdown*                                 ## "process_list_shutdown" is the file which contains logs of execution of this script 
rm -rf /flash/ipsecounters*
#rm -rf /flash/logs*

now=$(date +"%T")
LOGFILE="/flash/process_list_shutdown_logs"
echo `date` >> $LOGFILE

FILE=/flash/"ipsecounters.txt"
insmod /lib/modules/2.6.32.58-26-lfsm20988-ci1-fct/extra/hrtbt.ko
echo `date` >> $FILE
echo "FPA POOL" >> $FILE
cat /sys/kernel/fpapool/fpa_pool  >> $FILE
echo "Heartbeat Counter" >> $FILE
cat /sys/kernel/heartbeat/hbt_cntr >> $FILE
echo "IPSE FREE LIST" >> $FILE
/opt/trs/bin/secfg_test iptest_se_dbg_dump_freelist >> $FILE
echo "DEBUGSHOW" >> $FILE
/opt/trs/bin/fpdebugAccess show >>$FILE
echo "ABISE SHOW" >> $FILE
/opt/trs/bin/fpdebugAccess abis_show >>$FILE
sleep 1

echo `date` >> $FILE
echo "FPA POOL" >> $FILE
cat /sys/kernel/fpapool/fpa_pool  >> $FILE
echo "Heartbeat Counter" >> $FILE
cat /sys/kernel/heartbeat/hbt_cntr >> $FILE
echo "IPSE FREE LIST" >> $FILE
/opt/trs/bin/secfg_test iptest_se_dbg_dump_freelist >> $FILE
echo "DEBUGSHOW" >> $FILE
/opt/trs/bin/fpdebugAccess show >>$FILE
echo "ABISE SHOW" >> $FILE
/opt/trs/bin/fpdebugAccess abis_show >>$FILE
sleep 1

echo `date` >> $FILE
echo "FPA POOL" >> $FILE
cat /sys/kernel/fpapool/fpa_pool  >> $FILE
echo "Heartbeat Counter" >> $FILE
cat /sys/kernel/heartbeat/hbt_cntr >> $FILE
echo "IPSE FREE LIST" >> $FILE
/opt/trs/bin/secfg_test iptest_se_dbg_dump_freelist >> $FILE
echo "DEBUGSHOW" >> $FILE
/opt/trs/bin/fpdebugAccess show >>$FILE
echo "ABISE SHOW" >> $FILE
/opt/trs/bin/fpdebugAccess abis_show >>$FILE
sleep 1

/bin/ps -el >>  $LOGFILE
if [ $? -ne 0 ]
then
echo "FAILED TO TAKE PS -EL logs" >>  $LOGFILE
fi

echo "killing all BTS process" >>$LOGFILE
echo "Shutdown start time=" >>$LOGFILE
echo $(date +"%T") >>$LOGFILE

##############################################################################
## Function to check when a killing of process is complete.                 ##
##############################################################################
validateProcessKill() 
{
        if [ ! "$1" ]; then                                                
                echo "1 is empty"  >>$LOGFILE
                echo $(date +"%T") >>$LOGFILE
                return                                                  ## If STRING is empty i.e. process has killed as soon as killall command is fired ##
                                                                        ## Then return from this function to kill next task ##
        fi

        count=0
        while true; do                                                  ## If STRING contains pid of a process i.e.that task is not killed immediately after firing killall command ##
             if [ -d /proc/$1 ]; then                                   ## Then there should be a directory in /proc/ for that process ##
                     echo "`ps $1` not killed" >>$LOGFILE
                     sleep 1                                            ## Wait for 1 sec and again check wheather that directory in /proc/ exists ##

                     count=$((count+1))
                                                
                     if [ $count -ge 4 ]; then                          
                            return                                      ## A counter is maintained i.e. if a process is not killed in 4 sec then return from here to kill next process ##
                     fi                                                 ## Control won't reach here. This counter is maintained just for avoiding infinite loop.   ##
                     continue
             else
                     now=$(date +"%T") 
                     echo "$1 is killed now $now" >>$LOGFILE
                     break
             fi
        done
}
##############################################################################
echo " start killing "  >>$LOGFILE
echo $(date +"%T") >>$LOGFILE

killall -9 ukTmgrMon                                                    ## Kill a process by killall command ##
STRING=`pidof "ukTmgrMon"`                                              ## Getting pid of process ##
validateProcessKill $STRING                                             ## Validate that process is killed yet or not ##

killall -9 ukTmgr
STRING=`pidof "ukTmgr"`
validateProcessKill $STRING

killall -9 som_bc
STRING=`pidof "som_bc"`
validateProcessKill $STRING

killall -9 som_ah
STRING=`pidof "som_ah"`
validateProcessKill $STRING

killall -9 som_llc
STRING=`pidof "som_llc"`
validateProcessKill $STRING

killall -9 som_lapd
STRING=`pidof "som_lapd"`
validateProcessKill $STRING

killall -9 som_iua_sctp
STRING=`pidof "som_iua_sctp"`
validateProcessKill $STRING

killall -9 som_btsrv
STRING=`pidof "som_btsrv"`
validateProcessKill $STRING

killall -9 som_commis
STRING=`pidof "som_commis"`
validateProcessKill $STRING

killall -9 som_tcs
STRING=`pidof "som_tcs"`
validateProcessKill $STRING

killall -9 som_q1mgt
STRING=`pidof "som_q1mgt"`
validateProcessKill $STRING

killall -9 som_rp3m
STRING=`pidof "som_rp3m"`
validateProcessKill $STRING

killall -9 som_rp3r
STRING=`pidof "som_rp3r"`
validateProcessKill $STRING

killall -9 som_qdl
STRING=`pidof "som_qdl"`
validateProcessKill $STRING

killall -9 som_hwmgt
STRING=`pidof "som_hwmgt"`
validateProcessKill $STRING

killall -9 som_testm
STRING=`pidof "som_testm"`
validateProcessKill $STRING

killall -9 som_persm
STRING=`pidof "som_persm"`
validateProcessKill $STRING

killall -9 som_rmgw
STRING=`pidof "som_rmgw"`
validateProcessKill $STRING

killall -9 som_scgw
STRING=`pidof "som_scgw"`
validateProcessKill $STRING

killall -9 tmgrRestartTasks
STRING=`pidof "tmgrRestartTasks"`
validateProcessKill $STRING

killall -9 trsAlarms
STRING=`pidof "trsAlarms"`
validateProcessKill $STRING

killall -9 HubActor
STRING=`pidof "HubActor"`
validateProcessKill $STRING

killall -9 trcRemoteLogHdlr
STRING=`pidof "trcRemoteLogHdlr"`
validateProcessKill $STRING

killall -9 trsMgr
STRING=`pidof "trsMgr"`
validateProcessKill $STRING

killall -9 equipPollerThread
STRING=`pidof "equipPollerThread"`
validateProcessKill $STRING

killall -9 equipTempHdlrThread
STRING=`pidof "equipTempHdlrThread"`
validateProcessKill $STRING

killall -9 abisSeLogHandler
STRING=`pidof "abisSeLogHandler"`
validateProcessKill $STRING

killall -9 equipAlarmHdlrThread
STRING=`pidof "equipAlarmHdlrThread"`
validateProcessKill $STRING

killall -9 rmiTriggerThread
STRING=`pidof "rmiTriggerThread"`
validateProcessKill $STRING

killall -9 Abis4LoopThread
STRING=`pidof "Abis4LoopThread"`
validateProcessKill $STRING

killall -9 pathProtIPCThread
STRING=`pidof "pathProtIPCThread"`
validateProcessKill $STRING

killall -9 clockServiceHandlerThread
STRING=`pidof "clockServiceHandlerThread"`
validateProcessKill $STRING

killall -9 srtDaemon
STRING=`pidof "srtDaemon"`
validateProcessKill $STRING

killall -9 menuagent
STRING=`pidof "menuagent"`
validateProcessKill $STRING

killall -9 2kbasic
STRING=`pidof "2kbasic"`
validateProcessKill $STRING

killall -9 q1fmh
STRING=`pidof "q1fmh"`
validateProcessKill $STRING

killall -9 CC_Q1_SLAVE_COM
STRING=`pidof "CC_Q1_SLAVE_COM"`
validateProcessKill $STRING

killall -9 C_Q1_PACKET_COLLECTOR
STRING=`pidof "C_Q1_PACKET_COLLECTOR"`
validateProcessKill $STRING

killall -9 ffsAlarmWatcher
STRING=`pidof "ffsAlarmWatcher"`
validateProcessKill $STRING

killall -9 PollerHandler
STRING=`pidof "PollerHandler"`
validateProcessKill $STRING

killall -9 FTP_SWDLThread
STRING=`pidof "FTP_SWDLThread"`
validateProcessKill $STRING

killall -9 Q1tcpSettingsManager
STRING=`pidof "Q1tcpSettingsManager"`
validateProcessKill $STRING



echo "killing done" >>$LOGFILE
now=$(date +"%T")
echo $now >>$LOGFILE

echo "Backing Up VAR LOG" >>  $LOGFILE
tar -cvzf "/flash/logs.tgz" /var/log/
if [ $? -ne 0 ]
then
echo "FAILED TO TAKE VAR LOG BACK UP" >>  $LOGFILE
fi

##############################################################################
sleep 5

echo "All running Process" >>$LOGFILE
ps -A >>$LOGFILE                                    ## Output of ps -A ##
echo "Flash used by:-" >>$LOGFILE
ps `fuser -m /flash` >>$LOGFILE
sleep 5
echo "Shutdown stop time=" >>$LOGFILE
echo $(date +"%T") >>$LOGFILE


