#!/bin/bash
set +x
G_MML_FILE_PATH="/opt/huawei/snas/script/inspect_mml"
source $G_MML_FILE_PATH/CheckItems
CurInspectNum="150"
CurInspectFun="$(GetInspectType $CurInspectNum)"
RESULTFILE="${G_TMP_INSPECT_PATH}tmpResult${CurInspectFun}"
LOG_FILE="/var/log/inspect.log"
>${RESULTFILE}
isPass=0
DiskKickFlag="--"
UNCFaultFlag="--"

function LOG
{
   time=$(date)
   echo [${time}][$$][${CurInspectFun}]$@ >> ${LOG_FILE}
}

# 踢盘开关检查
function checkKicksSwitch()
{
    grep -q "DiskKickForbit" /opt/huawei/snas/etc/cm.ini
    if [ $? -ne 0 ];then
        DiskKickFlag="null"
        return
    fi
    DiskKickForbit=$( grep "DiskKickForbit=1" /opt/huawei/snas/etc/cm.ini )
    if [ "X$DiskKickForbit" == "X" ];then
        DiskKickFlag="close"
    else
        isPass=1
        DiskKickFlag="open"
        echo "[ERR]INFO:DiskKickForbit is open." >>$RESULTFILE 2>&1
    fi
}

function version_lt()
{
    test "$( echo "$@" | tr " " "\n" | sort -rV | head -n 1 )" != "$1";
}

# ARM直通机型东芝盘UNC故障上报检测
function checkUNCFaultReport()
{
    # 步骤1：检测环境是否为ARM直通机型（无RAID卡）并配置了东芝盘
    arch=$( arch )
    if [ "X${arch}" != X"aarch64" ]; then
        UNCFaultFlag="not involved"
        return
    fi
    toshiba=$( lsscsi | grep ATA | grep MG0 | wc -l )
    if [ ${toshiba} -eq 0 ]; then
        UNCFaultFlag="not involved"
        return
    fi
    # 检测message日志是否有：set dq config error:0x203 打印
    message_log_list=$(ls /var/log/backup/OS/message.log.*.tar.gz)
    count=0
    for item in $message_log_list;do
        file_size=$( du -s ${item}  | awk '{print $1}' )
        if [ ${file_size} -ge 102400 ];then
            echo "[ERR]INFO:the file(${item}) size is too big:${file_size}K, please manually confirm the cluster status and inspection result." >>$RESULTFILE 2>&1
            isPass=1
            return
        fi
        dq_config_error_backup=$((dq_config_error_backup+$(zgrep -a "set dq config error:0x203" ${item} | zgrep -a "0x0 0x100" | wc -l )))
    done

    file_size=$( du -s /var/log/message/message.log  | awk '{print $1}' )
    if [ ${file_size} -ge 102400 ];then
        echo "[ERR]INFO:/var/log/message/message.log size is too big:${file_size}K, please manually confirm the cluster status and inspection result." >>$RESULTFILE 2>&1
        isPass=1
        return
    fi
    dq_config_error=$( cat /var/log/message/message.log | grep -a "set dq config error:0x203" | grep -a "0x0 0x100" | wc -l )
    if [ ${dq_config_error_backup} -eq 0 ] && [ ${dq_config_error} -eq 0 ]; then
        UNCFaultFlag="not exist"
        return
    fi
    # 判断9000版本和对应的补丁版本
    version9000=$( grep -w "ProductVersion" /opt/huawei/deploy/package/version | awk -F"=" '{print $2}' )
    patch_version=$( ls /var/huawei/patch/cur/sph | awk -F'_' '{print$NF}' )
    # 7.1.1.SPH607
    if [ "X${version9000}" == X"V500R007C10SPC800" ]; then
        if [ "X${patch_version}" == X"" ] || version_lt $patch_version "7.1.1.SPH607"; then
            isPass=1
            UNCFaultFlag="exist"
            echo "[ERR]INFO:Exist UNC Fault. Install the patch version to 7.1.1.SPH607 or later." >>$RESULTFILE 2>&1
            return
        fi
    fi
    UNCFaultFlag="not exist"
}

checkKicksSwitch
checkUNCFaultReport

echo "DiskKickForbit:${DiskKickFlag}||UNCFault:${UNCFaultFlag}"   >>$RESULTFILE 2>&1
#打印是否巡检通过
echo "${CurInspectFun}_Pass $isPass" >>$RESULTFILE
LOG "${CurInspectFun}_Pass $isPass"

exit 0
