检查网站状态脚本
浏览量:992
在我们生产场景中,需要及时了解线上服务状态,我们除了zabbix等健康,还可以本地通过执行脚本来检查网站是否正常,那么我们应该怎么写脚本呢?接下来我给大家一个我自己在生产场景用到的一个脚本:
#!/bin/bash
# this script is created by zhangyang.
# e_mail:s.c.young@hotmail.com
. /etc/init.d/functions
check_count=0
url_list=(
https://www.zhsir.org
https://www.car2share-booking.com
)
function wait()
{
echo -n '3秒后,执行检查URL操作.';
for ((i=0;i<3;i++))
do
echo -n ".";sleep 1
done
echo
}
function check_url()
{
wait
for ((i=0; i<`echo ${#url_list[*]}`; i++))
do
wget -o /dev/null -T 3 --tries=1 --spider ${url_list[$i]} >/dev/null 2>&1
if [ $? -eq 0 ]
then
action "${url_list[$i]}" /bin/true
else
action "${url_list[$i]}" /bin/false
fi
done
((check_count++))
}
main(){
while true
do
echo "-------check count:${check_count}---------"
check_url
echo "-------check count:${check_count}---------"
sleep 2
if [ ${check_count} -eq 5 ];then
break
fi
done
}
main执行脚本结果如下:
-------check count:0--------- 3秒后,执行检查URL操作.... https://www.zhsir.org [ OK ] https://www.car2share-booking.com [ OK ] -------check count:1--------- ........

神回复
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。