前言:好久沒有寫博客了,上來把之前寫的博客幾乎全都清理掉了,想寫的時候寫上一些,蠻不錯。
創(chuàng)新互聯(lián)專注于企業(yè)成都全網(wǎng)營銷、網(wǎng)站重做改版、蘭考網(wǎng)站定制設計、自適應品牌網(wǎng)站建設、H5頁面制作、購物商城網(wǎng)站建設、集團公司官網(wǎng)建設、成都外貿網(wǎng)站建設、高端網(wǎng)站制作、響應式網(wǎng)頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為蘭考等各大城市提供網(wǎng)站開發(fā)制作服務。
shell監(jiān)控網(wǎng)站/tomcat狀態(tài),依靠返回狀態(tài)碼來進行判斷,返回200,302認為狀態(tài)是正常的,否則認為tomcat/nginx/LB/Haproxy/apache掛掉了,腳本實現(xiàn)如下:
1. 創(chuàng)建一個站點文件夾,吧需要監(jiān)控的地址都寫到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腳本實現(xiàn)監(jiān)控功能,使用curl訪問網(wǎng)站,過濾出返回的狀態(tài)碼當做判斷條件,如有返回狀態(tài)碼非200/302則發(fā)送郵件報警
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文件記錄,并發(fā)出郵件,達到報警效果


分享題目:使用shell腳本監(jiān)控網(wǎng)站運行狀態(tài)
瀏覽路徑:http://chinadenli.net/article10/ipcodo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、品牌網(wǎng)站建設、響應式網(wǎng)站、營銷型網(wǎng)站建設、外貿建站、品牌網(wǎng)站設計
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)