您好,登錄后才能下訂單哦!
前言:好久沒有寫博客了,上來把之前寫的博客幾乎全都清理掉了,想寫的時候寫上一些,蠻不錯。
shell監控網站/tomcat狀態,依靠返回狀態碼來進行判斷,返回200,302認為狀態是正常的,否則認為tomcat/nginx/LB/Haproxy/apache掛掉了,腳本實現如下:
1. 創建一個站點文件夾,吧需要監控的地址都寫到http_site文件里面
vim http_site
### Nginx site begin ###
http://192.168.129.86:38020
http://192.168.129.86:38021
### Nginx site end ###
### LB site begin ###
http://192.168.2.30:38020
http://192.168.2.30:38024/38025task
### LB site end ###
### Web site begin ###
http://192.168.129.91:8030
http://192.168.129.93:8030
### Web site end ###
### Task site begin ###
http://192.168.129.95:8032/38023task
http://192.168.129.95:8033/38027task
### Task site end ###
### Mobile site begin ###
http://192.168.129.92:8030
http://192.168.129.92:8040
### Mobile site end ###
2. 編寫shell腳本實現監控功能,使用curl訪問網站,過濾出返回的狀態碼當做判斷條件,如有返回狀態碼非200/302則發送郵件報警
vim check_site.sh
#!/bin/bash
mysite=/root/script/check_http/http_site
check_status=/root/script/check_http/temp_status
historyfile=/root/script/check_http/history/`date +%Y-%m-%d`/`date +%T`
failurefile=/root/script/check_http/history/`date +%Y-%m-%d`/`date +%T`_failure
mkdir /root/script/check_http/history/`date +%Y-%m-%d` &>/dev/null
for site in `grep -v -E "^#|^$" $mysite`
do
curl -s -I --connect-timeout 3 -m 5 $site | grep "HTTP/1.1" | awk '{print $2}' > $check_status
status=`cat $check_status`
if [[ $status -eq 200 ]] || [[ $status -eq 302 ]]
then
echo "###########################" >>$historyfile
echo "http_site $site Access Successful" >>$historyfile
else
echo "###########################" >>$historyfile
echo "http_site $site Access Failure" >>$historyfile
fi
done
grep "Access Failure" $historyfile &>/dev/null
if [ $? -eq 0 ]
then
echo -e "\n\nThe following tomcat is not started !!!\n" >> $failurefile
echo -e "Please check the services !!!\n" >> $failurefile
echo -e "#############################################\n" >> $failurefile
grep "Access Failure" $historyfile >> $failurefile
echo -e "\n#############################################" >> $failurefile
mail -s "SFA_Liby_Tomcat_Check !!!" baiyongjie@winchannel.net misterbyj@163.com tangzhiyu@winchannel.net < $failurefile
fi
3. 配置報警郵箱
vim /etc/mail.rc
set hold
set append
set ask
set crt
set dot
set keep
set emptybox
set indentprefix="> "
set quote
set sendcharsets=iso-8859-1,utf-8
set showname
set showto
set newmail=nopoll
set autocollapse
ignore received in-reply-to message-id references
ignore mime-version content-transfer-encoding
fwdretain subject date from to
set bsdcompat
set from=15600970600@163.com
set smtp=smtp.163.com
set smtp-auth-user=15600970600@163.com smtp-auth-password=Password smtp-auth=login
4.添加計劃任務,每5分鐘運行一次
crontab -e
*/5 * * * * /bin/bash /root/script/check_http/check_site.sh
5. 測試腳本,寫好以后已經運行好幾天了,效果還不錯,分享給大家
為了驗證效果,當時停了幾個tomcat,6月14號23:12分停掉的,停掉后運行腳本檢測到有tomcat沒有運行,會生成 _failure文件記錄,并發出郵件,達到報警效果
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。