#!/bin/bash

G_REMOTE_TMP_FILE="/tmp/tmpfileNodeIO"
G_REMOTE_TMP_FILE1="/tmp/tmpfileNodeIO1"
LOG_FILE="/var/log/inspect.log"
isPass=0
failedInfo="-"
Result=""
NFSFlag=0
CIFSFlag=0

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

getdiff()
{
    local outvalue=0

    if [ -z "$1" -o -z "$2" ];then
        echo "${failedInfo}"
        return
    fi
    outvalue=$(($2-$1))
    if [ $outvalue -lt 0 ];then
        outvalue=0
    fi

    echo $outvalue
}

checkIOPerform()
{
    local IOfile=$2
    local IOfile1=$3

    rcount=`cat $IOfile |grep "read count"|awk -F":" '{print $2}'`
    wcount=`cat $IOfile |grep "write count"|awk -F":" '{print $2}'`
    ccount=`cat $IOfile |grep "cmd count"|awk -F":" '{print $2}'`
    rbytes=`cat $IOfile |grep "read bytes"|awk -F":" '{print $2}'`
    wbytes=`cat $IOfile |grep "write bytes"|awk -F":" '{print $2}'`

    maxcalldelay=`cat $IOfile |grep "max call delay"|awk -F":" '{print $2}'`
    maxreaddelay=`cat $IOfile |grep "max read delay"|awk -F":" '{print $2}'`
    maxwritedelay=`cat $IOfile |grep "max write delay"|awk -F":" '{print $2}'`

    #ȡڶ
    rcount1=`cat $IOfile1 |grep "read count"|awk -F":" '{print $2}'`
    wcount1=`cat $IOfile1 |grep "write count"|awk -F":" '{print $2}'`
    ccount1=`cat $IOfile1 |grep "cmd count"|awk -F":" '{print $2}'`
    rbytes1=`cat $IOfile1 |grep "read bytes"|awk -F":" '{print $2}'`
    wbytes1=`cat $IOfile1 |grep "write bytes"|awk -F":" '{print $2}'`

    maxcalldelay1=`cat $IOfile1 |grep "max call delay"|awk -F":" '{print $2}'`
    maxreaddelay1=`cat $IOfile1 |grep "max read delay"|awk -F":" '{print $2}'`
    maxwritedelay1=`cat $IOfile1 |grep "max write delay"|awk -F":" '{print $2}'`

    #ȡֵ
    rcount2=`getdiff $rcount1 $rcount`
    wcount2=`getdiff $wcount1 $wcount`
    ccount2=`getdiff $ccount1 $ccount`
    rbytes2=`getdiff $rbytes1 $rbytes`
    wbytes2=`getdiff $wbytes1 $wbytes`
    if [ "$rcount2" = "${failedInfo}" -o "$wcount2" = "${failedInfo}" -o "$ccount2" = "${failedInfo}" -o "$rbytes2" = "${failedInfo}" -o "$wbytes2" = "${failedInfo}" ]; then
        echo "[ERR]Protocol($1) There are empty fields(-) in fields(Read count,Write count,Cmd count,Read bytes,Write bytes).ERRCODE(6)"
    fi

    maxcalldelay2=$maxcalldelay1
    maxreaddelay2=$maxreaddelay1
    maxwritedelay2=$maxwritedelay1

    result="Protocol:$1||Read count:${rcount2}||Write count:${wcount2}||Cmd count:${ccount2}||Read bytes:${rbytes2}||Write bytes:${wbytes2}||"
    result=${result}"||max call delay(ms):${maxcalldelay2}||max read delay(ms):${maxreaddelay2}||max write delay(ms):${maxwritedelay2}"

    echo "$result"

    return 0
}

#ȡڵ洢
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 [ "${node_service_type}" != "1" ];then
	#S3 
	LOG "node_service_type :${node_service_type}."
	exit 0
fi

[ -f "$G_REMOTE_TMP_FILE[CIFS]" ] && rm -f "$G_REMOTE_TMP_FILE[CIFS]"
[ -f "$G_REMOTE_TMP_FILE[NFS]" ] && rm -f "$G_REMOTE_TMP_FILE[NFS]"
[ -f "$G_REMOTE_TMP_FILE1[CIFS]" ] && rm -f "$G_REMOTE_TMP_FILE1[CIFS]"
[ -f "$G_REMOTE_TMP_FILE1[NFS]" ] && rm -f "$G_REMOTE_TMP_FILE1[NFS]"
#ȡCIFSIOͳ
/usr/local/bin/nas_proto_pum -m smb2 -c io_perform > $G_REMOTE_TMP_FILE[CIFS] 2>&1
if [ $? -ne 0 ]; then
    CIFSFlag=1
fi
#ȡNFSIO
/usr/local/bin/nas_proto_pum -m nfs -c io_perform > $G_REMOTE_TMP_FILE[NFS] 2>&1
if [ $? -ne 0 ]; then
    NFSFlag=1
fi

sleep 1

#ȡCIFSIOͳ
/usr/local/bin/nas_proto_pum -m smb2 -c io_perform > $G_REMOTE_TMP_FILE1[CIFS] 2>&1
if [ $? -ne 0 ]; then
    CIFSFlag=1
fi
#ȡNFSIO
/usr/local/bin/nas_proto_pum -m nfs -c io_perform > $G_REMOTE_TMP_FILE1[NFS] 2>&1
if [ $? -ne 0 ]; then
    NFSFlag=1
fi

#
if [ $CIFSFlag -eq 0 ]; then
    checkIOPerform "CIFS" $G_REMOTE_TMP_FILE1[CIFS] $G_REMOTE_TMP_FILE[CIFS]
else
    echo "[ERR]get IO data of cifs failed.ERRCODE(4)"
fi
if [ $NFSFlag -eq 0 ]; then
    checkIOPerform "NFS" $G_REMOTE_TMP_FILE1[NFS] $G_REMOTE_TMP_FILE[NFS]
else
    echo "[ERR]get IO data of nfs failed.ERRCODE(5)"
fi
[ -f "$G_REMOTE_TMP_FILE[CIFS]" ] && rm -f "$G_REMOTE_TMP_FILE[CIFS]"
[ -f "$G_REMOTE_TMP_FILE[NFS]" ] && rm -f "$G_REMOTE_TMP_FILE[NFS]"
[ -f "$G_REMOTE_TMP_FILE1[CIFS]" ] && rm -f "$G_REMOTE_TMP_FILE1[CIFS]"
[ -f "$G_REMOTE_TMP_FILE1[NFS]" ] && rm -f "$G_REMOTE_TMP_FILE1[NFS]"
#Ƿسɹ[ERR]ʶǷ
exit 0

