#!/bin/bash #××× QQ××× url.txt文件直接填寫須要監控的網址 第六版 #添加須要監控的網址 echo "evecom.net" >> url.txt #下降誤報,能夠配合IP監控腳本一塊兒用 cur_time(){ date "+%Y/%m/%d %H:%M:%S" } systemname(){ name=`cat url2.txt|grep $url|wc -l` if [ $name -eq 1 ];then cat url2.txt|grep -w $url else echo "$url" fi } [ ! -f /root/url.txt ] && echo "url.txt文件不存在" && exit 1 while read url do [ -z $url ] && echo "url.txt存在空格 檢查文件格式" && exit 1 for ((i=1;i<13;i++)) do rule1=`curl -i -s -k -L -m 10 $url|grep "200 OK"|wc -l` if [ $rule1 -eq 1 ];then echo "$(cur_time) 第$i次檢查$url網頁訪問成功" >> check.log break fi rule2=`curl -i -s -k -L -m 10 $url|sed -n '1p'|grep "200"|wc -l`#20190719新增長判斷規則 if [ $rule2 -eq 1 ];then echo "$(cur_time) 第$i次檢查$url網頁訪問成功" >> check.log break elif [ $i = 12 ];then echo "$(cur_time) $(systemname) 網頁連續$(expr $i \* 5)秒沒法訪問,請檢查!"|mail -s "【重要告警】網頁不可達" ×××@×××.com echo "$(cur_time) 第$i次檢查$url網頁訪問失敗" >> checkfail.log else echo "$(cur_time) 第$i次檢查$url網頁訪問失敗" >> checkfail.log sleep 5 fi done done < /root/url.txt -------------------------------------------------------------------------------- 1. 發送郵件 配置/etc/mail.rc就能夠實現 百度教程一堆 2. 如何使用 /root目錄下建立腳本 vi check.sh 受權可執行權限 chmod +x check.sh 每3分鐘執行一次腳本 調用crontab crontab –e */3 * * * * sh /root/check.sh ------------------------------------------------------- 監控網站可達性,當網站出現訪問不可達的狀況發出郵件告警。掃描頻率可修改; #!/bin/bash #20190705 url.txt文件直接填寫須要監控的網址 第四版 #添加須要監控的網址 echo "evecom.net" >> url.txt cur_time(){ date "+%Y/%m/%d %H:%M:%S" } [ ! -f /root/url.txt ] && echo "url.txt文件不存在" && exit 1 while read url do urldns=$(echo $url|sed 's@https://@@'|sed 's@http://@@') nslookup $urldns >/dev/null 2>&1 if [ $? -ne 0 ] ;then echo "$(cur_time) $url 網頁沒法解析" >> checkfail.log continue else for ((i=1;i<4;i++)) do connect=`curl -i -s -k -L -m 10 $url|grep "200 OK"|wc -l` #https要用-k 有跳轉-L -m 超時執行時間 if [ $connect -eq 1 ];then echo "$(cur_time) 第$i次檢查$url網頁訪問成功" >> check.log break elif [ $i = 3 ];then echo "$(cur_time) 網頁第$i次沒法訪問"|mail -s "網頁不可達告警" ×××@×××.com echo "$(cur_time) 第$i次檢查$url網頁訪問失敗" >> checkfail.log else echo "$(cur_time) 第$i次檢查$url網頁訪問失敗" >> checkfail.log sleep 5 fi done fi done < /root/url.txt ---------------------------------------------------------------------------------------------------------------------------------- #!/bin/bash #20190611 url.txt文件直接填寫須要監控的網址 第三版 #QQ××× time=`date +"%Y/%m/%d %H:%M.%S"` [ ! -f /root/url.txt ] && echo "url.txt文件不存在" && exit 1 while read url do nslookup $url >/dev/null 2>&1 if [ $? -ne 0 ] ;then echo "$time $url 網頁沒法解析" >> checkfail.log continue else for((i=1;i<4;i++)) do connect=`curl -i -s $url|grep "200 OK"|wc -l` if [ $connect -eq 1 ];then echo "$time 第$i次檢查$url網頁訪問成功" >> check.log break elif [ $i = 3 ];then echo "$time $url 網頁第$i次沒法訪問"|mail -s "網頁不可達告警" ×××@×××.com echo "$time 第$i次檢查$url網頁訪問失敗" >> checkfail.log else echo "$time 第$i次檢查$url網頁訪問失敗" >> checkfail.log fi done fi done < /root/url.txt QQ××× -------------------------------------- 如下是前面寫的2個版本 後來想一想仍是優化下 #!/bin/bash #20190511 第二版本 check(){ connect=`curl -i -s $url|grep "200 OK"|wc -l` } time=`date +"%Y/%m/%d %H:%M.%S"` [ ! -f /root/url.txt ] && echo "url.txt文件不存在" && exit 1 while read url do nslookup $url >/dev/null 2>&1 if [ $? -ne 0 ] ;then echo "$time $url 網頁沒法解析" >> $0.faillog continue else echo "" >> $0.log fi check if [ $connect -eq 1 ];then echo "$time 第1次檢查$url網頁訪問成功" >> $0.log else echo "$time 第1次檢查$url網頁訪問失敗" >> $0.faillog check if [ $connect -eq 1 ];then echo "$time 第2次檢查$url網頁訪問成功" >> $0.log else echo "$time 第2次檢查$url網頁訪問失敗" >> $0.faillog check if [ $connect -eq 1 ];then echo "$time 第3次檢查$url網頁訪問成功" >> $0.log else echo "$time $url 網頁3次沒法訪問"|mail -s "網頁不可達告警" ×××@×××.com echo "$time $url 網頁3次沒法訪問" >> $0.faillog fi fi fi done < /root/url.txt #!/bin/bash #初版 while read url do connect1=`curl -i -s $url|grep "200 OK"|wc -l` if [ $connect1 -eq 1 ];then echo `date +"%Y/%m/%d %H:%M.%S"` check $url first success >>/root/connect.log else connect2=`curl -i -s $url|grep "200 OK"|wc -l` echo `date +"%Y/%m/%d %H:%M.%S"` check $url first fail >>/root/fail.log if [ $connect2 -eq 1 ];then echo `date +"%Y/%m/%d %H:%M.%S"` check $url second success >>/root/connect.log else connect3=`curl -i -s $url|grep "200 OK"|wc -l` echo `date +"%Y/%m/%d %H:%M.%S"` check $url second fail >>/root/fail.log if [ $connect3 -eq 1 ];then echo `date +"%Y/%m/%d %H:%M.%S"` check $url success >>/root/connect.log else echo `date +"%Y/%m/%d %H:%M.%S"` Check $url for three failed visits|mail -s "網頁不可達告警" ×××@×××.com echo `date +"%Y/%m/%d %H:%M.%S"` check $url Third fail >>/root/fail.log fi fi fi done < /root/url.txt