#!/bin/bash

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

GetInspectType()
{
    case $1 in 
    157)
        #节点电子标签检查
        echo "CheckElecLabel"
        ;;
    270)
        echo "CheckNodeState"
	;;
    273) 
        echo "CheckBalanceStatus"
    ;;
    274) 
        echo "CheckCertificate"
	    ;;
    275)
        echo "CheckDataMoveEnd"
	    ;;
    293)
        echo "checkDiskNokickStatus"
        ;;
    294)
        echo "CheckCronProcessCount"
        ;;
    295)
        echo "checklldpad"
        ;;
    296)
        echo "checkKernelHotPatch"   
	;;  
    297)
        echo "CheckBirthTime"
        ;;
    298)
        echo "CheckFluidControl"
        ;;
    299)
        echo "CheckDlmStatus"
        ;;
    *)
        echo ""
        ;;
    esac
}

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

function restore_ifs
{
    IFS=$IFS_OLD
}

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

G_MML_PATH="/opt/huawei/snas/script/inspect_mml"
Params=$2
taskids=$1
if [ "$taskids" = "inspect_collect_nodeinfo" ]; then
    if [ -x /opt/inspect/inspect_collect_nodeinfo.sh ];then
       /opt/inspect/inspect_collect_nodeinfo.sh >/dev/null 2>&1
    fi
    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
