#!/bin/bash

source /OSM/script/os_load_base.sh

G_CCDB_EMERGENCY_CMD=("clearccdb" "cleardbfile" "cleardblog" "showdbinfo" "stopccdb" "showmd" "recoverymd" "operdb" "exportdb" "repairdb" "showdb" "setccdbdirtyflag" "resetccdbdirtyflag" "clearccdbblkraidinfo" "removemddisk" "setfaultdisk" "opermd" "showccdbraiddisk" "showlocalinfo" "preparedb" "backupalldbemergency" "operdbrecover" "operccdbserver")
G_CCDB_STORE_DIR="/OSM/ccdb/store"
G_CCDB_BACKUP_DIR="/OSM/coffer_data/ccdb/ccdb_backup"
#防呆白名单
G_CCDB_VERIFICATION_EMERGENCY_CMD=("clearccdb" "cleardbfile" "operdb" "recoverymd" "repairdb" "setccdbdirtyflag" "resetccdbdirtyflag" "clearccdbblkraidinfo" "opermd" "showlocalinfo" "preparedb" "backupalldbemergency" "operdbrecover" "operccdbserver")

#=============================================================================
# Function Name: CCDB_CMD_HELP
# Description  : 检查命令是否为高危命令
# Parameter    : cmd
# Returns      : 1-是; 0-否
#=============================================================================
function CCDB_CMD_HELP()
{
    echo -e "ccdb.sh -c clearccdb [local | peer | all]                                           : clear local-engine ccdb data."	    
    echo -e "ccdb.sh -c cleardbfile [dbName]                                                     : clear local-engine db file."            
    echo -e "ccdb.sh -c cleardblog [dbName]                                                      : clear local-engine db log file."  
    echo -e "ccdb.sh -c exportdb                                                                 : force export db file to '/home/permitdir/export_import'." 
    echo -e "ccdb.sh -c operdb [dbName] [cmdpath]                                                : operate local-engine db by SQL."  
    echo -e "ccdb.sh -c showstate                                                                : show ccdb service information."            
    echo -e "ccdb.sh -c showdbinfo                                                               : show all db name." 
    echo -e "ccdb.sh -c showdb [dbName]                                                          : show db information, including version,health degree."  
    echo -e "ccdb.sh -c showbackupdb                                                             : show ccdb backup file."
    echo -e "ccdb.sh -c showmd [online | offline]                                                : show information about block device to be used ccdb." 
    echo -e "ccdb.sh -c startccdb [local | peer | all]                                           : start local-engine ccdb service."
    echo -e "ccdb.sh -c stopccdb [local | peer | all]                                            : stop local-engine ccdb service."
    echo -e "ccdb.sh -c recoverymd [cmdpath] [online | offline]                                  : recovery block device to be used ccdb." 
    echo -e "ccdb.sh -c repairdb [db | all] [backupfile] [dbName]                                : auto repair db by the history backup db file."
    echo -e "ccdb.sh -c showccdbdirtyflag  [mem | disk]                                          : show ccdb dirty flag."
    echo -e "ccdb.sh -c setccdbdirtyflag [nid] [type] [validFlag]                                : set ccdb dirty flag, [type] is [0|1|2],0:nodeFlag,1:nvram, 2:mem."
    echo -e "ccdb.sh -c resetccdbdirtyflag                                                       : restore factory settings."
    echo -e "ccdb.sh -c clearccdbblkraidinfo                                                     : clear ccdb blk raid info."
    echo -e "ccdb.sh -c showccdbblkraidinfo                                                      : show ccdb blk raid info."
    echo -e "ccdb.sh -c closeccdbcrccheck                                                        : close ccdb crc check."
    echo -e "ccdb.sh -c showccdbcrccheck                                                         : show ccdb crc check."
    echo -e "ccdb.sh -c showupgradeinfo                                                          : show upgrade info."
    echo -e "ccdb.sh -c setupgradeloginfo [srcVersion] [dstVersion] [status] [raidFlagMap]       : set upgrade log info."
    echo -e "ccdb.sh -c clearupgradeloginfo                                                      : clear upgrade log info."
    echo -e "ccdb.sh -c setsysupgradepath [srcVersion] [dstVersion]                              : set sys upgrade path."
    echo -e "ccdb.sh -c setnodeversion [nid] [version]                                           : set node version info."
    echo -e "ccdb.sh -c setccdbupgradeinfo                                                       : set ccdb upgrade info."
    echo -e "ccdb.sh -c addupgradeevent                                                          : add upgrade event."
    echo -e "ccdb.sh -c clearlogservice [logId]                                                  : clear log service."
    echo -e "ccdb.sh -c startlogservice [logId]                                                  : start log service."
    echo -e "ccdb.sh -c resetstarttime                                                           : reset ccdb start work delay time."
    echo -e "ccdb.sh -c checkmd                                                                  : check ccdb device md200."
    echo -e "ccdb.sh -c removemddisk [diskId]                                                    : remove disk of md200."	
    echo -e "ccdb.sh -c opermd [cmd]                                                             : operate block device use input command."
    echo -e "ccdb.sh -c showccdbraiddisk                                                         : show ccdb md200 disk info."	
    echo -e "ccdb.sh -c setfaultdisk [diskId]                                                    : set fault disk of md200."
    echo -e "ccdb.sh -c startnvramadapter [nodemap]                                              : start nvram adapter."
    echo -e "ccdb.sh -c showclusterinfo [startWorkInfo | siteCompatibleVersion] [siteId]         : show cluster info."	
    echo -e "ccdb.sh -c showlocalinfo [type] [path]                                              : show db info,'type' is [tree|single|all],when type is single, 'path' is db absolute path, when type is 'tree' or 'all', 'path' is db dir or 'all', dir shou be [cache nvram store ccdb_backup ccdb_backup_protect], 'all' will show all dir info."
    echo -e "ccdb.sh -c preparedb [type] [path]                                                  : prepare for recover db, 'type' is [tar|cp|dump|clear], when type is tar, path shoudld be backup file list, when type is cp, path is running db path, when type is dump, path should in ccdb_tmp dir, when type is clear, clear all temp files."
    echo -e "ccdb.sh -c backupalldbemergency [path]                                              : backup all db in [cache nvram store ccdb_backup ccdb_backup_protect] to path [/OSM/coffer_data/ccdb/ /home/ibc_os_hs/ /home/permitdir/]."
    echo -e "ccdb.sh -c operdbrecover [type] [srcpath] [dstpath]                                 : operation db recover.'type' is [cp|rm], when type is cp, cp srcpath to dstpath, when type is rm, remove srcpath."
    echo -e "ccdb.sh -c operccdbserver [type]                                                    : operation ccdb_server start or stop; 'type' is [start|stop]"
    return
}

#=============================================================================
# Function Name: CCDB_CHECK_CMD_EMERGENCY
# Description  : 检查命令是否为高危命令
# Parameter    : cmd
# Returns      : 1-是; 0-否
#=============================================================================
function CCDB_CHECK_CMD_EMERGENCY()
{    
    L_USER_CMD=$1
    
    for cmd in ${G_CCDB_EMERGENCY_CMD[@]}    
    do        
        if [ "${cmd}" == "${L_USER_CMD}" ]; then            
            return 1        
        fi    
    done    
    
    return 0
}

#=============================================================================
# Function Name: CCDB_CHECK_SERVICE_STOP
# Description  : 检查命令是否为高危命令
# Parameter    : cmd
# Returns      : 1-是; 0-否
#=============================================================================
function CCDB_STOP_SERVICE()
{
    L_OPER_POSITION=$1
    if [ x$L_OPER_POSITION == x"local" ]; then
        OS_echo "[$0]ccdb stop local service."
        CCDB_STOP_LOCAL_SERVER    
        return 0
    elif [ x$L_OPER_POSITION == x"peer" ]; then
        OS_echo "[$0]ccdb stop peer service."
        CCDB_STOP_PEER_SERVER &>/dev/null
        echo "CCDB stop peer service." 
        return 0
    elif [ x$L_OPER_POSITION == x"all" ]; then
        OS_echo "[$0]ccdb stop local-engine service."
        CCDB_STOP_LOCAL_SERVER
        CCDB_STOP_PEER_SERVER &>/dev/null
        echo "CCDB stop peer service." 
        return 0
    else
        echo "param error!"
        OS_echo "[$0]ccdb stop service failed, param $1 error."   
        return 1
    fi
}

#=============================================================================
# Function Name: CCDB_STOP_LOCAL_SERVER
# Description  : 停止本地CCDB服务
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_STOP_LOCAL_SERVER()
{
    L_EXEC_CMD="ccdb stopserver"
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto
    OS_echo "[$0]ccdb stop local service."  
    return
}

#=============================================================================
# Function Name: CCDB_STOP_PEER_SERVER
# Description  : 停止对端CCDB服务
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_STOP_PEER_SERVER()
{
    L_REMOTE_IBC_IP=`OS_GetRemoteIBCIP`
    if [ x"$L_REMOTE_IBC_IP" == x"" ]; then
        echo "peer node offline!"
        OS_echo "[$0]peer node offline!"
        return;
    fi
	
    L_EXEC_CMD="echo -e \"ccdb stopserver local\nexit\n\" | diagnose --auto"
    os_expect_exec_cmd.sh $L_REMOTE_IBC_IP "$L_EXEC_CMD" 5
    OS_echo "[$0]ccdb stop peer service."  
    return
}

#=============================================================================
# Function Name: CCDB_SHOW_STATE
# Description  : 查看CCDB心跳信息及状态机
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_SHOW_STATE()
{
	echo "CCDB cluster heartbeat information:"
	echo "--------------------------------------------"
	L_EXEC_CMD="mon showhb"
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto
	echo " "
	echo "CCDB cluster Finite State Machine information:"
	echo "--------------------------------------------"
    L_EXEC_CMD="mon showsm"
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto
	return
}

#=============================================================================
# Function Name: CCDB_CLOSE_CRC_CHECK
# Description  : 关闭CCDB CRC校验
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_CLOSE_CRC_CHECK()
{
	L_EXEC_CMD="mon setsqlitecrc 0"
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
	return
}

#=============================================================================
# Function Name: CCDB_SHOW_CRC_CHECK
# Description  : 关闭CCDB CRC校验
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_SHOW_CRC_CHECK()
{
	L_EXEC_CMD="mon showsqlitecrc"
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto
	return
}

#=============================================================================
# Function Name: CCDB_SHOW_BACKUP_FILE
# Description  : 查看CCDB备份数据文件
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_SHOW_BACKUP_FILE()
{
    ls $G_CCDB_BACKUP_DIR
    return
}

#=============================================================================
# Function Name: CCDB_SHOW_DB_INFO
# Description  : 查看CCDB数据库文件
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_SHOW_DB_INFO()
{
    L_EXEC_CMD="ccdb showdbname" 
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
    OS_echo "[$0]CCDB show all db name."
    return
}

#=============================================================================
# Function Name: CCDB_CLEAR_DB
# Description  : 清CCDB数据库
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_CLEAR_DB()
{
    L_OPER_POSITION=$1
    if [ x$L_OPER_POSITION == x"local" ];then    
        CCDB_CLEAR_LOCAL_DB    
        return 0
    elif [ x$L_OPER_POSITION == x"peer" ];then
        CCDB_CLEAR_PEER_DB &>/dev/null
        echo "Clear peer CCDB."
        return 0
    elif [ x$L_OPER_POSITION == x"all" ];then
        CCDB_CLEAR_LOCAL_DB
        CCDB_CLEAR_PEER_DB &>/dev/null
        echo "Clear peer CCDB."
        return 0
    else
        echo "param error! usage: ccdb.sh -c clearccdb [local | peer | all]"
        OS_echo "[$0]clear ccdb data failed, param $1 error."   
        return 1
    fi
}

#=============================================================================
# Function Name: CCDB_CLEAR_LOCAL_DB
# Description  : execute ccdb clearlocal
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_CLEAR_LOCAL_DB()
{
    L_EXEC_CMD="ccdb clearlocal"
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
}

#=============================================================================
# Function Name: CCDB_CLEAR_PEER_DB
# Description  : execute ccdb clearlocal
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_CLEAR_PEER_DB()
{

    L_REMOTE_IBC_IP=`OS_GetRemoteIBCIP`
    if [ x"$L_REMOTE_IBC_IP" == x"" ]; then
        echo "peer node offline!"
        OS_echo "[$0]peer node offline!"
        return;
    fi
    
    L_EXEC_CMD="echo -e \"ccdb clearlocal\nexit\n\" | diagnose --auto"
    os_expect_exec_cmd.sh $L_REMOTE_IBC_IP "$L_EXEC_CMD" 5
    OS_echo "[$0]CCDB clear peer db."
    return 0
}

#=============================================================================
# Function Name: CCDB_CLEAR_DB_FILE
# Description  : execute ccdb db file
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_CLEAR_DB_FILE()
{
    L_DB_FILE_NAME=$1
    if [ x"$L_DB_FILE_NAME" == x"" ]; then
        echo "please input DB file name."
        exit 1
	fi
    L_EXEC_CMD="ccdb cleardbfile "$L_DB_FILE_NAME
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
    return
}

#=============================================================================
# Function Name: CCDB_CLEAR_DB_LOG_FILE
# Description  : execute ccdb db file
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_CLEAR_DB_LOG_FILE()
{
    L_DB_FILE_NAME=$1
    if [ x"$L_DB_FILE_NAME" == x"" ]; then
        echo "please input DB log file name."
        exit 1
	fi
    L_EXEC_CMD="ccdb cleardblogfile "$L_DB_FILE_NAME
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
    return
}

#=============================================================================
# Function Name: CCDB_LIST_DB_INFO
# Description  : 查询CCDB DB信息版本及健康度
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_LIST_DB_INFO()
{
    listType=$1
	
    #将文件系统mount起来
    L_EXEC_CMD="ccdb mountmd"
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto
	
    if [ x"$listType" == x"all" ]; then
        list=`ls -l $G_CCDB_STORE_DIR | grep "^d" | awk '{print $9}'`
    else
        list=`ls -l $G_CCDB_STORE_DIR | grep "^d" | awk '{print $9}' | grep "^${listType}_[0-9]\{1,\}$"`
    fi
    
    for dbDir in $list 
    do
        if [ x"dbDir" != x"lost+found" ]; then
            #获取数据库名
            childDirFileList=`ls -l $G_CCDB_STORE_DIR/$dbDir | grep -v "^total" | awk '{print $9}' | grep -v "^paxos_" | grep -vE "*-shm$" |grep -vE "*-wal$"`   
            for childDirFile in $childDirFileList 
                do
                    echo "DB Name : $childDirFile"
					echo "------------------------------------------------------"
                    L_EXEC_CMD="ccdb showversion "$G_CCDB_STORE_DIR/$dbDir/$childDirFile
                    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto
                    L_EXEC_CMD="ccdb checkup "$G_CCDB_STORE_DIR/$dbDir/$childDirFile
                    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto
                done
		fi
    done 
	
    #将文件系统unmount
    L_EXEC_CMD="ccdb unmountmd"
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto
	
    
}

#=============================================================================
# Function Name: CCDB_SHOW_DB_STATE
# Description  : 查询CCDB DB状态信息
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_SHOW_DB_STATE()
{
    L_DB_FILE_NAME=$1
    if [ x"$L_DB_FILE_NAME" == x"" ]; then
        CCDB_LIST_DB_INFO all
    else
        CCDB_LIST_DB_INFO $1
    fi
    return
}

#=============================================================================
# Function Name: CCDB_START_SERVICE
# Description  : 启动CCDB服务
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_START_SERVICE()
{
    L_OPER_POSITION=$1
    if [ x$L_OPER_POSITION == x"local" ];then    
        CCDB_START_LOCAL_SERVICE    
        return 0
    elif [ x$L_OPER_POSITION == x"peer" ];then
        CCDB_START_PEER_SERVICE &>/dev/null
        echo "CCDB start peer service."
        return 0
    elif [ x$L_OPER_POSITION == x"all" ];then
        CCDB_START_LOCAL_SERVICE
        CCDB_START_PEER_SERVICE &>/dev/null
        echo "CCDB start peer service."
        return 0
    else
        echo "param error!"
        OS_echo "[$0]ccdb start service failed, param $1 error."   
        return 1
    fi
}

#=============================================================================
# Function Name: CCDB_START_LOCAL_SERVICE
# Description  : 启动CCDB服务
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_START_LOCAL_SERVICE()
{
    L_PROCESS_PID=`pidof -x ccdb_server`
    L_PROCESS_NUM=`echo "L_PROCESS_PID"|wc -w`
    L_PROCESS_STATUS=`ps -ef | grep ccdb_server`
    if [ $L_PROCESS_NUM -gt 1 ];then    
        OS_echo "[$0]someone is collecting logs, please try again later."    
        OS_echo "[$0]pid num=$G_NUM,$G_STATUS"    
        return 1
    fi
    
    L_EXEC_CMD="ccdb startserver" 
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
    OS_echo "[$0]CCDB start local service."
    return 0
}

#=============================================================================
# Function Name: CCDB_START_PEER_SERVICE
# Description  : 启动对端CCDB服务
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_START_PEER_SERVICE()
{
    L_REMOTE_IBC_IP=`OS_GetRemoteIBCIP`
    if [ x"$L_REMOTE_IBC_IP" == x"" ]; then
        echo "peer node offline!"
        OS_echo "[$0]peer node offline!"
        return;
    fi
    
    L_EXEC_CMD="echo -e \"ccdb startserver\nexit\n\" | diagnose --auto"
    os_expect_exec_cmd.sh $L_REMOTE_IBC_IP "$L_EXEC_CMD" 5
    OS_echo "[$0]CCDB start peer service."
    return 0
}

#=============================================================================
# Function Name: CCDB_EXPORT_DB_FILE
# Description  : 导出CCDB数据
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_EXPORT_DB_FILE()
{
    L_EXEC_CMD="ccdb exportdb /home/permitdir/export_import"
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
    OS_echo "[$0]CCDB export DB file to /home/permitdir/export_import."
    return 0
}

#=============================================================================
# Function Name: CCDB_OPER_MD_DEVICE
# Description  : 操作MD设备信息
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_OPER_MD_DEVICE()
{
    L_OPER_CMD=$1
    if [ x"$L_OPER_CMD" == x"" ]; then
        echo "please input cmd."
        exit 1
    fi
    L_EXEC_CMD="ccdb opermd \"$1\" 0"
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
    OS_echo "[$0]CCDB oper md device $1."
    return 0
}

#=============================================================================
# Function Name: CCDB_OPER_MD_DEVICE
# Description  : 操作MD设备信息
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_OPER_MD_DEVICE_BY_CMD_FILE()
{
	L_CMD_PATH=$1
	L_CMD_MODE=$2
	if [ x"$L_CMD_PATH" == x"" ]; then
        echo "please input cmd file path."
        exit 1
	fi
	
	if [ x"$L_CMD_MODE" == x"" ]; then
            echo "please input cmd mode."
            exit 1
	fi
	
	if [ ! -f $L_CMD_PATH ]; then
        echo "cmd file not exist."
        exit 1
	fi
	
	dos2unix $L_CMD_PATH 1>/dev/null 2>&1
	
	while read LINE
	do
		if [ x"$LINE" == x"" ];then
			continue
		fi
	
		L_LINE=$(echo "${LINE}" | grep -i -E '^mdadm' | grep -i -E '^[^;|<>&]+$')
		if [ -n "$L_LINE" ]; then
			continue
		fi
		echo "check cmd fail. cmd line:${LINE}"
		OS_echo "[$0]CCDB check oper md device fail. cmd line:$L_LINE"
		exit 1
	done < $L_CMD_PATH
	
	if [ x"$L_CMD_MODE" == x"offline" ]; then
            CCDB_STOP_SERVICE all
        elif [ x"$L_CMD_MODE" == x"online" ]; then
            ls /dev/md200
            if [ $? != 0 ]; then
                echo "no md device on this node!"
                exit 1
            fi
        else
            echo "input mode error!"
            exit 1
	fi
	
	while read LINE
	do
		if [ x"$LINE" == x"" ];then
			continue
		fi
	
        L_LINE=$(echo "${LINE}" | awk -F "\\\\\\\\n||\\\\\\\\r" '{print $1}')
        if [ x"$L_CMD_MODE" == x"offline" ]; then
            L_EXEC_CMD="ccdb opermd \"$L_LINE\" 0" 
        else
            L_EXEC_CMD="ccdb opermd \"$L_LINE\" 1"
        fi
        echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
        OS_echo "[$0]CCDB oper md device $L_LINE."
	done < $L_CMD_PATH
	
    return 0
}

#=============================================================================
# Function Name: CCDB_OPER_MD_DEVICE_BY_INPUT_CMD
# Description  : 操作MD设备信息，不通过diagnose命令
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_OPER_MD_DEVICE_BY_INPUT_CMD()
{
    L_OPER_CMD="$1"
    if [ x"$L_OPER_CMD" == x"" ]; then
        echo "please input cmd."
        exit 1
    fi
	
	L_EXEC_CMD="ccdb opermd \"$L_OPER_CMD\" 1 0"
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
    OS_echo "[$0]CCDB oper md device cmd {$L_EXEC_CMD}."
    return 0
}
#=============================================================================
# Function Name: CCDB_SHOW_RAID_DISK
# Description  : 查询CCDB RAID块设备信息
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_SHOW_RAID_DISK()
{
    L_EXEC_CMD="ccdb showraiddisk"
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
    OS_echo "[$0]Show ccdb disk in md200."
    return 0
}

#=============================================================================
# Function Name: CCDB_RECOVERY_DB_FILE
# Description  : 从备份文件恢复CCDB数据
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_RECOVERY_DB_FILE()
{
	L_MODE=$1
	L_RECOVERY_FILE_NAME=$2
	L_RECOVERY_DB_NAME=$3

	if [ x"$L_MODE" == x"" ]; then
        echo "please input recovery mode."
        exit 1
	fi
	
	if [ x"$L_RECOVERY_FILE_NAME" == x"" ]; then
        echo "please input recovery file name."
        exit 1
	fi
	
	
	if [ x"$L_MODE" == x"db" ]; then
        L_MODE_PARAM=0
#   当前只备份了数据库文件
#	elif [ x"$L_MODE" == x"log" ]; then
#       L_MODE_PARAM=1
	elif [ x"$L_MODE" == x"all" ]; then
        L_MODE_PARAM=2
    else
        echo "recovery mode error."
        exit 1
	fi

	if [ x"$L_RECOVERY_DB_NAME" == x"" ]; then
        L_EXEC_CMD="ccdb recoveryccdb $L_MODE_PARAM $L_RECOVERY_FILE_NAME"
        echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
        OS_echo "[$0]CCDB recovery all DB file from $L_RECOVERY_FILE_NAME."
	else	
        L_EXEC_CMD="ccdb recoveryccdb $L_MODE_PARAM $L_RECOVERY_FILE_NAME $L_RECOVERY_DB_NAME"
        echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
        OS_echo "[$0]CCDB recovery DB file from $L_RECOVERY_FILE_NAME to $L_RECOVERY_DB_NAME."
	fi
	
    return 0
}

#=============================================================================
# Function Name: CCDB_EXEC_SQL
# Description  : 执行SQL
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_EXEC_SQL()
{
	L_DB_FILE_NAME=$1
	L_SQL_PATH=$2
	
	if [ x"$L_DB_FILE_NAME" == x"" ]; then
        echo "please input db file name."
        exit 1
	fi
	
	if [ x"$L_SQL_PATH" == x"" ]; then
        echo "please input cmd file path."
        exit 1
	fi
	
	if [ ! -f $L_SQL_PATH ]; then
        echo "cmd file not exist."
        exit 1
	fi
	
	dos2unix $L_SQL_PATH 1>/dev/null 2>&1

	while read LINE
	do
		if [ x"$LINE" == x"" ];then
			continue
		fi
		
		L_LINE=$(echo "${LINE}" | grep -i -E '^select|^update|^insert|^delete|^alter|^drop' | grep -i -E '^[^;|<>&]+$' )
		if [ -n "$L_LINE" ]; then
			continue
		fi
		echo "check cmd fail. cmd line:${LINE}"
		OS_echo "[$0]CCDB check $L_SQL_PATH sql:$L_LINE fail."
		exit 1
	done < $L_SQL_PATH
	
	#将文件系统mount起来
    L_EXEC_CMD="ccdb mountmd"
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto
	
	dbDir=`ls -l $G_CCDB_STORE_DIR | grep "^d" | awk '{print $9}' | grep "^${L_DB_FILE_NAME}_[0-9]\{1,\}$"`

	while read LINE
	do
		if [ x"$LINE" == x"" ];then
			continue
		fi
		
		L_LINE=$(echo "${LINE}" | awk -F "\\\\\\\\n||\\\\\\\\r" '{print $1}')
		L_EXEC_CMD="ccdb execsql $G_CCDB_STORE_DIR/$dbDir/$L_DB_FILE_NAME \"${L_LINE};\" "
		echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
		OS_echo "[$0]CCDB exec $L_DB_FILE_NAME sql:$L_LINE."
	done < $L_SQL_PATH

	#将文件系统unmount
    L_EXEC_CMD="ccdb unmountmd"
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto
    
    return 0
}

#=============================================================================
# Function Name: CCDB_SHOW_DIRTY_FLAG
# Description  : 查询脏节点数据标记
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_SHOW_DIRTY_FLAG()
{
    L_TYPE=$1
    if [ x"$L_TYPE" == x"mem" ]; then
        L_EXEC_CMD="ccdbDirty showmemflag"
	echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
        OS_echo "[$0]Show ccdb mem dirty flag."
	exit 1
    elif [ x"$L_TYPE" == x"disk" ]; then
        L_EXEC_CMD="ccdbDirty showdiskflag"
	echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
        OS_echo "[$0]Show ccdb disk dirty flag."
	exit 1
    else
        echo "please input type [mem | disk]."
        exit 1
    fi
   
    return
}

#=============================================================================
# Function Name: CCDB_SET_DIRTY_FLAG
# Description  : 设置脏节点数据标记
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_SET_DIRTY_FLAG()
{
    L_NID=$1
    L_TYPE=$2
    L_VALID_FLAG=$3

    if [ x"$L_NID" == x"" ]; then
        echo "please input nid."
        exit 1
    fi
    
    if [ x"$L_TYPE" == x"" ]; then
        echo "please input type."
        exit 1
    fi
    
    if [ x"$L_VALID_FLAG" == x"" ]; then
        echo "please input validflag."
        exit 1
    fi
    
    L_EXEC_CMD="ccdbDirty setflag $L_NID $L_TYPE $L_VALID_FLAG"
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
    OS_echo "[$0]Set ccdb node dirty flag."
    return 0
}

#=============================================================================
# Function Name: CCDB_RESET_DIRTY_FLAG
# Description  : 设置默认脏节点数据标记
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_RESET_DIRTY_FLAG()
{
    L_EXEC_CMD="ccdbDirty setdefaultflag" 
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
    OS_echo "[$0]Restore factory settings."
    return
}

#=============================================================================
# Function Name: CCDB_CLEAR_BLK_RAID
# Description  : 清理CCDB RAID信息
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_CLEAR_BLK_RAID()
{
    L_MODULEID=0

    L_EXEC_CMD="blkRaid clearBlkRaid $L_MODULEID"
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
    OS_echo "[$0]Clear ccdb blk raid info."
    return 0
}

#=============================================================================
# Function Name: CCDB_SHOW_BLK_RAID
# Description  : 查询CCDB RAID信息
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_SHOW_BLK_RAID()
{
    L_MODULEID=0

    L_EXEC_CMD="blkRaid showDiskSuper $L_MODULEID"
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
    OS_echo "[$0]Show ccdb blk raid info."
    return 0
}

#=============================================================================
# Function Name: CCDB_SHOW_UPGRADE_INFO
# Description  : 查询升级信息
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_SHOW_UPGRADE_INFO()
{
    L_EXEC_CMD="mon showupgradeinfo"
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto
	OS_echo "[$0]Show upgrade info."
	return 0
}

#=============================================================================
# Function Name: CCDB_SET_UPGRADE_LOG_INFO
# Description  : 设置升级日志信息
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_SET_UPGRADE_LOG_INFO()
{
    L_SRC_VERSION=$1
    L_DST_VERSION=$2
    L_STATUS=$3
    L_RAID_VALID_FLAG=$4
	
    if [ x"$L_SRC_VERSION" == x"" ]; then
        echo "please input srcVserion."
        exit 1
    fi
	
    if [ x"$L_DST_VERSION" == x"" ]; then
        echo "please input dstVserion."
        exit 1
    fi
    
    if [ x"$L_STATUS" == x"" ]; then
        echo "please input status."
        exit 1
    fi

    if [ x"$L_RAID_VALID_FLAG" == x"" ]; then
        echo "please input raidValidFlag."
        exit 1
    fi

    L_EXEC_CMD="mon setupgradelog $L_SRC_VERSION $L_DST_VERSION $L_STATUS $L_RAID_VALID_FLAG"
	echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
    OS_echo "[$0]Set ccdb upgrade log info."
    return 0
}

#=============================================================================
# Function Name: CCDB_CLEAR_UPGRADE_LOG_INFO
# Description  : 清除升级日志信息
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_CLEAR_UPGRADE_LOG_INFO()
{
    L_EXEC_CMD="mon clearupgradelog"
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto
    OS_echo "[$0]Clear upgrade log info."
    return 0
}

#=============================================================================
# Function Name: CCDB_SET_SYS_UPGRADE_PATH
# Description  : 设置系统升级路径信息
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_SET_SYS_UPGRADE_PATH()
{
    L_SRC_VERSION=$1
    L_DST_VERSION=$2

    if [ x"$L_SRC_VERSION" == x"" ]; then
        echo "please input srcVserion."
        exit 1
    fi
	
    if [ x"$L_DST_VERSION" == x"" ]; then
        echo "please input dstVserion."
        exit 1
    fi

    L_EXEC_CMD="mon setsysupgradepath $L_SRC_VERSION $L_DST_VERSION"
	echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
    OS_echo "[$0]Set sys upgrade path info."
    return 0
}

#=============================================================================
# Function Name: CCDB_SET_NODE_VERSION
# Description  : 设置节点版本信息
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_SET_NODE_VERSION()
{
    L_NID=$1
    L_NODE_VERSION=$2
    
    if [ x"$L_NID" == x"" ]; then
        echo "please input srcVserion."
        exit 1
    fi
	
    if [ x"$L_NODE_VERSION" == x"" ]; then
        echo "please input dstVserion."
        exit 1
    fi

    L_EXEC_CMD="mon setnodeversion $L_NID $L_NODE_VERSION"
	echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
    OS_echo "[$0]Set node version info."
    return 0
}

#=============================================================================
# Function Name: CCDB_SET_UPGRADE_INFO
# Description  : 设置CCDB升级信息
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_SET_UPGRADE_INFO()
{
    L_EXEC_CMD="mon setccdbupgradeinfo"
	echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
    OS_echo "[$0]Set ccdb upgrade info."
    return 0
}

#=============================================================================
# Function Name: CCDB_SET_UPGRADE_INFO
# Description  : 添加CCDB升级事件
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_ADD_UPGRADE_EVENT()
{
    L_EXEC_CMD="mon startupgrade"
	echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
    OS_echo "[$0]Add ccdb upgrade event."
    return 0
}

#=============================================================================
# Function Name: CCDB_CLEAR_LOG_SERVICE
# Description  : 清除logservice
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_CLEAR_LOG_SERVICE()
{
    L_LOG_INDEX=$1
    L_EXEC_CMD="logService clearlogservice $L_LOG_INDEX"
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
    OS_echo "[$0]Clear log service."
    return 0
}

#=============================================================================
# Function Name: CCDB_START_LOG_SERVICE
# Description  : 开启logservice
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_START_LOG_SERVICE()
{
	L_LOG_INDEX=$1
    L_EXEC_CMD="logService startlogservice $L_LOG_INDEX"
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
    OS_echo "[$0]Start log service."
    return 0
}

#=============================================================================
# Function Name: CCDB_RESET_START_TIME
# Description  : 设置默认开工延迟时间
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_RESET_START_TIME()
{
    L_EXEC_CMD="mon resetstarttime"
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
    OS_echo "[$0]Reset ccdb start work delay time."
    return 0
}

#=============================================================================
# Function Name: CCDB_CHECK_DEVICE_MD
# Description  : 设置默认开工延迟时间
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_CHECK_DEVICE_MD()
{
    L_EXEC_CMD="ccdb checkmd"
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
    OS_echo "[$0]Check ccdb device md200."
    return 0
}

#=============================================================================
# Function Name: CCDB_REMOVE_MD_DISK
# Description  : 从md200阵列中删除指定块设备
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_REMOVE_MD_DISK()
{
    L_MD_DISK=$1
    L_RETRY_LOOP=0

    if [ x"$L_MD_DISK" == x"" ]; then
        echo "please input disk id"
        return 1
    fi
    L_REMOVE_DEVICE=($(mdadm --detail /dev/md200 | grep /dev/"$L_MD_DISK$" | awk -F " " '{print $NF}'))
    if [ x"$L_REMOVE_DEVICE" == x"" ]; then
        echo "can not find /dev/$L_MD_DISK from md200, please check disk id"
        return 1
    fi
    mdadm --manage /dev/md200 --fail $L_REMOVE_DEVICE >/dev/kmsg 2>&1
    if [ $? -ne 0 ]; then
        echo "remove md disk $L_REMOVE_DEVICE of md200 fail"
        return 1
    fi
    OS_echo "[$0]set  md disk $L_REMOVE_DEVICE of md200 faulty success."
    while [ $L_RETRY_LOOP -lt 20 ]
    do
        L_DISK_INFO=($(mdadm --detail /dev/md200 | grep /dev/"$L_MD_DISK$" ))
        if [ x"$L_DISK_INFO" == x"" ]; then
            echo "remove md disk $L_REMOVE_DEVICE of md200 success"
            OS_echo "[$0]remove md disk $L_REMOVE_DEVICE of md200 success."
            return 0
        fi
        L_RETRY_LOOP=$[ $L_RETRY_LOOP + 1 ]
        sleep 0.5
    done
    echo "confirm remove md disk $L_REMOVE_DEVICE of md200 fail"
    return 1
}

#=============================================================================
# Function Name: CCDB_SET_MD_FAULT_DISK
# Description  : 将md200阵列中的某盘指定为故障盘
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_SET_MD_FAULT_DISK()
{
    L_MD_DISK=$1

    if [ x"$L_MD_DISK" == x"" ]; then
        echo "please input disk id"
        return 1
    fi

    L_EXEC_CMD="ccdb setfaultdisk $L_MD_DISK"
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
    OS_echo "[$0]Set ccdb md200 fault disk success."
    return 0
}

#=============================================================================
# Function Name: CCDB_START_NVRAM_ADAPTER
# Description  : 让nvramadapter开工
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_START_NVRAM_ADAPTER()
{
    L_NODEMAP=$1
	
    if [ x"$L_NODEMAP" == x"" ]; then
    	echo "please input nodemap"
	return 1
    fi
	
    L_EXEC_CMD="nvramadapter startwork $L_NODEMAP"
    echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
    OS_echo "[$0]start nvram adapter success."
    return 0
}

#=============================================================================
# Function Name: CCDB_SHOW_CLUSTER_INFO
# Description  : 查看集群信息
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_SHOW_CLUSTER_INFO()
{
    L_SHOW_TYPE=$1
    L_SHOW_PARAM=$2

    if [ x"$L_SHOW_TYPE" == x"" ]; then
        echo "please input show type[startWorkInfo | siteCompatibleVersion]."
        return 1
    fi	
	
    if [ x"$L_SHOW_TYPE" == x"startWorkInfo" ]; then
		L_EXEC_CMD="mon showsw"
		echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto 
		OS_echo "[$0]Show cluster info start work success."
        return 0
    elif [ x"$L_SHOW_TYPE" == x"siteCompatibleVersion" ]; then
        if [ x"$L_SHOW_PARAM" == x"" ]; then
            echo "please input show site id."
            return 1
        fi
		L_EXEC_CMD="mon showsv $L_SHOW_PARAM"
		echo -e "${L_EXEC_CMD}\nexit\n" | diagnose --auto
		OS_echo "[$0]Show cluster info, param:$L_SHOW_PARAM."
        return 0
    else
        echo "param error,please input show type[startWorkInfo | siteCompatibleVersion]!"
        OS_echo "[$0]show ccdb cluster info failed, param $L_SHOW_TYPE error."   
        return 1
    fi	
	
    return 0
}

#=============================================================================
# Function Name: CCDB_SHOW_MD200_INFO
# Description  : 查看md200信息
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_SHOW_MD200_INFO()
{
    L_CMD_MODE=$1

    if [ x"$L_CMD_MODE" == x"offline" ]; then
        CCDB_STOP_SERVICE all
        CCDB_OPER_MD_DEVICE "mdadm --detail /dev/md200"	
    else
        mdadm --detail /dev/md200
    fi	
	
    return 0
}

# Function Name: CCDB_SHOW_LOCAL_INFO_SINGLE
# Description  : 显示本地单个DB的integrity check及snapshot信息
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_SHOW_LOCAL_INFO_SINGLE()
{
    DIR_PATH=$1

    if [ ! -f ${DIR_PATH} ];then
        echo "${DIR_PATH} file is not exist."
        OS_echo "${DIR_PATH} file is not exist."
        return
    fi

    L_LINE=$(echo "${DIR_PATH}" | grep -i -E '^/OSM/ccdb/|^/OSM/coffer_data/ccdb/|^/home/ibc_os_hs/|^/home/permitdir/' | grep -i -E '^[^;|<>&]+$' )
    if [ -z "$L_LINE" ]; then
        echo "check ${DIR_PATH} fail. path line should in [/OSM/ccdb/ /OSM/coffer_data/ccdb/ /home/ibc_os_hs/ /home/permitdir/] folder."
        OS_echo "[$0]check ${DIR_PATH} fail. path line should in [/OSM/ccdb/ /OSM/coffer_data/ccdb/ /home/ibc_os_hs/ /home/permitdir/] folder."
        return
    fi

    DB_ID=`sqlite3 ${DIR_PATH} -batch "select value from db_info_tbl;" 2>/dev/null`
    CHECK_TEXT=`sqlite3 ${DIR_PATH} -batch "PRAGMA integrity_check;" 2>/dev/null`
    DB_VERSION=`sqlite3 ${DIR_PATH} -batch "select version from db_snapshot_version_tbl;" 2>/dev/null`
    BLANK_STR="                                                                                                                                   "

    DIR_PATH_PRINT=${DIR_PATH}${BLANK_STR}
    CHECK_TEXT_PRINT=${CHECK_TEXT}${BLANK_STR}
    DB_VERSION_PRINT=${DB_VERSION}${BLANK_STR}
    DB_ID_PRINT=${DB_ID}${BLANK_STR}

    echo "db_name                                                                                                                                    |snapshot_version    |db id  |integrity_check_info                  "
    echo "-------------------------------------------------------------------------------------------------------------------------------------------|--------------------|-------|--------------------------------------"

    echo "${DIR_PATH_PRINT:0:140} ${DB_VERSION_PRINT:0:20} ${DB_ID_PRINT:0:7} ${CHECK_TEXT_PRINT:0:38}"
    OS_echo "${DIR_PATH_PRINT:0:140} ${DB_VERSION_PRINT:0:20} ${DB_ID_PRINT:0:7} ${CHECK_TEXT_PRINT:0:38}"

    return
}

# Function Name: CCDB_SHOW_LOCAL_INFO_TREE
# Description  : 显示目录tree信息
# Parameter    : 目录信息, cache | nvram | store | ccdb_backup | ccdb_backup_protect | all
# Returns      : 
#=============================================================================
function CCDB_SHOW_LOCAL_INFO_TREE()
{
    DIR_PATH=$1
    PATH_CACHE=/OSM/ccdb/cache/
    PATH_NVRAM=/OSM/ccdb/nvram/
    PATH_STORE=/OSM/ccdb/store/
    PATH_BACKUP=/OSM/coffer_data/ccdb/ccdb_backup/
    PATH_BACKUP_PROTECT=/OSM/coffer_data/ccdb/ccdb_backup_protect/

    if [ x"${DIR_PATH}" == x"cache" ];then
        echo "show ${DIR_PATH} tree info--------------------------------------------------------------------------"
        tree -spigfDtu --timefmt="%Y-%m-%d %H:%M:%S" ${PATH_CACHE}
        echo -e "\n\n---------------------------------------------------------------------------------------"
        return
    fi

    if [ x"${DIR_PATH}" == x"nvram" ];then
        echo "show ${DIR_PATH} tree info--------------------------------------------------------------------------"
        tree -spigfDtu --timefmt="%Y-%m-%d %H:%M:%S" ${PATH_NVRAM}
        echo -e "\n\n---------------------------------------------------------------------------------------"
        return
    fi

    if [ x"${DIR_PATH}" == x"store" ];then
        echo "show ${DIR_PATH} tree info--------------------------------------------------------------------------"
        tree -spigfDtu --timefmt="%Y-%m-%d %H:%M:%S" ${PATH_STORE}
        echo -e "\n\n---------------------------------------------------------------------------------------"
        return
    fi

    if [ x"${DIR_PATH}" == x"ccdb_backup" ];then
        echo "show ${DIR_PATH} tree info--------------------------------------------------------------------------"
        tree -spigfDtu --timefmt="%Y-%m-%d %H:%M:%S" ${PATH_BACKUP}
        echo -e "\n\n---------------------------------------------------------------------------------------"
        return
    fi

    if [ x"${DIR_PATH}" == x"ccdb_backup_protect" ];then
        echo "show ${DIR_PATH} tree info--------------------------------------------------------------------------"
        tree -spigfDtu --timefmt="%Y-%m-%d %H:%M:%S" ${PATH_BACKUP_PROTECT}
        echo -e "\n\n---------------------------------------------------------------------------------------"
        return
    fi

    if [ x"${DIR_PATH}" == x"all" ];then
        echo "show ${DIR_PATH} tree info--------------------------------------------------------------------------"
        tree -spigfDtu --timefmt="%Y-%m-%d %H:%M:%S" ${PATH_CACHE}
        tree -spigfDtu --timefmt="%Y-%m-%d %H:%M:%S" ${PATH_NVRAM}
        tree -spigfDtu --timefmt="%Y-%m-%d %H:%M:%S" ${PATH_STORE}
        tree -spigfDtu --timefmt="%Y-%m-%d %H:%M:%S" ${PATH_BACKUP}
        tree -spigfDtu --timefmt="%Y-%m-%d %H:%M:%S" ${PATH_BACKUP_PROTECT}
        echo -e "\n\n---------------------------------------------------------------------------------------"
        return
    fi

    OS_echo "show local info tree mode ${DIR_PATH} fail, PATH should in [cache|nvram|store|ccdb_backup|ccdb_backup_protect|all]."
    echo "show local info tree  mode ${DIR_PATH} fail, PATH should in [cache|nvram|store|ccdb_backup|ccdb_backup_protect|all]."
    return
}

# Function Name: CCDB_SHOW_LOCAL_INFO_ALL_BY_PATH
# Description  : 显示目录all信息, [文件路径|DB ID|integrity check信息|快照版本]
# Parameter    : 目录信息, cache | nvram | store | ccdb_backup | ccdb_backup_protect | all
# Returns      : 
#=============================================================================
function CCDB_SHOW_LOCAL_INFO_ALL_BY_PATH()
{
    DIR_PATH=$1

    if [ ! -d ${DIR_PATH} ];then
        echo "${DIR_PATH} path is not exist."
        OS_echo "${DIR_PATH} path is not exist."
        return
    fi

    echo "db_name                                                                                                                                    |snapshot_version    |db id  |integrity_check_info                  "
    echo "-------------------------------------------------------------------------------------------------------------------------------------------|--------------------|-------|--------------------------------------"

    FILE_LIST=`find ${DIR_PATH} -type f | sort`
    for FILE_PATH in ${FILE_LIST}
    do
        FILE_NAME=${FILE_PATH##*/}
        if [[ $FILE_NAME =~ ^paxos_.*log$ ]]; then
            continue
        fi

        if [[ $FILE_NAME =~ .*-shm$ ]]; then
            continue
        fi

        if [[ $FILE_NAME =~ .*-wal$ ]]; then
            continue
        fi

        if [[ $FILE_NAME =~ .*tgz$ ]]; then
            continue
        fi

        DB_ID=`sqlite3 ${FILE_PATH} -batch "select value from db_info_tbl;" 2>/dev/null`
        CHECK_TEXT=`sqlite3 ${FILE_PATH} -batch "PRAGMA integrity_check;" 2>/dev/null`
        DB_VERSION=`sqlite3 ${FILE_PATH} -batch "select version from db_snapshot_version_tbl;" 2>/dev/null`
        BLANK_STR="                                                                                                                                "

        FILE_PATH_PRINT=${FILE_PATH}${BLANK_STR}
        CHECK_TEXT_PRINT=${CHECK_TEXT}${BLANK_STR}
        DB_VERSION_PRINT=${DB_VERSION}${BLANK_STR}
        DB_ID_PRINT=${DB_ID}${BLANK_STR}

        echo "${FILE_PATH_PRINT:0:140} ${DB_VERSION_PRINT:0:20} ${DB_ID_PRINT:0:7} ${CHECK_TEXT_PRINT:0:38}"
        OS_echo "${FILE_PATH_PRINT:0:140} ${DB_VERSION_PRINT:0:20} ${DB_ID_PRINT:0:7} ${CHECK_TEXT_PRINT:0:38}"
    done

    return
}


# Function Name: CCDB_SHOW_LOCAL_INFO_ALL
# Description  : 显示目录all信息, [文件路径|DB ID|integrity check信息|快照版本]
# Parameter    : 目录信息, cache | nvram | store | ccdb_backup | ccdb_backup_protect | all
# Returns      : 
#=============================================================================
function CCDB_SHOW_LOCAL_INFO_ALL_BACKUP_BY_PATH()
{
    DIR_PATH=$1
    DIR_TMP=/OSM/coffer_data/ccdb/ccdb_tmp/

    if [ ! -d ${DIR_PATH} ];then
        echo "${DIR_PATH} path is not exist."
        OS_echo "${DIR_PATH} path is not exist."
        return
    fi

    if [ ! -d ${DIR_TMP} ];then
        mkdir -p ${DIR_TMP} 1>/dev/null 2>&1
    fi
    rm -rf ${DIR_TMP}/* 1>/dev/null 2>&1

    FILE_LIST=`find ${DIR_PATH} -type f | sort`
    for FILE_PATH in ${FILE_LIST}
    do
        FILE_NAME=${FILE_PATH##*/}
        if [[ $FILE_NAME =~ ^backup-.*tar$ ]]; then
            echo "                                       "
            echo "${FILE_PATH} file info:                "
        else
            echo "${FILE_NAME} is not match."
            continue
        fi

        tar -zxvf ${FILE_PATH} -C ${DIR_TMP}  1>/dev/null 2>&1

        CCDB_SHOW_LOCAL_INFO_ALL_BY_PATH ${DIR_TMP}

        rm -rf ${DIR_TMP}/* 1>/dev/null 2>&1
    done

    rm -rf ${DIR_TMP} 1>/dev/null 2>&1

    return
}


# Function Name: CCDB_SHOW_LOCAL_INFO_ALL
# Description  : 显示目录all信息, [文件路径|DB ID|integrity check信息|快照版本]
# Parameter    : 目录信息, cache | nvram | store | ccdb_backup | ccdb_backup_protect | all
# Returns      : 
#=============================================================================
function CCDB_SHOW_LOCAL_INFO_ALL()
{
    DIR_PATH=$1
    PATH_CACHE=/OSM/ccdb/cache/
    PATH_NVRAM=/OSM/ccdb/nvram/
    PATH_STORE=/OSM/ccdb/store/
    PATH_BACKUP=/OSM/coffer_data/ccdb/ccdb_backup/
    PATH_BACKUP_PROTECT=/OSM/coffer_data/ccdb/ccdb_backup_protect/

    if [ x"${DIR_PATH}" == x"cache" ];then
        echo "show ${DIR_PATH} all info--------------------------------------------------------------------------"
        CCDB_SHOW_LOCAL_INFO_ALL_BY_PATH ${PATH_CACHE}
        echo -e "\n\n---------------------------------------------------------------------------------------"
        return
    fi

    if [ x"${DIR_PATH}" == x"nvram" ];then
        echo "show ${DIR_PATH} all info--------------------------------------------------------------------------"
        CCDB_SHOW_LOCAL_INFO_ALL_BY_PATH ${PATH_NVRAM}
        echo -e "\n\n---------------------------------------------------------------------------------------"
        return
    fi

    if [ x"${DIR_PATH}" == x"store" ];then
        echo "show ${DIR_PATH} all info--------------------------------------------------------------------------"
        CCDB_SHOW_LOCAL_INFO_ALL_BY_PATH ${PATH_STORE}
        echo -e "\n\n---------------------------------------------------------------------------------------"
        return
    fi

    if [ x"${DIR_PATH}" == x"ccdb_backup" ];then
        echo "show ${DIR_PATH} all info--------------------------------------------------------------------------"
        CCDB_SHOW_LOCAL_INFO_ALL_BACKUP_BY_PATH ${PATH_BACKUP}
        echo -e "\n\n---------------------------------------------------------------------------------------"
        return
    fi

    if [ x"${DIR_PATH}" == x"ccdb_backup_protect" ];then
        echo "show ${DIR_PATH} all info--------------------------------------------------------------------------"
        CCDB_SHOW_LOCAL_INFO_ALL_BACKUP_BY_PATH ${PATH_BACKUP_PROTECT}
        echo -e "\n\n---------------------------------------------------------------------------------------"
        return
    fi

    if [ x"${DIR_PATH}" == x"all" ];then
        echo "show ${DIR_PATH} all info--------------------------------------------------------------------------"
        CCDB_SHOW_LOCAL_INFO_ALL_BY_PATH ${PATH_CACHE}
        CCDB_SHOW_LOCAL_INFO_ALL_BY_PATH ${PATH_NVRAM}
        CCDB_SHOW_LOCAL_INFO_ALL_BY_PATH ${PATH_STORE}
        CCDB_SHOW_LOCAL_INFO_ALL_BACKUP_BY_PATH ${PATH_BACKUP}
        CCDB_SHOW_LOCAL_INFO_ALL_BACKUP_BY_PATH ${PATH_BACKUP_PROTECT}
        echo -e "\n\n---------------------------------------------------------------------------------------"
        return
    fi

    OS_echo "show local info all mode ${DIR_PATH} fail, PATH should in [cache|nvram|store|ccdb_backup|ccdb_backup_protect|all]."
    echo "show local info all  mode ${DIR_PATH} fail, PATH should in [cache|nvram|store|ccdb_backup|ccdb_backup_protect|all]."
    return
}

# Function Name: CCDB_SHOW_LOCAL_INFO
# Description  : 显示本地信息
# Parameter    : tree显示对应路径的tree命令信息, single显示单个DB的integrity check信息以及snapshot版本, all显示对应路径所有DB的integrity check信息以及snapshot版本
# Returns      : 
#=============================================================================
function CCDB_SHOW_LOCAL_INFO()
{
    MODE_TYPE=$1
    DIR_PATH=$2

    if [ x"${MODE_TYPE}" == x"tree" ];then
        CCDB_SHOW_LOCAL_INFO_TREE ${DIR_PATH}
        return
    fi

    if [ x"${MODE_TYPE}" == x"single" ];then
        CCDB_SHOW_LOCAL_INFO_SINGLE ${DIR_PATH}
        return
    fi

    if [ x"${MODE_TYPE}" == x"all" ];then
        CCDB_SHOW_LOCAL_INFO_ALL ${DIR_PATH}
        return
    fi

    OS_echo "show local info mode ${MODE_TYPE} fail, there is no ${MODE_TYPE} command."
    echo "show local info mode ${MODE_TYPE} fail, there is no ${MODE_TYPE} command."
    return
}

# Function Name: CCDB_PREPARE_LOCAL_DB_TAR
# Description  : 将备份压缩文件路径加压到特定目录并获取DB信息
# Parameter    : $1 备份压缩文件路径
# Returns      : 
#=============================================================================
function CCDB_PREPARE_LOCAL_DB_TAR()
{
    FILE_PATH=$1
    DIR_TMP=/OSM/coffer_data/ccdb/ccdb_tmp/

    if [ ! -f ${FILE_PATH} ];then
        echo "${FILE_PATH} path is not exist."
        OS_echo "${FILE_PATH} path is not exist."
        return
    fi

    L_LINE=$(echo "${FILE_PATH}" | grep -i -E '^/OSM/ccdb/|^/OSM/coffer_data/ccdb/|^/home/ibc_os_hs/|^/home/permitdir/' | grep -i -E '^[^;|<>&]+$' )
    if [ -z "$L_LINE" ]; then
        echo "check ${FILE_PATH} fail. path line should in [/OSM/ccdb/ /OSM/coffer_data/ccdb/ /home/ibc_os_hs/ /home/permitdir/] folder."
        OS_echo "[$0]check ${FILE_PATH} fail. path line should in [/OSM/ccdb/ /OSM/coffer_data/ccdb/ /home/ibc_os_hs/ /home/permitdir/] folder."
        return
    fi

    if [ ! -d ${DIR_TMP} ];then
        mkdir -p ${DIR_TMP} 1>/dev/null 2>&1
    fi
    rm -rf ${DIR_TMP}/* 1>/dev/null 2>&1

    FILE_NAME=${FILE_PATH##*/}
    if [[ $FILE_NAME =~ ^backup-.*tar$ ]]; then
        echo "                                       "
        echo "${FILE_PATH} file info:                "
    else
        echo "${FILE_NAME} is not match."
        continue
    fi

    tar -zxvf ${FILE_PATH} -C ${DIR_TMP}  1>/dev/null 2>&1

    CCDB_SHOW_LOCAL_INFO_ALL_BY_PATH ${DIR_TMP}

    return
}


# Function Name: CCDB_PREPARE_LOCAL_DB_CP
# Description  : 准备修复数据库
# Parameter    : 将运行的数据库及对应路径下的shm,wal文件拷贝至临时目录下,准备进行修复
# Returns      : 
#=============================================================================
function CCDB_PREPARE_LOCAL_DB_CP()
{
    FILE_PATH=$1
    DIR_TMP=/OSM/coffer_data/ccdb/ccdb_tmp/

    if [ ! -f ${FILE_PATH} ];then
        echo "${FILE_PATH} path is not exist."
        OS_echo "${FILE_PATH} path is not exist."
        return
    fi

    L_LINE=$(echo "${FILE_PATH}" | grep -i -E '^/OSM/ccdb/|^/OSM/coffer_data/ccdb/|^/home/ibc_os_hs/|^/home/permitdir/' | grep -i -E '^[^;|<>&]+$' )
    if [ -z "$L_LINE" ]; then
        echo "check ${FILE_PATH} fail. path line should in [/OSM/ccdb/ /OSM/coffer_data/ccdb/ /home/ibc_os_hs/ /home/permitdir/] folder."
        OS_echo "[$0]check ${FILE_PATH} fail. path line should in [/OSM/ccdb/ /OSM/coffer_data/ccdb/ /home/ibc_os_hs/ /home/permitdir/] folder."
        return
    fi

    if [ ! -d ${DIR_TMP} ];then
        mkdir -p ${DIR_TMP} 1>/dev/null 2>&1
    fi
    rm -rf ${DIR_TMP}/* 1>/dev/null 2>&1

    FILE_NAME=${FILE_PATH##*/}
    
    cp ${FILE_PATH}* ${DIR_TMP} 1>/dev/null 2>&1

    CCDB_SHOW_LOCAL_INFO_SINGLE ${DIR_TMP}${FILE_NAME}

    return
}

# Function Name: CCDB_PREPARE_LOCAL_DB_DUMP
# Description  : dump修复DB
# Parameter    : FILE_PATH DB路径为ccdb_tmp目录下
# Returns      : 
#=============================================================================
function CCDB_PREPARE_LOCAL_DB_DUMP()
{
    FILE_PATH=$1
    DIR_TMP=/OSM/coffer_data/ccdb/ccdb_tmp/
    DUMP_FILE=${FILE_PATH}_ccdb_dump_sql
    DUMP_DB_FILE=${FILE_PATH}_dump_db

    if [ ! -f ${FILE_PATH} ];then
        echo "${FILE_PATH} path is not exist."
        OS_echo "${FILE_PATH} path is not exist."
        return
    fi

    L_LINE=$(echo "${FILE_PATH}" | grep -i -E '^/OSM/coffer_data/ccdb/ccdb_tmp/' | grep -i -E '^[^;|<>&]+$' )
    if [ -z "$L_LINE" ]; then
        echo "check ${FILE_PATH} fail. path line should in [/OSM/coffer_data/ccdb/ccdb_tmp/] folder."
        OS_echo "[$0]check ${FILE_PATH} fail. path line should in [/OSM/coffer_data/ccdb/ccdb_tmp/] folder."
        return
    fi

    rm -f ${DUMP_FILE} 1>/dev/null 2>&1
    rm -f ${DUMP_DB_FILE} 1>/dev/null 2>&1

    sqlite3 ${FILE_PATH} -batch ".dump" > ${DUMP_FILE} 2>/dev/null
    sqlite3 ${DUMP_DB_FILE} -batch ".read ${DUMP_FILE}" 2>/dev/null

    echo "dump ${FILE_PATH} to ${DUMP_DB_FILE} start."

    echo "show ${FILE_PATH} file info:"
    CCDB_SHOW_LOCAL_INFO_SINGLE ${FILE_PATH}
    echo "     "

    echo "show ${DUMP_DB_FILE} file info:"
    CCDB_SHOW_LOCAL_INFO_SINGLE ${DUMP_DB_FILE}
    echo "     "

    echo "dump ${FILE_PATH} to ${DUMP_DB_FILE} end."
    return
}

# Function Name: CCDB_PREPARE_LOCAL_DB_CLEAR
# Description  : 清理临时修复数据库目录
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_PREPARE_LOCAL_DB_CLEAR()
{
    DIR_TMP=/OSM/coffer_data/ccdb/ccdb_tmp/

    rm -rf ${DIR_TMP} 1>/dev/null 2>&1

    OS_echo "clear parepare db tmp dir success."
    echo "clear parepare db tmp dir success."
    return
}

# Function Name: CCDB_PREPARE_LOCAL_DB
# Description  : 准备修复数据库
# Parameter    : $1 type [tar|cp|dump|clear], $2 path; type为tar时,path为备份文件路径,解压到临时目录; type为cp时, cp运行时数据库文件到临时目录; type为dump时,dump path路径下DB ; type为clear时, 清理临时目录;
# Returns      : 
#=============================================================================
function CCDB_PREPARE_LOCAL_DB()
{
    MODE_TYPE=$1
    DIR_PATH=$2

    if [ x"${MODE_TYPE}" == x"tar" ];then
        CCDB_PREPARE_LOCAL_DB_TAR ${DIR_PATH}
        return
    fi

    if [ x"${MODE_TYPE}" == x"cp" ];then
        CCDB_PREPARE_LOCAL_DB_CP ${DIR_PATH}
        return
    fi

    if [ x"${MODE_TYPE}" == x"dump" ];then
        CCDB_PREPARE_LOCAL_DB_DUMP ${DIR_PATH}
        return
    fi

    if [ x"${MODE_TYPE}" == x"clear" ];then
        CCDB_PREPARE_LOCAL_DB_CLEAR
        return
    fi

    OS_echo "prepare local db mode ${MODE_TYPE} fail, there is no ${MODE_TYPE} command."
    echo "prepare local db mode ${MODE_TYPE} fail, there is no ${MODE_TYPE} command."
    return
}

# Function Name: CCDB_BACKUP_AL_DB_EMERGENCY
# Description  : 一键备份所有运行目录下 cache nvram store的DB,并且将ccdb_backup ccdb_backup_protect备份目录数据一起压缩,导出到备份目录
# Parameter    : $1 DIR_PATH 数据导出目录
# Returns      : 
#=============================================================================
function CCDB_BACKUP_AL_DB_EMERGENCY()
{
    FILE_PATH=$1

    DIR_TMP=/OSM/coffer_data/ccdb/ccdb_tmp/
    TMP_FILT=${DIR_TMP}ccdb_backup_db_all.tgz
    EXPORT_TMP=/OSM/coffer_data/ccdb/ccdb_tmp/ccdb_emergency_tmp/
    PATH_CACHE=/OSM/ccdb/cache/
    PATH_NVRAM=/OSM/ccdb/nvram/
    PATH_STORE=/OSM/ccdb/store/
    PATH_BACKUP=/OSM/coffer_data/ccdb/ccdb_backup/
    PATH_BACKUP_PROTECT=/OSM/coffer_data/ccdb/ccdb_backup_protect/

    if [ ! -d ${FILE_PATH} ];then
        echo "${FILE_PATH} path is not exist."
        OS_echo "${FILE_PATH} path is not exist."
        return
    fi

    L_LINE=$(echo "${FILE_PATH}" | grep -i -E '^/OSM/coffer_data/ccdb/|^/home/ibc_os_hs/|^/home/permitdir/' | grep -i -E '^[^;|<>&]+$' )
    if [ -z "$L_LINE" ]; then
        echo "check ${FILE_PATH} fail. path line should in [/OSM/coffer_data/ccdb/ /home/ibc_os_hs/ /home/permitdir/] folder."
        OS_echo "[$0]check ${FILE_PATH} fail. path line should in [/OSM/coffer_data/ccdb/ /home/ibc_os_hs/ /home/permitdir/] folder."
        return
    fi

    if [ ! -d ${DIR_TMP} ];then
        mkdir -p ${DIR_TMP} 1>/dev/null 2>&1
    fi
    rm -rf ${DIR_TMP}/* 1>/dev/null 2>&1
    
    if [ ! -d ${EXPORT_TMP} ];then
        mkdir -p ${EXPORT_TMP} 1>/dev/null 2>&1
    fi
    
    cp -rf ${PATH_CACHE} ${EXPORT_TMP} 1>/dev/null 2>&1
    cp -rf ${PATH_NVRAM} ${EXPORT_TMP} 1>/dev/null 2>&1
    cp -rf ${PATH_STORE} ${EXPORT_TMP} 1>/dev/null 2>&1
    cp -rf ${PATH_BACKUP} ${EXPORT_TMP} 1>/dev/null 2>&1
    cp -rf ${PATH_BACKUP_PROTECT} ${EXPORT_TMP} 1>/dev/null 2>&1

    tar -zcvf ${TMP_FILT} ${EXPORT_TMP} 1>/dev/null 2>&1


    echo "show ${DIR_TMP} tree info--------------------------------------------------------------------------"
    tree -spigfDtu --timefmt="%Y-%m-%d %H:%M:%S" ${DIR_TMP}
    echo -e "\n\n---------------------------------------------------------------------------------------"

    cp ${TMP_FILT} ${FILE_PATH} 1>/dev/null 2>&1

    rm -rf ${DIR_TMP} 1>/dev/null 2>&1
    return 
}

# Function Name: CCDB_OPERATION_DB_RECOVER
# Description  : cp src到des;  rm src
# Parameter    : 
# Returns      : 
#=============================================================================
function CCDB_OPERATION_DB_RECOVER()
{
    MODE_TYPE=$1
    SRC_PATH=$2
    DEST_PATH=$3

    if [ ! -f ${SRC_PATH} ];then
        echo "${SRC_PATH} path is not exist."
        OS_echo "${SRC_PATH} path is not exist."
        return
    fi

    L_LINE=$(echo "${SRC_PATH}" | grep -i -E '^/OSM/ccdb/|^/OSM/coffer_data/ccdb/|^/home/ibc_os_hs/|^/home/permitdir/' | grep -i -E '^[^;|<>&]+$' )
    if [ -z "$L_LINE" ]; then
        echo "check ${SRC_PATH} fail. path line should in [/OSM/ccdb/ /OSM/coffer_data/ccdb/ /home/ibc_os_hs/ /home/permitdir/] folder."
        OS_echo "[$0]check ${SRC_PATH} fail. path line should in [/OSM/ccdb/ /OSM/coffer_data/ccdb/ /home/ibc_os_hs/ /home/permitdir/] folder."
        return
    fi

    if [ x"${MODE_TYPE}" == x"cp" ];then
        L_LINE=$(echo "${DEST_PATH}" | grep -i -E '^/OSM/ccdb/|^/OSM/coffer_data/ccdb/|^/home/ibc_os_hs/|^/home/permitdir/' | grep -i -E '^[^;|<>&]+$' )
        if [ -z "$L_LINE" ]; then
            echo "check ${DEST_PATH} fail. path line should in [/OSM/ccdb/ /OSM/coffer_data/ccdb/ /home/ibc_os_hs/ /home/permitdir/] folder."
            OS_echo "[$0]check ${DEST_PATH} fail. path line should in [/OSM/ccdb/ /OSM/coffer_data/ccdb/ /home/ibc_os_hs/ /home/permitdir/] folder."
            return
        fi

        cp -rfp ${SRC_PATH} ${DEST_PATH} 1>/dev/null 2>&1
        if [ $? -ne 0 ];then
            OS_echo "[$0]cp ${SRC_PATH} to ${DEST_PATH} fail."
            echo "cp ${SRC_PATH} to ${DEST_PATH} fail."
        else
            OS_echo "[$0]cp ${SRC_PATH} to ${DEST_PATH} success."
            echo "cp ${SRC_PATH} to ${DEST_PATH} success."
        fi
        chmod 660 ${DEST_PATH} 1>/dev/null 2>&1
        return
    fi

    if [ x"${MODE_TYPE}" == x"rm" ];then
        rm -f ${SRC_PATH} 1>/dev/null 2>&1
        if [ $? -ne 0 ];then
            OS_echo "[$0]rm ${SRC_PATH} fail."
            echo "rm ${SRC_PATH} fail."
        else
            OS_echo "[$0]rm ${SRC_PATH} success."
            echo "rm ${SRC_PATH} success."
        fi
        return
    fi

    OS_echo "operation db recover mode ${MODE_TYPE} fail, there is no ${MODE_TYPE} command."
    echo "operation db recover mode ${MODE_TYPE} fail, there is no ${MODE_TYPE} command."
    return
}

# Function Name: CCDB_OPERATION_CCDB_SERVER
# Description  : 启动CCDB_SERVER 或 停止CCDB_SERVER
# Parameter    : start op stop
# Returns      : 
#=============================================================================
function CCDB_OPERATION_CCDB_SERVER()
{
    MODE_TYPE=$1

    if [ x"${MODE_TYPE}" == x"start" ];then
        /OSM/bin/daemon /OSM/bin/ccdb_server
        L_SERVER_INFO=`ps -ef |grep ccdb_server | grep -v grep`

        echo "ps ccdb_server info: ${L_SERVER_INFO}."
        return
    fi

    if [ x"${MODE_TYPE}" == x"stop" ];then
        /OSM/bin/daemon -s /OSM/bin/ccdb_server

        echo "ps ccdb_server info: ${L_SERVER_INFO}."
        return
    fi

    OS_echo "[$0]operation ccdb_server mode ${MODE_TYPE} fail, there is no ${MODE_TYPE} command."
    echo "operation ccdb_server mode ${MODE_TYPE} fail, there is no ${MODE_TYPE} command."
    return
}

#=============================================================================
# Function Name: CCDB_VERIFICATION_AND_DANGER
# Description  : 白名单防呆处理，增加验证码输入与高危提示
# Parameter    : cmd为白名单队列
# Returns      : 0-防呆验证不成功; 1-防呆验证成功/不需要防呆
#=============================================================================
function CCDB_VERIFICATION_AND_DANGER()
{
    local L_CMD=$1
    for cmd in ${G_CCDB_VERIFICATION_EMERGENCY_CMD[@]}
    do
        if [ "${cmd}" == "${L_CMD}" ]; then
            #高危提示
            echo "DANGER: You are going to perform a danger command."
            echo "Suggestion: Before you perform this operation, make sure that the operation will not affect working of system."
            echo "Have you read danger alert message carefully?(y/n)"
            read -p "" L_INPUT
            if [ "${L_INPUT}" == "y" -o "${L_INPUT}" == "Y" ]; then
                #验证码输入
                echo "Enter \"I have read and understand the consequences associated with performing this operation.\" to confirm running this command."
                #校验文本输入
                VerificationCode="I have read and understand the consequences associated with performing this operation."
                #校验文本不允许容错
                read -p "" L_INPUT
                if [ "${L_INPUT}" != "${VerificationCode}" ];then
                    echo "Verification failed."
                    return 1
                else
                    return 0
                fi
            fi
            return 1
        fi
    done
    
    return 0
}




# ---------------------  main  -----------------------#
while getopts "hac:" options
do    
    case "$options" in        
        a)
            CCDB_CMD_HELP
            exit 0			
            ;;
        h)
            echo "ccdb.sh [-h] | [-a] | [-c] : It is just a cluster configuration database command. Enter ccdb.sh -a to obtain more information."
            exit 0
            ;;
        c) 
            command="$OPTARG" 	
            ;;		    		
        \?)            
            echo "usage: ccdb.sh -a"			
            exit 1            
            ;;    
    esac
done

CCDB_VERIFICATION_AND_DANGER $command
if [ $? -ne 0 ]; then
    exit 1 
fi  

CCDB_CHECK_CMD_EMERGENCY $command
if [ $? -ne 0 ]; then                    
    echo "DANGER: You are going to perform a danger command, this behavior causes database service interruption."                    
    echo "Suggestion: Before you perform this operation, make sure that the operation will not affect working of system."                    
    echo "Have you read danger alert message carefully?(y/n)"                    
    read -p "" L_INPUT                    
    if [ "${L_INPUT}" == "y" ]; then                        
        echo "Are you sure you really want to perform the operation?(y/n)"                        
        read -p "" L_INPUT                        
        if [ "${L_INPUT}" != "y" ]; then                            
            cd ${L_PWD} &>/dev/null                            
            exit 1                        
        fi                    
    else                        
        cd ${L_PWD} &>/dev/null                        
        exit 1                    
    fi                
fi

case "$command" in	
    clearccdb)	
        CCDB_STOP_SERVICE all
        CCDB_CLEAR_DB $3	
        exit 1	
        ;;	
    cleardbfile)
        CCDB_STOP_SERVICE all
        CCDB_CLEAR_DB_FILE $3		
        exit 1	
        ;;	
    cleardblog)		
        CCDB_STOP_SERVICE all
        CCDB_CLEAR_DB_LOG_FILE $3			
        exit 1	
        ;;
    showstate)		
        CCDB_SHOW_STATE		
        exit 1	
        ;; 
    showdbinfo)	
		CCDB_STOP_SERVICE all
        CCDB_SHOW_DB_INFO		
        exit 1	
        ;;
    showdb)	
        CCDB_STOP_SERVICE all
        CCDB_SHOW_DB_STATE $3		
        exit 1	
        ;; 
    showbackupdb)
        CCDB_SHOW_BACKUP_FILE		
        exit 1	
        ;; 
    showmd)
        CCDB_SHOW_MD200_INFO $3	
        exit 1	
        ;; 
    startccdb)		
        CCDB_START_SERVICE $3		
        exit 1	
        ;; 
    stopccdb)		
        CCDB_STOP_SERVICE $3 		
        exit 1	
        ;; 
    exportdb)
        CCDB_STOP_SERVICE all   
        CCDB_EXPORT_DB_FILE		
        exit 1	
        ;;   
    recoverymd)
        CCDB_OPER_MD_DEVICE_BY_CMD_FILE "$3" $4		
        exit 1	
        ;; 
    repairdb)		
        CCDB_STOP_SERVICE all	
		CCDB_RECOVERY_DB_FILE $3 $4 $5
        exit 1	
        ;; 
	operdb)
		CCDB_STOP_SERVICE all	
        CCDB_EXEC_SQL $3 "$4"
        exit 1
        ;;
    showccdbdirtyflag )    
        CCDB_SHOW_DIRTY_FLAG $3
        exit 1
        ;;
    setccdbdirtyflag)    
        CCDB_SET_DIRTY_FLAG $3 $4 $5
        exit 1
        ;;
    resetccdbdirtyflag)   
        CCDB_RESET_DIRTY_FLAG
        exit 1
        ;;
    clearccdbblkraidinfo)
        CCDB_STOP_SERVICE all    
        CCDB_CLEAR_BLK_RAID
        exit 1
        ;;
    closeccdbcrccheck)    
        CCDB_CLOSE_CRC_CHECK
        exit 1
        ;;
    showccdbcrccheck)    
        CCDB_SHOW_CRC_CHECK
        exit 1
        ;;
    showccdbblkraidinfo)    
        CCDB_SHOW_BLK_RAID
        exit 1
        ;;
    showupgradeinfo)
        CCDB_SHOW_UPGRADE_INFO
        exit 1
        ;;
    setupgradeloginfo)
        CCDB_SET_UPGRADE_LOG_INFO $3 $4 $5 $6
        exit 1
        ;;
    clearupgradeloginfo)
        CCDB_CLEAR_UPGRADE_LOG_INFO
        exit 1
        ;;
    setsysupgradepath)
        CCDB_SET_SYS_UPGRADE_PATH $3 $4
        exit 1
        ;;
    setnodeversion)
        CCDB_SET_NODE_VERSION $3 $4
        exit 1
        ;;
    setccdbupgradeinfo)
        CCDB_SET_UPGRADE_INFO
        exit 1
        ;;
    addupgradeevent)
        CCDB_ADD_UPGRADE_EVENT
        exit 1
        ;;
    clearlogservice)
        CCDB_CLEAR_LOG_SERVICE $3
        exit 1
        ;;
    startlogservice)
        CCDB_START_LOG_SERVICE $3
        exit 1
        ;;
    resetstarttime)    
        CCDB_RESET_START_TIME
        exit 1
        ;;
     checkmd)    
        CCDB_CHECK_DEVICE_MD
        exit 1
        ;; 
     removemddisk)    
        CCDB_REMOVE_MD_DISK $3
        exit 1
        ;; 
    setfaultdisk)    
        CCDB_SET_MD_FAULT_DISK $3
        exit 1
        ;;
    opermd)
        OPER_CMD=`echo ${@:3}`
        CCDB_OPER_MD_DEVICE_BY_INPUT_CMD "$OPER_CMD"
        exit 1
        ;;
    showccdbraiddisk)    
        CCDB_SHOW_RAID_DISK
        exit 1
        ;; 
    startnvramadapter)
	CCDB_START_NVRAM_ADAPTER $3
	exit 1
        ;;
    showclusterinfo)
	CCDB_SHOW_CLUSTER_INFO $3 $4
	exit 1
        ;;
    showlocalinfo)
        CCDB_SHOW_LOCAL_INFO $3 $4
        exit 1
        ;;
    preparedb)
        CCDB_PREPARE_LOCAL_DB $3 $4
        exit 1
        ;;
    backupalldbemergency)
        CCDB_BACKUP_AL_DB_EMERGENCY $3
        exit 1
        ;;
    operdbrecover)
        CCDB_OPERATION_DB_RECOVER $3 $4 $5
        exit 1
        ;;
    operccdbserver)
        CCDB_OPERATION_CCDB_SERVER $3
        exit 1
        ;;
esac

CCDB_CMD_HELP
