Monitoring Your Mongrel with Monit

From OCS Support Wiki

Jump to: navigation, search

[edit] Introduction

Using Monit, you can monitor your Mongrel process, automatically restarting it if it goes down. Optionally, you can configure it to send you an e-mail if it has to automatically restart it.

[edit] Prerequisites

Before proceeding with automatic monitoring and restarting with Monit on an OCS hosting account, you must have:

  • Successfully completed your Mongrel setup with it running correctly
  • Knowledge of SSH and editing files via the console

[edit] Instructions

The instructions are identical for both Webmin and cPanel accounts.

Connect to your account via SSH and run:

cd
mkdir ~/bin
mkdir ~/etc
cd ~/etc
wget http://scripts.ocssolutions.com/monitrc-mongrel
chmod 0600 monitrc

You will now need to edit the new file monitrc-mongrel in the ~/etc folder. It will contain at first:

set daemon 60

check process mongrel_rails with pidfile /home/username/rails_apps/yourapp/log/mongrel.pid
  start program = "/home/username/bin/start_mongrel"
  stop program = "/home/username/bin/stop_mongrel"
  if failed host yourserver.ocssolutions.com port 60000 protocol http
    then restart

You will need to change a few values before it will work correctly. Replace:

  • username with your FTP/SSH username
  • rails_apps/yourapp with the path to your Rails application
  • yourserver.ocssolutions.com with the hostname of the server you are on. You can obtain this by running the hostname command
  • 60000 with the port your Mongrel runs on

Once you do this, save the file and exit the editor. You now need to create a stop and start script. Do this by changing to the ~/bin folder with:

cd ~/bin

and then creating the files stop_mongrel and start_mongrel in your favorite text editor.

The stop_mongrel file should contain:

#!/bin/sh

killall -9 ruby mongrel_rails
rm -f ~/rails_apps/yourapp/log/mongrel.pid

Your start_mongrel file should contain:

#!/bin/sh

rm -f ~/rails_apps/yourapp/log/mongrel.pid
cd ~/rails_apps/yourapp
mongrel_rails start -e production -p 60000 -d

As in the ~/etc/monitrc file, replace the values above with values that match your information. You may also wish to slightly alter the way you start your Mongrel process. Just make sure that it starts in production mode.

Once you have done this, run:

cd ~/bin
chmod 0700 stop_mongrel
chmod 0700 start_mongrel

to make the scripts executable.

You will now need to edit your crontab and have Monit start up your Mongrel. To do this, run:

crontab -e

If you see a line starting with @reboot and it starts your mongrel, remove this line and add:

@reboot monit -c /home/username/etc/monitrc

Change username to match your FTP/SSH username. Save the file and exit. This will start Monit upon reboot of the server, and subsequently Monit will start your Rails application.

Now you need to start Monit so that it will start monitoring your Rails application. To do this, simply run:

monit -c /home/username/etc/monit

As in the previous commands, replace username to match your FTP/SSH username.

Monit will now be running and monitoring your Mongrel process. Should it terminate for any reason it will restart it for you.