#!/bin/bash

source /OSM/script/os_load_base.sh
GProcess=snmpagent

FLAG=0

function restartProcess()
{
	if [ $FLAG -eq 1 ]; then 
		chmod 775 /ISM/agent/snmp/lib/libsnmp++.so 
        mv /ISM/agent/snmp/lib/libsnmp++.so  /ISM/agent/snmp/lib/libsnmp_patch.tgz
		rm -f /ISM/agent/snmp/lib/libagent++.so
		tar zxf /ISM/agent/snmp/lib/libsnmp_patch.tgz -C /ISM/agent/snmp/lib/
		rm -f /ISM/agent/snmp/lib/libsnmp_patch.tgz
		chown daemon:daemon /ISM/agent/snmp/lib/libsnmp++.so
		chown daemon:daemon /ISM/agent/snmp/lib/libagent++.so
	fi

    #判定该进程是否已经被加载，是否还在运行
    red=$(ps -ww -eo pid,cmd | grep $GProcess | grep -v grep | awk '{print $1}')
    OS_echo "OMM_SNMPAGENT: oldPid: $red"
    if [ "$red" = "" ]; then
        #进程不在位，只有demon不在位的情况，同时如果不在位资源利用率也不会太高，此处不考虑，直接返回OK
        return 0
    else
        OS_echo "OMM_SNMPAGENT: snmpagent will be killed."
        cd /ISM/agent/snmp/conf/
        sh stop_agent.sh
        sleep 1
        sh start_agent.sh
        sleep 1
        cd -
		cd  /ISM/service_ctrl/conf/ 
		sh stop_servicectrl.sh 
		sleep 1
		sh start_servicectrl.sh 
		sleep 1
		cd -
        red=$(ps -ww -eo pid,cmd | grep $GProcess | grep -v grep | awk '{print $1}')
        OS_echo "OMM_SNMPAGENT: newPid: $red"
        if [ "$red" = "" ]; then
            return 1
        else
            return 0
        fi
    fi

    return 0
}

function PATCH_START()
{
    return 
}
function PATCH_END()
{
	FLAG=1
    restartProcess
    return
}
function PATCH_ROLLSTART()
{
    return
}
function PATCH_ROLLEND()
{
	FLAG=0
    restartProcess
    return
}
#start：打补丁前需要执行的操作，脚本的执行时间点在替换文件前
#end：打补丁后需要执行的操作，脚本的执行时间点在替换文件后
#rollstart：回滚补丁时需要执行的操作，脚本的执行时间点在文件被恢复之前。
#rollend：回滚补丁时需要执行的操作，脚本的执行时间点在文件被恢复之后。
#PATCH_CLOSE_SWITCH:先读取性能统计开关的状态，将其值保存到switch中,并关闭性能统计开关
#PATCH_OPEN_SWITCH:如果用户之前是打开状态，将其开关打开
OS_echo "OMM_ALARM: command is ($1)."
L_CMD=$1
case $L_CMD in
        "start") 
                PATCH_START
                ;;
        "end") 
                PATCH_END
                ;;
        "rollstart") 
                PATCH_ROLLSTART
                ;;
        "rollend") 
                PATCH_ROLLEND
                ;;
        *)
                OS_echo "unsupported option."
esac
exit 0

