#!/bin/bash

source /OSM/script/os_load_base.sh
GProcess=/OSM/bin/alarm


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

    return 0
}
function restartISMProcess()
{
    #执行DM重启进程
    ps -ef |grep -E "ibase|httpd"|grep -v grep |awk '{print $2}'|xargs kill   
    if [ $? -ne 0 ]; then
        OS_echo "RestartISM again."
        ps -ef |grep -E "ibase|httpd"|grep -v grep |awk '{print $2}'|xargs kill
    fi
    return 0;
}

function startProcess()
{
	#执行重启DM进程
	OS_echo "Begin to restart ISM process."
	restartISMProcess
	
	#执行重启ALARM进程
	OS_echo "Begin to restart alarm process."
	restartAlarmProcess
	
    return 0
}

function PATCH_START()
{  
    fstPad="       "
    otrPad="          "
    tmpResult=$(cat /OSM/conf/event_xve_zh.xml |grep 'eventID="17656623857666"')
    if [ -n "$tmpResult" ]; then
    OS_echo "Alarm id 17656623857666 has in file event_xve_zh.xml."
    else
    tmpStr=''$fstPad'<param eventID="17656623857666"\
'$otrPad'name="AD域控修改了存储机器账号的属性"\
'$otrPad'detail="存储控制器（ID ##00），租户（ID ##01，名称 ##02）检测到域名（##03）中存储机器账号属性修改为（##04）。"\
'$otrPad'description="存储控制器（ID ##00），租户（ID ##01，名称 ##02）检测到域名（##03）中存储机器账号属性修改为（##04）。"\
'$otrPad'suggestion="不涉及。"\
'$otrPad'paramDesc="N/A"\
'$otrPad'objID="203"\
'$otrPad'objName="域"\
'$otrPad'location="control-id=##00,tenant-id=##01,tenant-name=##02,domain_name=##03,attribute=##04"\
'$otrPad'alarmOid="" />'
	sed -i "/<\/eventDefinition>/i\\$tmpStr" /OSM/conf/event_xve_zh.xml
    OS_echo "Alarm id 17656623857666 inserted into file event_xve_zh.xml."
    fi

    tmpResult=$(cat /OSM/conf/event_xve_en.xml |grep 'eventID="17656623857666"')
    if [ -n "$tmpResult" ]; then
    OS_echo "Alarm id 17656623857666 has in file event_xve_en.xml."
    else
	tmpStr=''$fstPad'<param eventID="17656623857666"\
'$otrPad'name="The Storage Machine Account Attribute Is Modified By An AD Domain Controller"\
'$otrPad'detail="The storage machine account attribute in domain (##03) is modified to (##04) by AD domain controller (ID ##00) of tenant (ID ##01, name ##02)."\
'$otrPad'description="The storage machine account attribute in domain (##03) is modified to (##04) by AD domain controller (ID ##00) of tenant (ID ##01, name ##02)."\
'$otrPad'suggestion="N/A."\
'$otrPad'paramDesc="N/A"\
'$otrPad'objID="203"\
'$otrPad'objName="Domain"\
'$otrPad'location="control-id=##00,tenant-id=##01,tenant-name=##02,domain_name=##03,attribute=##04"\
'$otrPad'alarmOid="" />'
	sed -i "/<\/eventDefinition>/i\\$tmpStr" /OSM/conf/event_xve_en.xml
    OS_echo "Alarm id 17656623857666 inserted into file event_xve_en.xml."
    fi
    

    sqlite3 /OSM/conf/alarm_config.dat ".timeout 10000" "DELETE FROM alarmdefine_en where AlmID=17656623857666"
    sqlite3 /OSM/conf/alarm_config.dat ".timeout 10000" "DELETE FROM alarmdefine_cn where AlmID=17656623857666"
    
    sqlite3 /OSM/conf/alarm_config.dat ".timeout 10000" "INSERT INTO alarmdefine_cn VALUES('##00,##01,##02,##03,##04',17656623857666,'AD域控修改了存储机器账号的属性',2,'存储控制器（ID ##00），租户（ID ##01，名称 ##02）检测到域名（##03）中存储机器账号属性修改为（##04）。','不涉及。','存储控制器（ID ##00），租户（ID ##01，名称 ##02）检测到域名（##03）中存储机器账号属性修改为（##04）。',203,'域',2)"
    sqlite3 /OSM/conf/alarm_config.dat ".timeout 10000" "INSERT INTO alarmdefine_en VALUES('##00,##01,##02,##03,##04',17656623857666,'The Storage Machine Account Attribute Is Modified By An AD Domain Controller',2,'The storage machine account attribute in domain (##03) is modified to (##04) by AD domain controller (ID ##00) of tenant (ID ##01, name ##02).','N/A.','The storage machine account attribute in domain (##03) is modified to (##04) by AD domain controller (ID ##00) of tenant (ID ##01, name ##02).',203,'Domain',2)"
    startProcess
    return
}

function PATCH_END()
{
    return
}

function PATCH_ROLLSTART()
{
    sed -i  '/17656623857666/,+9d' /OSM/conf/event_xve_zh.xml
    sed -i  '/17656623857666/,+9d' /OSM/conf/event_xve_en.xml

    sqlite3 /OSM/conf/alarm_config.dat ".timeout 10000" "DELETE FROM alarmdefine_en where AlmID=17656623857666"
    sqlite3 /OSM/conf/alarm_config.dat ".timeout 10000" "DELETE FROM alarmdefine_cn where AlmID=17656623857666"
    
    return
}

function PATCH_ROLLEND()
{
    return
}
#start：打补丁前需要执行的操作，脚本的执行时间点在替换文件前
#end：打补丁后需要执行的操作，脚本的执行时间点在替换文件后
#rollstart：回滚补丁时需要执行的操作，脚本的执行时间点在文件被恢复之前。
#rollend：回滚补丁时需要执行的操作，脚本的执行时间点在文件被恢复之后。
#PATCH_CLOSE_SWITCH:先读取性能统计开关的状态，将其值保存到switch中,并关闭性能统计开关
#PATCH_OPEN_SWITCH:如果用户之前是打开状态，将其开关打开
#log "command is "$1

L_CMD=$1
case $L_CMD in
        "start") 
                PATCH_START
                ;;
        "end") 
                PATCH_END
                ;;
        "rollstart") 
                PATCH_ROLLSTART
                ;;
        "rollend") 
                PATCH_ROLLEND
                ;;
        *)
               #log "unsupported option."
esac
exit 0

