使用shell指令碼監控網站運行狀態

來源:互聯網
上載者:User

標籤: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指令碼監控網站運行狀態

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.