#!/bin/bash

#нڵ

G_INSPECT_MMLPATH="/opt/huawei/snas/script/inspect_mml"
. $G_INSPECT_MMLPATH/CheckItems
CurInspectNum="322"
CurInspectFun=`GetInspectType $CurInspectNum`
RESULTFILE="${G_TMP_INSPECT_PATH}/tmpResult${CurInspectFun}"
LOG_FILE="/var/log/inspect.log"
>$RESULTFILE

isPass=0

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

function CheckNvdimmSoftFault()
{
    local iRet=0
    checkresult=$( ls -lR /opt/huawei/snas/etc/monstore ) 
    iRet=$?
    errinfo=$( echo "${checkresult}" | grep "Input/output  error" )
    if [ "X${errinfo}" != "X" ] || [ $iRet -ne 0 ];then 
        isPass=1
        echo "[ERR]INFO:there was happened Nvdimm Soft fault in /opt/huawei/snas/etc/monstore." >>${RESULTFILE} 2>&1
        LOG "there was happened Nvdimm Soft fault in /opt/huawei/snas/etc/monstore"
    fi
    checkresult1=$( ls -lR /opt/huawei/snas/etc/monstorebase )
    iRet=$?
    errinfo1=$( echo "${checkresult1}" | grep "Input/output  error" )
    if [ "X${errinfo1}" != "X" ] || [ $iRet -ne 0 ];then 
        isPass=1
        echo "[ERR]INFO:there was happened Nvdimm Soft fault in /opt/huawei/snas/etc/monstorebase." >>${RESULTFILE} 2>&1
        LOG "there was happened Nvdimm Soft fault in /opt/huawei/snas/etc/monstorebase"
    fi

    return
}

function CheckZoneMount()
{
    local iRet=""
    local isExit=""
    local checklist="monstore monstorebase"
    local try_times=(1 2 3)
    local time=""
    local Permission=""

    for line in $checklist;do
        isExit=$( grep -w "/opt/huawei/snas/etc/$line" /proc/mounts | head -1 )
        if [ "X${isExit}" == "X" ];then
            isPass=1
            echo "[ERR]INFO:/opt/huawei/snas/etc/$line is not mount." >>${RESULTFILE} 2>&1
        else
            #Ƿһ,עǸ
            local subdivision=$( echo "${isExit}" | awk '{print $1}' )
            local count=$( df $subdivision 2>&1 | grep -v "df:" | grep -w $subdivision| wc -l )
            if [ $count -ne 1 ];then
                isPass=1
                echo "[ERR]INFO:$subdivision is not a partition." >>${RESULTFILE} 2>&1
            fi
            #ǲrwȨ.
            for time in ${try_times[*]}
            do
                Permission=$( echo "${isExit}" | awk -F',' '{print $1}' | awk '{print $NF}' )
                if [ X"${Permission}" == X"rw" ]
                then
                    break
                else
                    LOG "time ${time},Permission(${Permission}),/opt/huawei/snas/etc/$line mount attribute:${isExit}"
                    sleep 3
                    isExit=$( grep -w "/opt/huawei/snas/etc/$line" /proc/mounts | head -1 )
                fi
            done

            if [ "${Permission}" != "rw" ];then
                isPass=1
                echo "[ERR]INFO:The mount permission of /opt/huawei/snas/etc/$line is not rw." >>${RESULTFILE} 2>&1
            fi
        fi
    done

    if [ ${isPass} == 0 ];then
        echo "MountStatus:Normal" >>${RESULTFILE} 2>&1    
    fi 
}

CheckNvdimmSoftFault

CheckZoneMount
echo "${CurInspectFun}_Pass $isPass" >>${RESULTFILE} 2>&1
exit 0

