#!/bin/bash
source /OSM/script/os_load_base.sh
source /OSM/script/ocean_load_base.sh

nfs41_switch_need_on=0
#查看CCDB数据库租户0下NFS4.1开关状态
nfs41_ccdb_switch=$(sudo nas_proto_cfg -c get_nfs41_switch -z 0 | grep value |awk -F '>' '{print $2}'|awk -F '<' '{print $1}')

#租户0下数据库开关为开的场景下，不需要操作
if [[ $nfs41_ccdb_switch = "on" ]] ; then
    OS_echo "NFS41 Tenant 0 ccdb switch on, no need check."
    exit 0;
fi

#获取内核态租户0的NFS4.1开关
nfs41_kernel_switch=$(sudo nas_proto_pum -m nfs -c get_tenant_nfs41_vers -v 0 | grep -v Nfsv41_switch | awk '{print $2}')
#租户0下数据库开关为为关，且内核也为关的场景下，不需要操作
if [[ "$nfs41_ccdb_switch" =  "off" ]] && [[ $nfs41_kernel_switch = "0" ]] ; then
    OS_echo "NFS41 Tenant 0 ccdb switch off kernel switch 0, no need check."
    exit 0;
fi

#获取normal控制器列表
normal_controller_list=$(echo -e "sys showcls\nexit\n" |diagnose  --auto | grep "normal              " | grep -v mode | awk '{print $1}')
if [[ "$normal_controller_list" = "" ]] ; then
    OS_echo "All controller abnormal."
    exit 0;
fi
#查看所有控制器上是否存在NFS4.1业务
for controller in $normal_controller_list
do
    IBC_IP=`OS_GetIPBySlotId $controller`
    if [ $? -ne 0 ] ;then
        OS_echo "Controller $controller get ip failed."
        continue;
    fi
    #查看每个控制器上的session历史统计值
    session_count=$(sudo ssh ibc_os_hs@$IBC_IP < /dev/null "echo -e "cmm show pr NAS_PID_NFS41_SESSION'\\'nexit'\\'n" | \/OSM\/script\/diagnose --auto " | grep "IN ALL" | awk 'NR==1' | awk '{print $4}')
    OS_echo "Controller $controller ibc ip $IBC_IP nfsv41 session count $session_count."
    if [[ $session_count -gt 0 ]] ; then
       nfs41_switch_need_on=1;
       break;
    fi
done

OS_echo "Tenant 0 NFS41 CCDB switch $nfs41_ccdb_switch nfs41_kernel_switch $nfs41_kernel_switch nfs41_switch_need_on $nfs41_switch_need_on."
if [[ "$nfs41_ccdb_switch" ==  "off" ]] && [[ "$nfs41_kernel_switch" = "1" ]] && [[ $nfs41_switch_need_on -gt 0 ]] ;then
    set_switch_result=$(sudo nas_proto_cfg -c set_nfs41_switch -z 0 -v on | grep statusdesc |awk -F '>' '{print $2}'|awk -F '<' '{print $1}');
    OS_echo "Change tenant 0 nfsv41 switch on $set_switch_result."
fi
