#!/bin/sh

#NOS slub_debug script
#A wrapper script to enable/disable/show slub_debug information from a linux prompt

mmip=(127.2.1.1 127.2.1.2 127.2.1.0 127.2.2.0 127.2.1.15 127.2.1.16)
lcip=(127.2.1.1 127.2.1.2 127.2.1.3 127.2.1.4 127.2.1.7 127.2.1.8 127.2.1.9 127.2.1.10 127.2.1.11 127.2.1.12 127.2.1.13 127.2.1.14 127.2.2.1 127.2.2.2 127.2.2.3 127.2.2.4 127.2.2.7 127.2.2.8 127.2.2.9 127.2.2.10 127.2.2.11 127.2.2.12 127.2.2.13 127.2.2.14 )
rebootrequired=0

func_cpu_alive_check ()
{
    #/usr/bin/rsh $1 'ls -al > /dev/null' >  /dev/null 2>&1
    /bin/ping $1 -c1 -W1  > /dev/null 2>&1
    return $?
}

slub_enable ()
{
	/sbin/bootenv bootargs | grep slub_debug > /dev/null
	if [ $? = 0 ];
	then
		return
	fi
	var=`/sbin/bootenv bootargs`
	var2=`echo $var slub_debug`
	/sbin/bootenv bootargs "$var2"
}

slub_disable ()
{
	/sbin/bootenv bootargs | grep slub_debug > /dev/null
	if [ $? = 1 ];
	then
		return
	fi
	var=`/sbin/bootenv bootargs | sed -e 's/\<slub_debug\>//g'`
	/sbin/bootenv bootargs "$var"
}

func_local_on_off ()
{
	if [ $3 = 1 ];
	then
		slub_enable
	else
		slub_disable
	fi
}

func_log ()
{
    SWBD=`/sbin/sin 2>/dev/null | /bin/grep SWBD | /usr/bin/cut -d ' ' -f 2 | /usr/bin/cut -d ',' -f 1`
    #No difference between 1000 and 1001 from number of MM/LC point of view.
    if [ $SWBD = "SWBD1001" ];
    then
        SWBD="SWBD1000"
    fi

	/bin/rm -fr /tmp/nos_slub_debug/
	mkdir /tmp/nos_slub_debug/

    for mm in ${mmip[@]}; 
    do
        func_cpu_alive_check ${mm}
        if [ ! $? = 0 ];
        then
            continue
        fi

		mkdir /tmp/nos_slub_debug/${mm}
        /bin/rcp ${mm}:/var/log/messages* /tmp/nos_slub_debug/${mm}
        /bin/rcp ${mm}:/var/log/consolelog_* /tmp/nos_slub_debug/${mm}
        /bin/rcp ${mm}:/var/log/slabinfov /tmp/nos_slub_debug/${mm}
        /usr/bin/rsh ${mm} 'cp /proc/consolelog /tmp/'
        /bin/rcp ${mm}:/tmp/consolelog /tmp/nos_slub_debug/${mm}
    done

    if [ $SWBD = "SWBD1000" ];
    then
        for lc in ${lcip[@]}; 
        do
            func_cpu_alive_check ${lc}
            if [ ! $? = 0 ];
            then
                continue
            fi

			mkdir /tmp/nos_slub_debug/${lc}
			/bin/rcp ${lc}:/var/log/messages* /tmp/nos_slub_debug/${lc}
        	/bin/rcp ${lc}:/var/log/consolelog_* /tmp/nos_slub_debug/${lc}
			/bin/rcp ${lc}:/var/log/slabinfov /tmp/nos_slub_debug/${lc}
        	/usr/bin/rsh ${lc} 'cp /proc/consolelog /tmp/'
			/bin/rcp ${lc}:/tmp/consolelog /tmp/nos_slub_debug/${lc}
        done
    fi
	cd /tmp/
	tar cfz nos_slub_debug.tar.gz nos_slub_debug/
	mv /tmp/nos_slub_debug.tar.gz /var/log/
	echo "slub_debug logs available under /var/log/nos_slub_debug.tar.gz"
	/bin/rm -fr /tmp/nos_slub_debug/
}

func_reboot_log ()
{
    SWBD=`/sbin/sin 2>/dev/null | /bin/grep SWBD | /usr/bin/cut -d ' ' -f 2 | /usr/bin/cut -d ',' -f 1`
    #No difference between 1000 and 1001 from number of MM/LC point of view.
    if [ $SWBD = "SWBD1001" ];
    then
        SWBD="SWBD1000"
    fi

    for mm in ${mmip[@]}; 
    do
        func_cpu_alive_check ${mm}
        if [ ! $? = 0 ];
        then
            continue
        fi

		dvar=`/bin/date +"%F-%H-%M"`
        /usr/bin/rsh ${mm} cp /proc/consolelog /var/log/consolelog_$dvar
    done

    if [ $SWBD = "SWBD1000" ];
    then
        for lc in ${lcip[@]}; 
        do
            func_cpu_alive_check ${lc}
            if [ ! $? = 0 ];
            then
                continue
            fi
			dvar=`/bin/date +"%F-%H-%M"`
        	/usr/bin/rsh ${lc} cp /proc/consolelog /var/log/consolelog_$dvar
        done
    fi
}

func_show ()
{
    SWBD=`/sbin/sin 2>/dev/null | /bin/grep SWBD | /usr/bin/cut -d ' ' -f 2 | /usr/bin/cut -d ',' -f 1`
    #No difference between 1000 and 1001 from number of MM/LC point of view.
    if [ $SWBD = "SWBD1001" ];
    then
        SWBD="SWBD1000"
    fi

    for mm in ${mmip[@]}; 
    do
        func_cpu_alive_check ${mm}
        if [ ! $? = 0 ];
        then
            continue
        fi

		slubcfgvar=`/usr/bin/rsh ${mm} '/bin/grep SLUB_DEBUG /etc/fabos/nos_debug.conf'`
		echo $slubcfgvar | grep SLUB_DEBUG > /dev/null
		slubcfgvar2=$?

		if [ $slubcfgvar2 = 0 ];
		then
        	echo "MM ${mm}: slub_debug enabled at compile time. It can't be changed."
			continue
		fi

        var=`/usr/bin/rsh ${mm} '/sbin/bootenv bootargs'`
		echo $var | grep slub_debug > /dev/null
		var2=$?
		if [ $var2 = 0 ];
		then
        	echo "MM ${mm}: slub_debug enabled"
		fi

		if [ ! $var2 = 0 ] && [ ! $slubcfgvar2 = 0 ];
		then
        	echo "MM ${mm}: slub_debug not enabled"
		fi

    done

    if [ $SWBD = "SWBD1000" ];
    then
        for lc in ${lcip[@]}; 
        do
            func_cpu_alive_check ${lc}
            if [ ! $? = 0 ];
            then
                continue
            fi

			slubcfgvar=`/usr/bin/rsh ${lc} '/bin/grep SLUB_DEBUG /etc/fabos/nos_debug.conf'`
			echo $slubcfgvar | grep SLUB_DEBUG > /dev/null
			slubcfgvar2=$?

			if [ $slubcfgvar2 = 0 ];
			then
				echo "LC ${lc}: slub_debug enabled at compile time. It can't be changed."
			fi

			var=`/usr/bin/rsh ${lc} '/sbin/bootenv bootargs'`
			echo $var | grep slub_debug > /dev/null
			var2=$?
			if [ $var2 = 0 ];
			then
				echo "LC ${lc}: slub_debug enabled"
			fi

			if [ ! $var2 = 0 ] && [ ! $slubcfgvar2 = 0 ];
			then
				echo "LC ${lc}: slub_debug not enabled"
			fi
        done
    fi
}

func_enable_disable_slub_debug_all ()
{
    SWBD=`/sbin/sin 2>/dev/null | /bin/grep SWBD | /usr/bin/cut -d ' ' -f 2 | /usr/bin/cut -d ',' -f 1`
    #No difference between 1000 and 1001 from number of MM/LC point of view.
    if [ $SWBD = "SWBD1001" ];
    then
        SWBD="SWBD1000"
    fi

    for mm in ${mmip[@]}; 
    do
        func_cpu_alive_check ${mm}
        if [ ! $? = 0 ];
        then
            continue
        fi

		slubcfgvar=`/usr/bin/rsh ${mm} '/bin/grep SLUB_DEBUG /etc/fabos/nos_debug.conf'`
		echo $slubcfgvar | grep SLUB_DEBUG > /dev/null
		slubcfgvar2=$?

		#If slub_debug was enabled via linux config, it can't be disabled or enabled.
		if [ $slubcfgvar2 = 0 ];
		then
        	echo "MM ${mm}: slub_debug enabled at compile time. It can't be changed."
			continue
		fi

		if [ $1 = 1 ];
		then
        	/usr/bin/rsh ${mm} '/fabos/bin/nos_slub_debug -i -a 1'
		else
        	/usr/bin/rsh ${mm} '/fabos/bin/nos_slub_debug -i -a 0'
		fi
		rebootrequired=1
    done

    if [ $SWBD = "SWBD1000" ];
    then
        for lc in ${lcip[@]}; 
        do
            func_cpu_alive_check ${lc}
            if [ ! $? = 0 ];
            then
                continue
            fi

			slubcfgvar=`/usr/bin/rsh ${lc} '/bin/grep SLUB_DEBUG /etc/fabos/nos_debug.conf'`
			echo $slubcfgvar | grep SLUB_DEBUG > /dev/null
			slubcfgvar2=$?

			#If slub_debug was enabled via linux config, it can't be disabled or enabled.
			if [ $slubcfgvar2 = 0 ];
			then
				echo "LC ${lc}: slub_debug enabled at compile time. It can't be changed."
				continue
			fi

			if [ $1 = 1 ];
			then
				/usr/bin/rsh ${lc} '/fabos/bin/nos_slub_debug -i -a 1'
			else
				/usr/bin/rsh ${lc} '/fabos/bin/nos_slub_debug -i -a 0'
			fi
			rebootrequired=1
        done
    fi
}


func_usage ()
{
    echo "Usage : nos_slub_debug -a <1|0>"
    echo "      : to turn off eslub_debug use -a 0"
    echo "      : to turn on eslub_debug use -a 1"
    echo "Usage : nos_slub_debug -s"
    echo "      : to show where all slub_deubg is enabled"
    echo "Usage : nos_slub_debug -e"
    echo "      : to collect all debug log"
    echo "Usage : nos_slub_debug -i -a <1|0>"
    echo "      : Internal option. Not to be used by others"
    echo "Usage : nos_slub_debug -r"
    echo "      : Internal option. Not to be used by others"
    echo ""
}

if [ $# = 0 ];
then
	func_usage 
	exit 1
fi

if [ $1 = "-i" ];
then
	if [ ! $# = 3 ];
	then
		func_usage 
		exit 1
	fi

	func_local_on_off $1 $2 $3
	exit 0
fi

if [ $1 = "-a" ];
then
	if [ ! $# = 2 ];
	then
		func_usage 
		exit 1
	fi

	echo "This command execution will take 60-120 seconds"
	func_enable_disable_slub_debug_all $2

	if [ $rebootrequired = 1 ];
	then
		echo "Reboot required for the changes to take effect"
	fi
	exit 0
fi

if [ $1 = "-s" ];
then
	if [ ! $# = 1 ];
	then
		func_usage 
		exit 1
	fi

	echo "This command execution will take 60-120 seconds"
	func_show
	exit 0
fi

if [ $1 = "-e" ];
then
	if [ ! $# = 1 ];
	then
		func_usage 
		exit 1
	fi

	echo "This command execution will take 60-120 seconds"
	func_log
	exit 0
fi

if [ $1 = "-r" ];
then
	if [ ! $# = 1 ];
	then
		func_usage 
		exit 1
	fi

	func_reboot_log
	exit 0
fi

exit 0

