#!/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
热门文章更多>>
标签更多>>
专题更多>>
最新文章更多>>
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
- 使用 C# 6.0 功能运行 TFS 构建