標籤:shell 監控 網站 tomcat nginx
前言:好久沒有寫部落格了,上來把之前寫的部落格幾乎全都清理掉了,想寫的時候寫上一些,蠻不錯。
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 !!!" [email protected] [email protected] [email protected] < $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 [email protected]
set smtp=smtp.163.com
set [email protected] 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檔案記錄,並發出郵件,達到警示效果
650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />
650) this.width=650;" src="https://s5.51cto.com/wyfs02/M02/99/00/wKiom1lClCeTfB2yAACkpYPgP-o792.png" title="clipboard.png" alt="wKiom1lClCeTfB2yAACkpYPgP-o792.png" />
本文出自 “startuppp” 部落格,請務必保留此出處http://startuppp.blog.51cto.com/11847460/1937302
使用shell指令碼監控網站運行狀態