18 Nov 2012 » Checkup - a simple service response check

While having to deal with servers and especially with ensuring the uptime of services (including websites), you’ll come in the need to monitor these services automatically. There are several tools available that are up for this task, however, they include a steep learning curve. Tools like nagios, zabbix, icinga and others are very well able to provide you with all the tools you need. However, what if you just want to monitor a single service, or even smaller, a single website?

None of the named tools make it easy to get into them, they allow endless customization. Perfectly fine when you are a server administrator which has dealt with it most of his life, not so inviting when you are a developer. Enter Checkup, a tool I’ve written in the last weeks. While still a command line tool, its learning curve is pretty much non-existant.

Currently, Checkup supports only Http as service to check, and only supports notification by mail. A sample configuration would look like the following:

Checkup::Model.new(:foo, 'foo') do
  service Http do |service|
    service.url = 'http://example.de'
    service.method = :get
    service.expected_code = 200
    service.expected_response = /.+html.+/
  end
  
  notify_with Mail do |mail|
    mail.delivery_method      = :sendmail
    mail.from                 = 'info@example.de'
    mail.to                   = 'bar@example.de'
  
    mail.on_success = true
    mail.on_warning = true
    mail.on_failure = true
  end
end

This configuration will check the website at http://example.de for the occurrence of the regular expression /.+html.+/ and a status code of 200. It will send a mail regardless of the outcome the check(s) via sendmail to bar@example.de.

As the configuration is done completely in ruby, one can easily use the same definition to check multiple hosts at once:

%w(example.de example.com example.org example.net).each do |host|
  service Http do |service|
    service.url = "http://#{host}"
    service.method = :get
    service.expected_code = 200
    service.expected_response = /.+html.+/
  end
end

This will check the hosts defined in the array in order, and will send a mail for each host should it fail. The command line used to execute the model(s):

checkup perform -t=foo -r=~/path/to/models

Combined with the whenever gem, it is easy to generate a crontab that will check the current status of the defined hosts:

every 5.minutes do
  command "checkup perform -r /var/checkup -t foo"
end

which can be written to the crontab of the current user with whenever --update-crontab.