From ec2668b143bf4634621ebc1e93c1666c427fb304 Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Wed, 14 Nov 2012 21:48:31 +0000 Subject: Timeout correctly. --- lib/custodian/protocol-tests/http.rb | 102 ++++++++++++++++--------------- lib/custodian/protocol-tests/https.rb | 112 +++++++++++++++++----------------- 2 files changed, 107 insertions(+), 107 deletions(-) (limited to 'lib') diff --git a/lib/custodian/protocol-tests/http.rb b/lib/custodian/protocol-tests/http.rb index c11e27a..34fc472 100755 --- a/lib/custodian/protocol-tests/http.rb +++ b/lib/custodian/protocol-tests/http.rb @@ -113,64 +113,66 @@ class HTTPTest # # NOTE: This came from sentinel. def getURL (uri_str, timeout) - begin - uri_str = 'http://' + uri_str unless uri_str.match(/^http/) - url = URI.parse(uri_str) - http = Net::HTTP.new(url.host, url.port) - http.open_timeout = timeout - http.read_timeout = timeout - - if (url.scheme == "https") - http.use_ssl = true - http.verify_mode = OpenSSL::SSL::VERIFY_NONE - end - response = nil + timeout( timeout ) do + begin + uri_str = 'http://' + uri_str unless uri_str.match(/^http/) + url = URI.parse(uri_str) + http = Net::HTTP.new(url.host, url.port) + http.open_timeout = timeout + http.read_timeout = timeout + + if (url.scheme == "https") + http.use_ssl = true + http.verify_mode = OpenSSL::SSL::VERIFY_NONE + end - # - # Ensure we have a trailing "/" - # - if ( url.path.empty? ) - url.path = "/" - end + response = nil - if nil == url.query - response = http.start { http.get(url.path) } - else - response = http.start { http.get("#{url.path}?#{url.query}") } - end + # + # Ensure we have a trailing "/" + # + if ( url.path.empty? ) + url.path = "/" + end - case response - when Net::HTTPRedirection - then - newURL = response['location'].match(/^http/)? - response['Location']:uri_str+response['Location'] - return( getURL(newURL, timeout) ) - else - @status = response.code.to_i - @body = response.body - end + if nil == url.query + response = http.start { http.get(url.path) } + else + response = http.start { http.get("#{url.path}?#{url.query}") } + end - return true - rescue Errno::EHOSTUNREACH => ex - @error = "no route to host" - return false - rescue Timeout::Error => ex - @error = "time out reached" - return false - rescue Errno::ECONNREFUSED => ex - @error = "Connection refused" - return false - rescue => ex - @error = ex - return false + case response + when Net::HTTPRedirection + then + newURL = response['location'].match(/^http/)? + response['Location']:uri_str+response['Location'] + return( getURL(newURL, timeout) ) + else + @status = response.code.to_i + @body = response.body + end + + return true + rescue Errno::EHOSTUNREACH => ex + @error = "no route to host" + return false + rescue Timeout::Error => ex + @error = "time out reached" + return false + rescue Errno::ECONNREFUSED => ex + @error = "Connection refused" + return false + rescue Timeout::Error => e + @error = "TIMEOUT: #{e}" + return false + rescue => ex + @error = ex + return false + end end - @error = "Misc failure" - return false end - - end diff --git a/lib/custodian/protocol-tests/https.rb b/lib/custodian/protocol-tests/https.rb index fe0cd99..72dd42c 100755 --- a/lib/custodian/protocol-tests/https.rb +++ b/lib/custodian/protocol-tests/https.rb @@ -42,7 +42,6 @@ class HTTPSTest if ( @test_data["test_port"].nil? ) @error = "Missing port for the test." raise ArgumentError, @error - end end @@ -81,8 +80,6 @@ class HTTPSTest end end - - # # Do we need to search for text in the body of the reply? # @@ -116,64 +113,66 @@ class HTTPSTest # # NOTE: This came from sentinel. def getURL (uri_str, timeout) - begin - uri_str = 'http://' + uri_str unless uri_str.match(/^http/) - url = URI.parse(uri_str) - http = Net::HTTP.new(url.host, url.port) - http.open_timeout = timeout - http.read_timeout = timeout - - if (url.scheme == "https") - http.use_ssl = true - http.verify_mode = OpenSSL::SSL::VERIFY_NONE - end - response = nil + timeout( timeout ) do + begin + uri_str = 'http://' + uri_str unless uri_str.match(/^http/) + url = URI.parse(uri_str) + http = Net::HTTP.new(url.host, url.port) + http.open_timeout = timeout + http.read_timeout = timeout + + if (url.scheme == "https") + http.use_ssl = true + http.verify_mode = OpenSSL::SSL::VERIFY_NONE + end - # - # Ensure we have a trailing "/" - # - if ( url.path.empty? ) - url.path = "/" - end + response = nil - if nil == url.query - response = http.start { http.get(url.path) } - else - response = http.start { http.get("#{url.path}?#{url.query}") } - end + # + # Ensure we have a trailing "/" + # + if ( url.path.empty? ) + url.path = "/" + end - case response - when Net::HTTPRedirection - then - newURL = response['location'].match(/^http/)? - response['Location']:uri_str+response['Location'] - return( getURL(newURL, timeout) ) - else - @status = response.code.to_i - @body = response.body - end + if nil == url.query + response = http.start { http.get(url.path) } + else + response = http.start { http.get("#{url.path}?#{url.query}") } + end - return true - rescue Errno::EHOSTUNREACH => ex - @error = "no route to host" - return false - rescue Timeout::Error => ex - @error = "time out reached" - return false - rescue Errno::ECONNREFUSED => ex - @error = "Connection refused" - return false - rescue => ex - @error = ex - return false + case response + when Net::HTTPRedirection + then + newURL = response['location'].match(/^http/)? + response['Location']:uri_str+response['Location'] + return( getURL(newURL, timeout) ) + else + @status = response.code.to_i + @body = response.body + end + + return true + rescue Errno::EHOSTUNREACH => ex + @error = "no route to host" + return false + rescue Timeout::Error => ex + @error = "time out reached" + return false + rescue Errno::ECONNREFUSED => ex + @error = "Connection refused" + return false + rescue Timeout::Error => e + @error = "TIMEOUT: #{e}" + return false + rescue => ex + @error = ex + return false + end end - @error = "Misc failure" - return false end - - end @@ -187,13 +186,12 @@ if __FILE__ == $0 then # Sample data. # test = { - "target_host" => "http://www.steve.org.uk/", + "target_host" => "http://collector2.sh.bytemark.co.uk/", "test_type" => "http", "verbose" => 1, - "timeout" => 5, + "timeout" => 10, "test_port" => 80, - "test_alert" => "Steve's website is unavailable", - "http_text" => "Steve Kemp", + "test_alert" => "Collector is unavailable", "http_status" => "200" } -- cgit v1.2.1