目前网上关于网站存活监控的脚本很多,本站也放过几个,今天给大家再来个ping+curl监控网站并发邮件报警的脚本,我觉得这个脚本的思路很不错,大家有兴趣的可以下来看看. 
脚本内容: 
cat /root/soft_shell/check-www.sh 
	
		
		
		
		
		
		
			
				
					
						| 
							06 | 
						
							    list=(blog.slogra.com slogra.com) | 
					 
				
			 
		 
		
		
			
				
					
						| 
							08 | 
						
							    date=$(date -d "today" +"%Y-%m-%d-%H:%M:%S") | 
					 
				
			 
		 
		
		
		
		
		
			
				
					
						| 
							13 | 
						
							        if ping -c1 ${list[$i]} >/dev/null | 
					 
				
			 
		 
		
		
			
				
					
						| 
							15 | 
						
							            echo  $date:服务器${list[$i]}能ping通. | 
					 
				
			 
		 
		
		
			
				
					
						| 
							17 | 
						
							            if curl -m 10  ${list[$i]} > /dev/null | 
					 
				
			 
		 
		
		
			
				
					
						| 
							19 | 
						
							                echo  $date:服务器${list[$i]} ping不通,能打开网页. | 
					 
				
			 
		 
		
		
			
				
					
						| 
							21 | 
						
							                echo  "您好,据系统监测服务器${list[$i]}不能访问且ping不通,请及时处理!故障发生时间:$date"|mail -s "服务器${list[$i]}不能连接! 故障发生时间:$date"$mail | 
					 
				
			 
		 
		
		
			
				
					
						| 
							23 | 
						
							                    date=$(date -d "today" +"%Y-%m-%d-%H:%M:%S") | 
					 
				
			 
		 
		
			
				
					
						| 
							24 | 
						
							                    ping -c1 ${list[$i]} >/dev/null && echo "恭喜!服务器${list[$i]}已恢复正常,恢复时间:$date"|mail -s "服务器${list[$i]}已恢复正常! 恢复时间:$date" $mail | 
					 
				
			 
		 
		
		
		
		
		
		
		
		
		
	 
 
 
放入后台运行: 
nohup sh /path/file.sh &   #把脚本放到后台运行,程序自己会60秒检查一次网站. 
 
开机自启动: 
echo "nohup sh /root/soft_shell/check-www.sh & " >> /etc/rc.local 
 
下面还有个类似的脚本,但他控制了发邮件的频率: 
cat /root/soft_shell/check-web.sh 
	
		
		
		
			
				
					
						| 
							03 | 
						
							SITES=("http://blog.slogra.com" "http://slogra.com")# 要监控的网站 | 
					 
				
			 
		 
		
			
				
					
						| 
							04 | 
						
							NOTICE_EMAIL='me@example.com'                                # 管理员电邮 | 
					 
				
			 
		 
		
			
				
					
						| 
							05 | 
						
							MAXLOADTIME=10                                               # 访问超时时间设置 | 
					 
				
			 
		 
		
			
				
					
						| 
							06 | 
						
							REMARKFILE='/tmp/monitor_load.remark'                        # 记录时否发送过通知电邮,如发送过则一小时内不再发送 | 
					 
				
			 
		 
		
		
			
				
					
						| 
							08 | 
						
							EXPIRE=3600                                                  # 每次发送电邮的间隔秒数 | 
					 
				
			 
		 
		
		
		
			
				
					
						| 
							11 | 
						
							if [ -f "$REMARKFILE" ] && [ -s "$REMARKFILE" ]; then | 
					 
				
			 
		 
		
			
				
					
						| 
							12 | 
						
							    REMARK=$(cat $REMARKFILE) | 
					 
				
			 
		 
		
		
		
			
				
					
						| 
							15 | 
						
							    if [ $(( $NOW - $REMARK )) -gt "$EXPIRE" ]; then | 
					 
				
			 
		 
		
		
		
		
		
		
		
		
		
			
				
					
						| 
							24 | 
						
							for site in ${SITES[*]}; do | 
					 
				
			 
		 
		
		
			
				
					
						| 
							26 | 
						
							    printf "start to load ${site}\n" | 
					 
				
			 
		 
		
			
				
					
						| 
							27 | 
						
							    site_load_time=$(curl -o /dev/null -s -w "time_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}" "${site}") | 
					 
				
			 
		 
		
			
				
					
						| 
							28 | 
						
							    site_access=$(curl -o /dev/null -s -w %{http_code} "${site}") | 
					 
				
			 
		 
		
			
				
					
						| 
							29 | 
						
							    time_total=${site_load_time##*:} | 
					 
				
			 
		 
		
		
			
				
					
						| 
							31 | 
						
							    printf "$(date '+%Y-%m-%d %H:%M:%S')\n" | 
					 
				
			 
		 
		
			
				
					
						| 
							32 | 
						
							    printf "site load time\n${site_load_time}\n" | 
					 
				
			 
		 
		
			
				
					
						| 
							33 | 
						
							    printf "site access:${site_access}\n\n" | 
					 
				
			 
		 
		
		
		
			
				
					
						| 
							36 | 
						
							    if [ "$REMARK" = "" ]; then | 
					 
				
			 
		 
		
		
			
				
					
						| 
							38 | 
						
							        if [ "$time_total" = "0.000" ] || [ "$site_access" != "200" ]; then | 
					 
				
			 
		 
		
			
				
					
						| 
							39 | 
						
							            echo "Subject: ${site} can access $(date +%Y-%m-%d' '%H:%M:%S)" | sendmail ${NOTICE_EMAIL} | 
					 
				
			 
		 
		
		
		
		
			
				
					
						| 
							43 | 
						
							            if [ "${time_total%%.*}" -ge ${MAXLOADTIME} ]; then | 
					 
				
			 
		 
		
			
				
					
						| 
							44 | 
						
							                echo "Subject: ${site} load time total:${time_total} $(date +%Y-%m-%d' '%H:%M:%S)" | sendmail ${NOTICE_EMAIL} | 
					 
				
			 
		 
		
		
		
		
		
		
		
		
		
			
				
					
						| 
							53 | 
						
							if [ "$ISSEND" = "1" ]; then | 
					 
				
			 
		 
		
			
				
					
						| 
							54 | 
						
							    echo "$(date +%s)" > $REMARKFILE | 
					 
				
			 
		 
		
		
	 
 
 
ps: 
1.检查网站返回的http_code是否等于200,如不是200视为异常. 
2.检查网站的访问时间,超过MAXLOADTIME(10秒)视为异常. 
	3.发送通知邮件后,在/tmp/monitor_load.remark记录发送时间,在一小时内不重复发送,如一小时后则清空/tmp/monitor_load.remark. 
	脚本来源: 
	http://www.jbxue.com/article/15072.html 
	http://www.haiyun.me/archives/shell-check-server-mail.html 
 
 
      (责任编辑:IT) |