#!/bin/bash

source /OSM/script/os_load_base.sh

function startProcess()
{
	#执行重启IBASE进程
	OS_echo "Begin to restart IBASE process."
	sh /OSM/script/reload_alarmconfig.sh ibase
	
	#执行重启ALARM进程
	OS_echo "Begin to restart alarm process."
	sh /OSM/script/reload_alarmconfig.sh alarm

	#执行重启Snmp进程
	OS_echo "Begin to restart snmp process."
	sh /OSM/script/reload_alarmconfig.sh snmp

    return 0
}

function PATCH_START()
{  
    fstPad="       "
    otrPad="          "
    tmpResult=$(cat /OSM/conf/event_xve_zh.xml |grep 'eventID="68356813847"')
    if [ -n "$tmpResult" ]; then
    OS_echo "Alarm id 68356813847 has in file event_xve_zh.xml."
    else
    tmpStr=''$fstPad'<param eventID="68356813847"\
'$otrPad'name="CCDB集群备份失败"\
'$otrPad'detail="CCDB全量备份数据库失败，错误码：##01。"\
'$otrPad'description="CCDB全量备份数据库失败，错误码：##01。"\
'$otrPad'suggestion="请收集相关事件信息，然后联系技术支持工程师进行处理。"\
'$otrPad'paramDesc="N/A"\
'$otrPad'objID="60002"\
'$otrPad'objName="集群"\
'$otrPad'location="ip=##00,error-code=##01"\
'$otrPad'alarmOid="15.60002.13335" />'
	sed -i "/<\/eventDefinition>/i\\$tmpStr" /OSM/conf/event_xve_zh.xml
    sed -i "/<\/eventDefinition>/i\\$tmpStr" /OSM/coffer_data/omm/event_xve_zh.xml
    OS_echo "Alarm id 68356813847 inserted into file event_xve_zh.xml."
    fi

    tmpResult=$(cat /OSM/conf/event_xve_en.xml |grep 'eventID="68356813847"')
    if [ -n "$tmpResult" ]; then
    OS_echo "Alarm id 68356813847 has in file event_xve_en.xml."
    else
	tmpStr=''$fstPad'<param eventID="68356813847"\
'$otrPad'name="CCDB Cluster Backup Failed"\
'$otrPad'detail="CCDB full database backup failed. Error code: ##01."\
'$otrPad'description="CCDB full database backup failed. Error code: ##01."\
'$otrPad'suggestion="Collect related information and contact technical support engineers."\
'$otrPad'paramDesc="N/A"\
'$otrPad'objID="60002"\
'$otrPad'objName="Storage cluster"\
'$otrPad'location="ip=##00,error-code=##01"\
'$otrPad'alarmOid="15.60002.13335" />'
	sed -i "/<\/eventDefinition>/i\\$tmpStr" /OSM/conf/event_xve_en.xml
    sed -i "/<\/eventDefinition>/i\\$tmpStr" /OSM/coffer_data/omm/event_xve_en.xml
    OS_echo "Alarm id 68356813847 inserted into file event_xve_en.xml."
    fi
	
    tmpResult=$(cat /ISM/agent/snmp/conf/snmpagent/alarm_config.xml |grep 'eventID="68356813847"')
    if [ -n "$tmpResult" ]; then
    OS_echo "Alarm id 68356813847 has in file event_xve_en.xml."
    else
	tmpStr=''$fstPad'<param eventID="68356813847"\
'$otrPad'name="CCDB Cluster Backup Failed"\
'$otrPad'detail="CCDB full database backup failed. Error code: ##01."\
'$otrPad'description="CCDB full database backup failed. Error code: ##01."\
'$otrPad'suggestion="Collect related information and contact technical support engineers."\
'$otrPad'paramDesc="N/A"\
'$otrPad'objID="60002"\
'$otrPad'objName="Storage cluster"\
'$otrPad'location="ip=##00,error-code=##01"\
'$otrPad'alarmOid="15.60002.13335" />'
	sed -i "/<\/eventDefinition>/i\\$tmpStr" /ISM/agent/snmp/conf/snmpagent/alarm_config.xml
    OS_echo "Alarm id 68356813847 inserted into file alarm_config.xml."
    fi
    
    sqlite3 /OSM/conf/alarm_config.dat ".timeout 10000" "DELETE FROM alarmdefine_en where AlmID=68356813847"
    sqlite3 /OSM/conf/alarm_config.dat ".timeout 10000" "DELETE FROM alarmdefine_cn where AlmID=68356813847"
    
	sqlite3 /OSM/conf/alarm_config.dat ".timeout 10000" "INSERT INTO alarmdefine_cn VALUES('',68356813847,'CCDB集群备份失败',5,'CCDB全量备份数据库失败，错误码：##01。','请收集相关事件信息，然后联系技术支持工程师进行处理。','CCDB全量备份数据库失败，错误码：##01。',60002,'集群',0)"
    sqlite3 /OSM/conf/alarm_config.dat ".timeout 10000" "INSERT INTO alarmdefine_en VALUES('',68356813847,'CCDB Cluster Backup Failed',5,'CCDB full database backup failed. Error code: ##01.','Collect related information and contact technical support engineers.','CCDB full database backup failed. Error code: ##01.',60002,'Storage cluster',0)"
    startProcess
    return
}

function PATCH_END()
{
    return
}

function PATCH_ROLLSTART()
{
    sed -i  '/68356813847/,+9d' /OSM/conf/event_xve_zh.xml
    sed -i  '/68356813847/,+9d' /OSM/conf/event_xve_en.xml
    sed -i  '/68356813847/,+9d' /OSM/coffer_data/omm/event_xve_zh.xml
    sed -i  '/68356813847/,+9d' /OSM/coffer_data/omm/event_xve_en.xml
    sed -i  '/68356813847/,+9d' /ISM/agent/snmp/conf/snmpagent/alarm_config.xml

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

    startProcess
    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

