<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "[Linux]簡易網站發生故障通報程式"]]></title>
		<link>https://forum.andowson.com/posts/list/16.page</link>
		<description><![CDATA[Latest messages posted in the topic "[Linux]簡易網站發生故障通報程式"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>[Linux]簡易網站發生故障通報程式</title>
				<description><![CDATA[ 最近客戶的網站當機了幾次, 由於客戶的網站用戶會打電話給他們申訴, 故客戶希望在障礙發生時系統可以發出一個通知, 至少一個email, 以便大家可以早點處理. 但是當機的系統自身都難保了, 怎麼還能發出email求救信?故我們可轉而採用由別的系統來監控該目標網站(TARGET), 透過wget這個指令可以抓取遠端網頁的功能, 進行HTTP連線測試, 如果無法成功抓首頁回來, 便判斷為網站連線失敗, 然後發信通知網站負責人(OWNER),底下是這個shell script的內容: 
<br>
[code]#! /bin/sh 
<br>
# Name: webmon.sh 
<br>
# Author: Andowson Chang (andowson [at] gmail [dot] com) 
<br>
# Version: 0.2 
<br>
# Created: 2008-02-16 
<br>
# Last Modified: 2008-02-17 
<br>
# Description: Web connection monitor (health check) 
<br>
# If target web server failed, then notify its owner. 
<br>
# Here email is used for the simplest and cheapest way. 
<br>
# Usage: webmon.sh &lt;hostname&gt; &lt;owner email&gt; 
<br>
# Example: 
<br>
# 1. Single owner: 
<br>
# /root/admin/webmon.sh www.yourcompany.com you@yourcompany.com 
<br>
# 2.Multiple onwer: 
<br>
# /root/admin/webmon.sh www.yourcompany.com you@yourcompany.com,yourboss@yourcompany.com 
<br>
# 3. Set a cron job to keep checking a web site every few minutes. 
<br>
# */5 * * * * root /root/admin/webmon.sh www.yourcompany.com you@yourcompany.com,yourboss@yourcompany.com 
<br>
TARGET=$1 
<br>
OWNER=$2 
<br>
WEBMONLOG=/tmp/$TARGET.log 
<br>
export LANG=en_US 
<br>
wget -S -t 1 -T 5 http://$TARGET/ -O /tmp/index.html -o $WEBMONLOG 
<br>
RESULT=`grep "HTTP/1." $WEBMONLOG | awk '{print $2}'` 
<br>
TIMEOUT=`grep "timed out" $WEBMONLOG | wc -l` 
<br>
if [ $TIMEOUT == 1 ] || [ ! $RESULT == 200 ]; then 
<br>
 mail -s "[ALERT]$TARGET HTTP connection fail" $OWNER &lt; $WEBMONLOG 
<br>
fi 
<br>
# clean up 
<br>
rm -rf /tmp/index.html 
<br>
rm -rf $WEBMONLOG 
<br>
[/code] 
<br>
這裡用到了wget的幾個參數, 主要是要取得HTTP的回應代碼, 並控制重試次數及連線的Timeout時間, 詳細說明如下 
<br>
 -S 
<br>
 --server-response 
<br>
 Print the headers sent by HTTP servers and responses sent by FTP servers. 
<br>
<br>
 -t number 
<br>
 --tries=number 
<br>
 Set number of retries to number. Specify 0 or inf for infinite retrying. The default is to retry 20 times, with the exception of fatal errors 
<br>
 like connection refused or not found (404), which are not retried. 
<br>
<br>
 -T seconds 
<br>
 --timeout=seconds 
<br>
 Set the network timeout to seconds seconds. This is equivalent to specifying --dns-timeout, --connect-timeout, and --read-timeout, all at the same time. 
<br>
另外,這邊使用了export LANG=en_US來控制wget輸出訊息時的編碼為英文,以便配合cron使用. 
<br>
使用時, 只需將webmon.sh放到/root/admin目錄下, 並更改權限為可執行, 然後在/etc/crontab中新增一行即可: 
<br>
[code]*/5 * * * * root /root/admin/webmon.sh www.yourcompany.com you@yourcompany.com,yourboss@yourcompany.com[/code]]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/220/335.page</guid>
				<link>https://forum.andowson.com/posts/preList/220/335.page</link>
				<pubDate><![CDATA[Sun, 17 Feb 2008 18:20:31]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
	</channel>
</rss>