From: Walle Wallen on
Hey, I need a better way of checking if a webserver is online and
operational. The following code
works ok, but it sometimes returns the wrong result. Does anyone have a
better approach?

//Walle

def check_http(server)
Timeout::timeout(10.0) do
if(Net::HTTP.get_response(URI.parse("http://#{server}.something.com")).code
== "200")
return true
end
return false
end
rescue Timeout::Error
return false
rescue
return false
end
--
Posted via http://www.ruby-forum.com/.

From: Hassan Schroeder on
On Wed, Jun 2, 2010 at 10:31 AM, Walle Wallen <walle.sthlm(a)gmail.com> wrote:
> Hey, I need a better way of checking if a webserver  is online and
> operational. The following code works ok, but it sometimes returns the wrong result.

Under what circumstances does it fail?

> def check_http(server)
>  Timeout::timeout(10.0) do
>    if(Net::HTTP.get_response(URI.parse("http://#{server}.something.com")).code == "200")
>    return true
>    end
>    return false
>  end
>  rescue Timeout::Error
>  return false
>  rescue
>  return false
>  end

In any case, that seems rather convoluted; I cut it down to this:

def check_http(server)
Net::HTTP.get_response(URI.parse("http://#{server}")).code == "200"
end

:: and it seemed to work just fine. :-)

FWIW,
--
Hassan Schroeder ------------------------ hassan.schroeder(a)gmail.com
twitter: @hassan

From: Walle Wallen on
I need the timeout timer for servers that have crashed, running
extremely slow.
--
Posted via http://www.ruby-forum.com/.

From: Hassan Schroeder on
On Wed, Jun 2, 2010 at 12:33 PM, Walle Wallen <walle.sthlm(a)gmail.com> wrote:
> I need the timeout timer for servers that have crashed, running
> extremely slow.

OK, whatever. You originally said you were checking "if a webserver
is online and operational" -- I wouldn't consider one that's crashed or
can't respond to that request as "operational" :-)

Regardless, you still didn't describe the circumstances under which
your script gives inaccurate results.

--
Hassan Schroeder ------------------------ hassan.schroeder(a)gmail.com
twitter: @hassan

From: Walle Wallen on
Hehe, sometimes the timer kicks in, 10 seconds, even if the webserver is
running and is operational.
Thanks for your responds.

//Walle
--
Posted via http://www.ruby-forum.com/.