Created
October 2, 2012 08:59
-
-
Save sgirones/3817558 to your computer and use it in GitHub Desktop.
Monitor websites and send an email when a timeout is detected.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "yaml" | |
require "time" | |
require "net/smtp" | |
# params | |
filename = "results.data" | |
sleep_time = 1 # In seconds. Time between iterations | |
SMTP_SERVER = "localhost" | |
FROM_EMAIL = "websites_checker@domain.com" | |
TO_EMAIL = "your@email.com" | |
last_mtime = Time.now | |
def check_and_send_mail data | |
data["sites"].each do |site, status| | |
if status == "timeout" | |
puts "#{site} has timed out. Sending mail..." | |
msg = "From: #{FROM_EMAIL}>\nTo: #{TO_EMAIL}\nSubject: #{site} has timed out\n\n#{site} has timed out" | |
Net::SMTP.start(SMTP_SERVER, 25) do |smtp| | |
smtp.send_message msg, FROM_EMAIL, TO_EMAIL | |
end | |
puts "Sent!" | |
end | |
end | |
end | |
while 1 | |
# If last modified time is different from current modified time, new results are available. Check it. | |
if (last_mtime != File.new(filename).mtime) then | |
# Load data | |
data = "" | |
File.open(filename, "r") do |f| | |
data = YAML::load f.read | |
end | |
#Remember modified time | |
last_mtime = File.new(filename).mtime | |
# Check data and send an email if needed | |
begin | |
check_and_send_mail data | |
rescue Exception => e | |
puts "Error parsing data or sending email: #{e}" | |
end | |
end | |
sleep sleep_time | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "net/http" | |
require "timeout" | |
require "yaml" | |
require "time" | |
#params | |
sites = ["www.abiquo.com", "wiki.abiquo.com", "jira.abiquo.com"] #List of sites to monitor | |
timeout = 10 #In seconds. Timeout when connecting or reading from the site | |
sleep_time = 10 # In seconds. Time between iterations | |
filename = "results.data" | |
while 1 | |
puts "Checking..." | |
sites_status = {} | |
sites.each do |s| | |
begin | |
http = Net::HTTP.new(s) | |
http.read_timeout = timeout | |
http.open_timeout = timeout | |
res = http.get "/" | |
sites_status[s] = "#{res.code}#{res.message}" | |
rescue Timeout::Error => e | |
sites_status[s] = "timeout" | |
rescue Errno::ETIMEDOUT => e | |
sites_status[s] = "timeout" | |
end | |
end | |
File.open(filename, "w") do |f| | |
data = {"when" => Time.now, "sites" => sites_status} | |
f.puts data.to_yaml | |
end | |
puts "Done" | |
sleep sleep_time | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment