#!/bin/bash

#
#检查话单服务是否正常
#

#需要检查的两个路径
origDir="/obsbilling/var/uds/user/billing/s3fs/cdrfiles/orig"
storDir="/obsbilling/var/uds/user/billing/s3fs/cdrfiles/storage"
pname=`cat /opt/huawei/snas/etc/snas.ini | grep 'productType=' | awk -F '=' '{print $2}'`
if [ "X$pname" == "X80" ]; then
    origDir="/obsbilling/UDS_data/billing/s3fs/cdrfiles/orig"
    storDir="/obsbilling/UDS_data/billing/s3fs/cdrfiles/storage"
fi

dirPerm="700"
dirOwner="obsbilling:obsgrp"

isPass=0
permOrig=1
permStor=1
ownerOrig=1
ownerStor=1

#判断是否进行该检查，返回0，不需要，返回1，需要检查
#1.检查是否是S3节点
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" ];then
        source /opt/obs/scripts/common/s3_config_utility.sh >>/dev/null
        is_s3_management_node > /dev/null
        if [ $? -eq 0 ];then
            return 0;
        else
            return 1;
        fi
    else
        return 0;
    fi
}

#检查话单功能是否正常
function checkBILLService()
{
    local tmp=
    local isPass=0
    local origPass=1
    local storPass=1

    tmp=$(stat -c %a "$origDir" 2>>/dev/null)
    if [ "X$tmp" != "X$dirPerm" ];then
        isPass=1
        echo "[ERR]The file $origDir permissions($tmp) is not $dirPerm.ERRCODE(4)"
        origPass=0
    fi 
    tmp=$(stat -c %a "$storDir" 2>>/dev/null)
    if [ "X$tmp" != "X$dirPerm" ];then
        isPass=1
        echo "[ERR]The file $storDir permissions($tmp) is not $dirPerm.ERRCODE(4)"
        storPass=0
    fi 
    tmp=$(stat -c %U:%G "$origDir" 2>>/dev/null)
    if [ "X$tmp" != "X$dirOwner" ];then
        isPass=1
        echo "[ERR]The file $origDir Affiliated groups($tmp) is not $dirOwner.ERRCODE(5)"
        origPass=0
    fi 
    tmp=$(stat -c %U:%G "$storDir" 2>>/dev/null)
    if [ "X$tmp" != "X$dirOwner" ];then
        isPass=1
        echo "[ERR]The file $storDir Affiliated groups($tmp) is not $dirOwner.ERRCODE(5)"
        storPass=0
    fi

    ls ${origDir} > /dev/null 2>&1
    if [ $? -ne 0 ];then
        isPass=1
        echo "[ERR]Failed to execute command(ls ${origDir} ).ERRCODE(6)"
        origPass=0
    fi

    ls ${storDir} > /dev/null 2>&1
    if [ $? -ne 0 ];then
        isPass=1
        echo "[ERR]Failed to execute command(ls ${storDir} ).ERRCODE(6)"
        storPass=0
    fi

    echo "isPass:$isPass"
    echo "origPass:$origPass"
    echo "storPass:$storPass"
}

#如果不需要检查，则直接退出脚本执行,输出isCheck:0
if isNotNeedCheck;then
    echo "isCheck:0"
    exit 0;
fi

#未开启BILL功能
openBill=$(cat /opt/obs/obsconf/obs_sod.properties |grep billingOpen|sed "s/[[:space:]]//g"|awk -F "=" '{print $2}')
if [ "X$openBill" != "Xtrue" ];then
    echo "isCheck:1"
    echo "isPass:0"
    echo "origPass:1"
    echo "storPass:1"
    exit 0;
fi

#开始执行检查 
echo "isCheck:1" 
checkBILLService
