监控网站可以实时做到网站一个稳定判断了,下面整理了这些代码是可以监控网站是不是正常,如果不正确我们发邮件通知站长,这个有点像dnspod网站监控功能了,下面整理了一些例子,有需要的可进入看看。 # vi check_web_alive.sh --------------------------------------------------------------------- #!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH # define url WEB_URL=("http://www.it.net.cn" "http://m.111n.net" "http://www.it.net.cn") # check network NET_ALIVE=$(ping -c 5 8.8.8.8 |grep 'received'|awk 'BEGIN {FS=","} {print $2}'|awk '{print $1}') if [ $NET_ALIVE == 0 ]; then echo "Network is not active,please check your network configuration!" exit 0 fi # check url for((i=0; i!=${#WEB_URL[@]}; ++i)) { ALIVE=$(curl -o /dev/null -s -m 10 -connect-timeout 10 -w %{http_code} ${WEB_URL[i]} |grep"000000") if [ "$ALIVE" == "000000" ]; then echo "'${WEB_URL[i]}' can not be open,please check!" | mail -s "Website Notification to ${WEB_URL[i]}" yourname@example.com echo "failed" else echo "'${WEB_URL[i]}' is OK!" fi } -------------------------------------------------------------------------------- 方法二,利用curl监控网页shell脚本 ,因为此脚本的作用是按固定频率不停访问给出的URL,当网站不可访问时自动给设定邮箱发送告警邮件以通知用户.好了.来看脚本吧. #!/bin/sh # */2 * * * * sh /var/monitor/web_monitor.sh http:///www.it.net.cn # */2 * * * * sh /var/monitor/web_monitor.sh http:///www.it.net.cn 5 # */2 * * * * sh /var/monitor/web_monitor.sh http:///www.it.net.cn 10 export LANG=C URL="$1" EMAIL="rocdk890@gmail.com" # change for your mail address. LOG_FILE="/var/log/monitor/web_status_`date '+%Y%m'`.log" TMP_EMAIL="/var/monitor/.tmp.mail.`date '+%s'`" if [ $2 ] then sleep $2 fi # Define function "ECHO", append the timestamp at the head of every echo display. ECHO () { printf "%s " `date '+%Y-%m-%d %H:%M:%S'` echo $1 } # Define function HTTP_CODE, obtain the status of web service. HTTP_CODE () { http_code=`curl -m 10 -o /dev/null -s -w %{http_code} $URL` } # Define function MAIL. MAIL () { echo "$URL is not available now, pls pay attention." > $TMP_EMAIL echo "And the Server 's time is: " >> $TMP_EMAIL date >> $TMP_EMAIL echo >> $TMP_EMAIL echo "------" >> $TMP_EMAIL echo "BR" >> $TMP_EMAIL echo "Shell Robot." >> $TMP_EMAIL mail -s "Server Alert: $URL" $EMAIL < $TMP_EMAIL rm $TMP_EMAIL } n=0 HTTP_CODE if [ $http_code -eq 200 ] then ECHO "|http_code:200|+$n|webpage visit success.|$URL" >> $LOG_FILE else while [ $http_code -ne 200 ] do n=`expr $n + 1 ` ECHO "|http_code:$http_code|+$n|webpage visit failed. |$URL" >> $LOG_FILE if [ $n -eq 5 ]; then MAIL $1; exit 0 fi sleep 10 HTTP_CODE done fi # End. -------------------------------------------------------------------------------- 使用Linux自带的curl工具访问给出网址,并返回http_code,当http_code不等于200时即可认为网站存在访问故障,但考虑到实际的不稳定因素,当第一次返回http_code不等200时,脚本休眠10秒钟,然后继续访问,累计连续不可访问次数达到5次后触发告警邮件. 使用方法: 脚本后面需跟两个参数:第一个参数为监控网站的URL,第二个参数为延时时间(可选,以秒为单位,建议在对多个网站进行监控时添加),将此脚本添加到操作系统的crontab里面,按需求设定运行频率,建议2分钟一次.若有多个网站需监控,在crontab里面添加多行即可,每个URL一行,如下为同时对三个网站进行监控: */2 * * * * sh /var/monitor/web_monitor.sh http://www.it.net.cn */2 * * * * sh /var/monitor/web_monitor.sh http://www.it.net.cn 5 */2 * * * * sh /var/monitor/web_monitor.sh http://www.it.net.cn 10 最简单的办法,没测试过 linux shell curl命令获取http状态码 有时候在使用linux shell写网站状态监控的时候,希望通过判断网站返回的头信息的状态来判断网站的状态,使用以下命令即可解决: curl -I -m 10 -o /dev/null -s -w %{http_code} www.it.net.cn (责任编辑:IT) |