#!/bin/bash
#
# chkconfig: 2345 32 90
#description: i2node
### BEGIN INIT INFO
# Provides: i2node
# Required-Start: $network $remote_fs
# Required-Stop: $network $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Start the i2node daemon
### END INIT INFO

i2_start_arg=$1

# source function library
#. /etc/rc.d/init.d/functions
. /usr/local/sdata/scripts/check_sfs.sh
source /usr/local/sdata/scripts/script_bootup.sh

RETVAL=0
PS_I2NODE="success"
prog="i2node"

# Some functions to make the below more readable
SFS=sfs.ko
SFS3=/usr/local/sdata/modules/sfs3.ko
DATT=dattobd.ko
DATTOBD=/usr/local/sdata/modules/dattobd.ko
REISERFS=/usr/local/sdata/modules/reiserfs.ko
SDATAD=/usr/local/sdata/sbin/sdatad
SREPD=/usr/local/sdata/sbin/srepd
RPCSERVER=/usr/local/sdata/sbin/rpcserver
SLOGD=/usr/local/sdata/sbin/sdatalogd
I2HA=/usr/local/sdata/sbin/I2Availability
I2MON=/usr/local/sdata/sbin/i2monitor
I2HAD=/usr/local/sdata/sbin/HaDetector
COLLECTOR=/usr/local/sdata/sbin/collector

I2_STROAGE_OPTIMIZE_SH=/usr/local/sdata/scripts/storage_optimize.sh

runlevel=$(set -- $(runlevel); eval "echo \$$#" )

IPTABLES=iptables
IPTABLES_SAVE=iptables-save

IP6TABLES=ip6tables
IP6TABLES_SAVE=ip6tables-save
RPCPORT="26821"
RCV_PORT="26831"
MIR_PORT="26832"
REP_PORT="26833"
HYPERV_PORT="26835"
HA_HEARTBEAT_PORT="26850"
HA_ARBIT_PORT="26868"
SBT_DATA_PORT="26876"
ORACOPY_DATA_PORT="26895"
DB2_PORT="26887"
MSQ_PORT="26834"
NAS_SREPD_PORT="26873"
NAS_SDATAD_PORT="26871"
i2fw_save()
{
    #Debian/Ubuntu: /etc/iptables/rules.v4 & /etc/iptables/rules.v6
    # apt install iptables-persistent

    #RHEL/CentOS: /etc/sysconfig/iptables & /etc/sysconfig/ip6tables
    [ ! -d "/etc/iptables/" ] && mkdir -p /etc/iptables/
    [ ! -d "/etc/sysconfig/" ] && mkdir -p /etc/sysconfig/

    #netfilter-persistent save | /etc/init.d/iptables-persistent save
    $IPTABLES_SAVE > /etc/iptables/rules.v4 2>&1
    $IP6TABLES_SAVE > /etc/iptables/rules.v6 2>&1

    #service iptables save
    $IPTABLES_SAVE > /etc/sysconfig/iptables 2>&1
    #service ip6tables save
    $IP6TABLES_SAVE > /etc/sysconfig/ip6tables 2>&1
}

i2fw()
{
	filter=`lsmod | grep ^iptable_filter | awk -F' ' '{print $1}'`
	refnum=`lsmod | grep ^iptable_filter | awk -F' ' '{print $3}'`
	if [ -n "$filter" ]; then
		if [ $refnum -gt 0 ]; then
			$IPTABLES -F i2soft > /dev/null 2>&1
			$IPTABLES -D INPUT -j i2soft > /dev/null 2>&1
			$IPTABLES -N i2soft > /dev/null 2>&1

			$IPTABLES -A i2soft -p tcp --dport $RPCPORT -j ACCEPT

			$IPTABLES -A i2soft -p tcp --dport $RCV_PORT -j ACCEPT
			$IPTABLES -A i2soft -p tcp --dport $MIR_PORT -j ACCEPT
			$IPTABLES -A i2soft -p tcp --dport $REP_PORT -j ACCEPT

			$IPTABLES -A i2soft -p tcp --dport $HYPERV_PORT -j ACCEPT

			$IPTABLES -A i2soft -p tcp --dport $HA_HEARTBEAT_PORT -j ACCEPT
			$IPTABLES -A i2soft -p tcp --dport $HA_ARBIT_PORT -j ACCEPT

			$IPTABLES -A i2soft -p tcp --dport $NAS_SREPD_PORT -j ACCEPT
			$IPTABLES -A i2soft -p tcp --dport $NAS_SDATAD_PORT -j ACCEPT

			$IPTABLES -A i2soft -p tcp --dport $SBT_DATA_PORT -j ACCEPT
			$IPTABLES -A i2soft -p tcp --dport $ORACOPY_DATA_PORT -j ACCEPT

			$IPTABLES -A i2soft -p tcp --dport $DB2_PORT -j ACCEPT

			$IPTABLES -A i2soft -p tcp --dport $MSQ_PORT -j ACCEPT


			$IPTABLES -A i2soft -j RETURN

			$IPTABLES -I INPUT -j i2soft
            i2fw_save
		fi;
	fi;
}

i2fw_del()
{
	i2chain=`iptables --list | grep i2soft`
	if [ -n "$i2chain" ]; then
		$IPTABLES -F i2soft
		$IPTABLES -D INPUT -j i2soft
		$IPTABLES -X i2soft
        i2fw_save
	fi
}


i2fw_ipv6()
{
	filter=`lsmod | grep ^ip6table_filter | awk -F' ' '{print $1}'`
	refnum=`lsmod | grep ^ip6table_filter | awk -F' ' '{print $3}'`
	if [ -n "$filter" ]; then
		if [ $refnum -gt 0 ]; then
			$IP6TABLES -F i2soft_ipv6 > /dev/null 2>&1
			$IP6TABLES -D INPUT -j i2soft_ipv6 > /dev/null 2>&1
			$IP6TABLES -N i2soft_ipv6 > /dev/null 2>&1

			$IP6TABLES -A i2soft_ipv6 -p tcp --dport $RPCPORT -j ACCEPT

			$IP6TABLES -A i2soft_ipv6 -p tcp --dport $RCV_PORT -j ACCEPT
			$IP6TABLES -A i2soft_ipv6 -p tcp --dport $MIR_PORT -j ACCEPT
			$IP6TABLES -A i2soft_ipv6 -p tcp --dport $REP_PORT -j ACCEPT

			$IP6TABLES -A i2soft_ipv6 -p tcp --dport $HYPERV_PORT -j ACCEPT

			$IP6TABLES -A i2soft_ipv6 -p tcp --dport $HA_HEARTBEAT_PORT -j ACCEPT
			$IP6TABLES -A i2soft_ipv6 -p tcp --dport $HA_ARBIT_PORT -j ACCEPT

			$IP6TABLES -A i2soft_ipv6 -p tcp --dport $NAS_SREPD_PORT -j ACCEPT
			$IP6TABLES -A i2soft_ipv6 -p tcp --dport $NAS_SDATAD_PORT -j ACCEPT

			$IP6TABLES -A i2soft_ipv6 -p tcp --dport $SBT_DATA_PORT -j ACCEPT
			$IP6TABLES -A i2soft_ipv6 -p tcp --dport $ORACOPY_DATA_PORT -j ACCEPT

			$IP6TABLES -A i2soft_ipv6 -p tcp --dport $DB2_PORT -j ACCEPT

			$IP6TABLES -A i2soft_ipv6 -p tcp --dport $MSQ_PORT -j ACCEPT

			$IP6TABLES -A i2soft_ipv6 -j RETURN

			$IP6TABLES -I INPUT -j i2soft_ipv6
            i2fw_save
		fi;
	fi;
}

i2fw_del_ipv6()
{
	i2chain=`ip6tables --list | grep i2soft_ipv6`
	if [ -n "$i2chain" ]; then
		$IP6TABLES -F i2soft_ipv6
		$IP6TABLES -D INPUT -j i2soft_ipv6
		$IP6TABLES -X i2soft_ipv6
        i2fw_save
	fi
}
insmod_sfs()
{
    local sfs_conf
    local kver
    kver=`uname -r|awk -F "." '{print $1}'`
    sfs_conf="/etc/sdata/sfs.conf"
    if [ -f "$sfs_conf" ]; then
        TMP=`cat $sfs_conf | grep 'insmodsfs'`
        if [ "x$TMP" = "xinsmodsfs = 0" -o "x$TMP" = "xinsmodsfs=0" ]; then
            return 0
        fi
    fi

    local sfs_param_conf
    sfs_param_conf="/etc/sdata/sfs_param.conf"
    skip_writepages_arg=1
    if [ -f "$sfs_param_conf" ]; then
        TMP=`cat $sfs_param_conf | grep 'skip_writepages'`
        if [ "x$TMP" != "x" ]; then
            if [ "x$TMP" = "xskip_writepages = 0" -o "x$TMP" = "xskip_writepages=0" ]; then
                skip_writepages_arg=0
            fi
        fi
    fi

    cksfs_ds_drv_compat
    cksfs_check_4_insmod
    can_insmod=$?
    if [ $can_insmod -eq 0 ]
    then
        if [ $kver -eq 2 ]; then
            insmod $SFS3 > /dev/null 2>&1
        else
            insmod $SFS3 skip_writepages=$skip_writepages_arg skip_fallocate=$skip_writepages_arg> /dev/null 2>&1
        fi

        if [ $? -eq 0 ]
        then
            cksfs_create_file_kernel
        fi
    fi
    insmod $DATTOBD max_snap_devices=128 > /dev/null 2>&1
    return 0
}

rpc_start()
{
   kernel=`uname -r`
   if [ $kernel == "3.0.13-0.27-default" ] ; then
      export LD_LIBRARY_PATH=/usr/local/lib64
   fi
   $RPCSERVER
   if [ $kernel == "3.0.13-0.27-default" ] ; then
      export LD_LIBRARY_PATH=
   fi
}

start()
{
	echo -n $"Starting $prog: "
	insmod_sfs
	insmod $REISERFS > /dev/null 2>&1
	ulimit -c unlimited >/dev/null 2>&1
	do_bootup_script_by_flappy >/dev/null 2>&1

	$SDATAD
	$SREPD
	rpc_start
	$SLOGD
	$SBKLOGD
	$I2HA
	$I2MON
        $I2HAD
	$COLLECTOR
	
	i2fw
	i2fw_ipv6

	. $I2_STROAGE_OPTIMIZE_SH i2node

	RETVAL=$?
	echo
}

stop()
{
	echo -n $"Stopping $prog: "
	killall -9 sdatad
	killall -9 srepd
	killall -9 rpcserver
	killall -9 sdatalogd
	killall -9 I2Availability
	killall -9 i2monitor
        killall -9 collector
	#killall -9 HaDetector
	#i2fw_del
	#i2fw_del_ipv6
	sdwork -u && rmmod $SFS > /dev/null 2>&1
	rmmod $REISERFS > /dev/null 2>&1
	rmmod $DATT > /dev/null 2>&1
	RETVAL=$?
	echo
}

reload()
{
	echo -n $"Reloading $prog: "
	killall -9 sdatad
	killall -9 srepd
	killall -9 rpcserver
	killall -9 sdatalogd
	killall -9 I2Availability
	killall -9 i2monitor
    #killall -9 HaDetector
        killall -9 collector
	sdwork -u && rmmod $SFS > /dev/null 2>&1
	rmmod $REISERFS > /dev/null 2>&1
	rmmod $DATT > /dev/null 2>&1
	sleep 2
	insmod_sfs
	insmod $REISERFS > /dev/null 2>&1
	ulimit -c unlimited >/dev/null 2>&1
	$SDATAD
	$SREPD
	$RPCSERVER
	$SLOGD
	$SBKLOGD
	$I2HA
	$I2MON
        $I2HAD
        $COLLECTOR
	RETVAL=$?
	echo
}

fwrestart()
{	echo "i2fw del"
        i2fw_del
        i2fw_del_ipv6
	echo "i2fw add"
	i2fw
        i2fw_ipv6
	echo "i2fw restart success"
}

status()
{
	i_sdatad=$(pgrep sdatad)
	i_srepd=$(pgrep srepd)
	i_HaDetector=$(pgrep HaDetector)
	i_rpcserver=$(pgrep rpcserver)
	i_I2Availability=$(pgrep I2Availability)
	i_sdatalogd=$(pgrep sdatalogd)
	i_i2monitor=$(pgrep i2monitor)
        i_collector=$(pgrep collector)
	if [ -n "$i_sdatad" ];then
		echo -e "\033[40;34m ********sdatad is running********\033[0m"
	else
		echo -e "\033[40;31m ********sdatad is stopped**********\033[0m"
		PS_I2NODE="fail"
	fi
	if [ -n "$i_srepd" ];then
		echo -e "\033[40;34m ********srepd is running*********\033[0m"
	else
		echo -e "\033[40;31m ********srepd is stopped***********\033[0m"
		PS_I2NODE="fail"
	fi
	if [ -n "$i_HaDetector" ];then
		echo -e "\033[40;34m ********HaDetector is running****\033[0m"
	else
		echo -e "\033[40;31m ********HaDetector is stopped******\033[0m"
		PS_I2NODE="fail"
	fi
	if [ -n "$i_rpcserver" ];then
		echo -e "\033[40;34m ********rpcserver is running*****\033[0m"
	else
		echo -e "\033[40;31m ********rpcserver is stopped*******\033[0m"
		PS_I2NODE="fail"
	fi
	if [ -n "$i_I2Availability" ];then
		echo -e "\033[40;34m ********I2Availability is running\033[0m"
	else
		echo -e "\033[40;31m ********I2Availability is stopped**\033[0m"
		PS_I2NODE="fail"
	fi
	if [ -n "$i_sdatalogd" ];then
		echo -e "\033[40;34m ********sdatalogd is running*****\033[0m"
	else
		echo -e "\033[40;31m ********sdatalogd is stopped*******\033[0m"
		PS_I2NODE="fail"
	fi
	if [ -n "$i_i2monitor" ];then
		echo -e "\033[40;34m ********i2monitor is running*****\033[0m"
	else
		echo -e "\033[40;31m ********i2monitor is stopped*******\033[0m"
		PS_I2NODE="fail"
	fi
        if [ -n "$i_collector" ];then
                echo -e "\033[40;34m ********collector is running*****\033[0m"
        else
                echo -e "\033[40;31m ********collector is stopped*******\033[0m"
                PS_I2NODE="fail"
        fi
	if [ "$PS_I2NODE" = "success" ];then
		echo -e "\033[40;32m ########i2node is running########\033[0m"
		RETVAL=0
	else
		echo -e "\033[40;31m ########i2node is stopped #########\033[0m"
		RETVAL=3
	fi
}

case "$i2_start_arg" in
	start)
		start
                fwrestart
		;;
	stop)
		stop
		;;
	restart)
		stop
		sleep 2
		start
                fwrestart
		;;
	reload)
		reload
		;;
	status)
		status
		;;
	fwrestart)
		fwrestart
		;;
	*)
		echo $"Usage: $0 {start|stop|restart|reload|status|fwrestart}"
		RETVAL=1
esac
exit $RETVAL
