#!/bin/sh

Ori_ethtool_packet=0
Cur_ethtool_packet=0

Ori_ifconfig_packet=0
Cur_ifconfig_packet=0
Debug_on=0
debug_msg()
{
	if test $Debug_on = 1 ; then
		echo "$1"
	fi
}

try_arping()
{

	#check whether tx added by arping
	ori_tx_packets=`/sbin/ifconfig eth2|grep "TX packet"|awk -F':' '{print $2}'|awk -F' ' '{print $1}'`
	/sbin/arping 169.254.2.4 -I eth2 -c 1 > /dev/null 2>&1
	new_tx_packets=`/sbin/ifconfig eth2|grep "TX packet"|awk -F':' '{print $2}'|awk -F' ' '{print $1}'`
	debug_msg "`/bin/date` :$ori_tx_packets =? $new_tx_packets"

	if test "$ori_tx_packets" = "$new_tx_packets" ; then
		debug_msg "`/bin/date` :$ori_tx_packets = $new_tx_packets, do workaround"
		echo "`/bin/date` :$ori_tx_packets = $new_tx_packets, take action" >> /var/log/aqnrecord.log
		/sbin/ifconfig eth2 down
		sleep 3
		/sbin/ifconfig eth2 up
		#else
		#	echo "`/bin/date` :arping added  $ori_tx_packets, $new_tx_packets" >> /eth2_aping_workaround_result.txt
	fi
}

#main
while true; do
	#check link status
	link_status=`/bin/ethtool eth2|grep "Link detected" |awk -F': ' '{print $2}'`

	if test "$link_status" = "yes" ; then
		Cur_ethtool_packet=`/bin/ethtool -S eth2|grep "OutPackets"|grep Queue|awk -F': ' '{print $2}'`

		if test "$Cur_ethtool_packet" != "$Ori_ethtool_packet" ; then
			debug_msg "Cur_ethtool_packet != Ori_ethtool_packet"

			Cur_ifconfig_packet=`/sbin/ifconfig eth2|grep "TX packet"|awk -F':' '{print $2}'|awk -F' ' '{print $1}'`

			if test "$Ori_ifconfig_packet" = "$Cur_ifconfig_packet" ; then
				try_arping
			fi

		fi
		debug_msg "========start============"
		debug_msg "Ori_ethtool:$Ori_ethtool_packet"
		debug_msg "Cur_ethtool:$Cur_ethtool_packet"
		debug_msg " "
		debug_msg "Ori_ifcon:$Ori_ifconfig_packet  Cur_ifcon:$Cur_ifconfig_packet"
		debug_msg "========end=============="

		Ori_ethtool_packet=$Cur_ethtool_packet
		Ori_ifconfig_packet=$Cur_ifconfig_packet
	fi
	sleep 10
done

