admin 管理员组文章数量: 1184232
一年总有那么几次莫明停电,公司的服务器经不起这样的折腾
写了一个断电后UPS备用电源自动关机的脚本
原理就是检测路由器网关是否能ping通,长时间持续ping不通视为停电了
路由器不要接到ups上
用法
1.检测网关是否持续ping不通300次(forever模式,添加到启动任务即可)
sudo ./ups_check forever 192.168.1.1 300
2.检测网关是否持续ping不通30次(once模式,需要配合crond使用)
sudo ./ups_check once 192.168.1.1 30
#!/bin/sh
# halt command
HALT=halt
ping_forever_host(){
IP=$1
time_out=$2
count=3
kernel=`uname -s`
total_miss=0
echo "ups check host '${IP}' for (${time_out}) seconds, [forever mode]"
# ping host, if miss recieved packets, then add to total_miss
while true
do
case $kernel in
VMkernel) #esxi 5.1
ret=`ping -c ${count} -W 1 ${IP} 2>&1|grep 'packets transmitted'|sed 's/.*, \(.*\) packets received,.*/\1/'`
;;
Darwin) #MacOS X 10.7.4
ret=`ping -c ${count} -W 1 ${IP} 2>&1|grep 'packets transmitted'|sed 's/.*, \(.*\) packets received,.*/\1/'`
;;
Linux) #ubuntu 12.04
ret=`ping -c ${count} -W 1 ${IP} 2>&1|grep 'packets transmitted'|sed 's/.*, \(.*\) received,.*/\1/'`
;;
*)
echo "Unknown Architecture $kernel"
exit 1
;;
esac
miss=$((count-ret))
if [ $miss -eq $count ]; then
total_miss=$((total_miss+miss))
echo "total_miss: ${total_miss} --> ${time_out}"
else
total_miss=0
fi
# if miss count over limit, then halt the computer!!!
if [ $total_miss -ge $time_out ]; then
echo "SYSTEM WILL HALT AT '`date`'!!!"
${HALT}
break;
fi
done
}
ping_once_host(){
IP=$1
count=$2
time_out=$2
kernel=`uname -s`
total_miss=0
echo "ups check host '${IP}' for (${time_out}) seconds, [once mode]"
# ping host, if miss recieved packets, then add to total_miss
case $kernel in
VMkernel) #esxi 5.1
ret=`ping -c ${count} -W 1 ${IP} 2>&1|grep 'packets transmitted'|sed 's/.*, \(.*\) packets received,.*/\1/'`
;;
Darwin) #MacOS X 10.7.4
ret=`ping -c ${count} -W 1 ${IP} 2>&1|grep 'packets transmitted'|sed 's/.*, \(.*\) packets received,.*/\1/'`
;;
Linux) #ubuntu 12.04
ret=`ping -c ${count} -W 1 ${IP} 2>&1|grep 'packets transmitted'|sed 's/.*, \(.*\) received,.*/\1/'`
;;
*)
echo "Unknown Architecture $kernel"
exit 1
;;
esac
miss=$((count-ret))
if [ $miss -eq $count ]; then
total_miss=$((total_miss+miss))
echo "total_miss: ${total_miss} --> ${time_out}"
else
total_miss=0
fi
# if miss count over limit, then halt the computer!!!
if [ $total_miss -ge $time_out ]; then
echo "SYSTEM WILL HALT AT '`date`'!!!"
${HALT}
fi
}
main(){
action=$1;
case $action in
forever) #run forever
ping_forever_host $2 $3
;;
once) # run once
ping_once_host $2 $3
;;
*)
echo "usage: sudo ./ups_check forever 192.168.2.1 120"
echo "usage: sudo ./ups_check once 192.168.2.1 60"
exit 1
;;
esac
}
main $1 $2 $3
转载于:https://wwwblogs/feixiablog/p/7146849.html
版权声明:本文标题:停电后,在UPS电源下服务器自动关机脚本 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1763797149a3271084.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论