Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Is my site down?" Howto

Tags:

ruby

web

What's the best way to create a "Is my site down?" in Ruby? How should I do it to check it using HTTP(s) and Ping?

Thanks.

like image 636
donald Avatar asked Jan 21 '26 08:01

donald


1 Answers

Basically just use a http library to see if you can get (actually, HEADing would be better) the page they're pointing to. If you get a response then the server is up, otherwise (it doesn't respond or times out) it is down and you alert the user accordingly.

This isn't the cleanest way of doing it, but basically:

require 'net/http'
require 'uri'

def isUp( url )
    uri = URI.parse( url )

    begin
        Timeout::timeout(5) {
            Net::HTTP.start( uri.host, uri.port ) { |http|
                 http.head( uri.path )
            }
        }
    rescue Timeout::Error
        return false
    end

    return true 
end

You can probably get it to not wait for the timeout, and/or increase the timeout to avoid the timeout to avoid false positive, but this is a simple example.

like image 156
Reese Moore Avatar answered Jan 23 '26 19:01

Reese Moore