Skip to content

Instantly share code, notes, and snippets.

@ptthisdan
Created March 13, 2021 05:19
Show Gist options
  • Save ptthisdan/d2351d2fe00e05ef023aed09cbb6e4e6 to your computer and use it in GitHub Desktop.
Save ptthisdan/d2351d2fe00e05ef023aed09cbb6e4e6 to your computer and use it in GitHub Desktop.
ping the router, if ping fail>N, shutdown NAS
#!/bin/bash
# @author JasonLiu
# @since 2019/12/25
# @description ping the router,if power shutdown,router will shutdown,ping will fail,if ping fail>N,shutdown NAS
# idea first come from http://koolshare.cn/thread-120821-1-1.html
# */1 * * * * /share/Project/shell/powerchk.v5.sh >> /share/Project/shell/log/power.log
# crontab /etc/config/crontab && /etc/init.d/crond.sh restart
function usage()
{
echo "$ARG0 usage:
$ARG0 -c check parameter
$ARG0 -h help
$ARG0 -d drill,not really shutdown
"
}
# write qnap log
log=true
# UPS live minute
live_min=10
# nas shutdown cost minute
shutdown_min=5
# buffer minute
buffer_min=3
# ping check count
check_cnt=5
# ping check interval seconds
interval_sec=3
# ping fail shutdown count
fail_shutdown_cnt=2
# ping ip,default can set router
ip=192.168.50.1
# script crontab iterval
task_interval_sec=60
# shutdown cost second= shutdown cost + buffer time
shutdown_buffer_cost_min=`expr $shutdown_min + $buffer_min`
#echo "shutdown_buffer_cost_min = $shutdown_buffer_cost_min"
shutdown_buffer_cost_sec=`expr $shutdown_buffer_cost_min \* 60`
#echo "shutdown_buffer_cost_sec = $shutdown_buffer_cost_sec"
# check cost = check interval * check_count
check_cost_max_sec=`expr $interval_sec \* $check_cnt`
#echo "check_cost_max_sec = $check_cost_max_sec"
# check can use seconds = live_min - shutdown_cost - one_crontab_interval
check_can_use_sec=`expr $live_min \* 60 - $shutdown_buffer_cost_sec - $check_cost_max_sec - $task_interval_sec`
#echo "check_can_use_sec = $check_can_use_sec"
DRILL=false
while getopts "dhc" opts
do
case $opts in
c)
echo "shutdown cost(s) = $shutdown_buffer_cost_sec s"
echo "ping check cost(s) = $check_cost_max_sec s"
echo "ping check can use(s) = $check_can_use_sec s"
if [ $check_cost_max_sec -gt $check_can_use_sec ]; then
echo "alert:ping check cost time < ping can use time !!! may got not enought time to shutdown"
else
echo "parameter all OK!!!"
fi
exit;
;;
h)
usage; exit;
;;
d)
DRILL=true
esac
done
now=`date "+%Y-%m-%d %H:%M:%S"`
# ping fail counter
fail_cnt=1
while [ $fail_cnt -le $check_cnt ]; do
if ping -c 1 $ip > /dev/null; then
echo "$now $ip ping success"
if [ $log = true ]; then
write_log "[powercheck] ping succ" 4
fi
break
else
echo "$ip ping fail,count $fail_cnt"
let fail_cnt++
if [ $fail_cnt -ge $fail_shutdown_cnt ]; then
echo "$now $ip ping fail,fail count $fail_cnt great than $fail_shutdown_cnt,start to shutdown NAS"
if [ $log = true ]; then
write_log "ping fail, shut down NAS" 2
fi
if [ $DRILL = true ]; then
echo "drill end ,$ip check fail"
exit;
else
/sbin/poweroff
fi
break
fi
sleep $interval_sec
fi
done
# Fix QNAP NAS crontab issue:
# Add This in /etc/config/crontab
# */1 * * * * bash /share/CACHEDEV1_DATA/homes/xiadan/.Qsync/ups/powerchk.sh
# Then
# crontab /etc/config/crontab && /etc/init.d/crond.sh restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment