#!/bin/bash
#包含头脚本
source /OSM/script/os_load_base.sh

UPGRADE_SH=upgrade_preck.sh
G_CUR_SYS_VERSION_FILE=/OSM/conf/versions.conf
G_AUC_SYS_VERSION_DIR=/startup_disk/image/boot_osp
G_PKG_VERSION_FILE=/OSM/update/versions.conf
G_PCIE_PKG_VERSION_FILE=/startup_disk/conf/conf_local/pcie_version.conf
G_PCIE_CURRENT_VERSION_FILE=/OSM/update/pcie_version_cur
G_UPDATE_TMP_DIR=/OSM/update
G_BAY_CONFIG_FILE=/OSM/conf/bay_config.ini
G_BAY_CONFIG_FILE_IN_DISK=/startup_disk/conf/conf_local/bay_config.ini
G_DISK_UPD=/startup_disk/image/upd_tmp
G_UPDATE_STATE_DIR=/startup_disk/conf/conf_local
G_UPDATE_STATE_FILE=state_upd.lua
G_SYNC_REBOOT_FILE=/OSM/update/syncreboot
G_PRODUCT_INI=/OSM/conf/product.ini
G_DISK_UTIL=/tmp/diskusagetmp
G_DISK_ID=/tmp/diskidtmp
G_DISK_INFO=/tmp/diskinformation
G_ULTRA_CHECK=/tmp/ultraCheck
G_CPU_USAGE=/tmp/cpuusagetmp
G_PRODUCT_TYPE=$(grep "Product Name is" /proc/osp/bsp | cut -d' ' -f4 | tr 'a-z' 'A-Z')
#热补丁相关
G_AUC_SYS_DIR=/startup_disk/image/
G_UPG_HP_TMP_PATCH_CONF="/tmp/hotpatch_version.conf"
#热补丁相关

declare -a G_DISK_ID_LIST

GetCPUUsage_QOS()
{
diagnose --auto << EOF
overload_ctrl show cur_cpu_load
quit
EOF
}

OS_ExecDiagnose()
{
    local L_CMD=$1
    local L_FILE=$2

    if [ "$L_FILE" == "" ]; then
diagnose --auto <<EOF
$L_CMD
quit
EOF
    else
diagnose --auto <<EOF > "$L_FILE"
$L_CMD
quit
EOF
    fi
    return 0
}
# **************************************************************************** #
# Function Name:
# Description:
# Parameter:
# Return:
# **************************************************************************** #
OS_CHECK_FREE_MEM_ENOUGH()
{   
     #如果dha在运行，需要先停止dha, 此脚本是BDM维护，此处只负责调用，不关心结果；
    if [ -f /OSM/script/bdm_stop_dha_aimodel.sh ]
    then
        chmod +x /OSM/script/bdm_stop_dha_aimodel.sh
        /OSM/script/bdm_stop_dha_aimodel.sh
    fi

    sleep 10

    #cpu检查前降低auto_tiering速度，协议在V5R7C20增加，不关心执行结果,sleep 10s后进行cpu检查
    nas_proto_pum -m auto_tiering -c set_need_control_speed -v 1 &>/dev/null
    sleep 10
    #check memory is enough
    echo 3 >/proc/sys/vm/drop_caches

    FREE_MEM=`free -m|grep ^Mem|awk '{print $4}'`
    OS_echo "begin to check the cpu using when upgrade check."
    #调用QOS的diagnose命令检查CPU利用率
    [ -f $G_CPU_USAGE ] && rm $G_CPU_USAGE
    local var=0
    for((i=0;i<10;i++))
    do
        GetCPUUsage_QOS > $G_CPU_USAGE
        tmpvar=`cat $G_CPU_USAGE |grep "CPU load average" |awk '{print $4}'`
        var=$(expr $var + $tmpvar) 
        sleep 1
    done
    L_CUR_CPU_USING=$(expr $var / 10 )
    #cpu检查后恢复auto_tiering速度，协议在V5R7C20增加，不关心执行结果
    nas_proto_pum -m auto_tiering -c set_need_control_speed -v 0 &>/dev/null
    echo "upd sysresourcecheck"
    echo "$L_CUR_CPU_USING,$FREE_MEM"
    return 0
}

# **************************************************************************** #
# Function Name: OS_CheckDiskUsage
# Description: check if the disk usage is more than threshold
# Parameter:
# Return:
# **************************************************************************** #
GetDiskUsage()
{
diagnose --auto << EOF > $G_DISK_UTIL
sddebug userate 60
quit
EOF
}

OS_CheckDiskUsage()
{
    local -i L_OneDiskUsage=0
    local -i L_DiskUtilUsageResult=0
    local -i L_DiskCount=0
    local -i L_DiskAverage=0
    local -i L_DiskSum=0
    local -i L_DiskUsageIgnore=10
    
    #检查前降低auto_tiering速度，协议在V5R7C20增加，不关心执行结果,sleep 60s后进行cpu检查（注：检查磁盘利用率是获取前60秒的数据，所有睡眠60秒）
    nas_proto_pum -m auto_tiering -c set_need_control_speed -v 1 &>/dev/null
    OS_echo " this func to check disk usage"
    sleep 60
    [ -f $G_DISK_UTIL ] && rm $G_DISK_UTIL
    GetDiskUsage
    while read sdid util type; 
    do
        if [ -n "$(echo $sdid | grep [a-zA-Z])" -o -z "$sdid" ]; then
            OS_echo "${UPGRADE_SH}: sdid=$sdid"
            continue;
        fi
        L_OneDiskUsage=$util
        OS_echo "${UPGRADE_SH}: sdid=$sdid  util=${L_OneDiskUsage}"
        #将利用率小于等于10的盘排除开，不计算平均值和最大值，可以避免长期利用率为零或者很低的盘对平均值计算的影响
        if [ ${L_DiskUsageIgnore} -gt ${L_OneDiskUsage} ]; then
            OS_echo "${UPGRADE_SH}: sdid=$sdid diskusage less than 10, continue"
            continue;
        fi
        [ ${L_OneDiskUsage} -gt ${L_DiskUtilUsageResult} ] && L_DiskUtilUsageResult=${L_OneDiskUsage}
        L_DiskCount=$(( $L_DiskCount + 1 ))
        L_DiskSum=$(($L_DiskSum + $L_OneDiskUsage ))
    done < $G_DISK_UTIL &>/dev/null
    
    #获取到的磁盘数不为0的时候，计算磁盘平均利用率
    if [ 0 -ne $L_DiskCount ]; then
        L_DiskAverage=$(( $L_DiskSum / $L_DiskCount ))
    fi
    #检查后恢复auto_tiering速度，协议在V5R7C20增加，不关心执行结果
    nas_proto_pum -m auto_tiering -c set_need_control_speed -v 0 &>/dev/null
    echo "upd diskusagecheck"
    echo "${L_DiskUtilUsageResult},${L_DiskAverage}"

    return 0
}

# **************************************************************************** #
# Function Name: OS_CheckSSDAndSASDiskUsage
# Description: check SSD and SAS disk usage
# Parameter:
# Return:
# **************************************************************************** #
OS_CheckSSDAndSASDiskUsage()
{
    local -i L_OneDiskUsage=0
    local -i L_SSDDiskUtilUsageResult=0
    local -i L_SSDDiskCount=0
    local -i L_SSDDiskAverage=0
    local -i L_SSDDiskSum=0
    
    local -i L_SASDiskUtilUsageResult=0
    local -i L_SASDiskCount=0
    local -i L_SASDiskAverage=0
    local -i L_SASDiskSum=0
    L_DiskType=""
    
    [ -f $G_DISK_UTIL ] && rm $G_DISK_UTIL
    GetDiskUsage
    while read sdid util type; 
    do
        if [ -n "$(echo $sdid | grep [a-zA-Z])" -o -z "$sdid" ]; then
            OS_echo "${UPGRADE_SH}: sdid=$sdid"
            continue;
        fi
        L_OneDiskUsage=$util
        L_DiskType=$type
        OS_echo "${UPGRADE_SH}: sdid=$sdid  util=${L_OneDiskUsage} type=${L_DiskType}"
        if [ "${L_DiskType}" == "SSD" ] || [ "${L_DiskType}" == "SLC_SSD" ] || [ "${L_DiskType}" == "MLC_SSD" ]; then
            [ ${L_OneDiskUsage} -gt ${L_SSDDiskUtilUsageResult} ] && L_SSDDiskUtilUsageResult=${L_OneDiskUsage}
            L_SSDDiskCount=$(( $L_SSDDiskCount + 1 ))
            L_SSDDiskSum=$(($L_SSDDiskSum + $L_OneDiskUsage ))
        else
            [ ${L_OneDiskUsage} -gt ${L_SASDiskUtilUsageResult} ] && L_SASDiskUtilUsageResult=${L_OneDiskUsage}
            L_SASDiskCount=$(( $L_SASDiskCount + 1 ))
            L_SASDiskSum=$(($L_SASDiskSum + $L_OneDiskUsage ))

        fi
    done < $G_DISK_UTIL &>/dev/null
    
    OS_echo "${UPGRADE_SH}: L_SSDDiskCount=$L_SSDDiskCount  L_SASDiskCount=$L_SASDiskCount"
    #计算磁盘平均利用率
    if [ "0" != "$L_SSDDiskCount" ];then
        L_SSDDiskAverage=$(( $L_SSDDiskSum / $L_SSDDiskCount ))
    fi
    if [ "0" != "$L_SASDiskCount" ];then
        L_SASDiskAverage=$(( $L_SASDiskSum / $L_SASDiskCount ))
    fi
    
    echo "upd diskupgradeusagecheck"
    echo "${L_SSDDiskUtilUsageResult},${L_SSDDiskAverage};${L_SASDiskUtilUsageResult},${L_SASDiskAverage}"

    return 0
}

# **************************************************************************** #
# Function Name: OM_ExecuteMMLCommand
# Description: 执行diagnose命令
# Parameter:
# Return:
# **************************************************************************** #
OM_ExecuteDiagnoseCommand()
{ 
    OS_echo "exec diagnose "
(
diagnose --auto <<EOF
$1
quit
EOF
) 
return 0
}

OM_SetMemRecoverFlagInvalid()
{ 
    OS_echo "exec diagnose "
(
diagnose --auto <<EOF
cmm show memrecoverflag
cmm set memrecoverflaginvalid
quit
EOF
)  >>/OSM/update/diagnoseinfo

    local L_SET_FLAG=`cat /OSM/update/diagnoseinfo | grep success`
    if [ -z "$L_SET_FLAG" ]; then
        OS_echo "SetMemRecoverFlagInvalid fail."
        echo "expect_failed"
        return 1
    fi
    OS_echo "SetMemRecoverFlagInvalid success."
    echo "expect_success"
    return 0

}



# **************************************************************************** #
# Function Name:
# Description:
# Parameter:
# Return:
# **************************************************************************** #
OS_SET_DISK_RW()
{
    L_DISK_BOOT=`cat /proc/mounts |grep $G_DISK_MOUNT_IMAGE |awk '{print $1}'`
    if [ -z "$L_DISK_BOOT" ] ; then
        L_DISK_BOOT=$G_DISK_BOOT
    else 
        L_DISK_BOOT=${L_DISK_BOOT%[0-9]}
    fi

    OS_echo "${UPGRADE_SH}: Disk Boot Dir = [$L_DISK_BOOT], Set Disk Boot Dir RW Begin" >/dev/null
    
    OS_SetStartUpDiskProperty $L_DISK_BOOT rw
    if [ 0 != $? ] ; then
        OS_echo "${UPGRADE_SH}: Set Disk Boot Dir RW Error" >/dev/null
        return 1
    fi
    
    OS_echo "${UPGRADE_SH}: Set Disk Boot Dir RW Success" >/dev/null
    return 0
}

# **************************************************************************** #
# Function Name:
# Description:
# Parameter:
# Return:
# **************************************************************************** #
OS_SET_DISK_RO()
{
    L_DISK_BOOT=`cat /proc/mounts |grep $G_DISK_MOUNT_IMAGE |awk '{print $1}'|awk -F 1 '{print $1}'`
    if [ -z "$L_DISK_BOOT" ] ; then
        L_DISK_BOOT=$G_DISK_BOOT
    else 
        L_DISK_BOOT=${L_DISK_BOOT%[0-9]}
    fi

    OS_echo "${UPGRADE_SH}: Disk Boot Dir = [$L_DISK_BOOT], Set Disk Boot Dir RO Begin" >/dev/null
    
    for((i=0;i<3;i++))
    do
        OS_SetStartUpDiskProperty $L_DISK_BOOT ro
        if [ 0 = $? ] ; then
        break       
        fi
        OS_echo "${UPGRADE_SH}:Remount image RO failed, i=$i"
        sleep 5
    done
    
    if [ $i -ge 3 ] ; then
        OS_echo "${UPGRADE_SH}: Set Disk Boot Dir RO Error" >/dev/null
        return 1
    else
        OS_echo "${UPGRADE_SH}: Set Disk Boot Dir RO Success" >/dev/null
        return 0
    fi

    return 0
}

# **************************************************************************** #
# Function Name: UPGRADE_GET_SYS_VERSION
# Description: 比较版本兼容性
# Parameter:
# Return:
# **************************************************************************** #
UPGRADE_GET_SYS_VERSION()
{
    OS_echo "[$UPGRADE_SH]: Compare sys version">/dev/null

    L_VERSION_FLIE_DIR=${G_CUR_SYS_VERSION_FILE%/*}    
    OS_echo "CurDir $L_VERSION_FLIE_DIR"
    L_VERSION_FLIE_NAME=${G_CUR_SYS_VERSION_FILE##*/}
    OS_echo "CurVer $L_VERSION_FLIE_NAME"

    L_CUR_VERSION_STR=`cat $G_CUR_SYS_VERSION_FILE |grep -m 1 Version |awk -F"=" '{print $2}'`
    echo "upd issameversion" 
    echo $L_CUR_VERSION_STR  
    return 0
}

# **************************************************************************** #
# Function Name: UPGRADE_SYS_VER_COMPARE
# Description: 比较版本兼容性
# Parameter:
# Return:
# **************************************************************************** #
UPGRADE_SYS_VER_COMPARE()
{
    OS_echo "[$UPGRADE_SH]: Get sys version">/dev/null

    L_VERSION_FLIE_DIR=${G_CUR_SYS_VERSION_FILE%/*}    
    OS_echo "CurDir $L_VERSION_FLIE_DIR"
    L_VERSION_FLIE_NAME=${G_CUR_SYS_VERSION_FILE##*/}
    OS_echo "CurVer $L_VERSION_FLIE_NAME"

    OS_SET_DISK_RW
    L_AUC_VER_FILE=$G_AUC_SYS_VERSION_DIR/$L_VERSION_FLIE_NAME
    OS_echo "AucVer $L_AUC_VER_FILE"
    OS_SET_DISK_RO

    L_CUR_VERSION_STR=`cat $G_CUR_SYS_VERSION_FILE |grep -m 1 Version |awk -F"=" '{print $2}'`
    L_AUC_VERSION_STR=`cat $L_AUC_VER_FILE |grep -m 1 Version |awk -F"=" '{print $2}'`

    declare -i L_CUR_VERSION=0
    declare -i L_AUC_VERSION=0

    #将获取的版本号中的.和T去掉, 看做整数, 进行比较
    L_CUR_VERSION=`echo "$L_CUR_VERSION_STR" | sed 's/[.T]//g'`
    L_AUC_VERSION=`echo "$L_AUC_VERSION_STR" | sed 's/[.T]//g'`

 
    #版本一致
    if [ $L_CUR_VERSION -eq $L_AUC_VERSION  ];then
        OS_echo "[$UPGRADE_SH]: Mirror SYS Version[$L_CUR_VERSION] is EQUAL TO Local SYS Version[$L_AUC_VERSION]">/dev/null
        echo "upd syscompatiblecheck"
        echo "True"
        return 0
    fi

    #对板比本板高
    if [ $L_CUR_VERSION -gt $L_AUC_VERSION  ];then
        OS_echo "[$UPGRADE_SH]: Mirror SYS Version[$L_CUR_VERSION] is GREATER than Local SYS Version [$L_AUC_VERSION]">/dev/null
        echo "upd syscompatiblecheck"
        echo "False"
        return 0
    fi

    #对板比本板低
    OS_echo "[$UPGRADE_SH]: Mirror SYS Version[$L_CUR_VERSION] is LOWER than Local SYS Version [$L_AUC_VERSION]">/dev/null
    echo "upd syscompatiblecheck"
    echo "False"
    return 0
}

UPGRADE_SYS_COMPATIBLE()
{
    OS_echo "[$UPGRADE_SH]: UPGRADE_SYS_COMPATIBLE">/dev/null
    Ver_Type=$1
    L_CUR_VERSION_STR=`cat $G_CUR_SYS_VERSION_FILE |grep -m 1 Version |awk -F"=" '{print $2}'`
    OS_echo "[$UPGRADE_SH]: cur version $L_CUR_VERSION_STR">/dev/null

    declare -i L_CUR_VERSION=0
    if [ "$Ver_Type" == "COMPATIBLE" ] ; then
        declare -i ComLine=$(sed -n -e '/\[COMPATIBLE\]/=' $G_PKG_VERSION_FILE)
    fi
    if [ "$Ver_Type" == "BLACKLIST" ] ; then
        declare -i ComLine=$(sed -n -e '/\[BLACKLIST\]/=' $G_PKG_VERSION_FILE)
    fi
    ComLine=ComLine+1
    OS_echo "upgrade ComLine $ComLine">/dev/null

    #将获取的版本号中的.和T去掉, 看做整数, 进行比较
    L_CUR_VERSION=`echo "$L_CUR_VERSION_STR" | sed 's/[.T]//g'`
    
    ComVersion=`echo $(sed -n "${ComLine}p" $G_PKG_VERSION_FILE)| awk -F"=" '{print $2}'`
    
    declare -i Index=1
    ComFirst=`echo $ComVersion|awk -F";" '{print $'$Index'}'` 
    OS_echo "upgrade ComFirst $ComFirst">/dev/null 
    while true ;do
        declare -i ComVerlow=`echo $ComFirst|awk -F"-" '{print $1}'`
        OS_echo "upgrade ComVerlow $ComVerlow">/dev/null 
        declare -i ComVerhig=`echo $ComFirst|awk -F"-" '{print $2}'`
        OS_echo "upgrade ComVerhig $ComVerhig">/dev/null 
        #是否在白名单，或者在当前取出的黑名单中
        if [ $L_CUR_VERSION -ge $ComVerlow ] && [ $L_CUR_VERSION -le $ComVerhig ];then
            if [ "$Ver_Type" == "COMPATIBLE" ] ; then
                echo "upd checkblackwhitelist"
                echo "white"
                return 2
            fi
            if [ "$Ver_Type" == "BLACKLIST" ] ; then
                echo "upd checkblackwhitelist"
                echo "black"
                return 0
            fi
        fi
        Index=Index+1
        ComFirst=`echo $ComVersion|awk -F";" '{print $'$Index'}'`
        if [ -z "$ComFirst" ] ; then
            if [ "$Ver_Type" == "COMPATIBLE" ] ; then
                OS_echo "[$UPGRADE_SH]: not in white list">/dev/null
                return 1
            fi
            OS_echo "[$UPGRADE_SH]: not in black list">/dev/null
            echo "upd checkblackwhitelist"
            echo "none"
            return 0
        fi 
    done
    return 0
}

# **************************************************************************** #
# Function Name: UPGRADE_SYS_VER_BALCK
# Description: 比较版本兼容性
# Parameter:
# Return:
# **************************************************************************** #
UPGRADE_SYS_VER_BALCK()
{
    OS_echo "[$UPGRADE_SH]: Get Black White List">/dev/null
    
    if [ ! -f $G_PKG_VERSION_FILE ] ; then
        echo "upd checkblackwhitelist"
        echo "pkgerror"
        return 0
    fi
    
    VER_TYPE=COMPATIBLE
    UPGRADE_SYS_COMPATIBLE $VER_TYPE 
    #当前版本在白名单中
    if [ "$?" == "2" ] ; then
        return 0
    fi   
    VER_TYPE=BLACKLIST
    UPGRADE_SYS_COMPATIBLE $VER_TYPE 
    return 0
}

UPGRADE_GET_FW_COMPATIBILITY()
{
    OS_echo "[$UPGRADE_SH]: Get Firmware compatibility" > /dev/null

    #获取全局版本号
    local cur_sys_ver=$(cat $G_CUR_SYS_VERSION_FILE |grep -m 1 Version |awk -F"=" '{print $2}')

    #执行检查兼容性原子
    /OSM/bin/upgrade_atom -f CHECK_SYS_COMPATIBLE -t sync -v ${cur_sys_ver} -n all -w all
    if [ "$?" != "0" ]; then    
        return 1
    fi

    return 0
}

#获取当前PCIE交换机版本
OM_GetDiagnoseInfo()
{
    L_PCIE_SW_ID=$1
    OS_echo "exec diagnose "
(
diagnose --auto <<EOF
dswa getdswver $L_PCIE_SW_ID
quit
EOF
) > $G_PCIE_CURRENT_VERSION_FILE
return 0
}

#校验PCIE版本
OM_CheckPcieVer()
{
    #入参1表示当前的PCIE版本号
    L_TMP_VERSION=$1
    
    if [ -z "$L_TMP_VERSION" ];then
        OS_echo "[$UPGRADE_SH]: OM_CheckPcieVer:Get tmp version is null.">/dev/null
        return 3
    fi
    OS_echo "[$UPGRADE_SH]: OM_CheckPcieVer:tmp version is: $L_TMP_VERSION">/dev/null

    L_CUR_PCIE_VERSION=`echo $L_TMP_VERSION | awk -F '=' '{print $1}'`  
    L_SYS_VERSION=`echo $L_TMP_VERSION | awk -F '=' '{print $2}'`
    
    #版本号获取后，如果存在补丁版本号，版本号部分将包含SPH或者SPC的补丁版本号，需要去掉，只获取前10位，否则将无法匹配到versions.conf 
    L_SYS_VERSION=`echo ${L_SYS_VERSION:0:10}`

    #获取当前的PCIE版本为空
    if [ -z "$L_CUR_PCIE_VERSION" ];then
        OS_echo "[$UPGRADE_SH]: OM_CheckPcieVer:Get cur pcie version is null.">/dev/null
        return 3
    fi
    
    if [ -z "$L_SYS_VERSION" ];then
        OS_echo "[$UPGRADE_SH]: OM_CheckPcieVer:Get SYS version is null.">/dev/null
        return 3
    fi
    
    OS_echo "[$UPGRADE_SH]: OM_CheckPcieVer:cur pcie version is: $L_CUR_PCIE_VERSION">/dev/null
    OS_echo "[$UPGRADE_SH]: OM_CheckPcieVer:SYS version is: $L_SYS_VERSION">/dev/null
    
    local version_osp=$(cat /startup_disk/image/boot_osp/versions.conf |grep "Version" -m 1 |awk -F "=" '{print $2}')
    if [ -z "$version_osp" ];then
        OS_echo "[$UPGRADE_SH]: OM_CheckPcieVer:Get version_osp is null.">/dev/null
    else
        OS_echo "[$UPGRADE_SH]: OM_CheckPcieVer:osp version is: $version_osp">/dev/null
    fi
    
    local version_bak=$(cat /startup_disk/image/boot_osp_bak/versions.conf |grep "Version" -m 1 |awk -F "=" '{print $2}')
    if [ -z "$version_bak" ];then
        OS_echo "[$UPGRADE_SH]: OM_CheckPcieVer:Get version_bak is null.">/dev/null
    else
    OS_echo "[$UPGRADE_SH]: OM_CheckPcieVer:bak version is: $version_bak">/dev/null
    fi
    
    local version_tmp=$(cat /startup_disk/image/boot_osp_tmp/versions.conf |grep "Version" -m 1 |awk -F "=" '{print $2}')
    if [ -z "$version_tmp" ];then
        OS_echo "[$UPGRADE_SH]: OM_CheckPcieVer:Get version_tmp is null.">/dev/null
    else
    OS_echo "[$UPGRADE_SH]: OM_CheckPcieVer:tmp version is: $version_tmp">/dev/null
    fi  
    
    if [[ $L_SYS_VERSION == $version_osp ]];then
        targetDir=/startup_disk/image/boot_osp/versions.conf
    elif [[ $L_SYS_VERSION == $version_bak ]];then
        targetDir=/startup_disk/image/boot_osp_bak/versions.conf
    elif [[ $L_SYS_VERSION == $version_tmp ]];then
        targetDir=/startup_disk/image/boot_osp_tmp/versions.conf
    else
        OS_echo "[$UPGRADE_SH]: OM_CheckPcieVer:NOT COMPIT!!! $L_SYS_VERSION,return 3.">/dev/null       
        #没有匹配到任何文件，说明系统存在问题，直接退出
        return 3
    fi
    
    OS_echo "[$UPGRADE_SH]: OM_CheckPcieVer:target dir is: $targetDir">/dev/null
    
    #获取PCIE升级包的版本
    if [ -e "$targetDir" ];then
        L_PKG_PCIE_VERSION=`awk -F '=' '/\[PCIEDSW\]/{a=1}a==1&&$1~/Version/{print $2;exit}' $targetDir`
        if [ "$L_PKG_PCIE_VERSION" == "" ];then
            OS_echo "[$UPGRADE_SH]: OM_CheckPcieVer:PCIE version in $targetDir is blank, get from $G_PCIE_PKG_VERSION_FILE .">/dev/null
            L_PKG_PCIE_VERSION=`cat $G_PCIE_PKG_VERSION_FILE |grep -m 1 DSWVER |awk -F "=" '{print $2}'`
        fi
    else
        #获取包版本失败则返回
        OS_echo "[$UPGRADE_SH]: OM_CheckPcieVer:Get cur pcie pkg version fail.">/dev/null
        return 3
    fi
    
    OS_echo "[$UPGRADE_SH]: OM_CheckPcieVer:pcie upgrade pkg version is: $L_PKG_PCIE_VERSION">/dev/null
    if [ -z "$L_PKG_PCIE_VERSION" ];then
       OS_echo "[$UPGRADE_SH]: OM_CheckPcieVer:PCIE pkg version is NULL, return 3."
       return 3
    fi
    
    #版本相同则返回0，版本不同则返回2
    if [ "$L_CUR_PCIE_VERSION" == "$L_PKG_PCIE_VERSION" ];then
        OS_echo "[$UPGRADE_SH]: OM_CheckPcieVer:the PCIE version is same.">/dev/null
        return 0
    else
        OS_echo "[$UPGRADE_SH]: OM_CheckPcieVer:the PCIE version is not same.">/dev/null
        return 2
    fi
}

#升级包与当前版本不一致时检查当前固件的兼容性
UPD_CHECK_CUR_VER_COMPATIB()
{    
    L_CUR_VERSION_STR=`cat $G_CUR_SYS_VERSION_FILE |grep -m 1 Version |awk -F"=" '{print $2}'`
    OS_echo "[$UPGRADE_SH]: upgrade:cur version $L_CUR_VERSION_STR">/dev/null
    declare -i L_CUR_VERSION=0
    L_CUR_VERSION=`echo "$L_CUR_VERSION_STR" | sed 's/[.T]//g'`

    L_PKG_VERSION_STR=`cat $G_PKG_VERSION_FILE |grep -m 1 Version |awk -F"=" '{print $2}'`
    OS_echo "[$UPGRADE_SH]: upgrade:pkg version $L_PKG_VERSION_STR">/dev/null
    declare -i L_PKG_VERSION=0
    L_PKG_VERSION=`echo "$L_PKG_VERSION_STR" | sed 's/[.T]//g'`
    
    #版本不一致时，要检查当前运行的固件是否都兼容
    if [ $L_CUR_VERSION != $L_PKG_VERSION ];then
        UPGRADE_GET_FW_COMPATIBILITY
        if [ "$?" != "0" ]; then
            echo "upg checkfwverinfo"
            echo fail
            return 1
        fi
    fi

    echo "upg checkfwverinfo"
    echo success
    return 0
}

#升级包与当前版本一致时检查当前固件的兼容性
UPD_COMPARE_SYS_VER()
{    
    L_CUR_VERSION_STR=`cat $G_CUR_SYS_VERSION_FILE |grep -m 1 Version |awk -F"=" '{print $2}'`
    OS_echo "[$UPGRADE_SH]: upgrade:cur version $L_CUR_VERSION_STR">/dev/null
    declare -i L_CUR_VERSION=0
    L_CUR_VERSION=`echo "$L_CUR_VERSION_STR" | sed 's/[.T]//g'`

    L_PKG_VERSION_STR=`cat $G_PKG_VERSION_FILE |grep -m 1 Version |awk -F"=" '{print $2}'`
    OS_echo "[$UPGRADE_SH]: upgrade:pkg version $L_PKG_VERSION_STR">/dev/null
    declare -i L_PKG_VERSION=0
    L_PKG_VERSION=`echo "$L_PKG_VERSION_STR" | sed 's/[.T]//g'`
    
    #获取SYS版本是否相同
    if [ $L_CUR_VERSION -eq $L_PKG_VERSION ];then
        #不兼容时代表工具失败重新升级
        UPGRADE_GET_FW_COMPATIBILITY
        if [ "$?" != "0" ]; then    
            return 0
        fi
        return 1
    fi
    
    return 0
}

#升级前检查系统版本，如果相同则不允许升级
UPD_CHECK_SYS_VER()
{
    OS_echo "[$UPGRADE_SH]: Check sys version">/dev/null
    
    UPD_COMPARE_SYS_VER
    ret=$?
    
    echo "upg checkversion"
    #版本相同，返回False
    if [ $ret -eq 1 ];then
        echo "False"
    else
        echo "True"
    fi

    return 0
}

#判断是否为降级
CHECK_IS_DOWNGRADE()
{
    OS_echo "[$UPGRADE_SH]: Check whether the downgrade">/dev/null
    
    L_CUR_VERSION_STR=`cat $G_CUR_SYS_VERSION_FILE |grep -m 1 Version |awk -F"=" '{print $2}'`
    OS_echo "[$UPGRADE_SH]: upgrade:cur version is $L_CUR_VERSION_STR">/dev/null
    declare -i L_CUR_VERSION=0
    L_CUR_VERSION=`echo "$L_CUR_VERSION_STR" | sed 's/[.T]//g'`

    L_PKG_VERSION_STR=`cat $G_PKG_VERSION_FILE |grep -m 1 Version |awk -F"=" '{print $2}'`
    OS_echo "[$UPGRADE_SH]: upgrade:pkg version is $L_PKG_VERSION_STR">/dev/null
    declare -i L_PKG_VERSION=0
    L_PKG_VERSION=`echo "$L_PKG_VERSION_STR" | sed 's/[.T]//g'`

    #降级，返回1
    if [ $L_CUR_VERSION -gt $L_PKG_VERSION ];then
        return 1
    fi

    return 0
}

#在线升级前检查系统版本，如果版本相同或者是降级则不允许
UPD_CHECK_SYS_VER_WHEN_ONLINE_UPGRADE()
{
    OS_echo "[$UPGRADE_SH]: Check sys version when online upgrade">/dev/null
    
    UPD_COMPARE_SYS_VER
    ret=$?
    
    echo "upg onlinecheckversion"
    #版本相同，返回Same
    if [ $ret -eq 1 ];then
        echo "Same"
    else
        CHECK_IS_DOWNGRADE
        ret=$?
        #升级场景，返回Success，降级场景，返回Lower
        if [ $ret -eq 0 ];then
            echo "Success"
        else
            echo "Lower"
        fi
    fi

    return 0
}

# **************************************************************************** #
# Function Name: CHECK_BEFORE_INSTALL_HOTPATCH
# Description: 热补丁升级前检查
# Parameter:
# Return:
# **************************************************************************** #
CHECK_BEFORE_INSTALL_HOTPATCH()
{
    OS_echo "[$UPGRADE_SH]: upgrade- CHECK_BEFORE_INSTALL_HOTPATCH">/dev/null
    
    local L_UPG_HOTPATCH_PATH_NAME=$1
    /OSM/script/os_upgrade_hotpatch.sh -g $L_UPG_HOTPATCH_PATH_NAME &>/dev/null
    L_IsSpecPatch=$? 
    
    #判断是否是特殊补丁
    if [ 10 -eq ${L_IsSpecPatch} ];then
        #特殊补丁不需要升级前的检查
        echo "upg hotpachcheck"
        echo "true"
        return 0
    fi
    
    #热补丁升级前的检查
    /OSM/script/os_upgrade_hotpatch.sh -w install $L_UPG_HOTPATCH_PATH_NAME &>/dev/null
    L_RET=$? 
    
    if [ 0 -eq ${L_RET} ];then
        OS_echo "[$UPGRADE_SH]: upgrade- CHECK_BEFORE_INSTALL_HOTPATCH = 0">/dev/null
        echo "upg hotpachcheck"
        echo "true"
        return 0
    fi
    
    if [ 1 -eq ${L_RET} ];then
        OS_echo "[$UPGRADE_SH]: upgrade- CHECK_BEFORE_INSTALL_HOTPATCH = 1">/dev/null
        echo "upg hotpachcheck"
        echo "pkgerror"
        return 0
    fi
    
    if [ 2 -eq ${L_RET} ];then
        OS_echo "[$UPGRADE_SH]: upgrade- CHECK_BEFORE_INSTALL_HOTPATCH = 2">/dev/null
        echo "upg hotpachcheck"
        echo "versionerror"
        return 0
    fi
    
    if [ 3 -eq ${L_RET} ];then
        OS_echo "[$UPGRADE_SH]: upgrade- CHECK_BEFORE_INSTALL_HOTPATCH = 3">/dev/null
        echo "upg hotpachcheck"
        echo "reinstall"
        return 0
    fi
    
    if [ 4 -eq ${L_RET} ];then
        OS_echo "[$UPGRADE_SH]: upgrade- CHECK_BEFORE_INSTALL_HOTPATCH = 4">/dev/null
        echo "upg hotpachcheck"
        echo "memoryerror"
        return 0
    fi
    
    if [ 5 -eq ${L_RET} ];then
        OS_echo "[$UPGRADE_SH]: upgrade- CHECK_BEFORE_INSTALL_HOTPATCH = 5">/dev/null
        echo "upg hotpachcheck"
        echo "sysdiskerror"
    fi
    
    if [ 6 -eq ${L_RET} ];then
        OS_echo "[$UPGRADE_SH]: upgrade- CHECK_BEFORE_INSTALL_HOTPATCH = 6">/dev/null
        echo "upg hotpachcheck"
        echo "sysbusy"
        return 0
    fi
    
    if [ 8 -eq ${L_RET} ];then
        OS_echo "[$UPGRADE_SH]: upgrade- CHECK_BEFORE_INSTALL_HOTPATCH = 8">/dev/null
        echo "upg hotpachcheck"
        echo "hotpatcherror"
        return 0
    fi
    
    if [ 11 -eq ${L_RET} ];then
        OS_echo "[$UPGRADE_SH]: upgrade- CHECK_BEFORE_INSTALL_HOTPATCH = 11">/dev/null
        echo "upg hotpachcheck"
        echo "relationshiperror"
        return 0
    fi
       
    L_CUR_VERSION_STR=`cat $G_CUR_SYS_VERSION_FILE |grep -m 1 Version |awk -F"=" '{print $2}'`
    echo "upg hotpachcheck" 
    echo "false"  
    return 0
   
}

OS_UPG_HP_IS_NEED_REBOOT()
{   
    local L_BOOT_LINK
    L_BOOT_LINK=`ls -l $G_AUC_SYS_DIR | grep -a "boot ->" |awk '{print $11}'`
    if [ "$L_BOOT_LINK" == "boot_osp_bak" ] ; then
        if [ -f $G_SYNC_REBOOT_FILE ];then
            OS_echo "sync upgrade is need reboot">/dev/null
            rm -rf $G_SYNC_REBOOT_FILE
            return 0
        fi
        OS_echo "sync upgrade is no need reboot">/dev/null
    fi
    OS_echo "sync upgrade boot->boot_osp and no need reboot."
    return 1
}

CHECK_SSD()
{
    local L_DISK_FLAG=/startup_disk/image/boot/initrd
    local L_STATE_UPD_FILE=$G_UPDATE_STATE_DIR/$G_UPDATE_STATE_FILE
    local L_STATE_VERSION_FILE=$G_UPDATE_STATE_DIR/state_version.lua

    #state_upd.lua这个文件如果在这一步还存在，说明是之前残留下来的，应该删除
    if [ -f $L_STATE_UPD_FILE ] && [ ! -f $L_STATE_VERSION_FILE ]; then
        OS_echo "${UPGRADE_SH} state_upd.lua is exist, remove it.">/dev/null
        rm -f $L_STATE_UPD_FILE >/dev/null
    fi

    if [ -e "$L_DISK_FLAG" ];then
        OS_echo "${UPGRADE_SH} The $L_DISK_FLAG is exist">/dev/null
        #设置启动盘为可写
        OS_SET_DISK_RW
        #设置port链接
        if [ ! -e "$G_DISK_MOUNT_IMAGE/boot/update_pkg/${G_PRODUCT_TYPE}/port" ]; then
            OS_echo "[$UPGRADE_SH]: port symbolic link does not exist, need create."
            cd "$G_DISK_MOUNT_IMAGE/boot/update_pkg/${G_PRODUCT_TYPE}"
            ln -s ../port port
            if [ 0 != $? ]; then
                OS_echo "[$UPGRADE_SH]: Failed to create port symbolic link."
            fi
        fi
        #设置diskfirmware链接
        if [ ! -e "$G_DISK_MOUNT_IMAGE/boot/update_pkg/${G_PRODUCT_TYPE}/diskfirmware" ]; then
            OS_echo "[$UPGRADE_SH]: diskfirmware symbolic link does not exist, need create."
            cd "$G_DISK_MOUNT_IMAGE/boot/update_pkg/${G_PRODUCT_TYPE}"
            ln -s ../diskfirmware diskfirmware
            if [ 0 != $? ]; then
                OS_echo "[$UPGRADE_SH]: Failed to create diskfirmware symbolic link."
            fi
        fi
        return 0
    fi
    
    OS_echo "${UPGRADE_SH} The $L_DISK_FLAG is not exist">/dev/null
    return 1
}

UPGRADE_POST_CHECK_PCIE_VERSION()
{
    L_PCIE_NUM=2
    L_RetryTimes=3
    L_PCIE_VERSION=""
    L_PKG_VERSION=$(head -2 ${G_CUR_SYS_VERSION_FILE} | grep -oE "[0-9]+")
    L_CtrlNum=$(grep CtrlTotal ${G_BAY_CONFIG_FILE_IN_DISK} | awk -F '=' '{print $2}')

    # 检查是否存在PCIE交换机 CheckIsExistPcie
    /OSM/script/upgrade_pcie.sh -c
    if [ "$?" != "0" ]; then
        OS_echo "[$UPGRADE_SH]:It is V3 controller, no pcie switch.">/dev/null
        echo "upg postcheckversion"
        echo "True"
        return 0;
    fi

    # TV2R2双控可能没有bay_config文件 升级后版本检查也认为成功
    if [ -z "${L_CtrlNum}" ]; then
        OS_echo "${UPGRADE_SH} There is no bay_config file." > /dev/null
        echo "upg postcheckversion"
        echo "True"

        return 0;
    fi

    for (( i=0; i<L_PCIE_NUM; i++ ));
    do
        /OSM/bin/upgrade_atom -f NOTIFY_DEV_CHK_PCIE_SW_VERSION -n $i
        [ $? == 0 ] && continue

        OS_echo "${UPGRADE_SH} PCIE ${i} version is not the same." > /dev/null
        echo "upg postcheckversion"
        echo "False"

        return 1;
    done

    OS_echo "${UPGRADE_SH} All of the PCIE version is the same." > /dev/null
    echo "upg postcheckversion"
    echo "True"

    return 0
}

OS_DEL_UPGRADE_STATE_FILES()
{   
    OS_echo "UPGRADE_SH: delete upgrade state files" >/dev/null
    #delete upgrade state file
    if [ -d $G_UPDATE_STATE_DIR ];then
        cd $G_UPDATE_STATE_DIR
        rm  $G_UPDATE_STATE_FILE &>/dev/kmsg
        cd - &>/dev/null
    fi
    echo "expect_success"
    return 0
}

OS_CHECK_STATE_UPD()
{   
    OS_echo "UPGRADE_SH: Check upgrade state files" >/dev/null
    if [ -f $G_UPDATE_STATE_DIR/$G_UPDATE_STATE_FILE ]; then
        OS_echo "UPGRADE_SH: Upgrade state files exists" >/dev/null
        echo "expect_success" 
        return 0
    fi
    OS_echo "UPGRADE_SH: Upgrade state files not exists" >/dev/null
    echo "expect_failed" 
    return 1
}

#检查告警是否存在，存在则返回1，不存在则返回0
UPGRADE_CHECK_ALARM_STATE()
{
    OS_echo "${UPGRADE_SH}: EXEC diagnose" >/dev/null
(
diagnose --auto <<EOF
alarm querybyid $1 1 $2
quit
EOF
)>/OSM/update/upgradealarminfo

    #检查告警是否存在，存在则返回1，不存在则返回0
    local L_ALARM_NUMBER=$(grep "Alarm Record Number is" /OSM/update/upgradealarminfo | grep -oE "[0-9]+")

    if [ -z "$L_ALARM_NUMBER" ];then
        OS_echo "${UPGRADE_SH}: Alarm $1 not exist !" >/dev/null
        return 0
    fi

    if [ "$L_ALARM_NUMBER" != "0" ];then
        OS_echo "${UPGRADE_SH}: Alarm $1 exist !" >/dev/null
        return 1
    fi

    OS_echo "${UPGRADE_SH}: Alarm $1 not exist !" >/dev/null
    
    return 0
}

CheckThirdUltraPath()
{
diagnose --auto << EOF > $G_ULTRA_CHECK
tgt show ultrapath
quit
EOF
}

#检查多路径告警
UPGRADE_CHECK_ULTRA_PATH()
{
    #引擎间ojb恢复串行，所有引擎obj恢复以后TGT才会通知驱动建链，调整超时时间为：4(引擎)*6min=24min
    local L_RetryTimes=24
    local L_SleepTime=0
    local L_WaitTime=0
    local L_OldAlarmRet=0
    local L_NewAlarmRet=0

    #获取控制器数量
    num=`cat /OSM/conf/bay_config.ini | grep "CtrlTotal=" | wc -l`
    if [ "$num" == "1" ]; then  
        L_PEER_NUM=`cat /OSM/conf/bay_config.ini | grep "CtrlTotal=" | awk -F "=" '{print $2}'`
        cat /proc/osp/bsp |grep "Frame Model" |grep "6U" >/dev/null
        #18000&6800环境
        if [ "$?" == "0" ]; then
            if [ "$L_PEER_NUM" -gt "4" ]; then
                val=`expr $L_PEER_NUM / 4`
                L_RetryTimes=`expr $val \* $L_RetryTimes`
            fi
        else
            if [ "$L_PEER_NUM" -gt "2" ]; then
                val=`expr $L_PEER_NUM / 2`
                L_RetryTimes=`expr $val \* $L_RetryTimes`
            fi
        fi
    fi

    ##等待多路径告警恢复，最长等6分钟
    while [ true ]
    do
        if [ ${L_RetryTimes} -eq 0 ];then
            OS_echo "${UPGRADE_SH}: Sleep 6min and ultra path alarm exist." >/dev/null
            return 1;
        fi
        
        #发送调试命令查询多路径故障告警，存在则返回1，不存在则返回0
        #在V500R007C61之前使用老ID
        UPGRADE_CHECK_ALARM_STATE "64425885721" "1"
        let L_OldAlarmRet=$?       
        #在V500R007C61以及之后的版本使用新ID
        UPGRADE_CHECK_ALARM_STATE "64425885748" "1"
        let L_NewAlarmRet=$?

        #如果有告警则等6分钟等待链路恢复
        if [ 1 == $L_OldAlarmRet ] || [ 1 == $L_NewAlarmRet ];then
            sleep 15
        else
            break;
        fi

        let "L_RetryTimes-=1"
        let "L_SleepTime+=15"
    done
    
    if [ $L_SleepTime -ge 360 ];then
        OS_echo "${UPGRADE_SH}: Wait time is ${L_SleepTime}, do not need wait any more." >/dev/null
        return 0
    fi 
    let L_WaitTime=360-${L_SleepTime}
    OS_echo "${UPGRADE_SH}: Wait time is ${L_WaitTime}." >/dev/null

    CheckThirdUltraPath
    diagnoseResult=$(grep -i "All hosts have" $G_ULTRA_CHECK) 
    OS_echo "${UPGRADE_SH}: Check result is: (${diagnoseResult})." >/dev/null
    
    rm $G_ULTRA_CHECK >/dev/null
    checkResult=$(echo "${diagnoseResult}" | grep yes)
    #返回yes，代表全都安装了多路径，可以直接返回，否则等待
    if [ -n "${checkResult}" ];then
        OS_echo "${UPGRADE_SH}: Check result is yes." >/dev/null
        #待tgt合入后删除下面sleep的这一行
        sleep ${L_WaitTime}
        return 0
    fi

    sleep ${L_WaitTime}
    return 0
}

#检查设备链路状态
UPGRADE_CHECK_DEVICE_LINK()
{
    OS_echo "${UPGRADE_SH}: Begin to check the device link state." >/dev/null
    local L_6U_Pruduct=$(cat $G_PRODUCT_INI |grep -m 1 ProductName |awk -F"=" '{print $2}' |grep -E '(6800 V3|6900 V3)')
    local LOCAL_NID=0
    local DEVICE_LINK_FILE=/OSM/conf/deviceLink
diagnose --auto << EOF > $DEVICE_LINK_FILE
sys showcls
quit
EOF
    LOCAL_NID=$(grep -i "local node id" $DEVICE_LINK_FILE | grep -oE "[0-9]+")
    OS_echo "${UPGRADE_SH}: LOCAL_NID=($LOCAL_NID)." >/dev/null
    rm -rf $DEVICE_LINK_FILE
    
    if [ -n "$L_6U_Pruduct" ]; then
        OS_echo "[$UPGRADE_SH]:It is 6U 4-controller.">/dev/null
        echo "upd checkdevicelink"
        echo "True"
        return 0;
    fi
    
    if [ 0 -ne "$LOCAL_NID" ] && [ 1 -ne "$LOCAL_NID" ] && [ 2 -ne "$LOCAL_NID" ];then
        OS_echo "${UPGRADE_SH}: This node is not the master or slave node." >/dev/null
        echo "upd checkdevicelink"
        echo "True"
        return 0
    fi
    
    om_check_connection.sh
    if [ 0 == $? ];then
        OS_echo "${UPGRADE_SH}: The device link state is connected." >/dev/null
        echo "upd checkdevicelink"
        echo "True"
        return 0
    fi
    OS_echo "${UPGRADE_SH}: The device link state is disconnected." >/dev/null
    echo "upd checkdevicelink"
    echo "False"
    return 1
}

#离线升级前检查告警
CHECK_ALARM_BEFORE_OFFLINE_UPGRADE()
{
    local MML_PARA=$1
    local ALARM_ID=$(echo $MML_PARA | grep -oE "[0-9]+")
    OS_echo "${UPGRADE_SH}: The para is $MML_PARA" >/dev/null
    #查询未清除未恢复的告警
    UPGRADE_CHECK_ALARM_STATE "$ALARM_ID" "1"
    if [ "1" == "$?" ]; then
        echo "$MML_PARA"
        echo "True"
        return 0
    fi
    
    echo "$MML_PARA"
    echo "False"
    return 0
}

CHECK_PKG_IN_UPDATE_DIR()
{
    local pkg_path=$1
    if [ ! -f "$pkg_path" ];then
        OS_echo "${UPGRADE_SH} Package $pkg_path does not exist."
        return 1
    else
        OS_echo "${UPGRADE_SH} Get package $pkg_path this node."
        return 0
    fi
}

UPGRADE_CHECK_DISK_USE_RATE()
{
    if [ -f $G_DISK_INFO ];then
        param=$(cat "$G_DISK_INFO" |awk -F ' ' '{print $3}')
        if [ "" != "$param" ];then
            OS_echo "$param"
            unset G_DISK_ID_LIST
            UPGRADE_GET_DISKID $param
            if [ 0 != ${#G_DISK_ID_LIST} ];then
                OS_CalcDiskUseRateWithList ${G_DISK_ID_LIST[*]}
                return 0
            fi
        fi   
    fi
    OS_CheckSSDAndSASDiskUsage
    return 0
}

UPGRADE_GET_DISKID()
{
    local str_location=$1
    local list_location=$(echo $str_location|tr "[;,]" "\n")
    local L_location=""
    local L_sdid=0
    OS_echo "$list_location"
    local num=0
    local L_CMD="ld showsds"
    
    
    [ -f $G_DISK_ID ] && rm $G_DISK_ID
    OS_ExecDiagnose "$L_CMD" "$G_DISK_ID"
    
    while read location ldid sdid others;
    do
        if [ -n "$(echo $sdid | grep '[0-9]')" ]; then
            L_location=$location
            L_sdid=$sdid
            
            for l in $list_location;do
                if [ "$L_location" == "$l" ];then
                    G_DISK_ID_LIST[$num]=$L_sdid
                    num=$(( num + 1 ))
                fi
            done
        fi
        
    done < $G_DISK_ID 
    return 0
}

OS_CalcDiskUseRateWithList()
{
    local i_sdid="$@"
    local -i L_OneDiskUsage=0
    local -i L_SSDDiskUtilUsageResult=0
    local -i L_SSDDiskCount=0
    local -i L_SSDDiskAverage=0
    local -i L_SSDDiskSum=0
    
    local -i L_SASDiskUtilUsageResult=0
    local -i L_SASDiskCount=0
    local -i L_SASDiskAverage=0
    local -i L_SASDiskSum=0
    local L_DiskType=""
    
    [ -f $G_DISK_UTIL ] && rm $G_DISK_UTIL
    GetDiskUsage
    
    while read sdid util type; 
    do
        if [ -n "$(echo $sdid | grep "[a-zA-Z]")" -o -z "$sdid" ]; then
            continue;
        fi
        L_OneDiskUsage=$util
        L_DiskType=$type
        for i in $i_sdid;do
            if [ "$sdid" == "$i" ];then
                OS_echo "get it ${L_OneDiskUsage}"
                if [ "${L_DiskType}" == "SSD" ] || [ "${L_DiskType}" == "SLC_SSD" ] || [ "${L_DiskType}" == "MLC_SSD" ]; then
                    [ ${L_OneDiskUsage} -gt ${L_SSDDiskUtilUsageResult} ] && L_SSDDiskUtilUsageResult=${L_OneDiskUsage}
                    L_SSDDiskCount=$(( L_SSDDiskCount + 1 ))
                    L_SSDDiskSum=$((L_SSDDiskSum + L_OneDiskUsage ))
                else
                    [ ${L_OneDiskUsage} -gt ${L_SASDiskUtilUsageResult} ] && L_SASDiskUtilUsageResult=${L_OneDiskUsage}
                    L_SASDiskCount=$(( L_SASDiskCount + 1 ))
                    L_SASDiskSum=$((L_SASDiskSum + L_OneDiskUsage ))
                fi
            fi
        done       
    done < $G_DISK_UTIL
    
    OS_echo "${UPGRADE_SH}: L_SSDDiskCount=$L_SSDDiskCount  L_SASDiskCount=$L_SASDiskCount"
    #计算磁盘平均利用率
    if [ "0" != "$L_SSDDiskCount" ];then
        L_SSDDiskAverage=$(( $L_SSDDiskSum / $L_SSDDiskCount ))
    fi
    if [ "0" != "$L_SASDiskCount" ];then
        L_SASDiskAverage=$(( $L_SASDiskSum / $L_SASDiskCount ))
    fi
    
    echo "upd diskupgradeusagecheck"
    echo "${L_SSDDiskUtilUsageResult},${L_SSDDiskAverage};${L_SASDiskUtilUsageResult},${L_SASDiskAverage}"
    
    return 0
}
# **************************************************************************** #
# Function Name:IS_NEED_CALLBACK
# Description:
# Parameter:
# Return:1：需要回调，0：不需要回调
# **************************************************************************** #
function IS_NEED_CALLBACK()
{
    local L_TMP_FILE="/tmp/showtracediagnoseinfo"
    OS_ExecDiagnose "sys showtrace" "$L_TMP_FILE"
    OS_echo "showtrace output: $(cat "$L_TMP_FILE")"
    cat "$L_TMP_FILE" | grep "NODE_RECOVERY" | grep "Running"
    if [ "$?" != "0" ]; then
        rm -f "$L_TMP_FILE"
        return 0
    fi
    OS_ExecDiagnose "sys showtrace 11" "$L_TMP_FILE"
    OS_echo "showtrace 11 output: $(cat "$L_TMP_FILE")"
    cat "$L_TMP_FILE" | tail -2 | head -1 | grep -E "NotifyUpgradeSyncVersion|NotifyUpgradeSyncFw"
    if [ "$?" == "0" ]; then
        rm -f "$L_TMP_FILE"
        return 1
    fi
    rm -f "$L_TMP_FILE"
    return 0
}
# --------------------------  main  ------------------------------------------ #

#为了调用OS_SET_DISK_RW
OS_GetDriverID

L_INPUT_PARA_NUM=$#

if [ $L_INPUT_PARA_NUM -lt 1 ] ; then
    OS_echo "${UPGRADE_SH}: [os_system_update.sh]wrong input num: $#." >/dev/null
else
        L_TMP=`echo $1 | grep '\-[a-zA-Z0-9]'`
    if [ -z "$L_TMP" ] ; then
        OS_echo "${UPGRADE_SH}: [os_system_update.sh]wrong input para $1." >/dev/null
    fi
fi

while getopts rdc:svbft:jkg:a:m:whlqpuyz:A:BDK options 2>/dev/null
do
    case $options in
        p)  OS_echo "${UPGRADE_SH} [-p]: Check state_upd.lua" >/dev/null
            OS_CHECK_STATE_UPD
            exit $?            
            ;;
        r)  OS_echo "${UPGRADE_SH} [-r]: Check CPU and Memory" >/dev/null
            OS_CHECK_FREE_MEM_ENOUGH
            exit $?            
            ;;
        d)  OS_echo "${UPGRADE_SH} [-d]: Check Disk Usage" >/dev/null
            OS_CheckDiskUsage
            exit $?
            ;;
        D)  OS_echo "${UPGRADE_SH} [-D]: Check SSD and SAS usage" >/dev/null
            UPGRADE_CHECK_DISK_USE_RATE
            exit $?
            ;;    
        c)  OS_echo "${UPGRADE_SH} [-c]: Exec diagnose command" >/dev/null
            G_PARA="$OPTARG" 
            OM_ExecuteDiagnoseCommand "$G_PARA"
            exit $?
            ;;
        l)  OS_echo "${UPGRADE_SH} [-l]: Exec clear mem flag" >/dev/null
            OM_SetMemRecoverFlagInvalid 
            exit $?
            ;;
        q)  OS_echo "UPGRADE_SH [-q]: delete upgrade state file" >/dev/null
            OS_DEL_UPGRADE_STATE_FILES
            exit $?
            ;;
        s)  OS_echo "${UPGRADE_SH} [-s]: Check Sys compatibility" >/dev/null
            UPGRADE_SYS_VER_COMPARE
            exit $?
            ;;
        v)  OS_echo "${UPGRADE_SH} [-v]: Get Sys Version" >/dev/null
            UPGRADE_GET_SYS_VERSION
            exit $?
            ;;
        b)  OS_echo "${UPGRADE_SH} [-b]: Get Black White List" >/dev/null
            UPGRADE_SYS_VER_BALCK
            exit $?
            ;;
        f)  OS_echo "${UPGRADE_SH} [-f]: Get Firmware compatibility" >/dev/null
            UPD_CHECK_CUR_VER_COMPATIB
            exit $?
            ;;
        j)  OS_echo "${UPGRADE_SH} [-j]: Compare " >/dev/null
            UPD_COMPARE_SYS_VER
            exit $?
            ;;
        g)  OS_echo "${UPGRADE_SH} [-g]: check pcie version " >/dev/null
            OM_CheckPcieVer $OPTARG
            exit $?
            ;;
        k)  OS_echo "${UPGRADE_SH} [-k]: Check version " >/dev/null
            UPD_CHECK_SYS_VER
            exit $?
            ;;
        K)  OS_echo "${UPGRADE_SH} [-K]: Check version when online upgrade " >/dev/null
            UPD_CHECK_SYS_VER_WHEN_ONLINE_UPGRADE
            exit $?
            ;;
        a)  OS_echo "${UPGRADE_SH} [-a]: Upgrade hotpatch check" >/dev/null
            CHECK_BEFORE_INSTALL_HOTPATCH $OPTARG
            exit $?
            ;; 
        m)  OS_echo "${UPGRADE_SH} [-m]: Upgrade is need reboot" >/dev/null
            OS_UPG_HP_IS_NEED_REBOOT $OPTARG
            exit $?
            ;;
        w)  OS_echo "${UPGRADE_SH} [-w]: check ssd" >/dev/null
            CHECK_SSD 
            exit $?
            ;;
        h)  OS_echo "${UPGRADE_SH} [-h]: Post check pcie version" >/dev/null
            UPGRADE_POST_CHECK_PCIE_VERSION 
            exit $?
            ;;
        u)  OS_echo "${UPGRADE_SH} [-u]: Check Ultra Path" >/dev/null
            UPGRADE_CHECK_ULTRA_PATH 
            exit $?
            ;;
        y)    OS_echo "${UPGRADE_SH} [-y]: Check Device Link" >/dev/null
            UPGRADE_CHECK_DEVICE_LINK 
            exit $?
            ;;
        A)  OS_echo "${UPGRADE_SH} [-A]: Check alarm before offline upgrade" >/dev/null
            CHECK_ALARM_BEFORE_OFFLINE_UPGRADE "$OPTARG" 
            exit $?
            ;;
        z)  OS_echo "${UPGRADE_SH} [-z]: Check if package is in /OSM/update/" >/dev/null
            CHECK_PKG_IN_UPDATE_DIR "$OPTARG"
            exit $?
            ;;
        B) OS_echo "${UPGRADE_SH} [-B]: Callback sys." >/dev/null
            IS_NEED_CALLBACK
            if [ "$?" == "1" ]; then
                /OSM/bin/upgrade_atom -f NOTIFY_SYS_POWER_ON_RESURT -t sync 
            fi
            ;;
        *) OS_echo "${UPGRADE_SH}: invalid options" >/dev/null
           ;;
    esac
done

exit 0

