Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mike1011/fb2774a6e4f26f1e38bd5b54cb6e0734 to your computer and use it in GitHub Desktop.
Save mike1011/fb2774a6e4f26f1e38bd5b54cb6e0734 to your computer and use it in GitHub Desktop.
Monit custom scripts for my Rails 4 App
First, setup and install monit on Ubuntu server
###get the latest
sudo apt-get update
##install monit
sudo apt-get install monit
##edit monit and add the below custom scripts
sudo vim /etc/monit/monitrc
##reload and verify
sudo monit reload
sudo monit
sudo monit status
##add default added nginx script and include it
sudo ln /etc/monit/conf-available/nginx /etc/monit/conf-enabled/nginx
##check the web interface by www.myapp.com:2812
############ BELOW SCRIPTS ARE ADDED IN /etc/monit/monitrc , preferably in SERVICES BLOCK######
######===========custom script added for elasticsearch============================
check process elasticsearch with pidfile /var/run/elasticsearch/elasticsearch.pid
start program = "/etc/init.d/elasticsearch start"
stop program = "/etc/init.d/elasticsearch stop"
if 5 restarts within 5 cycles then timeout
#if failed host 127.0.0.1 port 9200 type http then restart
if failed host 127.0.0.1 port 9200 with timeout 10 seconds for 10 cycles then restart
##===============custom script for postgresql 9.5=================================
check process postgres with pidfile /var/run/postgresql/9.5-main.pid
group database
start program = "/etc/init.d/postgresql start"
stop program = "/etc/init.d/postgresql stop"
if failed unixsocket /var/run/postgresql/.s.PGSQL.5432 protocol pgsql
then restart
##============custom script for puma===============================================
check process myapp-puma
with pidfile /home/deployer/apps/myapp/shared/tmp/pids/puma.pid
start program = "/usr/bin/nohup /bin/bash -c 'cd /home/deployer/apps/myapp/current; PATH=$PATH:/usr/local/bin:/home/deployer/.rvm/bin RAILS_ENV=production bundle exec puma -C config/puma/production.rb >/home/deployer/apps/myapp/shared/tmp/puma.out 2>/home/deployer/apps/myapp/shared/tmp/puma.err </dev/null' &"
stop program = "/bin/bash -c 'cd /home/deployer/apps/myapp && if [ -f shared/tmp/pids/puma.pid ]; then cat shared/tmp/pids/puma.pid && echo 'STOP' | xargs kill -9; rm shared/tmp/pids/puma.pid; fi'"
if totalmem > 300.0 MB for 5 cycles then restart
if cpu usage > 95% for 5 cycles then restart
group myapp
##================custom script for redis====================================
check process redis
with matching 'redis-server'
start program = "/bin/systemctl start redis"
stop program = "/bin/systemctl stop redis"
if failed host 127.0.0.1 port 6379 then restart
if 5 restarts within 5 cycles then timeout
##===========ping your website every few seconds to verify whethers its up and running or not=========
check host www.myapp.com with address www.myapp.com
if failed
icmp type echo count 5 with timeout 15 seconds
then alert
##============custom script for sidekiq==================================================
check process sidekiq
with matching 'sidekiq'
start program = "/usr/bin/nohup /bin/bash -c 'cd /home/deployer/apps/myapp/current; PATH=$PATH:/usr/local/bin:/home/deployer/.rvm/bin RAILS_ENV=production bundle exec service sidekiq start'"
stop program = "/usr/bin/nohup /bin/bash -c 'cd /home/deployer/apps/myapp/current; PATH=$PATH:/usr/local/bin:/home/deployer/.rvm/bin RAILS_ENV=production bundle exec service sidekiq stop'"
I have referred multiple sites to come out with my own customise implementation..
References =>>>>>>>
https://cbabhusal.wordpress.com/2016/09/29/install-and-configure-monit-web-interface/
https://www.monterail.com/blog/2015/deployment-setup-for-jruby-rails-app-with-puma-mina-and-monit
https://www.lugolabs.com/articles/monitoring-ruby-on-rails-applications-with-monit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment