#!/bin/bash

#
#巡检S3FS服务是否正常
#
#

#判断是否不需要巡检该节点，0：不需要巡检； 1：需要巡检
function isNotNeedCheck()
{
    local 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 [ "X$node_service_type" == "X2" -o "X$node_service_type" == "X3" ];then
        return 1;
    fi
}

#巡检S3FS是否正常
#1.判断是否存在/mnt/s3fs, 
#    9000 /obsbilling/var/uds/user/billing/s3fs 
#    UDS  /obsbilling/UDS_data/billing/s3fs
#2.判断obs用户是否能够正常列举这两个文件
function checkS3FSService()
{
    local sMount="/mnt/s3fs"
    local bMount="/obsbilling/var/uds/user/billing/s3fs"
    
    pname=`cat /opt/huawei/snas/etc/snas.ini | grep 'productType=' | awk -F '=' '{print $2}'`
    if [ "X$pname" == "X80" ]; then
        bMount="/obsbilling/UDS_data/billing/s3fs"
    fi
    
    local isPass=0
    local lsS3FS=1
    local lsBILL=1
    local mS3FS=1
    local mBILL=1
    local nNum=

   
    nNum=$(mount |grep "${bMount}" -c)
    if [ $nNum == 0 ];then
        isPass=1
        echo "[ERR]the BILL ${bMount} mount failed ,success mount num is 0.ERRCODE(4)"
        mBILL=0
    fi

    nNum=$(mount |grep "${sMount}" -c)
    if [ $nNum == 0 ];then
        isPass=1
        echo "[ERR]the S3FS ${sMount} mount failed ,success mount num is 0.ERRCODE(5)"
        mS3FS=0
    fi

    su - obs -c "ls ${bMount}" 2> /dev/null
    if [ $? -ne 0 ];then
        isPass=1
        echo "[ERR]the BILL ${bMount} ls failed , return($?).ERRCODE(6)"
        lsBILL=0
    fi
    
    su - obs -c "ls ${sMount}" 2> /dev/null
    if [ $? -ne 0 ];then
        isPass=1
        echo "[ERR]the S3FS ${sMount} ls failed , return($?).ERRCODE(7)"
        lsS3FS=0
    fi
    
    
    
    echo "isPass:$isPass"
    echo "s3fs:$mS3FS:$lsS3FS" 
    echo "bill:$mBILL:$lsBILL"
    echo "bPath:$bMount"
    echo "sPath:$sMount" 
}

#如果不需要检查，直接退出,同时输出isCheck:0,代表不是S3节点,没有做检查
if isNotNeedCheck;then
    echo "isCheck:0"
    exit 0
fi

echo "isCheck:1"

#检查服务
checkS3FSService
