#!/bin/bash

LOG_FILE="/var/log/inspect.log"

GetInspectType()
{
    case $1 in
    148)
        ##SMR磁盘运行状态检查
        echo "CheckSMRdiskinfo"
        ;;
    157)
        #节点电子标签检查
        echo "CheckElecLabel"
        ;;
    270)
        echo "CheckNodeState"
        ;;
    273) 
        echo "CheckBalanceStatus"
        ;;
    274) 
        echo "CheckCertificate"
        ;;
    275)
        echo "CheckDataMoveEnd"
        ;;
    294)
        echo "checkDiskNokickStatus"
        ;;
    311)
        echo "CheckBirthTime"
        ;;
    312)
        echo "checkKernelHotPatch"   
        ;;
    313)
        echo "CheckDepXML"   
        ;;
    314)
        echo "CheckAllCertificate"
        ;;
    315)
        echo "nas_check_quota_scan"
	;;
    316)
        echo "CheckMasterSpecialConfig"
	;;
    317)
        echo "CheckALLNodeSpecialConf"
	;;
    328)
        echo "checkDiskModePage"
        ;;	
    350)
        echo "CheckStorageVersion"
        ;;
    351)
        echo "CheckHardWareVersion"
        ;;
    352)
        echo "CheckFrontAndBackPort"
        ;;
    353)
        echo "CheckFrontIPNumber"
        ;;
    354)
        echo "CheckBackNetworkIPaddr"
        ;;
    355)
        echo "CheckClusterRedundancyRatio"
        ;;
    356)
        echo "CheckClusterNodeNumber"
        ;;    
    357)
        echo "CheckNodePoolDiskConsistency"
        ;;
    358)
        echo "CheckManagerNet"
        ;; 
    *)
        echo ""
        ;;
    esac
}

function set_ifs
{
    IFS_OLD=$IFS
    IFS=$'\n'
}

function restore_ifs
{
    IFS=$IFS_OLD
}

function LOG
{
    echo [`date`][$0]$@ >> $LOG_FILE
}

function get_node_backnet_ip
{
    local nid=$1
    local temp=1
    local iRet=0
    sqlite3 /opt/huawei/snas/etc/cm_conf.db "select IP_ADDR from CM_NODE_IP_T where NID=$nid and SUBNET_ID=1 order by NID limit 1"
    iRet=$?
    while [[ ${iRet} -ne 0 ]]
    do
        sleep 2
        LOG "[$FUNCNAME]Failed to get back subnet IP of $nid from db"
        temp=$((temp + 1))
        if [ $temp -gt 5 ];then
            break
        fi
        sqlite3 /opt/huawei/snas/etc/cm_conf.db "select IP_ADDR from CM_NODE_IP_T where NID=$nid and SUBNET_ID=1 order by NID limit 1"
        iRet=$?
    done
    return $iRet
}

function ChangeProperty()
{
    local change_type=$1
    local property=""
    LOG "TYPE:${change_type},FILE:${Paras}"
    

    for changepath in ${Paras}
    do
        if [ -d ${changepath} ];then
            path_type=0
            property=$( lsattr -a ${changepath} | grep "/.$" |awk '{print $1}' ) 
        else
            path_type=1
            property=$( lsattr -a ${changepath} | awk '{print $1}' ) 
        fi
        if [ "X${property}" == "X" ];then
            continue
        fi
        if [ "${change_type}" == "add" ];then
            if [[ ${property} =~ "i" ]];then
                continue
            fi
            if [ "${path_type}" == "0" ];then
                chattr +i ${changepath} >/dev/null 2>&1
            else 
                chattr -f +i ${changepath} >/dev/null 2>&1
            fi
        elif [ "${change_type}" == "del" ];then
            if [[ ${property} =~ "i" ]];then
                if [ "${path_type}" == "0" ];then
                    chattr -i ${changepath} >/dev/null 2>&1
                else 
                    chattr -f -i ${changepath} >/dev/null 2>&1
                fi
            fi
        else
            continue
        fi
    done 
    return
}



Add_i_attr="add"
Del_i_attr="del"
G_MML_PATH="/opt/huawei/snas/script/inspect_mml"
Paras=${@:2}
taskids=$1

#更改入口脚本的i属性
if [ "${taskids}" == "${Add_i_attr}" -o "${taskids}" == "${Del_i_attr}" ];then
    ChangeProperty "${taskids}" 
    exit 0
fi
if [ "$taskids" = "checkCoreFile" ] && [ "X$2" != "X" ]; then
    # 233 检查是否有CORE文件   分别在/corefile/crash和/corefile目录中查找是否有近30天的文件产生
    find /corefile/crash -mtime -30 -type f -print | grep -w "vmcore" | grep .e$ > $2 2>&1
    find /corefile -maxdepth 1 -mtime +-1 -mtime -30 ! -type d -print| grep -v "ipmitool" >> $2 2>&1
    df -m /corefile | grep "/corefile" | awk '{print "AvailableSpace="$4}' >> $2 2>&1
    exit 0
fi
if [ "${taskids}" == "updateCert" -o  "${taskids}" == "restoreCert" ]; then
    /opt/huawei/snas/script/inspect_mml/certRmtUpdate.sh $@
    exit $?
fi
for taskid in $taskids
do
{
    InspectFun=`GetInspectType $taskid`
    
    if [ "${InspectFun}" != "" ]; then
        #获取此脚本的运行参数
        CurParam=""
        ss=${InspectFun}-
        for Param in $Params
        do
        {   
            tmpStr=$(echo $Param |grep $InspectFun)
            if [ "$tmpStr" != "" ];then
                CurParam=$(echo $Param |awk -F "$ss" '{printf $2}'|sed 's/-/ /g')
                break;
            fi
        }
        done

        InspectBeginTime=$(date +%s)
        $G_MML_PATH/$InspectFun "$CurParam"
        InspectEndTime=$(date +%s)
        difftime=$(($InspectEndTime - $InspectBeginTime))
        
        #判断巡检项是否通过，若不通过，并且前一次巡检时间小于250秒，那么重新再巡检一次
        PassCode=$(cat /tmp/tmpResult${InspectFun} |grep ${InspectFun}_Pass |awk '{printf $2}')
        if [ "$PassCode" = "1" ] && [ $difftime -lt 250 ];then
            $G_MML_PATH/$InspectFun "$CurParam"
        fi
    fi
}&
done
wait

for taskid in $1
do
{   
    InspectFun=`GetInspectType $taskid`

    if [ "${InspectFun}" != "" ]; then
        echo  ${InspectFun}
        cat "/tmp/tmpResult${InspectFun}"
        echo  ${InspectFun}_End
    fi

}
done
