前言:好久沒有寫博客了,上來把之前寫的博客幾乎全都清理掉了,想寫的時(shí)候?qū)懮弦恍?,蠻不錯(cuò)。
創(chuàng)新互聯(lián)專注于企業(yè)成都全網(wǎng)營(yíng)銷、網(wǎng)站重做改版、蘭考網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5頁面制作、購物商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為蘭考等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
shell監(jiān)控網(wǎng)站/tomcat狀態(tài),依靠返回狀態(tài)碼來進(jìn)行判斷,返回200,302認(rèn)為狀態(tài)是正常的,否則認(rèn)為tomcat/nginx/LB/Haproxy/apache掛掉了,腳本實(shí)現(xiàn)如下:
1. 創(chuàng)建一個(gè)站點(diǎn)文件夾,吧需要監(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腳本實(shí)現(xiàn)監(jiān)控功能,使用curl訪問網(wǎng)站,過濾出返回的狀態(tài)碼當(dāng)做判斷條件,如有返回狀態(tài)碼非200/302則發(fā)送郵件報(bào)警
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. 配置報(bào)警郵箱
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.添加計(jì)劃任務(wù),每5分鐘運(yùn)行一次
crontab -e
*/5 * * * * /bin/bash /root/script/check_http/check_site.sh
5. 測(cè)試腳本,寫好以后已經(jīng)運(yùn)行好幾天了,效果還不錯(cuò),分享給大家
為了驗(yàn)證效果,當(dāng)時(shí)停了幾個(gè)tomcat,6月14號(hào)23:12分停掉的,停掉后運(yùn)行腳本檢測(cè)到有tomcat沒有運(yùn)行,會(huì)生成 _failure文件記錄,并發(fā)出郵件,達(dá)到報(bào)警效果
分享題目:使用shell腳本監(jiān)控網(wǎng)站運(yùn)行狀態(tài)
瀏覽路徑:http://aaarwkj.com/article10/ipcodo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、品牌網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、營(yíng)銷型網(wǎng)站建設(shè)、外貿(mào)建站、品牌網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)