shell迷你版監控指令碼

來源:互聯網
上載者:User

標籤:shell   linux   監控   

寫了一個shell版mini監控指令碼,純屬跟大家分享學習下,有興趣學習shell的可以看下。(*_*)

總共3個指令檔service.sh, daemon.sh ,tcp.sh

實現了一些準系統:連接埠監控,存活監控,掉線郵件警示,重新上線郵件通知

 

將3個檔案放在同個目錄下

設定:在tcp.sh中可以設定郵件通知地址,與郵件通頻率次數

運行: ./service.sh start

 

650) this.width=650;" title="q1.jpg" src="http://s3.51cto.com/wyfs02/M01/59/97/wKiom1TYiFiCEPAzAADZn1sFlDM579.jpg" alt="wKiom1TYiFiCEPAzAADZn1sFlDM579.jpg" />

 

主要檔案代碼如下:

tcp.sh檔案

#!/bin/bashLANG=C#郵件地址設定fromaddress="[email protected]"    #From地址toaddress="[email protected]"    #發送地址ccaddress="[email protected]" #抄送地址#郵件警示從複次數remun=2#被監控伺服器、連接埠列表server_all_list=(192.168.70.25:2004 192.168.70.24:80 192.168.16.56:80 )##################################################################採用mail決定發送警示訊息。send_msg(){/bin/grep "$server_ip" mun.tmp > /dev/null 2>&1if [ $? -eq 0 ]then    mun=$(grep "$server_ip" mun.tmp | awk -F ‘:‘ ‘{print $2}‘)    if [ $mun -lt $remun ];then echo "$messages" | mail -c $ccaddress -s "$server_ip is down" $toaddress -- -f $fromaddress newmun=$[$mun + 1] /bin/sed -i ‘s/‘$server_ip‘:‘$mun‘/‘$server_ip‘:‘$newmun‘/g‘ mun.tmp    fielse    echo "$server_ip:1" >> mun.tmp    echo "$messages" | mail -c $ccaddress -s "$server_ip is down" $toaddress -- -f $fromaddress# echo "$messages"# echo "$server_ip"fi}#重設郵件已發送次數記錄reset_mun(){/bin/sed -i ‘/‘$server_ip‘/d‘ mun.tmpecho "server $server_ip online" | mail -c $ccaddress -s "$server_ip server online" $toaddress -- -f $fromaddress}###########server_all_len=${#server_all_list[*]}i=0while  [ $i -lt $server_all_len ]do   server_ip=$(echo ${server_all_list[$i]} | awk -F ‘:‘ ‘{print $1}‘)   server_port=$(echo ${server_all_list[$i]} | awk -F ‘:‘ ‘{print $2}‘)       #status:    0,http down    1,http ok    2,http down but ping ok        if nc -vv -z -w 10 $server_ip $server_port > /dev/null 2>&1       then           status=1           messages="server $server_ip,port $server_port can‘t access!"       else           if ping -c 1 $server_ip > /dev/null 2>&1           then               status=2               messages="server $server_ip,Port $server_port cannot access,but Ping is connected!"           else               status=0               messages="server $server_ip,port $server_port and Ping cannot access!"           fi       fi  if [ $status -eq 1 ];then  grep "$server_ip" mun.tmp > /dev/null 2>&1  if [ $? -eq 0 ];then   reset_mun  fi else  send_msg fi    let i++done

 

daemon.sh

#!/bin/shdir=`pwd`while truedo   /bin/sh $dir/tcp.sh > /dev/null 2>&1   sleep 180done

 

service.sh

#!/bin/shdir=`pwd`start() {ps -ef |grep -v grep|grep daemon.sh > /dev/null 2>&1if [ $? -eq 0 ];thenexitelse/usr/bin/nohup /bin/sh $dir/daemon.sh 2>&1 > /dev/null &fi}stop() {PID=$(ps -ef |grep -v grep|grep daemon.sh|awk ‘{print $2}‘)kill $PID}reload() {stopstart}status() {ps -ef |grep -v grep|grep daemon.sh > /dev/null 2>&1if [ $? -eq 0 ];thenecho "daemon.sh is running..."elseecho "daemon.sh is stop"fi}case "$1" in    start)        start && exit 0        $1        ;;    stop)        stop && exit 0        $1        ;;    status)status && exit 0        $1        ;;    reload)        reload && exit 0        $1        ;;    *)        echo $"Usage: $0 {start|stop|status|reload}"        exit 2esac

 

本文出自 “itdept” 部落格,請務必保留此出處http://itdept.blog.51cto.com/1034307/1613115

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.