This library monitors your programs or resources and notifies you by email or log if anything goes wrong.
This gem is hosted on gemcutter.org
gem install benotified
require 'be_notified'
BeNotified::Monitor.new do
alert_when "Something went wrong" do
# Put your code here
# Return true if something went wrong
# or false if everything was fine
end
end
This library can be used in two different ways:
First: Run it from crontab to monitor some resources, for exmaple:
BeNotified::Monitor.new do
alert_when "Staging server is down" do
host_not_available?("staging.example.com")
end
end
or
BeNotified::Monitor.new do
alert_when "Log file does not exist" do
File.exists?('/var/log/messages') == false
end
end
Second: Wrap your program around it and be informed when something goes wrong. I will use imaginary program called db_updater as an example:
BeNotified::Monitor.new do
alert_when "Db was not updated" do
# Require your program here
require 'db_updater'
# Get the count of the records before run
count_before_run = DBUpdater::Update::Record.all.count
# Run your application
DBUpdater::Update.run
# Get the count after the run
count_after_run = DBUpdater::Update::Record.all.count
# Now decide if anything went wrong
count_after_run == count_before_run
end
end
Options can be set up in the three different ways:
First: By default program will log to standard output.
Second: Define properties directly in the code. An example of setting logging and email notification:
BeNotified::Monitor.new do
custom_options = {
:notifier_type => [BeNotified::Notifiers::Log, BeNotified::Notifiers::Email]
:email => {
:smtp_address => 'localhost',
:smtp_port => 25,
:domain => 'example.com',
:username => 'login',
:password => 'pass',
:to => '[email protected]',
:from => '[email protected]',
:subject => 'The Subject'
}
}
# Make sure you merge the options
configuration(custom_options)
end
Third: Create a file .be_notified in your home directory. It stores the configuration in JSON format. An example of setting application to log to the file:
{"logger_file":"/tmp/benotified.log"}
This library is shipped with some helper methods that can be used to monitor resources:
-
host_not_alive? - checks if a given host if alive or not
alert_when "Staging server is down" do host_not_available?("staging.example.com") end
-
number_of_files - checks the number of files in a given directory
alert_when "Number of files on the desktop is greater then 50" do number_of_files("/Users/dominik/Desktop") > 50 end
-
program_running? - checks if a given program is running
alert_when "Application server is not running" do ! program_running?("jboss") end
-
size_of_file - checks the size of a given file in GB, MB or KB
alert_when "JBoss log file is bigger then 1GB" do size_of_file("/usr/local/jboss/server/default/log/server.log") > 1.GB end
- Revise the code and suggest improvements / changes
- Write more helper methods
- Add new ways of notifying user