Skip to content

Instantly share code, notes, and snippets.

@leemour
Last active January 16, 2019 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leemour/4cc631dc1d7dc733a9ebe464473646fa to your computer and use it in GitHub Desktop.
Save leemour/4cc631dc1d7dc733a9ebe464473646fa to your computer and use it in GitHub Desktop.
Monit with Puma, Sidekiq, Nginx, PostgreSQL, Disk space and Memory limits, using rbenv and nvm
# /etc/monit/conf.d/app
set daemon 30 with start delay 60
set log "/srv/www/app/shared/log/monit.log" # syslog facility log_daemon
set httpd port 2812
allow localhost
allow login:password
# with ssl {
# pemfile: /etc/ssl/certs/app_com.pem
# }
set mailserver smtp.gmail.com port 587
username "example@gmail.com" password "password"
using tls
set alert alert@gmail.com but not on { instance }
mail-format {
from: app Monit <monit@app.ru>
reply-to: support@app.ru
subject: [$HOST] $SERVICE $EVENT at $DATE
message: Monit $ACTION $SERVICE at $DATE on $HOST: $DESCRIPTION.
Yours sincerely,
monit
}
### Disk Usage ###
check filesystem Ubuntu with path "/dev/vda1"
if space usage > 90% then alert
### Nginx ###
check process nginx with pidfile "/run/nginx.pid"
group www
start program = "/etc/init.d/nginx start"
stop program = "/etc/init.d/nginx stop"
if cpu usage > 90% for 3 cycles then alert
if cpu usage > 90% for 3 cycles then restart
if memory usage > 90% for 3 cycles then alert
if memory usage > 90% for 3 cycles then restart
if failed host 127.0.0.1 port 80 protocol http
then restart
if 3 restarts within 9 cycles then alert
if 5 restarts within 5 cycles then timeout
### Postgresql ###
check process postgresql
with pidfile "/var/lib/postgresql/11/main/postmaster.pid"
group database
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 usage > 90% for 2 cycles then restart
if memory usage > 90% 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"
group database
start program = "/etc/init.d/redis-server start"
stop program = "/etc/init.d/redis-server stop"
if totalmem > 100 Mb then alert
if children > 255 for 3 cycles then restart # stop?
if cpu usage > 90% for 2 cycles then restart
if memory usage > 90% for 2 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 "/srv/www/app/shared/tmp/pids/sidekiq-0.pid"
depends on redis-server
group queue
start program = "/bin/su - bookshore -c 'cd /srv/www/app/current && PATH=$HOME/.nvm/versions/node/v10.13.0/bin:~/.rbenv/shims:~/.rbenv/bin:$PATH RBENV_ROOT=$HOME/.rbenv RBENV_VERSION=2.5.3 $HOME/.rbenv/bin/rbenv exec bundle exec sidekiq --index 0 --pidfile /srv/www/app/shared/tmp/pids/sidekiq-0.pid --environment production --logfile /srv/www/app/shared/log/sidekiq.log --config /srv/www/app/current/config/sidekiq.yml --daemon'"
stop program = "/bin/su - bookshore -c 'cd /srv/www/app/current && RBENV_ROOT=$HOME/.rbenv RBENV_VERSION=2.5.3 $HOME/.rbenv/bin/rbenv exec bundle exec sidekiqctl stop /srv/www/app/shared/tmp/pids/sidekiq-0.pid 10'"
if cpu usage > 90% then restart
if memory usage > 90% for 2 cycles then restart
if 3 restarts within 5 cycles then timeout
### Puma ###
check process puma_master
with pidfile "/srv/www/app/shared/tmp/puma.pid"
depends on postgresql, redis-server
group ruby
start program = "/bin/su - bookshore -c 'cd /srv/www/app/current && PATH=$HOME/.nvm/versions/node/v10.13.0/bin:~/.rbenv/shims:~/.rbenv/bin:$PATH RBENV_ROOT=$HOME/.rbenv RBENV_VERSION=2.5.3 $HOME/.rbenv/bin/rbenv exec bundle exec puma -C /srv/www/app/shared/config/puma.rb --daemon'"
stop program = "/bin/su - bookshore -c 'cd /srv/www/app/current && RBENV_ROOT=$HOME/.rbenv RBENV_VERSION=2.5.3 $HOME/.rbenv/bin/rbenv exec bundle exec pumactl -S /srv/www/app/shared/tmp/puma.state -F /srv/www/app/shared/config/puma.rb stop'"
if cpu usage > 90% for 2 cycles then restart
if memory usage > 94% for 2 cycles then restart
if 3 restarts within 5 cycles then timeout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment