Simple web application monitoring with Watir

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 = ["",""] #This is a list of urls
EMAILS_TO = [""] # This must be a list of addresses.
SMTP_SERVER_NAME = "localhost"
LOG_FILE_NAME = "./watir_monitor_log.txt"

def check_page_available(url)
      start_time =
      ie_page = Watir::IE.start(url)
      load_time = ( - start_time).to_s
      if (ie_page.check_for_http_error() or ie_page.text.include?('The page cannot be displayed')) then
         result = false
         result = true
      puts "EXCEPTION RAISED: #{$!}"
      result = false
   return result, load_time

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 \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

# Start of Script
host_name = Socket.gethostname
MONITORED_URLS.each do |url|
	puts url
	date_time = "%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: . There may be a problem with this site." )
	end, 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 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.

Author: Alister Scott

Alister is an Excellence Wrangler for Automattic.

One thought on “Simple web application monitoring with Watir”

Comments are closed.