#!/bin/bash

# This script is use to monitor the omcMain.
# Restart omcd when the omcMain is stopped
# When the omcMain process is suspended
#  single: restart omcd
#  dual:   stop omcd
# The script run every minute and will write the event into /usr/local/omc/log/omcMainWatcherxxx.log
# Time: 	2009-06-11
# Author:	Zheng Jianhui

if [ `whoami` != "root" ] ; then
	echo "Permission denied! Need root user"
	exit
fi

/usr/local/omc/bin/dual/check_proc_status.sh

#mysql_user=`/usr/local/mssBak/script/encryption 1 "6266706d736f7a7c7b6b7f7b7f"`
#mysql_pw=`/usr/local/mssBak/script/encryption 1 "2b3a392757557b697b7e7e757b777c"`
mysql_user=administrator
mysql_pw=*86#ROtartsinim



#----- Get the omcSysNo -------------------------
confDir=/usr/local/omc/bin/conf
if [ `cat $confDir/omcd.conf |grep -c 'omcSysNo=0'` -eq 1 ] ; then
	omcSysNo=0
else
	omcSysNo=1
fi

#----- Get the dual mode ------------------------
#----- 0 single,1 dual   ------------------------
if [ `cat $confDir/omcd.conf |grep -c 'omcRunMode=0'` -eq 1 ] ; then
	omcRunMode=0
else
	omcRunMode=1
fi


############################################################
################# Write event log  #########################
################# Parameter: content #######################
function writeLog()
{
	watcherLogFile=/usr/local/omc/log/watcher`date +%Y%m%d`.log
	timestr=`date +%H:%M:%S`
	#echo $timestr
	`echo "$timestr $1" >> $watcherLogFile`
}
#writeLog hello
################# End writeLog function ####################
################# Insert alarm log #########################
############## Parameter: stop, suspend ####################
function insertAlarmLog()
{
	case "$1" in
	'stop')

#----- Get the previous record number -----------
prev_record_arr=`/usr/bin/mysql -u$mysql_user -p$mysql_pw <<_EOF_
SELECT count(*) as record_count FROM OMC_PUB.sysAlarmLog WHERE sysTypeNo=0 and sysNo=$omcSysNo and compCode=130 and alarmCode=1 and clearTime = '0000-00-00 00:00:00';
_EOF_`
prev_record=`echo $prev_record_arr | awk '{print $2}'`

#----- Insert the alarm -------------------------
if [ "$prev_record" == "0" ] ; then
/usr/bin/mysql -u$mysql_user -p$mysql_pw <<_EOF_
INSERT INTO OMC_PUB.sysAlarmLog (sysTypeNo,sysNo,subSysNo,compCode,alarmCode,alarmTime) VALUES (0,$omcSysNo,0,130,1,CURRENT_TIMESTAMP);
_EOF_
fi

	;;
	'suspend')

#----- Get the previous record number -----------
prev_record_arr=`/usr/bin/mysql -u$mysql_user -p$mysql_pw <<_EOF_
SELECT count(*) as record_count FROM OMC_PUB.sysAlarmLog WHERE sysTypeNo=0 and sysNo=$omcSysNo and compCode=130 and alarmCode=2 and clearTime = '0000-00-00 00:00:00';
_EOF_`
prev_record=`echo $prev_record_arr | awk '{print $2}'`

#----- Insert the alarm -------------------------
if [ "$prev_record" == "0" ] ; then
/usr/bin/mysql -u$mysql_user -p$mysql_pw <<_EOF_
INSERT INTO OMC_PUB.sysAlarmLog (sysTypeNo,sysNo,subSysNo,compCode,alarmCode,alarmTime) VALUES (0,$omcSysNo,0,130,2,CURRENT_TIMESTAMP);
_EOF_
fi

	;;
	esac
}
################# End insertAlarmLog function #################


#----- Check the process id of the omcMain -------
#. /etc/rc.d/init.d/functions

procName=omcMain
#pid=`status $procName`
pid=`pidof $procName`
#if [ "$pid" == "omcMain is stopped" ]; then
if [ "$pid" == "" ]; then

#----- omcMain is stopped -----------------
		insertAlarmLog stop
		writeLog "omcMain is stop, restart it"
		tmpLogFile=/usr/local/omc/log/watcher`date +%Y%m%d`.log
		`tail -5 /usr/local/omc/install.log >> $tmpLogFile`
    wxc2dm=`/usr/local/omc/bin/wxc2_omcd restart`
    
else

#----- omcMain is running -----------------
update_time_arr=`/usr/bin/mysql -u$mysql_user -p$mysql_pw <<_EOF_
SELECT UNIX_TIMESTAMP(updateTime) as updateTime FROM OMC_PUB.sysInfo WHERE sysTypeNo=0 and sysNo=$omcSysNo;
_EOF_`
update_time=`echo $update_time_arr | awk '{print $2}'`
if [ -z "$update_time" ]; then
	writeLog "omcMain is just running, check heartbeat later"
	exit
fi

now_time=`date +%s`
time_delay=$[$now_time - $update_time]

#echo update_time=$update_time
#echo now_time=$now_time
#echo time_delay=$time_delay

#----- check heartbeat updatetime ---------
if [ "$time_delay" -ge "180" ]; then

	insertAlarmLog suspend
	writeLog "omcMain is suspended"
	if [ "$omcRunMode" -eq "0" ]; then
		writeLog "omcMain is running in single mode, restart it"
		wxc2dm=`/usr/local/omc/bin/wxc2_omcd restart`
	else
		writeLog "omcMain is running in dual mode, restart it"
		wxc2dm=`/usr/local/omc/bin/wxc2_omcd restart`
	fi
fi



fi

ProcList="iptrans omcMain paraComm subsComm logCollector omcCleaner smcli cdrCollector subsDataBackup ftpSend alarmAgent nrtrde sftpSend alive bsscomm"

logFile=/usr/local/omc/log/watcher`date +%Y%m%d`.log

printf "`date '+%D %H:%M:%S'` " >> $logFile
for ProcName in $ProcList; do
        if [ -z "`pidof $ProcName`" ]; then
                state="N"
        else
                state="Y"
        fi
        printf "$state " >> $logFile
done
printf "\n" >> $logFile


chmod 777 /usr/local/apache/htdocs/ftpFile
chmod 640 /usr/local/omc/log/*
chmod -R 777 /usr/local/apache/htdocs/db_backup/


#echo Finish



