Monit Configuration for Rails, Sidekiq, Mysqld and Redis

1. About Monit

Monit is a small Open Source utility for managing and monitoring Unix systems. Monit conducts automatic maintenance and repair and can execute meaningful causal actions in error situations.

2. Install Monit

1
2
3
yum install monit
chkconfig monit on
service monit start

3. Configure Monit Web Server

/etc/monit.d/monitrc

1
2
set httpd port 2812
  allow localhost

4. Configurations

1
2
3
4
5
6
7
8
check process httpd
  with pidfile /etc/httpd/run/httpd.pid
  start program = "/etc/init.d/httpd start"
  stop program = "/etc/init.d/httpd stop"
  if failed host localhost port 8081
  protocol http then restart
  if totalmem > 1024 MB for 2 cycles then restart
  if 5 restarts within 5 cycles then timeout
1
2
3
4
5
6
check process sidekiq
  with pidfile /var/your_app/shared/tmp/pids/sidekiq.pid
  start program = "/bin/bash -l -c 'cd /var/your_app/current && bundle exec sidekiq --index 0 --pidfile /var/your_app/shared/tmp/pids/sidekiq.pid --environment production --logfile /var/your_app/shared/log/sidekiq.log --daemon'"
  stop program = "/bin/bash -l -c 'cd /var/your_app/current && bundle exec sidekiqctl stop /var/your_app/shared/tmp/pids/sidekiq.pid 10'"
  if totalmem is greater than 512 MB for 3 cycles then restart
  if 3 restarts within 5 cycles then timeout
1
2
3
4
5
6
check process mysqld
  with pidfile /var/run/mysqld/mysqld.pid
  start program = "/etc/init.d/mysqld start"
  stop program = "/etc/init.d/mysqld stop"
  if failed host 127.0.0.1 port 3306 then restart
  if 5 restarts within 5 cycles then timeout
1
2
3
4
5
6
check process redis
  with pidfile /var/run/redis/redis.pid
  start program = "/etc/init.d/redis start"
  stop program = "/etc/init.d/redis stop"
  if failed host 127.0.0.1 port 6379 then restart
  if 5 restarts within 5 cycles then timeout

5. Reference

http://mmonit.com/monit/

http://railscasts.com/episodes/375-monit