Thoughts, information and reflections about technology

site monitoring with Ruby on Rails

I needed a way to show the status (Up/down) of internal Ruby applications.  I found an answer on StackOverflow that pointed me in the right direction (Apologies that I can’t locate that post). . I modified that somewhat by putting it into a method and passing the site name to it.

If you put the following code in a controller, and all it as indicated in the remarks from your view, it will check the site and respond with UP or down.  I have a list of about a half dozen applications in a table with a status column.

NOTE – adding this WILL slow the page response where you are using multiple calls to check_apps. I’m noticing about a 2-3 second load time instead of an instantaneous load.



# ————————-  check apps ——————————# This method will do a quick and dirty check to see if a site is up or not.
# call the method with check_apps(‘http://www.sitename.com:port’)# the port is optional
helper_method :check_apps

def

def check_apps(site)
    require ‘net/http’

    require ‘json’

 

    status = ‘-‘

 


    url = URI.parse(site)    req = Net::HTTP::Get.new(url.to_s)
    begin

        res = Net::HTTP.start(url.host, url.port) do |http|          http.request(req)          status = “UP”
end
rescue
        end
rescue
    rescue       status = ‘down’
end

    return
    end

    return

    return status


end

#  ———————————————————-

 


Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact me
Archives
Categories
Amazon Disclosure

We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to Amazon.com and affiliated sites.