Created
December 6, 2015 07:52
-
-
Save xiaohui-zhangxh/597c822d5bbcadd12ffe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
PING_COUNT=10 | |
INFO_LOG=/var/log/ups_monitor.log | |
ERROR_LOG=/var/log/ups_monitor.err | |
STATUS_LOG=/tmp/ups_monitor.log | |
SHUTDOWN_LOG=/var/log/ups_monitor_shutdown.log | |
LOCKER=/tmp/ups_monitor.lock | |
SHUTDOWN_COUNTER=5 | |
exec 1>$INFO_LOG; | |
exec 2>$ERROR_LOG; | |
if [ -f $LOCKER ]; then | |
echo "I'm running in another process" | |
exit 0; | |
fi | |
touch $LOCKER | |
shutdown_myself() { | |
echo "shuting down system..." | |
echo `date` >> $SHUTDOWN_LOG | |
shutdown -hP now | |
} | |
update_status() { | |
echo "Updating status '$1' to $STATUS_LOG" | |
touch $STATUS_LOG | |
counter=`cat $STATUS_LOG` | |
if [ $1 = "online" ]; then | |
echo > $STATUS_LOG | |
return 0 | |
fi | |
if [ $1 = 'offline' ]; then | |
if [[ $counter ]]; then | |
counter=$(($counter+1)); | |
else | |
counter=1; | |
fi | |
echo " counter is $counter" | |
if ((counter >= SHUTDOWN_COUNTER)); then | |
shutdown_myself | |
else | |
echo $counter > $STATUS_LOG | |
fi | |
return 0; | |
fi | |
return 1; | |
} | |
if [[ $1 ]]; then | |
gateway=$1 | |
else | |
gateway=`route -n | grep -e 'UG'|awk '{ print $2 }'` | |
fi | |
echo Gateway is $gateway | |
ping_failed=`ping -c $PING_COUNT ${gateway}|grep '100% packet loss'` | |
if [[ $ping_failed ]]; then | |
update_status 'offline' | |
else | |
update_status 'online' | |
fi | |
rm $LOCKER |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment