#!/bin/bash
set +x
############################################################
# 所属模块	: OceanStor9000 巡检工具
# 功能      : 协议删除操作日志目录检查
# 创建时间	: 2017年02月18日
# 描述      ：
#           开关关闭不通过(修改时间2019/3/20)
#           在开关打开的情况下，如果目录未设置则不通过
#           如果目录设置但目录实际不存在不通过，其余通过
############################################################
source /opt/huawei/snas/script/inspect_mml/CommonFunc.sh
LOGFILE="/var/log/inspect.log"

function LOG()
{
    echo [`date`][$$]$@ >> $LOGFILE
}

dirtem="/tmp/tmpcheckNasLogDir_Mml"
iscreatdir=""
issetdir=""
checkdirpath=""
dirpath=""
isdirpath=""

function checkNasLogDir_Mml()
{
    #已巡检项名称创建一个临时目录用来生成nas文件，避免多个巡检项同时调用此文件
    mkdir /tmp/checkNasLogDir
    nas="/tmp/checkNasLogDir/nas.sh"
    echo "#!/bin/bash" > $nas
    echo "\$*" >> $nas
    chmod +x $nas

    #查询开关是否打开
    $nas "/usr/local/bin/nas_proto_cfg -c get_operation_log" >$dirtem
    iscreatdir=$(cat $dirtem |grep -w opt_log_level|cut -d ">" -f 2|cut -d "<" -f 1)
    if [ $iscreatdir -eq 0 ]; then
        LOG "[$FUNCNAME] the cluster is not open NasLogDir."
        return 1
    fi

    #查询是否设置目录
    $nas "/usr/local/bin/nas_proto_cfg -c get_opt_log_dir" >$dirtem
    issetdir=$(cat $dirtem|grep "<opt_log_dir/>")
    if [ "X${issetdir}" != "X" ]; then
        issetdir=0
        LOG "[$FUNCNAME] don't set the dirpath,not pass!"
        return 4
    fi
    issetdir=1
    #查询目录是否为真,并且检查该路径是否为文件
    dirpath=$(cat $dirtem |grep "<opt_log_dir>" |awk -F">" '{print $2}' |awk -F"<" '{print $1}' |sed 's/\/mnt\/fs\/share\///g')
    checkdirpath=$(cat $dirtem |grep "<opt_log_dir>" |awk -F">" '{print $2}' |awk -F"<" '{print $1}')
    $nas "stat $checkdirpath"|grep directory >/dev/null 2>&1
    if [ $? -eq 0 ]; then
        return 0
    else
        LOG "$[FUNCNAME] the dirpath : $checkdirpath is not exist!"
        return 4
    fi
}

checkNasLogDir_Mml
iRet=$?
echo "ispass:$iRet;isopen:$iscreatdir;isset:$issetdir;isdir:$dirpath"
rm -rf /tmp/checkNasLogDir
rm /tmp/tmpcheckNasLogDir_Mml

