Skip to content

Instantly share code, notes, and snippets.

@beglov
Last active September 2, 2021 12:28
Show Gist options
  • Save beglov/d1f43d179f40af0e839f19503781ab4f to your computer and use it in GitHub Desktop.
Save beglov/d1f43d179f40af0e839f19503781ab4f to your computer and use it in GitHub Desktop.
Monit rules for typical rails app
### Nginx ###
check process nginx with pidfile /run/nginx.pid
start program = "/usr/sbin/service nginx start"
stop program = "/usr/sbin/service nginx stop"
if cpu > 60% for 2 cycles then alert
if cpu > 80% for 5 cycles then restart
if memory usage > 80% for 5 cycles then restart
if failed host localhost port 80 protocol http
then restart
if 3 restarts within 5 cycles then timeout
### Puma ###
check process puma with pidfile "/home/deploy/tms/shared/tmp/pids/puma.pid"
start program = "/usr/bin/sudo -iu deploy /bin/bash -c 'cd /home/deploy/tms/current && ~/.rvm/bin/rvm default do bundle exec puma -C /home/deploy/tms/shared/puma.rb --daemon'"
stop program = "/usr/bin/sudo -iu deploy /bin/bash -c 'cd /home/deploy/tms/current && ~/.rvm/bin/rvm default do bundle exec pumactl -S /home/deploy/tms/shared/tmp/pids/puma.state stop'"
### Postgresql ###
check process postgresql with pidfile /var/run/postgresql/12-main.pid
start program = "/usr/sbin/service postgresql start"
stop program = "/usr/sbin/service postgresql stop"
if failed host localhost port 5432 protocol pgsql then restart
if cpu > 80% then restart
if memory usage > 80% for 2 cycles then restart
if 5 restarts within 5 cycles then timeout
### Redis ###
check process redis-server with pidfile "/var/run/redis/redis-server.pid"
start program = "/usr/sbin/service redis start"
stop program = "/usr/sbin/service redis stop"
if totalmem > 100 Mb then alert
if children > 255 for 5 cycles then stop
if cpu usage > 95% for 3 cycles then restart
if memory usage > 80% for 5 cycles then restart
if failed host 127.0.0.1 port 6379 then restart
if 5 restarts within 5 cycles then timeout
### Sidekiq ###
check process sidekiq with pidfile "/home/deploy/tms/shared/tmp/pids/sidekiq-0.pid"
start program = "/bin/su - deploy -c 'cd /home/deploy/tms/current && /home/deploy/.rvm/bin/rvm default do bundle exec sidekiq --index 0 --pidfile /home/deploy/tms/shared/tmp/pids/sidekiq-0.pid --environment production --logfile /home/deploy/tms/shared/log/sidekiq.log --daemon'"
stop program = "/bin/su - deploy -c 'cd /home/deploy/tms/current && /home/deploy/.rvm/bin/rvm default do bundle exec sidekiqctl stop /home/deploy/tms/shared/tmp/pids/sidekiq-0.pid 10'"
if cpu > 80% then restart
if memory usage > 80% for 2 cycles then restart
if 3 restarts within 3 cycles then timeout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment