写了个shell脚本 监控网站 是否能ping通(前提是你iptables设置允许ping),还有测试 访问网站首页 返回的状态 是否 是 200 ,如果出现异常会发邮件 和 短信,ping域名不通了 , 说明你的网关到ISP那的网络不通 ,ping IP 地址不通 , 说明你的网关到你的服务器的网络有问题 , ping 域名不通 , ping IP地址通, 那就应该是DNS解析的问题了。
shell脚本:
#!/bin/sh if [ $# -ne 1 ] # -ne 不等于 then echo "Usage: $0 <URL>"; # $0 得到当前文件名 exit; fi if ping -c 2 $1 # $1 得到第一个参数 then echo "server is running" else echo "$1 is down" | mail -s "$1 is down" wang.fl1429@gmail.com curl "http://sms.api.bz/fetion.php?username=15001100000&password=12345&sendto=15001100000&message=$1%20is%20down" fi response=$(curl -s -I -L $1 | grep HTTP); status=${response#* }; # Strip off characters up to the first space status=${status:0:3}; # Just use the 3 digit status code if [ "$status" != "200" ] then echo "Error fetching $1. Status code $status" | mail -s "$1 response code is not 200" wang.fl1429@gmail.com; curl "http://sms.api.bz/fetion.php?username=15001100000&password=12345&sendto=15001100000&message=$1%20response%20status%20code%20$status" fi
或者
#!/bin/sh if [ $# -ne 1 ] # -ne 不等于 then echo "Usage: $0 <URL>"; # $0 得到当前文件名 exit; fi ping -c 2 $1 &>/dev/null # $1 得到第一个参数 , -c 2 是发送两次数据包 , &>/dev/null 输出内容null if [ $? = 0 ] # $?输出命令退出代码:0为命令正常执行,1-255为有出错 then echo "server is running" else echo "$1 is down" | mail -s "$1 is down" wang.fl1429@gmail.com curl "http://sms.api.bz/fetion.php?username=15001100000&password=12345&sendto=15001100000&message=$1%20is%20down" fi response=$(curl -s -I -L $1 | grep HTTP); status=${response#* }; # Strip off characters up to the first space status=${status:0:3}; # Just use the 3 digit status code if [ "$status" != "200" ] then echo "Error fetching $1. Status code $status" | mail -s "$1 response code is not 200" wang.fl1429@gmail.com; curl "http://sms.api.bz/fetion.php?username=15001100000&password=12345&sendto=15001100000&message=$1%20response%20status%20code%20$status" fi
让shell脚本添加 可执行
chmod +x monitor_domain.sh
添加 crontab 任务
>crontab -e # m h d mon dow command */10 * * * * /usr/local/system/shell/monitor_domain.sh "blog.wxianfeng.com" # 每10分钟运行一次
发短信 api 用的是别人的 , 以后自己可以搭一个 API , 发短信除了这个办法 , 还有一个办法 , 申请一个 139的email , 139 的email可以设置邮件 短信提醒 , 同时收到邮件 和 短信,不错 ,
我的centos 5.5 在执行 mail 命令时提示找不到 , 需要安装mailx 库解决:
yum install mailx
see:
http://www.coderholic.com/server-status-checker-shell-script-2/
http://blog.s135.com/fetion_api/