#!/bin/sh -x 

################################################################################
#
#    5639-VC4 and 5639-VW1
#
#    ? Copyright Inspur Corp. 2006, 2016 All Rights Reserved.
#
#    The source code for this program is not published or
#    otherwise divested of its trade secrets, irrespective of
#    what has been deposited with the U.S. Copyright Office.
#
################################################################################

cimMemLog=/dumps/cimom/cimMemUse.${SVC_NODEID}
cimPIDFile="/data/cimom/cimserver_start.conf"

# default memory limitation for MCS is set to 195m
#MemLimit=195000
#TDLimit=115
#MEMWarning=150000   
#TDWarning=80

sleep 15
echo `date` >> ${cimMemLog} 2>&1
if [ -r "$cimPIDFile" ]
then
   cimPID=`cat ${cimPIDFile}` 
   echo "cimserver ($cimPID) is started..." >> ${cimMemLog} 2>&1
else
   echo "cimserver tries to start..." >> ${cimMemLog} 2>&1
fi    

#export SVCAGENT_DATA=/data/cimom
#export SVCAGENT_HOME=/compass/cimom

#. $SVCAGENT_HOME/config/envConf

function cimMemoryMonitor() {

# check if it needs to rotate the log.
LineCount=`cat ${cimMemLog} | wc -l `
if [ ${LineCount} -ge 50000 ]; then
mv ${cimMemLog} ${cimMemLog}.bak
fi

#  check if it needs to restart cimom.
#for i in `seq 3`
#do
#    retValue=`cimcli -n root/PG_InterOP --timeout 60 ei -niq CIM_ObjectManager 2>&1`;
#    echo "$retValue" |grep -q "Exception"
#    if [ $? -eq 0 ]
#    then
#	    echo `date` >> ${cimMemLog} 2>&1;
#        echo "$retValue"  >> ${cimMemLog} 2>&1;
#        if [ $i -eq 3 ]
#        then
#            isKillRequired=0;
#        else
# Get connect problem, wait for 6 seconds to try again
#            sleep 6;
#        fi
#    else
# No connection problem, exist loop        
#        break;
#   fi
#done

#if [ $isKillRequired ] && [ $cimPID ]
#then
#	echo `date` >> ${cimMemLog} 2>&1;
#	echo "Get exception to connect to cimserver three times" >> ${cimMemLog} 2>&1;
#	kill -9 $cimPID 2>&1;
#    if [ $? -eq 0 ]
#    then
#        echo "Hit connection problem *** KILLED : [ $cimPID ] ***" >> ${cimMemLog} 2>&1;
#    else
#        echo "Fail to kill : [ $cimPID ] ***" >> ${cimMemLog} 2>&1;
#    fi    
#    unset isKillRequired;
#    unset cimPID;
#fi
}

while true
do
cimMemoryMonitor
sleep 60
done
