#!/bin/sh
## Check whether the ethernet interface is up, but has no IP address
## If so try to get the interface working properly again

DATE=$(date +%Y_%m_%d)
FILENAME=/var/sqllog/net_log_$DATE.log
TIME=$(date)

IFACEUP=$(/sbin/ifconfig | sed -n '/^\<eth[0]\>/{p}' | wc -l)
HASIP=$(/sbin/ifconfig | sed -n '/^\<eth[0]\>/{N;p}' | sed '/^[ ]*inet addr:/!d; s/^[ ]*inet addr://;q' | sed 's/ .*$//' | wc -l)
COUNT=0
MAXCOUNT=5

# Is eth0 interface down?
if [ $IFACEUP -eq 0 ]
then
	echo "Eth0 interface is down"
	echo "$TIME : Eth0 interface is down. Going to exit and wait for the system to bring it up" >> $FILENAME
	exit 0
fi

if [ $HASIP -gt 0 ]
then
	echo "Eth0 is up and has IP. Do nothing."
	exit 0
fi

while [ $COUNT -lt $MAXCOUNT ]
do
	/etc/init.d/networking restart

	HASIP=$(/sbin/ifconfig | sed -n '/^\<eth[0]\>/{N;p}' | sed '/^[ ]*inet addr:/!d; s/^[ ]*inet addr://;q' | sed 's/ .*$//' | wc -l)

	if [ $HASIP -gt 0 ]
	then
		TIME=$(date)
		echo "Restart worked. Eth0 is up and has IP."
		echo "$TIME : Count = $COUNT. Restart worked. Eth0 is up and has IP." >> $FILENAME
		exit 0
	fi

	(( COUNT++ ))
	echo "COUNT: $COUNT"
	sleep 1
done

TIME=$(date)
echo "Final COUNT: $COUNT"
echo "$TIME : Count = $COUNT. Restart failed. Eth0 is up but does not have an IP address. Will try again later." >> $FILENAME
exit 0

