#!/bin/bash

LOG_FILE="/var/log/inspect.log"
CIFS_FILE="/tmp/tmpfileCifsShareDir"
MMM_FILE="/tmp/tmpfilemmlRedundantRatio"
NFS_FILE="/tmp/tmpfileNfsShareDir"

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

function RedundantRatio()
{
    if [ "$1" == "1" ] && [ "$2" == "1" ];then
        echo "+1"
        return 0
    fi
    
    if [ "$1" == "2" ] && [ "$2" == "2" ];then
        echo "+2"
        return 0
    fi
    
    if [ "$1" == "2" ] && [ "$2" == "1" ];then
        echo "+2:1"
        return 0
    fi
    
    if [ "$1" == "3" ] && [ "$2" == "3" ];then
        echo "+3"
        return 0
    fi
    
    if [ "$1" == "3" ] && [ "$2" == "1" ];then
        echo "+3:1"
        return 0
    fi
    
    if [ "$1" == "4" ] && [ "$2" == "4" ];then
        echo "+4"
        return 0
    fi
}

#获取cifs共享目录
function GetCifsShareDir()
{
    /tmp/${tmpdir}/nas.sh "nas_proto_cifs -c show-page -f 0 -t 50" |grep "<path>" >$CIFS_FILE 
    iRet=$?
    if [ $iRet -ne 0 ];then
        sharedircifs=1
        LOG "[$FUNCNAME][$LINENO] does not cifs sharedir"
    fi
    
    while read line
    do
        cifs_path=$(echo $line |awk -F">" '{print $2}' |awk -F"<" '{print $1}' |sed 's/localfs/'mnt\\/fs'/g')
        /tmp/${tmpdir}/nas.sh "stat $cifs_path" >/dev/null 2>&1
        if [ $? -ne 0 ];then
            #目录不存在，共享存在
            echo "Share_Type=CIFS||Share_Dir=$cifs_path||RedundantRatio=no"
            LOG "[$FUNCNAME][$LINENO] The cifs directory does not exist"
            continue
        fi
        /usr/local/bin/MmlBatch 4016 "cm shareDirRedundantRatio show $cifs_path" >$MMM_FILE
        dos2unix $MMM_FILE >/dev/null 2>&1
        Rdc=$(grep Rdc $MMM_FILE |awk -F= '{print $2}' |tr -d ' ')
        Rdn=$(grep Rdn $MMM_FILE |awk -F= '{print $2}' |tr -d ' ')
        Ratio=$(RedundantRatio $Rdc $Rdn)
        echo "Share_Type=CIFS||Share_Dir=$cifs_path||RedundantRatio=$Ratio"
        LOG "[$FUNCNAME][$LINENO] cifs Share_Dir:$cifs_path||RedundantRatio:$Ratio ||Rdc:$Rdc||Rdn:$Rdn"
    done <$CIFS_FILE
    
    return 0
}

#获取nfs共享目录
function GetNfsShareDir()
{
    /tmp/${tmpdir}/nas.sh "nas_proto_nfs -c show_share -f 1 -t 50" |grep "<share_path>" >$NFS_FILE
    iRet=$?
    if [ $iRet -ne 0 ];then
        sharedirnfs=1
        LOG "[$FUNCNAME][$LINENO] does not nfs sharedir"
    fi
    while read line
    do
        nfs_path_tmp=$(echo $line |awk -F">" '{print $2}' |awk -F"<" '{print $1}')
        nfs_path="/mnt/fs/share${nfs_path_tmp}"
        /tmp/${tmpdir}/nas.sh "stat $nfs_path" >/dev/null 2>&1
        if [ $? -ne 0 ];then
            echo "Share_Type=NFS||Share_Dir=$nfs_path||RedundantRatio=no"
            LOG "[$FUNCNAME][$LINENO] The nfs directory does not exist"
            continue
        fi
        /usr/local/bin/MmlBatch 4016 "cm shareDirRedundantRatio show $nfs_path" >$MMM_FILE 
        dos2unix $MMM_FILE >/dev/null 2>&1
        Rdc=$(cat $MMM_FILE |grep Rdc |awk -F= '{print $2}')
        Rdn=$(cat $MMM_FILE |grep Rdn |awk -F= '{print $2}')
        Ratio=$(RedundantRatio $Rdc $Rdn)
        echo "Share_Type=NFS||Share_Dir=$nfs_path||RedundantRatio=$Ratio"
        LOG "[$FUNCNAME][$LINENO] nfs Share_Dir:$nfs_path||RedundantRatio:$Ratio ||Rdc:$Rdc||Rdn:$Rdn"
    done <$NFS_FILE
}

tmpdir=$(date +%s)
mkdir -p /tmp/${tmpdir}
touch /tmp/${tmpdir}/nas.sh 
echo "#!/bin/bash" >/tmp/${tmpdir}/nas.sh 
echo "\$*" >>/tmp/${tmpdir}/nas.sh  
chmod +x /tmp/${tmpdir}/nas.sh 


sharedircifs=0
sharedirnfs=0

GetCifsShareDir

GetNfsShareDir

if [ $sharedircifs -eq 1 ] && [ $sharedirnfs -eq 1 ];then
    echo "Share_Type=--||Share_Dir=--||RedundantRatio=--"
fi

rm -rf /tmp/${tmpdir}/nas.sh
rm -rf $CIFS_FILE
rm -rf $MMM_FILE
rm -rf $NFS_FILE

exit 0
