#!/bin/bash
# USAGE: linkcheck.sh X Y -- if X not responding to ping for Y minutes then reboot (wakes up after every Y minutes)
# MUST BE RUN ONLY ONCE (IT WILL MODIFY ROOTS CRONTAB)
# FOR MANDRAKE 10 (SysV style)
#CRONPATH=/var/spool/cron/root
#cmSTOP_CROND="/etc/init.d/crond stop"
#cmSTART_CROND="/etc/init.d/crond start"
# FOR SLACKWARE (router)
cmSTART_CROND="/usr/sbin/crond -l10 >>/var/adm/cron 2>&1"
CRONPATH="/var/spool/cron/crontabs/root"
cmSTOP_CROND="/bin/killall crond"
IFACE="/etc/orionet/iface_external"
LOCKFILE="/var/lock/nolink.now"
cmPING="/bin/ping -qc 3"
cmREBOOT="/sbin/ifconfig $IFACE down; /sbin/ifconfig $IFACE up; /etc/rc.d/rc.inet1"
ADDR=$1
DELAY=$2
if [ $# = 0 ]; then
echo "USAGE: linkcheck.sh X Y -- if X not responding to ping for Y minutes then reboot (wakes up after every Y minutes)
MUST BE RUN ONLY ONCE (IT WILL MODIFY ROOTS CRONTAB)"
/usr/bin/logger -p syslog.info -t linkcheck.sh Started without paramenters. Exiting
exit 0
fi
#if started with 2 parameters then rewrite crontab else just check link with X and reboot if lockfile present
if [ $# = 2 ]; then
rm -f $LOCKFILE
echo Setting new parameters: ping $ADDR every $DELAY minutes
/usr/bin/logger -p syslog.info -t linkcheck.sh Setting new parameters: ping $ADDR every $DELAY minutes
if [ -f $CRONPATH ];
then sed -e '/linkcheck/d' $CRONPATH > /tmp/tmp.cron
mv -f /tmp/tmp.cron $CRONPATH
else echo "No crontab found at specified location... Exiting"
exit 99
fi
echo "Linkcheck: Removing previous startup line from root's crontab."
# stopping crond
$cmSTOP_CROND >> /var/log/syslog
if [ $? -eq 0 ]; then
echo "Stopping crond successful"
# adding new line to crontab
echo "*/$DELAY * * * * "/usr/local/openbill/router/bin/linkcheck.sh ${ADDR}"" >> $CRONPATH
# try to start crontab
$cmSTART_CROND >> /var/log/syslog
if [ $? -eq 0 ]; then
echo "Linkcheck: Starting crond successful."
else echo "Linkcheck: Starting crond failed."
exit 99
fi
else echo "Linkcheck: Killing crond failed."
exit 99
fi
exit 0
fi
# checking exit code of ping command...(if one argument given)
if [ $# = 1 ]; then
$cmPING $ADDR >> /dev/null
if [ $? -ne 0 ]; then
if [ -f $LOCKFILE ];
then
# link failed lockfile exist - restarting.
/usr/local/openbill/router/bin/mailsignal.sh No link to $ADDR. Restaring $IFACE by linkcheck.sh.
/usr/bin/logger -p syslog.error -t linkcheck.sh No link to $ADDR. Restaring interface $IFACE.
$cmREBOOT
if [ $? -ne 0 ]; then
/usr/bin/logger -p syslog.error -t linkcheck.sh Failed restarting $IFACE...
/usr/local/openbill/router/bin/mailsignal.sh Failed restaring $IFACE by linkcheck.sh.
fi
# link failed - create lock
else touch $LOCKFILE
/usr/bin/logger -p syslog.warning -t linkcheck.sh No link to host $ADDR. Restart of $IFACE will be forced next check.
exit 99
fi
# if lockfile exist and link is ok - removing lock.
else if [ -f $LOCKFILE ]; then
rm -fr $LOCKFILE
fi
fi
fi