#!/bin/sh
# vpnchk -- Monitor VPN Connection and restart as necessary.
# A single parameter is required: vpnchk <peer_name>
# Ping REMOTE_VPN_HOST approximately every 10 seconds. Keep track of
# failed pings by incrementing COUNT. If pings are good, always reset
# COUNT back to zero. Only take corrective action when the number of
# failed pings reaches THRESH(hold). Notify root by mail whenever the
# status of the vpn connection has changed.
#
REMOTE_VPN_HOST=${1}
MAILTO=root@localhost
#
if [ "${REMOTE_VPN_HOST}" = "" ]; then
echo "Syntax: vpnchk <peer_name>"
exit
fi
#
CHK_TEXT="call ${REMOTE_VPN_HOST}"
THRESH=3
COUNT=0
while [ : ]; do # loop forever
if ping -c 5 ${REMOTE_VPN_HOST} 1>/dev/null 2>/dev/null ; then
COUNT=0
if [ -f /tmp/.vpn-down ]; then
rm -f /tmp/.vpn-down
MSG="VPN Connection is -UP-: `date "+%H:%M on %m/%d/%Y"`"
echo ${MSG} | mailx -s"${MSG}" ${MAILTO}
fi
else
COUNT=`expr ${COUNT} + 1`
if [ ${COUNT} -ge ${THRESH} ]; then
if [ ! -f /tmp/.vpn-down ]; then
touch /tmp/.vpn-down
MSG="VPN Connection is DOWN: `date "+%H:%M on %m/%d/%Y"`"
echo ${MSG} | mailx -s"${MSG}" ${MAILTO}
fi
PID=`ps -awwjx | grep -v grep | grep "${CHK_TEXT}" | awk '{print $2}'`
if [ ! "${PID}" = "" ]; then
for xPID in ${PID} ; do kill -KILL ${PID} ; done
COUNT=0
sleep 60
fi
nohup /usr/pkg/sbin/vpn fire start &
sleep 150
fi
fi
sleep 10
done
# end |