Ruby net/http で Web サービスが動いているか確認する

March 2, 2008

Web サービスが停止していないかどうか確認するスクリプト。 open-uri を使うとタイムアウトが設定できないので、net/http で行う。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/env ruby
require 'net/http'
PROXY_ADDRESS = nil \# プロキシサーバのアドレス
PROXY_PORT = nil \# プロキシサーバのポート
def check\_site\_alive(domain, port = 80)
begin
Net::HTTP.start(domain, port, PROXY_ADDRESS, PROXY_PORT) do |http|
http.open_timeout = 20
http.read_timeout = 40
\# ↓は Web サービスにあわせて書き換える
response = http.post(path, data)
return response.code == '200'
end
rescue Exception =\> e
puts e
return false
end
end
puts check\_site\_alive("webservice.domain")

tilfin freelance software engineer