Created
March 16, 2011 10:28
-
-
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...
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)