監控網站80連接埠
很多時候我們不知道網站80連接埠是否正常,當然我以前的shell監控網站狀態(續)這篇文章也可以監控網站,這裡只是給大家提供另外一個思路去監控網站.
指令碼內容:
代碼如下 |
複製代碼 |
vi check-80.sh #!/bin/bash nmap -P0 -p80 -sS -vv blog.slogra.com|grep 80 |tail -n1>/tmp/nmap.txt check_80=`cat /tmp/nmap.txt|awk '{print $2}'` if [ "$check_80" != "open" ]; then body="www.111cn.net port 80 is error!" subject="blog.slogra.com port 80 down,check it now!@from monitor `date +%T`" echo "${body}"| mutt -s "${subject}" rocdk@163.com fi |
shell指令碼監控網站是否正常
1. 編寫Shell指令碼,儲存到/opt/http_monitor
代碼如下 |
複製代碼 |
#!/bin/bash #網站url地址 URL="http://www.111cn.net/" #擷取http響應代碼 HTTP_CODE=`curl -o /dev/null -s -w "%{http_code}" "${URL}"` #echo $HTTP_CODE #伺服器能正常響應,應該返回200的代碼 if [ $HTTP_CODE != 200 ];then #重啟服務 service httpd restart fi |
2. 增加到crontab,使用crontab -e命令添加新的任務:
Shell代碼
代碼如下 |
複製代碼 |
#每分鐘運行一次 */1 * * * * /opt/http_monitor |
3. 停掉服務,測試,cool!一分鐘後自動啟動了!可是中文亂碼了,查了一下原來是沒有載入系統變數,好辦,在http_monitor中加入locale環境變數就可以了:
Shell代碼
代碼如下 |
複製代碼 |
export LC_ALL=zh_CN.UTF-8 |
注意
很多朋友使用dnspod監控,但它只能監控伺服器是否可串連,如果網站中毒了可能就無法監測了。