summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTyler Mercier <tylermercier@gmail.com>2013-07-26 11:38:16 -0600
committerTyler Mercier <tylermercier@gmail.com>2013-07-26 11:38:16 -0600
commit5e5a390f7860eb8f5c0a56bb617353adae98faed (patch)
treec68e7d56602113f8a1a3d687d0064af1e73c330f
parentf6f238c88a0324a3ee60783479e35671b50bdf32 (diff)
add monit recipe
-rw-r--r--config/deploy.rb1
-rw-r--r--config/recipes/monit.rb37
-rw-r--r--config/recipes/postgresql.rb1
-rw-r--r--config/recipes/templates/monit/monitrc.erb26
-rw-r--r--config/recipes/templates/monit/nginx.erb5
-rw-r--r--config/recipes/templates/monit/postgresql.erb5
-rw-r--r--config/recipes/templates/monit/unicorn.erb15
-rw-r--r--config/recipes/templates/unicorn.rb.erb2
8 files changed, 92 insertions, 0 deletions
diff --git a/config/deploy.rb b/config/deploy.rb
index d366501..0ac0220 100644
--- a/config/deploy.rb
+++ b/config/deploy.rb
@@ -8,6 +8,7 @@ load "config/recipes/postgresql"
load "config/recipes/nodejs"
load "config/recipes/rbenv"
load "config/recipes/newrelic"
+load "config/recipes/monit"
set :application, "parley"
set :user, "deployer"
diff --git a/config/recipes/monit.rb b/config/recipes/monit.rb
new file mode 100644
index 0000000..31ee6dd
--- /dev/null
+++ b/config/recipes/monit.rb
@@ -0,0 +1,37 @@
+namespace :monit do
+ desc "Install Monit"
+ task :install do
+ run "#{sudo} apt-get -y install monit"
+ end
+ after "deploy:install", "monit:install"
+
+ desc "Setup all Monit configuration"
+ task :setup do
+ monit_config "monitrc", "/etc/monit/monitrc"
+ nginx
+ postgresql
+ unicorn
+ syntax
+ reload
+ end
+ after "deploy:setup", "monit:setup"
+
+ task(:nginx, roles: :web) { monit_config "nginx" }
+ task(:postgresql, roles: :db) { monit_config "postgresql" }
+ task(:unicorn, roles: :app) { monit_config "unicorn" }
+
+ %w[start stop restart syntax reload].each do |command|
+ desc "Run Monit #{command} script"
+ task command do
+ run "#{sudo} service monit #{command}"
+ end
+ end
+end
+
+def monit_config(name, destination = nil)
+ destination ||= "/etc/monit/conf.d/#{name}.conf"
+ template "monit/#{name}.erb", "/tmp/monit_#{name}"
+ run "#{sudo} mv /tmp/monit_#{name} #{destination}"
+ run "#{sudo} chown root #{destination}"
+ run "#{sudo} chmod 600 #{destination}"
+end
diff --git a/config/recipes/postgresql.rb b/config/recipes/postgresql.rb
index 5cc405f..1158b6e 100644
--- a/config/recipes/postgresql.rb
+++ b/config/recipes/postgresql.rb
@@ -2,6 +2,7 @@ set_default(:postgresql_host, "localhost")
set_default(:postgresql_user) { application }
set_default(:postgresql_password) { Capistrano::CLI.password_prompt "PostgreSQL Password: " }
set_default(:postgresql_database) { "#{application}_production" }
+set_default(:postgresql_pid) { "/var/run/postgresql/9.1-main.pid" }
namespace :postgresql do
desc "Install the latest stable release of PostgreSQL."
diff --git a/config/recipes/templates/monit/monitrc.erb b/config/recipes/templates/monit/monitrc.erb
new file mode 100644
index 0000000..88816e0
--- /dev/null
+++ b/config/recipes/templates/monit/monitrc.erb
@@ -0,0 +1,26 @@
+set daemon 30
+
+set logfile /var/log/monit.log
+set idfile /var/lib/monit/id
+set statefile /var/lib/monit/state
+
+set eventqueue
+ basedir /var/lib/monit/events
+ slots 100
+
+# set mailserver smtp.gmail.com port 587
+# username "foo@example.com" password "secret"
+# using tlsv1
+# with timeout 30 seconds
+
+set alert ryan@railscasts.com
+
+set httpd port 2812
+ allow admin:"secret"
+
+check system <%= application %>
+ if loadavg(5min) > 2 for 2 cycles then alert
+ if memory > 75% for 2 cycles then alert
+ if cpu(user) > 75% for 2 cycles then alert
+
+include /etc/monit/conf.d/*
diff --git a/config/recipes/templates/monit/nginx.erb b/config/recipes/templates/monit/nginx.erb
new file mode 100644
index 0000000..c1d6dd5
--- /dev/null
+++ b/config/recipes/templates/monit/nginx.erb
@@ -0,0 +1,5 @@
+check process nginx with pidfile /var/run/nginx.pid
+ start program = "/etc/init.d/nginx start"
+ stop program = "/etc/init.d/nginx stop"
+ if children > 250 then restart
+ if 5 restarts within 5 cycles then timeout
diff --git a/config/recipes/templates/monit/postgresql.erb b/config/recipes/templates/monit/postgresql.erb
new file mode 100644
index 0000000..22e5652
--- /dev/null
+++ b/config/recipes/templates/monit/postgresql.erb
@@ -0,0 +1,5 @@
+check process postgresql with pidfile <%= postgresql_pid %>
+ start program = "/etc/init.d/postgresql start"
+ stop program = "/etc/init.d/postgresql stop"
+ if failed host localhost port 5432 protocol pgsql then restart
+ if 5 restarts within 5 cycles then timeout
diff --git a/config/recipes/templates/monit/unicorn.erb b/config/recipes/templates/monit/unicorn.erb
new file mode 100644
index 0000000..f75722a
--- /dev/null
+++ b/config/recipes/templates/monit/unicorn.erb
@@ -0,0 +1,15 @@
+check process <%= application %>_unicorn with pidfile <%= unicorn_pid %>
+ start program = "/etc/init.d/unicorn_<%= application %> start"
+ stop program = "/etc/init.d/unicorn_<%= application %> stop"
+
+<% unicorn_workers.times do |n| %>
+ <% pid = unicorn_pid.sub(".pid", ".#{n}.pid") %>
+ check process <%= application %>_unicorn_worker_<%= n %> with pidfile <%= pid %>
+ start program = "/bin/true"
+ stop program = "/usr/bin/test -s <%= pid %> && /bin/kill -QUIT `cat <%= pid %>`"
+ if mem > 200.0 MB for 1 cycles then restart
+ if cpu > 50% for 3 cycles then restart
+ if 5 restarts within 5 cycles then timeout
+ alert ryan@railscasts.com only on { pid }
+ if changed pid 2 times within 60 cycles then alert
+<% end %>
diff --git a/config/recipes/templates/unicorn.rb.erb b/config/recipes/templates/unicorn.rb.erb
index 136b86b..d9fdbb3 100644
--- a/config/recipes/templates/unicorn.rb.erb
+++ b/config/recipes/templates/unicorn.rb.erb
@@ -31,4 +31,6 @@ after_fork do |server, worker|
if defined?(ActiveRecord::Base)
ActiveRecord::Base.establish_connection
end
+ child_pid = server.config[:pid].sub(".pid", ".#{worker.nr}.pid")
+ system("echo #{Process.pid} > #{child_pid}")
end