Dec 27

Mongrel Cluster and Monit

by Peter Jones / December 27, 2006
Mongrel is great, but like a lot of people, strange environmental issues cause mongrel processes to SIGSEG and die (in my case it's caused by ferret. This isn't ideal on production servers, so I installed monit to restart mongrel when this happens.

The only problem with monit is the duplication in the configuration file for watching mongrel clusters. Enter mongrel_monit, a ruby script to update a monitrc file with the necessary configuration to monitor your entire cluster.

Updating Your Monitrc

The mongrel_monit script will update/add configuration commands to your monitrc file to watch your mongrel cluster. It includes some comment delimiters so you can run it more than once, e.g. after you update your mongrel cluster configuration.

cp /path/to/monitrc monitrc.orig # keep a backup
mongrel_monit --mongrel=/path/to/mongrel/config --monit=/path/to/monitrc

The best part, when you decide to bump your cluster size up to 15, just run mongrel_monit again to get monit to watch all of the mongrel processes. No more copy and paste!

Customization and Future

Right now, mongrel_monit is a top-down ruby script, nothing fancy. Near the top is an ERB template that can be edited to customize the generated monit configuration. If you make this script better, please let me know.

But What About Capistrano?

If you deploy your application with capistrano then monit will freak-out when you restart mongrel during the restart task. I put the following code in my config/deploy.rb file to overcome this:

desc "Restart the mongrel cluster via monit"
task :restart_mongrel_cluster, :roles => :app do
    sudo "monit -g mongrel restart all"
end

You might even want to include a task to update the monitrc config between a stop and start command (instead of using restart). That way, if you deploy with an updated mongrel cluster config, the monit configuration to monitor that cluster will be updated as well.

Further Reading

Updates

  • May 27, 2007: This idea grew up and is now a project called Bowtie
  • May 30, 2008: Fixed link to the script, sorry about that

Tags: mongrel monit

2 Comments:


Andrew Janssen

Fri, May 30

mongrel_monit looks great, but the link is broken. Would you consider republishing it? Thanks for your work!


Peter Jones

Fri, May 30

Andrew, I’ve fixed the link in the article. Thanks for the heads-up. You really should take a look at Bowtie instead though.


Post a comment.