Monit and Mongrel

This post about Monit and Mongrel is based on this fantastic post, Monit makes Mongrel play nice!

Install Monit from source:

$ wget http://www.tildeslash.com/monit/dist/monit-4.10.1.tar.gz
$ tar xvfz monit-4.10.1.tar.gz
$ cd monit-4.10.1
$ ./configure -prefix=/usr  (the default prefix is /usr/local)
$ make
$ make install

Configure Monit:

In the monit source you will find a sample monitrc which is completely commented out. You can then copy it to wherever you like and then edit it. Here is my monitrc.

# Start monit in the background (run as a daemon) and check services at
# 1-minute intervals.
set daemon  60

# Set logging.
set logfile /tmp/monit.log

# Set the list of mail servers for alert delivery.
set mailserver localhost

# Set alert recipients.
set alert foo@example.com                # receive all alerts

# Start Monit's embedded web server.
set httpd port 2812 and
     allow admin:monit      # require user 'admin' with password 'monit'

# Mongrel - Development
# Check that Mongrel is running and that it responds to HTTP
# requests. Check its resource usage such as cpu and memory. If the
# process is not running, monit will restart it by default. In case
# the service was restarted very often and the problem remains, it is
# possible to disable monitoring using the TIMEOUT statement.
check process mongrel-dev with pidfile /rails/log/mongrel.pid
    start program = "/usr/bin/mongrel_rails start -d  -c /rails
                          -p 3000 -P /rails/log/mongrel.pid"
    stop program  = "/usr/bin/mongrel_rails stop
                           -P /rails/log/mongrel.pid"
    if cpu > 50% for 2 cycles then alert
    if cpu > 80% for 5 cycles then restart
    if totalmem > 60.0 MB for 5 cycles then restart
    if loadavg(5min) greater than 10 for 8 cycles then restart
    if failed port 3000 protocol http
       with timeout 15 seconds
       for 2 cycles
       then restart
    if 3 restarts within 5 cycles then timeout
    group mongrel-dev

After you are done configuring monitrc copy it to /etc.

$ sudo cp monitrc /etc

To test the configuration:

$ sudo monit -t

Run Monit:

To start monit:

$ sudo monit

To restart monit:

$ sudo monit reload

To stop monit:

$ sudo monit quit

To see monit’s log (assuming you are using my configuration):

$ tail -f /tmp/monit.log

Learn More:

Monit Getting Started
Monit Manual