#!/bin/sh
set +x

G_MML_FILE_PATH="/opt/huawei/snas/script/inspect_mml/"
Remote_filename="MemPoolUsage"
LOG_FILE="/var/log/inspect.log"
source $G_MML_FILE_PATH/CheckItems
source $G_MML_FILE_PATH/CommonFunc.sh
G_REMOTE_TMP_FILE="/tmp/tmpfile"
CurInspectNum="215"
CurInspectFun="$(GetInspectType $CurInspectNum)"
RESULTFILE="/tmp/tmpResult${CurInspectFun}"
filename="CheckDataConsistent"
MDS="mds"
MONC="monc"
>${RESULTFILE}

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

function checkDataConsistent_Async()
{
    isPass=0
    local Result=""
    local result_log=/var/log/nluninfo.log
    local TASK_ID_MDS=0
    local TASK_ID_MONC=1

    localip=`GetLocalIp`

    if [ -z "${localip}" ]; then
        LOG "[$LINENO]Error:Get localip failed."
        return 0
    fi

    #checkڵͣDFSִ
    node_service_type=$(egrep '[[]|^'node_service_type'=' /opt/huawei/snas/etc/snas.ini | tr -d '\n' | grep -Po '(?<=[[]'NODE'[]]'node_service_type'=)[0-9]+')

    if [ "${node_service_type}" != "1" ];then
        echo "INFO:Does not involve" >> ${RESULTFILE} 2>&1
        LOG "[$LINENO]node_service_type :${node_service_type}."
        return 0
    fi

    $G_MML_FILE_PATH/ShowInspectMml $localip 988 'mds objid show' > $G_REMOTE_TMP_FILE$filename$MDS 2>&1
    iRet_mds=$?
    sleep 15
    $G_MML_FILE_PATH/ShowInspectMml $localip 988 'monc nluninfo 0'  >  $G_REMOTE_TMP_FILE$filename$MONC 2>&1
    iRet_monc=$?
    if [ $iRet_mds -ne 0 -o $iRet_monc -ne 0 ]; then
        echo "[ERR]INFO:Failed to execute command(ShowInspectMml $localip 988 ('mds objid show' or 'monc nluninfo 0'))." >>${RESULTFILE} 2>&1
        LOG "[$LINENO] Error: ${iRet_mds}, ${iRet_monc}"
        return 1
    fi

    set_ifs
    dos2unix $G_REMOTE_TMP_FILE$filename$MDS > /dev/null 2>&1
    #ȡ
    root_dir1=`grep "ROOT_DIR" $G_REMOTE_TMP_FILE$filename$MDS|awk -F: '{print $2}' | awk -F, '{print $1}' |sed 's/^ *//g'|sed "/^$/d"|awk -F/ '{print $1}'`
    root_dir2=`grep "ROOT_DIR" $G_REMOTE_TMP_FILE$filename$MDS|awk -F: '{print $2}' | awk -F, '{print $1}' |sed 's/^ *//g'|sed "/^$/d"| awk -F/ '{print $2}'`
    LOG "[$LINENO]root_dir1:$root_dir1 ,root_dir2:$root_dir2"

    fs_sblock1=`grep "FS_SBLOCK" $G_REMOTE_TMP_FILE$filename$MDS|awk -F: '{print $2}' | awk -F, '{print $1}' |sed 's/^ *//g'|sed "/^$/d"|awk -F/ '{print $1}'`
    fs_sblock2=`grep "FS_SBLOCK" $G_REMOTE_TMP_FILE$filename$MDS|awk -F: '{print $2}' | awk -F, '{print $1}' |sed 's/^ *//g'|sed "/^$/d"|awk -F/ '{print $2}'`
    LOG "[$LINENO]fs_sblock1:$fs_sblock1,fs_sblock2: $fs_sblock2"

    #nlun map
    if [ "$root_dir1" == "" ] || [ "$root_dir2" == "" ] || [ "$fs_sblock1" == "" ] || [ "$fs_sblock2" == "" ]; then
        echo "[ERR]INFO:Failed to get objid (root_dir1:$root_dir1 ,root_dir2:$root_dir2, fs_sblock1:$fs_sblock1, fs_sblock2: $fs_sblock2)." >>${RESULTFILE} 2>&1
        LOG "[$LINENO]INFO:Failed to get objid (root_dir1:$root_dir1 ,root_dir2:$root_dir2, fs_sblock1:$fs_sblock1, fs_sblock2: $fs_sblock2)."
        restore_ifs
        return 1
    fi

    LOG "[$LINENO]get objid (root_dir1:$root_dir1 ,root_dir2:$root_dir2,fs_sblock1:$fs_sblock1,fs_sblock2: $fs_sblock2)ok"

    checknlun_Reault=`grep ${root_dir1} $result_log`
    if [ "${checknlun_Reault}" == "" ]; then
        LOG "[$LINENO]checknlun_Reault is Null"
        echo "[ERR]INFO:Failed to get nluninfo by $root_dir1." >>${RESULTFILE} 2>&1
        restore_ifs
        return 1
    fi

    Primary_id=`echo "$checknlun_Reault" | sed 's/[[:space:]]/\n/g'|grep Primary|awk -F'(' '{print $2}'`
    LOG "[$LINENO]Primary_id:$Primary_id"
    if [ "${Primary_id}" == "" ]; then
        LOG "[$LINENO]Primary_id is Null"
        echo "[ERR]INFO:Failed to get Primary_id by $checknlun_Reault." >>${RESULTFILE} 2>&1
        restore_ifs
        return 1
    fi
    echo "root_dir1:${root_dir1}" >>${RESULTFILE} 2>&1
    echo "root_dir2:${root_dir2}" >>${RESULTFILE} 2>&1
    echo "fs_sblock1:${fs_sblock1}" >>${RESULTFILE} 2>&1
    echo "fs_sblock2:${fs_sblock2}" >>${RESULTFILE} 2>&1
    echo "Primary_id:${Primary_id}" >>${RESULTFILE} 2>&1
    restore_ifs
}

checkDataConsistent_Async
[ -f "$G_REMOTE_TMP_FILE$filename$MDS" ] && rm -f "$G_REMOTE_TMP_FILE$filename$MDS"
[ -f "$G_REMOTE_TMP_FILE$filename$MONC" ] && rm -f "$G_REMOTE_TMP_FILE$filename$MONC"
