Monitor your systems using Monit on shared hosts non-root – Example with Rails3/Capistrano

Do you know Monit? You definitely should. This is a high-quality and mature Open Source project, very well documented, that can monitor all your system resources (global and process-based). It is lightweight, efficient, can take corrective actions (restart servers…), provides real-time web interface, sends alerts, and is so easy to install and to use.

Now that pitching is done, I will show you how to install it and configure it to monitor a Rails application running on Unicorn, deployed using Capistrano and on shared web host having no root privileges.

  1. Install Monit on your production environment
  2. Configure Monit to monitor a Rails3/Unicorn process
  3. Run Monit and check everything runs fine
  4. Integrate it in Capistrano deployments

Install Monit on your production environment

First things first: the installation of Monit on the production environment.

  1. Download Monit source files from Monit site, and upload them to your production environment.
  2. Deflate it in a repository, then apply the common GNU compilation and installation methods. Do not forget to specify the path to the ./configure script with --prefix option: it will be the installation destination.
    tar xvzf monit-5.5.tar.gz
    cd monit-5.5
    ./configure --prefix=/home/myuser/monit
    make
    make install
    
  3. Add the binary folder of monit to your PATH variable. This can be done in a startup file such as ~/.bashrc:
    export PATH=${PATH}:/home/myuser/monit/bin
    
  4. Test your Monit installation by invoking the Monit binary:
    > monit -V
    This is Monit version 5.5
    Copyright (C) 2001-2012 Tildeslash Ltd. All Rights Reserved.
    

Configure Monit to monitor a Rails3/Unicorn process

Monit uses generally the file .monitrc as a configuration file. Here we check how to set directives in it to monitor a running Rails 3 application above a Unicorn server.
The Rails 3 application is running from “/home/myuser/myapp/current” in our examples (deployed from Capistrano).

Monit provides by default a very well documented configuration file named monitrc in the directory where you deflated the source files. You should copy it to .monitrc and then modify it to your needs.

  1. Set Monit process to run as a daemon, waking up every minute:
    set daemon  60
    
  2. Write a log file. This log will then be accessible from the web interface.
    set logfile /home/myuser/log_monit
    
  3. Set a mail SMTP server to send alerts (can be localhost if your mail runs locally):
    set mailserver my_smtp_server.com               # primary mailserver
        username monit@my_site.com password "Password"
    
  4. Set the originator of emails sent by Monit:
    set mail-format { from: monit@my_site.com }
    
  5. Set the receiver of alerts:
    set alert admin@my_site.com
    
  6. Setup a running web interface on a given port (here 12007), and set HTTP authentication on it.
    set httpd port 12007 and
        allow admin:"monit"      # require user 'admin' with password 'monit'
    
  7. Monitor your Rails application using the Unicorn PID file: give commands to start and stop it, eventually monitor some resources.
    check process my_rails_app with pidfile /home/myuser/myapp/shared/pids/unicorn.pid
      start program = "/home/myuser/myapp/unicorn_script.sh start" with timeout 60 seconds
      stop program = "/home/myuser/myapp/unicorn_script.sh stop"
      if totalcpu is greater than 50% for 5 cycles then alert
      if totalmemory is greater than 5% then restart
    
    
  8. Write the unicorn_script.sh script to start and stop Unicorn server. Here is an example of such file. Adapt it to your needs. Do not forget to set all your needed environment variables in it as the Monit daemon will later be invoked using Capistrano remotely, and environment will not be set (.bashrc file won’t be sourced).
    #!/bin/bash
    
    # Set the environment, as required by Monit
    export PATH="/home/myuser/bin:/home/myuser/ruby/gems/bin:/home/myuser/monit/bin:${PATH}"
    export RUBYOPT="${RUBYOPT} -I/home/myuser/rubygems/inst/lib"
    export GEM_PATH="/home/myuser/ruby/gems:/usr/lib/ruby/gems/1.8"
    export GEM_HOME="/home/myuser/ruby/gems"
    
    start () {
      cd /home/myuser/myapp/current
      BUNDLE_GEMFILE=/home/myuser/myapp/current/Gemfile bundle exec unicorn -c /home/myuser/myapp/current/config/unicorn/production.rb -E production -D
    }
    
    stop () {
      kill -s QUIT $(cat /home/myuser/myapp/shared/pids/unicorn.pid)
    }
    
    case $1 in
      start)
        start
      ;;
      stop)
        stop
      ;;
      *)
      echo $"Usage: $0 {start|stop}"
      exit 1
      ;;
    esac
    
    exit 0
    
  9. Test that your configuration file is ok by running Monit:
    > monit -t
    Control file syntax OK
    

    If an error occurs, investigate and correct it before continuing.

Run Monit and check everything runs fine

  1. Run Monit:
    > monit
    

    Pretty simple, he?

  2. Check the web interface, listening on port 12007 in this example (http://my_site.com:12007). It will first prompt you for the user name and password you have set in ~/.monitrc.

    Monit

Integrate it in Capistrano deployments

Now that Monit monitors your Rails application, it is important to stop monitoring during the restart of the server and restart monitoring afterwards. This is easily integrated into Capistrano.

In your Capistrano deploy file, just add the following at the end:

# Monit tasks
namespace :monit do
  task :start do
    run 'monit'
  end
  task :stop do
    run 'monit quit'
  end
end

# Stop Monit during restart
before 'unicorn:restart', 'monit:stop'
after 'unicorn:restart', 'monit:start'

If you are not using Unicorn, replace 'unicorn:restart' with 'deploy:restart', and you should be set.

That’s it!
You should really take a deep look into the commented Monit configuration file and Monit documentation, as you will find real treasures to monitor on your system.

About Muriel Salvan

I am a freelance project manager and polyglot developer, expert in Ruby and Rails. I created X-Aeon Solutions and rivierarb Ruby meetups. I also give trainings and conferences on technical topics. My core development principles: Plugins-oriented architectures, simple components, Open Source power, clever automation, constant technology watch, quality and optimized code. My experience includes big and small companies. I embrace agile methodologies and test driven development, without giving up on planning and risks containment methods as well. I love Open Source and became a big advocate.
Howto, Monitoring, Ruby on Rails, Web development , , , , , , , , , , , , ,

2 comments


Leave a Reply

Your email address will not be published. Required fields are marked *