Home > View Post

Monit Configuration Example


posted May 05, 2008 11:23 ago by gersham


Monit is an excellent utility that I use to ensure the health of my Mongrel processes, my webserver, mailserver and other proceses. Should a process fail it'll be restarted. Should the system consume too much memory or have too high a load I'll receive notification emails. It's great. Highly suggested for any Rails install.

Download and install monit (part of the package management system with most distributions), then edit the email addresses, username and password below and place the contents inside /etc/monit/monitrc, restarting monit afterwards.


set daemon  60
set logfile syslog facility log_daemon
set mailserver localhost
set mail-format { from: alert@example.com }
set alert recipient@example.com

set httpd port 7999 and
   allow username:password

check system meharg.com
   if loadavg (1min) > 4 then alert
   if loadavg (5min) > 3 then alert
   if memory usage > 95% then alert
   if cpu usage (user) > 95% then alert

check process nginx with pidfile /var/run/nginx.pid
   group www-data
   start program  "/etc/init.d/nginx start"
   stop program  "/etc/init.d/nginx stop"
   if failed host meharg.com port 80 protocol http
      and request "/" then alert
   if cpu is greater than 60% for 2 cycles then alert
   if cpu > 90% for 5 cycles then restart
   if children > 50 then restart
   if loadavg(5min) greater than 10 for 8 cycles then alert
   if 3 restarts within 5 cycles then timeout

check process mysql with pidfile /var/run/mysqld/mysqld.pid
   group mysql
   start program = "/etc/init.d/mysql start" 
   stop program = "/etc/init.d/mysql stop"
   if failed host 127.0.0.1 port 3306 then restart
   if 5 restarts within 5 cycles then timeout

check process mongrel_3000 with pidfile /var/www/apps/meharg.com/tmp/pids/mongrel.3000.pid
   group mongrel
   start program = "/usr/bin/mongrel_rails start -d -e production -a 127.0.0.1 -c /var/www/apps/meharg.com/ --user mongrel --group mongrel -p 3000 -P /var/www/apps/meharg.com/tmp/pids/mongrel.3000.pid -l /var/www/apps/meharg.com/log/mongrel.3000.log"
   stop program = "/usr/bin/mongrel_rails stop -P /var/www/apps/meharg.com/tmp/pids/mongrel.3000.pid"
   if failed host 127.0.0.1 port 3000 protocol http
      and request "/" then alert
   if totalmem > 100 Mb then restart
   if cpu is greater than 60% for 2 cycles then alert
   if cpu > 80% for 5 cycles then restart
   if loadavg(5min) greater than 10 for 8 cycles then restart
   if 3 restarts within 5 cycles then timeout

check process mongrel_3001 with pidfile /var/www/apps/meharg.com/tmp/pids/mongrel.3001.pid
   group mongrel
   start program = "/usr/bin/mongrel_rails start -d -e production -a 127.0.0.1 -c /var/www/apps/meharg.com/ --user mongrel --group mongrel -p 3001 -P /var/www/apps/meharg.com/tmp/pids/mongrel.3001.pid -l /var/www/apps/meharg.com/log/mongrel.3001.log"
   stop program = "/usr/bin/mongrel_rails stop -P /var/www/apps/meharg.com/tmp/pids/mongrel.3001.pid"
   if failed host 127.0.0.1 port 3001 protocol http
      and request "/" then alert
   if totalmem > 100 Mb then restart
   if cpu is greater than 60% for 2 cycles then alert
   if cpu > 80% for 5 cycles then restart
   if loadavg(5min) greater than 10 for 8 cycles then restart
   if 3 restarts within 5 cycles then timeout

check process mongrel_3002 with pidfile /var/www/apps/meharg.com/tmp/pids/mongrel.3002.pid
   group mongrel
   start program = "/usr/bin/mongrel_rails start -d -e production -a 127.0.0.1 -c /var/www/apps/meharg.com/ --user mongrel --group mongrel -p 3002 -P /var/www/apps/meharg.com/tmp/pids/mongrel.3002.pid -l /var/www/apps/meharg.com/log/mongrel.3002.log"
   stop program = "/usr/bin/mongrel_rails stop -P /var/www/apps/meharg.com/tmp/pids/mongrel.3002.pid"
   if failed host 127.0.0.1 port 3002 protocol http
      and request "/" then alert
   if totalmem > 100 Mb then restart
   if cpu is greater than 60% for 2 cycles then alert
   if cpu > 80% for 5 cycles then restart
   if loadavg(5min) greater than 10 for 8 cycles then restart
   if 3 restarts within 5 cycles then timeout