#!/bin/bash

#检查前端ip网络地址规划
G_INSPECT_MMLPATH="/opt/huawei/snas/script/inspect_mml"
source ${G_INSPECT_MMLPATH}/CheckItems
CurInspectNum="353"
CurInspectFun="$( GetInspectType $CurInspectNum )"
RESULTFILE="/tmp/tmpResult${CurInspectFun}"
>${RESULTFILE}


LOG_FILE="/var/log/inspect.log"

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

function checkfrontip()
{
    local isPass=0
    local cm_db_file="/opt/huawei/snas/etc/cm_conf.db"
    if [ ! -f "${cm_db_file}" ];then
        isPass=1
        echo "INFO:the file ${cm_db_file} does not exist.The Cluster is abnormal." >>${RESULTFILE} 2>&1
        echo "${CurInspectFun}_Pass ${isPass}" >>${RESULTFILE} 2>&1
        LOG "[$FUNCNAME]${CurInspectFun}_Pass ${isPass},the file ${cm_db_file} does not exist."
        return
    fi
    
    node_num=$( /usr/bin/sqlite3 ${cm_db_file} "select * from CM_NODE_T" | wc -l )
    frontip_num=$( /usr/bin/sqlite3 ${cm_db_file} "select * from CM_NODE_IP_T where SUBNET_ID=1" | wc -l )
    if [ "X${node_num}" == "X0" -o "X${frontip_num}" == "X0" ];then
        isPass=1
        echo "INFO:Get Cluster information failed.The Cluster node number is ${node_num},the cluster front ip number is ${frontip_num}." >>${RESULTFILE} 2>&1
        echo "${CurInspectFun}_Pass ${isPass}" >>${RESULTFILE} 2>&1
        LOG "[$FUNCNAME]${CurInspectFun}_Pass ${isPass},The Cluster node number is ${node_num},the cluster front ip number is ${frontip_num}."
        return
    fi
    echo "NodeNum:${node_num}||FrontIpNum:${frontip_num}" >>${RESULTFILE} 2>&1
    quotient=$(( ${frontip_num} / ${node_num} ))
    remainder=$(( ${frontip_num} % ${node_num} ))
    if [ "X${quotient}" != "X2" -o "X${remainder}" != "X0" ];then
        isPass=1
        echo "INFO:The number of frontip of the cluster is not double the number of nodes." >>${RESULTFILE} 2>&1
        echo "${CurInspectFun}_Pass ${isPass}" >>${RESULTFILE} 2>&1
        LOG "[$FUNCNAME]${CurInspectFun}_Pass ${isPass},The number of frontip of the cluster is not double the number of nodes,NodeNum=${node_num},FrontIpNum=${frontip_num}."
        return
    fi
    echo "${CurInspectFun}_Pass ${isPass}" >>${RESULTFILE} 2>&1
    LOG "[$FUNCNAME]${CurInspectFun}_Pass ${isPass},NodeNum=${node_num},FrontIpNum=${frontip_num}."
    return
}

checkfrontip
