Skip to content

Instantly share code, notes, and snippets.

@ik5
Created November 16, 2011 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ik5/1370702 to your computer and use it in GitHub Desktop.
Save ik5/1370702 to your computer and use it in GitHub Desktop.
A watchdog for ahn v0.1
#!/usr/bin/env ruby
require 'rubygems'
require 'mail' # gem install mail
require 'fileutils' # for 1.8
BASE_PATH = '' # Place your path here for ahn
DEAMON = '' # Place the deamon path and name here
def send_email
ip = get_ip # You should create this code yourself
mail = Mail.new
mail.to '' # Write who should get the email
mail.from '' # who sent it ?
mail.subject 'Server crash - restarted it'
mail.body 'The server %s had ahn crashed.' % ip
mail.add_file "#{BASE_PATH}/log/adhearsion.log"
# configure your email settings here
mail.delivery_method.settings = {:address => '',
:authentication => nil,
:enable_starttls_auto => true,
:domain => ip, :port => 25,
:password => nil, :user_name => nil }
mail.deliver
rescue
end
begin
pid = File.open("#{BASE_PATH}/adhearsion.pid").read.chomp # if file not exists, it will raise an exception ...
Process.kill 0, pid.to_i
rescue => e
send_email
FileUtils.mv("#{BASE_PATH}/log/adhearsion.log",
"#{BASE_PATH}/log/adhearsion.log.#{Time.now.strftime('%d%m%Y%H%M%S')}")
system("#{DEAMON} restart")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment