#!/bin/bash

source /OSM/script/os_load_base.sh

fstPad="       "
otrPad="          "

recon_fail_alarm_id=64441942133
recon_fail_alarm_sql_cn=""
recon_fail_alarm_sql_en=""
recon_fail_alarm_info_cn=""
recon_fail_alarm_info_en=""

precopy_fail_alarm_id=64441942134
precopy_fail_alarm_sql_cn=""
precopy_fail_alarm_sql_en=""
precopy_fail_alarm_info_cn=""
precopy_fail_alarm_info_en=""

function log()
{
    OS_echo "[OMM_ALARM][HotPatch]$*"
}

function restartProcess()
{
    OS_echo "Begin to restart OMM process."
    sh /OSM/script/reload_alarmconfig.sh ibase
    sh /OSM/script/reload_alarmconfig.sh snmp
    sh /OSM/script/reload_alarmconfig.sh alarm
    return 0
}

function init()
{
recon_fail_alarm_sql_cn="INSERT INTO "alarmdefine_cn" VALUES('##01,##06',64441942133,'硬盘域重构失败',5,'硬盘域（ID ##01，名称 ##00）重构失败。','请收集相关事件信息，然后联系技术支持工程师进行处理。','硬盘域（ID ##01，名称 ##00）中硬盘（##02{0:控制框;1:硬盘框;2:引擎} ##03，槽位号 ##04，类型 ##05{0:FC;1:SAS;2:SATA;3:SSD;4:NL-SAS;5:SLC SSD;6:MLC SSD;7:FC_SED;8:SAS_SED;9:SATA_SED;10:SSD_SED;11:NL-SAS_SED;12:SLC SSD_SED;13:MLC SSD_SED;14:NVMe SSD;16:NVMe SSD_SED;17:SCM SSD}，序列号 ##06）重构失败，错误码:##07。',266,'硬盘域',0);"
recon_fail_alarm_sql_en="INSERT INTO "alarmdefine_en" VALUES('##01,##06',64441942133,'Failed To Reconstruct The Disk In The Disk Domain',5,'Failed to reconstruct the disk in disk domain (ID ##01, name ##00).','Collect related information and contact technical support engineers.','Failed to reconstruct disk (##02{0:controller enclosure;1:disk enclosure;2:engine} ##03, slot ID ##04, type ##05{0:FC;1:SAS;2:SATA;3:SSD;4:NL-SAS;5:SLC SSD;6:MLC SSD;7:FC_SED;8:SAS_SED;9:SATA_SED;10:SSD_SED;11:NL-SAS_SED;12:SLC SSD_SED;13:MLC SSD_SED;14:NVMe SSD;16:NVMe SSD_SED;17:SCM SSD}, SN ##06) in disk domain (ID ##01, name ##00). Error code: ##07.',266,'Disk domain',0);"
precopy_fail_alarm_sql_cn="INSERT INTO "alarmdefine_cn" VALUES('##01,##06',64441942134,'硬盘预拷贝失败',5,'硬盘域（ID ##01，名称 ##00）预拷贝失败。','请收集相关事件信息，然后联系技术支持工程师更换故障的硬盘。','硬盘域（ID ##01，名称 ##00）中硬盘（##02{0:控制框;1:硬盘框;2:引擎} ##03，槽位号 ##04，类型 ##05{0:FC;1:SAS;2:SATA;3:SSD;4:NL-SAS;5:SLC SSD;6:MLC SSD;7:FC_SED;8:SAS_SED;9:SATA_SED;10:SSD_SED;11:NL-SAS_SED;12:SLC SSD_SED;13:MLC SSD_SED;14:NVMe SSD;16:NVMe SSD_SED;17:SCM SSD}，序列号 ##06）预拷贝失败，错误码:##07。',266,'硬盘域',0);"
precopy_fail_alarm_sql_en="INSERT INTO "alarmdefine_en" VALUES('##01,##06',64441942134,'Disk Pre-copy Failed',5,'Pre-copy of the disk in disk domain (ID ##01, name ##00) failed.','Collect related information and contact technical support engineers to replace the faulty disk.','Pre-copy of disk (##02{0:controller enclosure;1:disk enclosure;2:engine} ##03, slot ID ##04, type ##05{0:FC;1:SAS;2:SATA;3:SSD;4:NL-SAS;5:SLC SSD;6:MLC SSD;7:FC_SED;8:SAS_SED;9:SATA_SED;10:SSD_SED;11:NL-SAS_SED;12:SLC SSD_SED;13:MLC SSD_SED;14:NVMe SSD;16:NVMe SSD_SED;17:SCM SSD}, SN ##06) in disk domain (ID ##01, name ##00) failed. Error code: ##07.',266,'Disk domain',0);"

recon_fail_alarm_info_cn=''$fstPad'<param eventID="64441942133"\
'$otrPad'name="硬盘域重构失败"\
'$otrPad'detail="硬盘域（ID ##01，名称 ##00）中硬盘（##02{0:控制框;1:硬盘框;2:引擎} ##03，槽位号 ##04，类型 ##05{0:FC;1:SAS;2:SATA;3:SSD;4:NL-SAS;5:SLC SSD;6:MLC SSD;7:FC_SED;8:SAS_SED;9:SATA_SED;10:SSD_SED;11:NL-SAS_SED;12:SLC SSD_SED;13:MLC SSD_SED;14:NVMe SSD;16:NVMe SSD_SED;17:SCM SSD}，序列号 ##06）重构失败，错误码:##07。"\
'$otrPad'description="硬盘域（ID ##01，名称 ##00）重构失败。"\
'$otrPad'suggestion="请收集相关事件信息，然后联系技术支持工程师进行处理。"\
'$otrPad'paramDesc="N/A"\
'$otrPad'objID="266"\
'$otrPad'objName="硬盘域"\
'$otrPad'location="pool-name=##00,pool-id=##01,FrameType=##02,FrameID=##03,SlotID=##04,real-disk-type=##05,sn=##06,error-code=##07"\
'$otrPad'alarmOid="15.266.118" />'

recon_fail_alarm_info_en=''$fstPad'<param eventID="64441942133"\
'$otrPad'name="Failed To Reconstruct The Disk In The Disk Domain"\
'$otrPad'detail="Failed to reconstruct disk (##02{0:controller enclosure;1:disk enclosure;2:engine} ##03, slot ID ##04, type ##05{0:FC;1:SAS;2:SATA;3:SSD;4:NL-SAS;5:SLC SSD;6:MLC SSD;7:FC_SED;8:SAS_SED;9:SATA_SED;10:SSD_SED;11:NL-SAS_SED;12:SLC SSD_SED;13:MLC SSD_SED;14:NVMe SSD;16:NVMe SSD_SED;17:SCM SSD}, SN ##06) in disk domain (ID ##01, name ##00). Error code: ##07."\
'$otrPad'description="Failed to reconstruct the disk in disk domain (ID ##01, name ##00)."\
'$otrPad'suggestion="Collect related information and contact technical support engineers."\
'$otrPad'paramDesc="N/A"\
'$otrPad'objID="266"\
'$otrPad'objName="Disk domain"\
'$otrPad'location="pool-name=##00,pool-id=##01,FrameType=##02,FrameID=##03,SlotID=##04,real-disk-type=##05,sn=##06,error-code=##07"\
'$otrPad'alarmOid="15.266.118" />'

precopy_fail_alarm_info_cn=''$fstPad'<param eventID="64441942134"\
'$otrPad'name="硬盘预拷贝失败"\
'$otrPad'detail="硬盘域（ID ##01，名称 ##00）中硬盘（##02{0:控制框;1:硬盘框;2:引擎} ##03，槽位号 ##04，类型 ##05{0:FC;1:SAS;2:SATA;3:SSD;4:NL-SAS;5:SLC SSD;6:MLC SSD;7:FC_SED;8:SAS_SED;9:SATA_SED;10:SSD_SED;11:NL-SAS_SED;12:SLC SSD_SED;13:MLC SSD_SED;14:NVMe SSD;16:NVMe SSD_SED;17:SCM SSD}，序列号 ##06）预拷贝失败，错误码:##07。"\
'$otrPad'description="硬盘域（ID ##01，名称 ##00）预拷贝失败。"\
'$otrPad'suggestion="请收集相关事件信息，然后联系技术支持工程师更换故障的硬盘。"\
'$otrPad'paramDesc="N/A"\
'$otrPad'objID="266"\
'$otrPad'objName="硬盘域"\
'$otrPad'location="pool-name=##00,pool-id=##01,FrameType=##02,FrameID=##03,SlotID=##04,real-disk-type=##05,sn=##06,error-code=##07"\
'$otrPad'alarmOid="15.266.119" />'

precopy_fail_alarm_info_en=''$fstPad'<param eventID="64441942134"\
'$otrPad'name="Disk Pre-copy Failed"\
'$otrPad'detail="Pre-copy of disk (##02{0:controller enclosure;1:disk enclosure;2:engine} ##03, slot ID ##04, type ##05{0:FC;1:SAS;2:SATA;3:SSD;4:NL-SAS;5:SLC SSD;6:MLC SSD;7:FC_SED;8:SAS_SED;9:SATA_SED;10:SSD_SED;11:NL-SAS_SED;12:SLC SSD_SED;13:MLC SSD_SED;14:NVMe SSD;16:NVMe SSD_SED;17:SCM SSD}, SN ##06) in disk domain (ID ##01, name ##00) failed. Error code: ##07."\
'$otrPad'description="Pre-copy of the disk in disk domain (ID ##01, name ##00) failed."\
'$otrPad'suggestion="Collect related information and contact technical support engineers to replace the faulty disk."\
'$otrPad'paramDesc="N/A"\
'$otrPad'objID="266"\
'$otrPad'objName="Disk domain"\
'$otrPad'location="pool-name=##00,pool-id=##01,FrameType=##02,FrameID=##03,SlotID=##04,real-disk-type=##05,sn=##06,error-code=##07"\
'$otrPad'alarmOid="15.266.119" />'
}

# $1 - alarmdefine_cn or alarmdefine_en
# $2 - alarm_id 
# $3 - alarm_sql 
function addAlarmToDefine()
{
    cmd="sqlite3 /OSM/conf/alarm_config.dat \"SELECT AlmID FROM $1 WHERE AlmID=$2\"" 
    #log $cmd
    tmpResult=`eval ${cmd}`
    if [ ! -n "$tmpResult" ]; then
        sqlite3 /OSM/conf/alarm_config.dat ".timeout 10000" "$3"
        log "Insert alarm id $2 to table $1."
    else
        log "Alarm id $2 already exists in table $1."
    fi 

    return
}

# $1 - event_xve_zh.xml or event_xve_en.xml
# $2 - alarm_id 
# $3 - alarm_info
function addAlarmToEventXml()
{
    cmd="cat /OSM/conf/$1|grep 'eventID=\"$2\"'"
    #log $cmd
    tmpResult=`eval ${cmd}`
    if [ -n "$tmpResult" ]; then
        log "Alarm id $2 already exists in file $1."
    else  
        sed -i "/<\/eventDefinition>/i\\$3" /OSM/conf/$1
        log "Alarm id $2 inserted into file $1."
    fi 
    
    return
}

# $1 - alarm_id 
# $2 - alarm_info
function addAlarmToSnmpXml()
{
    cmd="cat /ISM/agent/snmp/conf/snmpagent/alarm_config.xml|grep 'eventID=\"$1\"'"
    #log $cmd
    tmpResult=`eval ${cmd}`
    if [ -n "$tmpResult" ]; then
        log "Alarm id $1 already exists in file alarm_config.xml."
    else
        sed -i "/<\/eventDefinition>/i\\$2" /ISM/agent/snmp/conf/snmpagent/alarm_config.xml
        log "Alarm id $1 inserted into file alarm_config.xml."
    fi 
}

function PATCH_START()
{
    init
    log "Patch Start: Add alarm id($recon_fail_alarm_id, $precopy_fail_alarm_id) to alarm define."
    addAlarmToDefine "alarmdefine_cn" $recon_fail_alarm_id "$recon_fail_alarm_sql_cn"
    addAlarmToDefine "alarmdefine_en" $recon_fail_alarm_id "$recon_fail_alarm_sql_en"
    addAlarmToDefine "alarmdefine_cn" $precopy_fail_alarm_id "$precopy_fail_alarm_sql_cn"
    addAlarmToDefine "alarmdefine_en" $precopy_fail_alarm_id "$precopy_fail_alarm_sql_en"
    
    addAlarmToEventXml "event_xve_zh.xml" $recon_fail_alarm_id "$recon_fail_alarm_info_cn"
    addAlarmToEventXml "event_xve_en.xml" $recon_fail_alarm_id "$recon_fail_alarm_info_en"
    addAlarmToEventXml "event_xve_zh.xml" $precopy_fail_alarm_id "$precopy_fail_alarm_info_cn"
    addAlarmToEventXml "event_xve_en.xml" $precopy_fail_alarm_id "$precopy_fail_alarm_info_en"
    
    addAlarmToSnmpXml $recon_fail_alarm_id "$recon_fail_alarm_info_en"
    addAlarmToSnmpXml $precopy_fail_alarm_id "$precopy_fail_alarm_info_en"
    
    log "Patch end: Resart alarm process."
    restartProcess
    return 
}
function PATCH_END()
{
    return
}
function PATCH_ROLLSTART()
{
    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

