#!/bin/sh

func_perf()
{
	/data/bin/script -q /tmp/perf_script.log -c "/data/bin/perf top >/var/log/debug/collect_perf.log" >/dev/null 2>&1
}

func_top()
{
	sh -c '/bin/top -d 5 >/var/log/debug/collect_top.log ' >/dev/null 2>&1
}

func_tcpdump()
{
	file=/var/log/debug/collect_tcpdump_para.txt
	intf_buff=port1
	filter=tcp
	max_cnt=400
	tmp_buff=/var/log/debug/collect_tcpdump.pcap.tmp

	enable=$(cat $file | grep enable | awk -F '=' '{print $2}')
	if [ "$enable" == 1 ]; then
		intf_buff=$(cat $file| grep intf_buff|awk -F '=' '{print $2}')
		filter=$(cat $file| grep filter|awk -F '=' '{print $2}')
		max_cnt=$(cat $file| grep max_cnt|awk -F '=' '{print $2}')
		
		sh -c "/data/bin/tcpdump -i $intf_buff \"$filter\" -w $tmp_buff -c $max_cnt -n" >/dev/null 2>&1
	fi
}

func_other()
{
	echo "-----------ifconfig------------"
	ifconfig
	echo "-------------------------------"

	echo "------------netstat -nlt-------"
	netstat -nlt
	echo "-------------------------------"

	echo "------------free -m------------"
	free -m
	echo "-------------------------------"

	echo "------------df -h--------------"
	df -h	
	echo "-------------------------------"

	echo "------------route -n-----------"
	route -n
	echo "-------------------------------"
}

restore_tcpdump_para()
{
	file=/var/log/debug/collect_tcpdump_para.txt

	echo enable=1 > $file
	echo intf_buff=any >> $file
	echo filter= >> $file
	echo max_cnt=4000 >> $file
}
clean_file_tmp()
{
	rm /tmp/perf-vdso.so-* /tmp/perf_script.log >/dev/null 2>&1
}
func_stop()
{
	func_check_collect_inline 
	inline_flag=$?
	if [ $inline_flag == 1 ]; then
		#echo 'online kill'
		pid_tmp=`ps | grep '/data/bin/perf top'| grep -v grep| grep -v sh| awk '{print $1}'`
		if [ "$pid_tmp" != '' ]; then
			kill -9 $pid_tmp
		fi	

		pid_tmp=`ps | grep '/bin/top -d 5'| grep -v grep| grep -v sh| awk '{print $1}'`
		if [ "$pid_tmp" != '' ]; then
			kill -9 $pid_tmp
		fi

		pid_tmp=`ps | grep '/data/bin/tcpdump'| grep -v grep| grep -v sh| awk '{print $1}'`
		if [ "$pid_tmp" != '' ]; then
			kill -9 $pid_tmp
		fi
		tmp_buff=/var/log/debug/collect_tcpdump.pcap.tmp
		file_buff=/var/log/debug/collect_tcpdump.pcap
		mv $tmp_buff $file_buff >/dev/null 2>&1
		#restore_tcpdump_para 
	fi

	#echo 'clean'
	clean_file_tmp;
}

func_check_collect_inline()
{
	pid_tmp=`ps | grep 'collect_script start' | grep -v grep| awk '{print $1}'`
	#echo $pid_tmp
	if [ "$pid_tmp" != '' ]; then
		pid_tmp=`ps | grep '/data/bin/perf top'| grep -v grep| grep -v sh| awk '{print $1}'`
		if [ "$pid_tmp" != '' ]; then
			return 1;
		fi	

		pid_tmp=`ps | grep '/bin/top -d 5'| grep -v grep| grep -v sh| awk '{print $1}'`
		if [ "$pid_tmp" != '' ]; then
			return 1;
		fi

		return 0;
	else
		return 0;
	fi	
}

func_help()
{
	echo 'usage:'
	echo 'collect_script perf'
	echo 'collect_script top'
	echo 'collect_script tcpdump'
	echo 'collect_script other'
	echo 'collect_script stop'
	echo 'collect_script check'
}

if [ $# -ne 1 ]; then
        func_help;
        exit 1
fi

if [ $1 == 'perf' ]; then
        func_perf;
elif [ $1 == 'top' ]; then
        func_top;
elif [ $1 == 'tcpdump' ]; then
        func_tcpdump 
elif [ $1 == 'other' ]; then
        func_other > /var/log/debug/collect_other.log;
elif [ $1 == 'stop' ]; then
        func_stop;
elif [ $1 == 'start' ]; then
	func_check_collect_inline
	inline_flag=$?
	#echo $inline_flag
	if [ $inline_flag == 1 ]; then
		#echo 'online exit'
		exit 1;
	else 
		#echo 'no online start'

		func_perf &
		func_top  &
		func_tcpdump &
		func_other  > /var/log/debug/collect_other.log &

		wait
	fi
elif [ $1 == 'check' ]; then
	func_check_collect_inline 
	inline_flag=$?
	if [ $inline_flag == 1 ]; then
		echo 'online'
	else 
		echo 'no online'
	fi
else
        func_help;
fi


