#!/bin/bash
#space在minisystem下的命令脚本

#全局变量定义
ISOLATION_FILE_PATH="/OSM/coffer_data/space_isolation_lun_list"
ISOLATION_FILE_NAME="space_isolation_lun_list"
OPERATION_CODE_CREATEFILE="0x100F000B0037"
OPERATION_CODE_DELETEFILE="0x100F000B0038"
OPERATION_CODE_ADDLUNID="0x100F000B0039"
OPERATION_CODE_DELETELUNID="0x100F000B003A"
OPERATION_TYPE="0"

#用户信息
LOG_USER_NAME=""
LOG_IP=""

#检查是否已经加载系统控制KO
CHECK_KO_INSMOD()
{
	L_INSMOD_OSPSYS=`lsmod |awk '{print $1}' | grep 'lun_ctrl'`
	if [ -z "$L_INSMOD_OSPSYS" ]; then
		echo 'The lun_ctrl module is not insmod, the command do not execute.'
		exit 1
	fi
}

#清除IP和用户名信息
SPACE_CLEARSESSIONUSERINFO()
{
	LOG_USER_NAME=""
	LOG_IP=""
}
#获取IP和用户名信息
SPACE_GETSESSIONUSERINFO()
{
	local L_RET

	L_RET=$(who am i)
	LOG_USER_NAME=$(echo "$L_RET" | awk -F "[ ]" '{print $1}')
	LOG_IP=$(echo "$L_RET" | awk -F "[()]" '{print $2}')
}
#发送alarm告警
SPACE_SENDALARMABOUTISOLATIONLUN()
{
	SPACE_GETSESSIONUSERINFO
	
	if [ ! -n "$2" ]; then
		L_EXEC_FLAG="AlarmSendLongCmd sendLongAlarm $1 $OPERATION_TYPE $LOG_USER_NAME:$LOG_IP,$ISOLATION_FILE_NAME"
	else
		L_EXEC_FLAG="AlarmSendLongCmd sendLongAlarm $1 $OPERATION_TYPE $LOG_USER_NAME:$LOG_IP,$2,$ISOLATION_FILE_NAME"
	fi
	
	echo -e "${L_EXEC_FLAG}\nexit\n" | diagnose --auto >> /dev/null
	
	SPACE_CLEARSESSIONUSERINFO
}

#设置批量切LUN的个数
LUNCTRL_SETLOOPSWITCHLUNNUM()
{
	L_EXEC_FLAG="lunctrl setloopswitchlunnum $1"
	echo -e "${L_EXEC_FLAG}\nexit\n" | diagnose --auto
}

#重置批量切LUN的个数
LUNCTRL_RESETLOOPSWITCHLUNNUM()
{
	L_EXEC_FLAG="lunctrl resetloopswitchlunnum"
	echo -e "${L_EXEC_FLAG}\nexit\n" | diagnose --auto
}

#从隔离LUN ID统计文件中删除某个隔离LUN
LUNCTRL_DELETEISOLATIONLUN()
{
	local LUN_ID
	local QUERY_LUN_ID

	LUN_ID="$1"
	if [ -f $ISOLATION_FILE_PATH ]; then
	
		if [ ! -n "$LUN_ID" ]; then
			echo "Cannot execute command, [lunid] argument is null."
			exit
		fi

		QUERY_LUN_ID=`grep -w "$LUN_ID" $ISOLATION_FILE_PATH`
		
		if [ "$QUERY_LUN_ID" == "$LUN_ID" ]; then
		
			sed -i "/\<$LUN_ID\>/d" $ISOLATION_FILE_PATH
			SPACE_SENDALARMABOUTISOLATIONLUN $OPERATION_CODE_DELETELUNID $LUN_ID
			
			echo "Delete LUN ID $LUN_ID success."
			exit
			
		else
			echo "The specified [lunid] does not exist."
			exit
		fi

	else
		echo "The configuration file does not exist."
		exit
	fi
}

#查询隔离LUN ID统计文件中隔离LUN ID信息
LUNCTRL_SHOWISOLATIONLUN()
{
	if [ -f $ISOLATION_FILE_PATH ]; then
		echo "ISOLATION_LUN_ID_LIST"
		cat $ISOLATION_FILE_PATH
		exit
	else
		echo "The configuration file does not exist."
		exit
	fi
}

#修改或增加隔离LUN ID统计文件中隔离LUN ID信息
LUNCTRL_ADDISOLATIONLUN()
{
	local LUN_ID
	local QUERY_LUN_ID

	LUN_ID="$1"
	if [ -f $ISOLATION_FILE_PATH ]; then
	
		if [ ! -n "$LUN_ID" ]; then
			echo "Cannot execute command, [lunid] argument is null."
			exit
		fi
		
		QUERY_LUN_ID=`grep -w "$LUN_ID" $ISOLATION_FILE_PATH`
		
		if [ "$QUERY_LUN_ID" == "$LUN_ID" ]; then
			echo "The specified [lunid] already exists. "
			exit
		else
		
			echo "$LUN_ID" >> $ISOLATION_FILE_PATH
			SPACE_SENDALARMABOUTISOLATIONLUN $OPERATION_CODE_ADDLUNID $LUN_ID
			
			echo "Add LUN ID $LUN_ID success."
			exit
			
		fi
		
	else
		echo "The configuration file does not exist."
		exit
	fi
}

#删除隔离LUN ID统计文件
LUNCTRL_DELETEISOLATIONLUNIDFILE()
{
	if [ -f $ISOLATION_FILE_PATH ]; then
	
		rm $ISOLATION_FILE_PATH
		SPACE_SENDALARMABOUTISOLATIONLUN $OPERATION_CODE_DELETEFILE

		echo "Delete configuration file success."
		exit
	else
		echo "The configuration file does not exist."
		exit
	fi
}

#创建隔离LUN ID统计文件
LUNCTRL_CREATEISOLATIONLUNIDFILE()
{
	if [ ! -f $ISOLATION_FILE_PATH ]; then
	
		touch $ISOLATION_FILE_PATH
		SPACE_SENDALARMABOUTISOLATIONLUN $OPERATION_CODE_CREATEFILE

		echo "Create configuration file success."
		exit
	else
		echo "The configuration file already exists."
		exit
	fi
}

#修改LUN相关DB表
LUNCTRL_CHANGEDBTABLE()
{
	L_EXEC_FLAG="lunctrl changedbtable $1 $2 $3 $4"
	echo -e "${L_EXEC_FLAG}\nexit\n" | diagnose --auto
}

function SPACE_Setunmapsize() {
        L_EXEC_FLAG="econf monmodifycfg $1 $2 $3"
    	echo -e "${L_EXEC_FLAG}\nexit\n" | diagnose --auto
}

#检查函数是否加载
CHECK_KO_INSMOD

while getopts ":ha" options
do
    case "$options" in
        h)	
            echo -e "space.sh [-h] | [-a] : It is just a space control command. Enter "space.sh -a" to obtain more information."
			exit 0
			;;
	a) 
            echo -e "space.sh setloopswitchlunnum         : Modify loop switch lun num(Range: [1,100], default: 100),usage: space.sh setloopswitchlunnum [num]."
            echo -e "space.sh resetloopswitchlunnum       : Reset loop switch lun num(default: 100),usage: space.sh resetloopswitchlunnum."
            echo -e "space.sh createisolationlunidfile    : Create the configuration file,usage: space.sh createisolationlunidfile."
            echo -e "space.sh deleteisolationlunidfile    : Delete the configuration file,usage: space.sh deleteisolationlunidfile."
            echo -e "space.sh showisolationlun            : Show isolation lun id infomation,usage: space.sh showisolationlun."
            echo -e "space.sh deleteisolationlun [lunid]  : Delete isolation lun [lunid](such as: 128 or 0x80),usage: space.sh deleteisolationlun [lunid]."
            echo -e "space.sh addisolationlun [lunid]     : Add isolation lun [lunid](such as: 128 or 0x80),usage: space.sh addisolationlun [lunid], If lunid already exists, do not add it repeatedly."
            echo -e "space.sh changedbtable [tablename] [objid] [field name] [value]     : Change DB table field value,usage: space.sh changedbtable [tablename] [objid] [field name] [value]."
	    echo -e "space.sh setunmapsize                : Set unmap size(Range: [1,16] unit:MB, default: 16), usage: space.sh setunmapsize GLOBAL space_unmapsize [size]."
            exit 0
			;;		    
     	\?)
            echo "usage: space.sh -h"
			exit 1
                        ;;
    esac
done

case "$1" in
	setloopswitchlunnum)
		LUNCTRL_SETLOOPSWITCHLUNNUM $2
		exit 0
	;;
	resetloopswitchlunnum)
		LUNCTRL_RESETLOOPSWITCHLUNNUM
		exit 0
	;;
	createisolationlunidfile)
		LUNCTRL_CREATEISOLATIONLUNIDFILE
		exit 0
	;;
	deleteisolationlunidfile)
		LUNCTRL_DELETEISOLATIONLUNIDFILE
		exit 0
	;;
	showisolationlun)
		LUNCTRL_SHOWISOLATIONLUN
		exit 0
	;;
	addisolationlun)
		LUNCTRL_ADDISOLATIONLUN $2
		exit 0
	;;
	deleteisolationlun)
		LUNCTRL_DELETEISOLATIONLUN $2
		exit 0
	;;
	changedbtable)
		LUNCTRL_CHANGEDBTABLE $2 $3 $4 $5
		exit 0
	;;
	setunmapsize)
		SPACE_Setunmapsize $2 $3 $4
		exit 0
        ;;
    *)
        echo "usage: space.sh -h"
        exit 1
esac

exit 1
