標籤:des blog http 使用 os io 檔案 ar
宕機監控警示程式
一. 需求來源
宕機對營運人員來說,最痛苦了。怎樣檢測一台server是否還在正常執行,假設該server宕機,怎樣在第一時間監測到並通知一線營運人員進行維護,最大化降低損失。
二. 程式功能
對指定server進行宕機監測,假設確實宕機,則發送email到139郵箱(綁定手機,實現簡訊警示)
三.來源程式
#!/bin/bash#author longxibendi#blog http://blog.csdn.net/longxibendi#function ping a host and output to file ping_longxibendi.log#ping destinationfunction_ping (){ ping -c 3 172.29.141.115 > ping_longxibendi.log}#downtime detection and send email to SAfunction_downtime_detection_AND_sendemail (){ if [ "`cat ping_longxibendi.log | grep Unreachable`" != "" ] ; then /usr/local/bin/sendEmail -f monitor_sys@163.com -t longxibendi@139.com -s smtp.163.com -u "Server downtime" -xu monitor_sys -xp 123456789 -m "`date;echo "172.29.141.115" ` " fi}#main functionfunction_main (){while truedo function_ping ; sleep 2 function_downtime_detection_AND_sendemail ; sleep 2done}function_main ;
四.程式說明
1.通過 function_ping ,每隔4秒對 主機 172.29.141.115 進行ping 操作,(當然,假設主機172.29.141.115的防火牆,或者內過濾ICMP包,那麼這個程式顯然不能用)將輸出重新導向到ping_longxibendi.log 檔案裡
2.通過 function_downtime_detection_AND_sendemail ,每隔4秒,通過ping_longxibendi.log推斷是否有沒有ping通的跡象,假設有,則調用 sendEmail 郵件(手機簡訊)警示。
3.監控間隔時間說明 ,能夠將 第一個 sleep 改為 150 ,第二個 sleep 改為 150 ,這樣每隔5分鐘監控一次。
四.使用環境說明
1.主機 A(172.29.141.112) 主機B (172.29.141.115) ,
在A上部署該監控程式(monitor_down.sh),用於監控B
2.正常情況下A能ping通B
由於用的ping命令,所以假設使用該程式,須要在正常情況下A ping 通 B 。對企業來說,這可能就須要防火牆和Linux核心參數(當然,假設之前沒有改動net.ipv4.icmp_echo_ignore_all,則不須要調整)
3.安裝了 sendEmail 並 在139郵箱注冊(綁定手機),方可有郵件(簡訊)警示提示
五.程式測試
[[email protected] monitor]# sh monitor_down.sh
May 21 20:33:46 localhost sendEmail[9175]: Email was sent successfully!
May 21 20:33:56 localhost sendEmail[9204]: Email was sent successfully!
Terminated
[[email protected] monitor]#
六.程式擴充
這個程式,僅僅是實現宕機監控並警示,但沒有實現容錯移轉,自己主動切換功能。事實上,僅僅要略微改動一下程式就能夠實現容錯移轉,自己主動切換。容錯移轉,比方能夠通過在熱備機A上部署該程式,監控B,一旦B宕機,則A運行浮動改IP和更新下層serverarp列表就可以。能夠參考
http://blog.csdn.net/longxibendi/archive/2011/05/21/6436606.aspx
聲明:本文檔能夠任意更改,但必須署名原作者
鳳凰舞者 qq:578989855