#!/bin/bash
set +x

#version:1.0

dir_path="/opt/huawei/snas/script/inspect_mml"
ds_key="DS"
nlun_key="Primary"
ds_map="/proc/monc_dsmap"
nlun_map="/var/log/nluninfo.log"
layout_path="/var/log/layout"
incons_file="/home/inconsfile"
ds_list="/var/log/ds_list"
RESULT_PATH="/var/log/check_result"
log_file="/var/log/check_consistent.log"
SNAS_CONFIG_PATH="/opt/huawei/snas/etc/snas.ini"

g_ds_id=0
g_done_cnt=0
g_start_time=0
g_end_time=0
g_interval_time=0
#2G
g_file_size=2097152
g_back_ip=0

DSID_GET_ERR=1
NLUN_GET_ERR=2
PRI_CHNG_ERR=3
OTHER_ERR=4

function print_log()
{
    description=$1

    echo "["`date +%Y-%m-%d-%H:%M:%S`"][$description]" >> $log_file
}

function get_local_node_info()
{
    g_back_ip=`grep ipaddr_1 $SNAS_CONFIG_PATH | awk -F "=" '{print $2}'`

    cat $ds_map | grep "$ds_key"  | grep -v "\*\*\*\*" > $ds_list
	
    g_ds_id=`cat $ds_list | grep "$g_back_ip " | awk '{print $2}'`
}

function get_nlun_info()
{
    MmlBatch 4004 "logfc set logflowctrl 8 0" > /dev/null
    
    sleep 0.0000001
    
    MmlBatch 988 "monc nluninfo 0" > /dev/null
    
    sleep 0.0000001

    MmlBatch 4004 "logfc set logflowctrl 8 1" > /dev/null
    
    sleep 0.0000001
}

function get_file_dis()
{
    MmlBatch 988 "show filedis $g_file_path" > /dev/null
    
    sleep 0.0000001
    
    sort -t'(' -k6,7 -u ${layout_path}/${g_file_name}.${g_inode_id}.layout > ${layout_path}/${g_file_name}.${g_inode_id}.layout.uniq
}

function check_result()
{
    grep "check consfile" $incons_file > /dev/null
    ret=$?
    if [ $ret -eq 0 ];then
        echo "check finish, some file unequal, please look up $incons_file."
        exit 1
    fi

    echo "Check finish, all file equal."
    exit 0
}

function check_if_end()
{
    g_end_time=`date +"%Y-%m-%d %H:%M:%S"`
    
    start_time=`date -d  "$g_start_time" +%s`
    end_time=`date -d  "$g_end_time" +%s`
    #hour
    interval=`echo "$(($end_time-$start_time))/60" | bc`   

    file_size=`ls -l $incons_file -k | awk '{print $5}'`	

    if [ $g_done_cnt -ge $g_check_cnt -o $interval -ge $g_interval_time -o $file_size -ge $g_file_size ];then
        print_log "scan end done[$g_done_cnt] interval[$interval] filesize[$file_size]."	
        return 0
    fi
    
    return $OTHER_ERR
}

function check_one_object()
{
    nlun=$1
    object=$2
    
    $dir_path/check_expect $g_back_ip $nlun $object > ${RESULT_PATH} 
    sed -i -e 's/\x1B\[0;[3-4][0-9]m//g' -e 's/\x0D//g' -e 's/\x00//g' ${RESULT_PATH} >/dev/null 2>&1
    chmod 640 ${RESULT_PATH}
    cat ${RESULT_PATH} | grep "can not find primary pool in this DS" > /dev/null
    if [ $? -eq 0 ];then
        return $PRI_CHNG_ERR
    fi
	
    sleep 0.000001
	
    return 0
}

function check_consistent()
{
    g_start_time=`date +"%Y-%m-%d %H:%M:%S"`
    
    while read object
    do
        obj_id=`echo $object | awk -F'[()]' '{print $10}'`
        if [ "$obj_id" == "" ];then
            continue
        fi
			
        nlun_id=`echo $object | awk -F'[()]' '{print $12}'`
        if [ "$nlun_id" == "" ];then
            continue
        fi

        ds_id=`cat $nlun_map | grep "$nlun_id NewPt" | awk -F'[()]' '{print $2}' |awk '{print $1}'`
        if [ "$ds_id" != "$g_ds_id" ];then
            continue
        fi

        check_one_object $nlun_id $obj_id 
        if [ $? -ne 0 ];then
            print_log "$nlun_id primary change to other DS."			  
        fi		
			
        sleep 0.000001
    done < ${layout_path}/${g_file_name}.${g_inode_id}.layout.uniq
    
    #check_result
}

function main()
{
    get_local_node_info
    if [ "$g_ds_id" == "0" ];then
        print_log "get local node info fail."
        exit $DSID_GET_ERR
    fi

    get_nlun_info
    
    get_file_dis

    check_consistent   
   
    rm -f ${ds_list}
    rm -f ${layout_path}/${g_file_name}.${g_inode_id}.layout
    rm -f ${layout_path}/${g_file_name}.${g_inode_id}.layout.uniq
}

if [ $# -ne 2 ] ;then
    echo "check_file_consistent filepath inodeid."
    exit $OTHER_ERR
fi  

g_file_path=$1
g_inode_id=$2
g_file_name=`echo "$g_file_path" | awk -F'/' '{print $NF}'`

main

