Skip to content

Instantly share code, notes, and snippets.

@bka
Created March 16, 2011 10:28
Show Gist options
  • Save bka/872292 to your computer and use it in GitHub Desktop.
Save bka/872292 to your computer and use it in GitHub Desktop.
check if a webserver is reachable, if not send an email, can be used via cron...
require 'rubygems'
require 'action_mailer'
require 'mechanize'
# Delivery
SENDER_MAIL = "[email protected]"
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "mail.gmx.de",
:port => 25,
:domain => "www.gmx.de",
:authentication => :login,
:user_name => "[email protected]",
:password => "...",
}
# Recipients
MAIL_ADDRESS = ["[email protected]","..."]
# What to look for
URLS_TO_CHECK = [
"http://www.....de"
]
class Mailer < ActionMailer::Base
def exception_notification(text)
content_type "text/plain"
recipients MAIL_ADDRESS
from SENDER_MAIL
subject "Warning"
body :text => text
end
end
Mailer.template_root = File.dirname(__FILE__)
exception = ""
URLS_TO_CHECK.each do |url|
begin
a = Mechanize.new.get(url)
rescue
exception += "Can not reach #{url}. #{$!.inspect}\n"
end
end
if exception != ""
Mailer.deliver_exception_notification(exception)
end
@dmke
Copy link

dmke commented Mar 22, 2011

Thanks, I wrote a nifty server-side script for periodic status mails based on your code: https://github.com/dmke/cronmail

(Besides, I erased the deprecation warnings you'll get, if you are using actionmailer >= 3.0.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment