#!/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

G_INSPECT_MMLPATH="/opt/huawei/snas/script/inspect_mml"
source $G_INSPECT_MMLPATH/CheckItems
CurInspectNum="269"
CurInspectFun="$(GetInspectType $CurInspectNum)"
RESULTFILE="/tmp/tmpResult${CurInspectFun}"
>${RESULTFILE}
LOG_FILE="/var/log/inspect.log"
CHECK_PASSED=0

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

#判断是否进行该检查，返回0，不需要，返回1，需要检查
#1.检查是否是S3节点
function isNotNeedCheck()
{
	local multiazLSRole=$(cat /opt/huawei/snas/etc/multiaz.ini | grep "LSRole" | awk -F"=" '{print $2}'| tr -d "")
    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 [ "$multiazLSRole" = "0" ]; then
		return 0;
	fi
	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
        origPass=0
    fi
    tmp=$(stat -c %a "$storDir" 2>>/dev/null)
    if [ "X$tmp" != "X$dirPerm" ];then
        isPass=1
        storPass=0
    fi
    tmp=$(stat -c %U:%G "$origDir" 2>>/dev/null)
    if [ "X$tmp" != "X$dirOwner" ];then
        isPass=1
        origPass=0
    fi
    tmp=$(stat -c %U:%G "$storDir" 2>>/dev/null)
    if [ "X$tmp" != "X$dirOwner" ];then
        isPass=1
        storPass=0
    fi

    ls ${origDir} > /dev/null 2>&1
    if [ $? -ne 0 ];then
        isPass=1
        origPass=0
    fi

    ls ${storDir} > /dev/null 2>&1
    if [ $? -ne 0 ];then
        isPass=1
        storPass=0
    fi

    if [ 0 -eq ${origPass} ];then
        echo "[ERR]INFO:Check Operation-Specific CDR path failed" >> ${RESULTFILE} 2>&1
    else
        echo "INFO:Check Operation-Specific CDR path success" >> ${RESULTFILE} 2>&1
    fi

    if [ 0 -eq ${storPass} ];then
        echo "[ERR]INFO:Check Storage-Specific CDR path failed" >> ${RESULTFILE} 2>&1
    else
        echo "INFO:Check Storage-Specific CDR path success" >> ${RESULTFILE} 2>&1
    fi

    echo "${CurInspectFun}_Pass ${isPass}" >>${RESULTFILE} 2>&1
    LOG "[$LINENO]origPass:${origPass} storPass:${storPass} isPass:${isPass}."
}

#如果不需要检查，则直接退出脚本执行,输出isCheck:0
if isNotNeedCheck;then
    echo "INFO:Does not involve" >> ${RESULTFILE} 2>&1
    echo "${CurInspectFun}_Pass ${CHECK_PASSED}" >>${RESULTFILE} 2>&1
    LOG "[$LINENO]not need check, pass."
    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 "INFO:Check Operation-Specific CDR path success" >> ${RESULTFILE} 2>&1
    echo "INFO:Check Storage-Specific CDR path success" >> ${RESULTFILE} 2>&1
    echo "${CurInspectFun}_Pass ${CHECK_PASSED}" >>${RESULTFILE} 2>&1
    exit 0;
fi

#开始执行检查

checkBILLService
