One of things I love about Watir is its flexibility. For example, you can quickly and easily write a script to monitor your web application availability and schedule it to repeat periodically.
I like the idea of application monitoring that acts like a real user. It’s all well and good to monitor a server’s CPU and memory but if the user can’t access the logon page then the application is not doing its job.
I use SMTP to send an email if a page is unavailable. There is some good information about using SMTP connections in ruby available here. You need to specify an SMTP server which most organisations already have running, otherwise you can run one locally or use a public one such as Gmail. You can set up notification groups which then send text messages as well.
require "watir" # For connecting to web pages
require "socket" # For getting host name
require "net/smtp" # For sending email
MONITORED_URLS = ["http://www.google.com","http://www.ruby-lang.org/en/"] #This is a list of urls
EMAIL_FROM = "your.email@example.com"
EMAILS_TO = ["your.email@example.com"] # This must be a list of addresses.
SMTP_SERVER_NAME = "localhost"
LOG_FILE_NAME = "./watir_monitor_log.txt"
def check_page_available(url)
begin
start_time = Time.now
ie_page = Watir::IE.start(url)
load_time = (Time.now - start_time).to_s
if (ie_page.check_for_http_error() or ie_page.text.include?('The page cannot be displayed')) then
result = false
else
result = true
end
rescue
puts "EXCEPTION RAISED: #{$!}"
result = false
end
ie_page.close
return result, load_time
end
def send_email(subject)
Net::SMTP.start(SMTP_SERVER_NAME) do |smtp_server|
EMAILS_TO.each do |email_address|
email_message = "From: Web Site Monitoring Script <#{EMAIL_FROM}>\n"
email_message << "To: #{email_address}\n"
email_message << "Subject: #{subject}\n"
email_message << "#{subject}\n\n"
smtp_server.send_message email_message, EMAIL_FROM, email_address
end
end
end
# Start of Script
host_name = Socket.gethostname
MONITORED_URLS.each do |url|
puts url
date_time = Time.now.strftime( "%A %d/%m/%Y %I:%M %p" )
result, load_time = check_page_available(url)
if not result then
send_email( "Could not connect to URL: <#{url}>. There may be a problem with this site." )
end
File.open(LOG_FILE_NAME, File::WRONLY|File::APPEND|File::CREAT, 0666) do |log_file|
log_file.puts "#{date_time},#{host_name},#{url},#{result},#{load_time}"
end # File closes automatically
end
# End of Script
I like to record page load times for historical data collection. I have logged this information locally for simplicity but you can also easily log this information to a wiki page (such as Confluence) so that it is easy for others to access as well.
Once you have the ruby script ready you can simply schedule it to run as a Windows task every 10 minutes or so. I usually create a one line batch file to call the script with the -b flag so the browser doesn’t display during execution.


May 17, 2008 at 12:44 pm |
if you to the same thing with C# or vb.net.
Try http://www.InCisif.net.