#!/bin/bash

# Signalling
trap 'exit 0' SIGTERM
trap '' SIGHUP

# grep
arg1="NETDEV WATCHDOG: eth0: transmit timed out"
#arg2="dhcpd: receive_packet failed on eth0: Network is down"

# Syslog
tag=$0
prio="kern.err"

fifo=/var/log/check_nic

IFCONFIG=$(which ifconfig)
SLEEP=$(which sleep)
EGREP=$(which egrep)
DATE=$(which date)
HOSTNAME=$(which hostname)
LOGGER=$(which logger)

while true
do
        read line < $fifo
        echo $line | \
          $EGREP -e "$arg1" \
          > /dev/null 2>&1
        if [ $? -eq 0 ] ; then
                $IFCONFIG eth0 down
                $SLEEP 1
                $IFCONFIG eth0 up
                $LOGGER -t $tag -p $prio "eth0 down, restarting..." 
                $SLEEP 5
        fi
done