#!/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 -9  
    if [ $? -ne 0 ]; then
        OS_echo "RestartISM again."
        ps -ef |grep -E "ibase|httpd"|grep -v grep |awk '{print $2}'|xargs kill -9
    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="65498382392"')
    if [ -n "$tmpResult" ]; then
    OS_echo "Alarm id 65498382392 has in file event_xve_zh.xml."
    else
    tmpStr=''$fstPad'<param eventID="65498382392"\
'$otrPad'name="SMB2/3协议的打开文件数量达到最大规格"\
'$otrPad'detail="控制器（##00{0:控制框;1:硬盘框;2:引擎} ##01，ID ##02{0:A;1:B;2:C;3:D}）上SMB2/3协议的打开文件数量达到最大规格的##03，新的文件可能无法打开。"\
'$otrPad'description="控制器（##00{0:控制框;1:硬盘框;2:引擎} ##01，ID ##02{0:A;1:B;2:C;3:D}）上SMB2/3协议的打开文件数量达到最大规格##03。"\
'$otrPad'suggestion="步骤1  把客户端访问的共享挂载到其它控制器。操作完成后，如果告警未清除=>[步骤2]。&#10;步骤2  请收集相关事件信息，然后联系技术支持工程师进行处理。"\
'$otrPad'paramDesc="N/A"\
'$otrPad'objID="16386"\
'$otrPad'objName="NAS协议"\
'$otrPad'location="FrameType=##00,FrameID=##01,controller-id=##02,scale=##03"\
'$otrPad'alarmOid="15.16386.83" />'
	sed -i "/<\/eventDefinition>/i\\$tmpStr" /OSM/conf/event_xve_zh.xml
    OS_echo "Alarm id 65498382392 inserted into file event_xve_zh.xml."
    fi

    tmpResult=$(cat /OSM/conf/event_xve_en.xml |grep 'eventID="65498382392"')
    if [ -n "$tmpResult" ]; then
    OS_echo "Alarm id 65498382392 has in file event_xve_en.xml."
    else
	tmpStr=''$fstPad'<param eventID="65498382392"\
'$otrPad'name="The Number Of Opened SMB 2 And SMB 3 Files Reaches The Upper Limit"\
'$otrPad'detail="The number of files opened through SMB 2.0/3.0 on controller (##00){0:controller enclosure;1:disk enclosure;2:engine} ##01, ID ##02{0:A;1:B;2:C;3:D}) reaches ##03 of the upper limit. New files may fail to be opened."\
'$otrPad'description="The number of files opened through SMB 2.0/3.0 on controller (##00){0:controller enclosure;1:disk enclosure;2:engine} ##01, ID ##02{0:A;1:B;2:C;3:D}) reaches ##03 of the upper limit."\
'$otrPad'suggestion="Step1 Mount the accessed share to another controller. If the alarm persists=>[Step2].&#10;Step2 Collect related information and contact technical support engineers."\
'$otrPad'paramDesc="N/A"\
'$otrPad'objID="16386"\
'$otrPad'objName="NAS protocol"\
'$otrPad'location="FrameType=##00,FrameID=##01,controller-id=##02,scale=##03"\
'$otrPad'alarmOid="15.16386.83" />'
	sed -i "/<\/eventDefinition>/i\\$tmpStr" /OSM/conf/event_xve_en.xml
    OS_echo "Alarm id 65498382392 inserted into file event_xve_en.xml."
    fi
    

    sqlite3 /OSM/conf/alarm_config.dat ".timeout 10000" "DELETE FROM alarmdefine_en where AlmID=65498382392"
    sqlite3 /OSM/conf/alarm_config.dat ".timeout 10000" "DELETE FROM alarmdefine_cn where AlmID=65498382392"
    
    sqlite3 /OSM/conf/alarm_config.dat ".timeout 10000" "INSERT INTO alarmdefine_cn VALUES('##00,##01,##02',65498382392,'SMB2/3协议的打开文件数量达到最大规格',5,'控制器（##00{0:控制框;1:硬盘框;2:引擎} ##01，ID ##02{0:A;1:B;2:C;3:D}）上SMB2/3协议的打开文件数量达到最大规格##03。','步骤1  把客户端访问的共享挂载到其它控制器。操作完成后，如果告警未清除=>[步骤2]。步骤2  请收集相关事件信息，然后联系技术支持工程师进行处理。','控制器（##00{0:控制框;1:硬盘框;2:引擎} ##01，ID ##02{0:A;1:B;2:C;3:D}）上SMB2/3协议的打开文件数量达到最大规格的##03，新的文件可能无法打开。',16386,'NAS协议',0)"
    sqlite3 /OSM/conf/alarm_config.dat ".timeout 10000" "INSERT INTO alarmdefine_en VALUES('##00,##01,##02',65498382392,'The Number Of Opened SMB 2 And SMB 3 Files Reaches The Upper Limit',5,'The number of files opened through SMB 2.0/3.0 on controller (##00){0:controller enclosure;1:disk enclosure;2:engine} ##01, ID ##02{0:A;1:B;2:C;3:D}) reaches ##03 of the upper limit.','Step1 Mount the accessed share to another controller. If the alarm persists=>[Step2].
    Step2 Collect related information and contact technical support engineers.','The number of files opened through SMB 2.0/3.0 on controller (##00){0:controller enclosure;1:disk enclosure;2:engine} ##01, ID ##02{0:A;1:B;2:C;3:D}) reaches ##03 of the upper limit. New files may fail to be opened.',16386,'NAS protocol',0)"
    startProcess
    return
}

function PATCH_END()
{
    return
}

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

    sqlite3 /OSM/conf/alarm_config.dat ".timeout 10000" "DELETE FROM alarmdefine_en where AlmID=65498382392"
    sqlite3 /OSM/conf/alarm_config.dat ".timeout 10000" "DELETE FROM alarmdefine_cn where AlmID=65498382392"
    
    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

