#!/bin/bash

#检查CA内存资源,发现非空目录配额导致CA内存泄漏的风险局点，并自动完成该规避措施


G_INSPECT_MMLPATH="/opt/huawei/snas/script/inspect_mml"
source $G_INSPECT_MMLPATH/CheckItems

CurInspectNum="315"
CurInspectFun=`GetInspectType $CurInspectNum`
LOGPATH="/tmp/tmp${CurInspectFun}"
RESULTFILE="/tmp/tmpResult${CurInspectFun}"
>$RESULTFILE

#CA内存泄露规避
ConfigFile="/opt/huawei/snas/etc/snas.ini"
NeedCreaPath="/mnt/fs/system/quota_scan"
NeedCreaFile="/mnt/fs/system/quota_scan/quota_scan_0000000000"
LOG_FILE="/var/log/inspect.log"
isPass=0

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

function CheckQuota()
{
    local iRet1=""
    local iRet2=""
    if [ ! -d "${NeedCreaPath}" ];then
        isPass=1
        echo "INFO:the path quota_scan does not exist in File system" >>$RESULTFILE 2>&1
        #规避手段
        mkdir ${NeedCreaPath} >/dev/null 2>&1
        iRet1=$?
        if [ 0 -eq "${iRet1}" ];then
            LOG "[CreateQuota]create ${NeedCreaPath} success"
        fi
	touch "${NeedCreaFile}" >/dev/null 2>&1
        iRet2=$?
        if [ 0 -eq "${iRet2}" ];then
            LOG "[CreateQuota]create ${NeedCreaFile} success"
        fi
	return

    fi

    if [ ! -f "${NeedCreaFile}" -a -d "${NeedCreaPath}" ];then
        isPass=1
        echo "INFO:the file quota_scan/quota_scan_0000000000 does not exist in File system" >>$RESULTFILE 2>&1
        touch "${NeedCreaFile}" >/dev/null 2>&1
        iRet2=$?
        if [ 0 -eq "${iRet2}" ];then
            LOG "[CreateQuota]create ${NeedCreaFile} success"
        fi
	return
    fi
    
    echo "CA Resource :Normal" >>$RESULTFILE 2>&1

}

function main()
{   
    local checkversion="V300R006C20SPC200"
    product_version=$( grep -w "ProductVersion" /opt/huawei/deploy/package/version |awk -F"=" '{print $2}' | sed 's/ //g' )
    if [ "X${checkversion}" != "X${product_version}" ];then
        echo "INFO:The ProductVersion ${product_version} is not ${checkversion} ,no need to be checked" >>$RESULTFILE 2>&1
        return
    fi
    
    if [ ! -f "${ConfigFile}" ];then
        LOG "The file ${ConfigFile} does not exist.Cannot tell if the current system is a file system."
	echo "INFO:The file ${ConfigFile} does not exist.Cannot tell if the current system is a file system" >>$RESULTFILE 2>&1
	isPass=1
        return
    fi
    node_service_type="$( grep -w "node_service_type" ${ConfigFile} |awk -F"=" '{print $2}' )"
    #集群中存在DFS节点时,即集群集群服务类型为文件系统/S3+文件系统/Swift+文件系统时, 才对/mnt/fs/system 进行操作
    if [ "X1" == "X${node_service_type}" ];then
        CheckQuota
    else
        echo "INFO:The node is not dfs node,no need to be checked." >>$RESULTFILE 2>&1
    fi
}

main
#打印是否巡检通过
echo "${CurInspectFun}_Pass $isPass" >>$RESULTFILE 2>&1
LOG "${CurInspectFun}_Pass $isPass"


