poise-monit-compat 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/.foodcritic +1 -0
  3. data/.gitignore +11 -0
  4. data/.kitchen.yml +8 -0
  5. data/.travis.yml +20 -0
  6. data/.yardopts +7 -0
  7. data/Berksfile +30 -0
  8. data/CHANGELOG.md +9 -0
  9. data/Gemfile +36 -0
  10. data/LICENSE +201 -0
  11. data/README.md +139 -0
  12. data/Rakefile +17 -0
  13. data/chef/attributes/default.rb +63 -0
  14. data/chef/recipes/default.rb +47 -0
  15. data/chef/recipes/postfix.rb +31 -0
  16. data/chef/recipes/postgresql.rb +32 -0
  17. data/chef/recipes/rabbitmq.rb +31 -0
  18. data/chef/recipes/resque.rb +25 -0
  19. data/chef/recipes/resque_scheduler.rb +30 -0
  20. data/chef/recipes/ssh.rb +30 -0
  21. data/chef/recipes/unicorn.rb +25 -0
  22. data/chef/templates/compat.conf.erb +43 -0
  23. data/chef/templates/resque.conf.erb +21 -0
  24. data/chef/templates/unicorn.conf.erb +17 -0
  25. data/lib/poise_monit_compat.rb +21 -0
  26. data/lib/poise_monit_compat/cheftie.rb +17 -0
  27. data/lib/poise_monit_compat/resources.rb +26 -0
  28. data/lib/poise_monit_compat/resources/monitrc.rb +95 -0
  29. data/lib/poise_monit_compat/version.rb +20 -0
  30. data/poise-monit-compat.gemspec +42 -0
  31. data/test/cookbooks/monit_test/attributes/default.rb +17 -0
  32. data/test/cookbooks/monit_test/metadata.rb +19 -0
  33. data/test/cookbooks/monit_test/recipes/default.rb +17 -0
  34. data/test/gemfiles/chef-12.gemfile +19 -0
  35. data/test/gemfiles/master.gemfile +25 -0
  36. data/test/integration/default/serverspec/default_spec.rb +37 -0
  37. data/test/spec/default_spec.rb +110 -0
  38. data/test/spec/monitrc_spec.rb +41 -0
  39. data/test/spec/postfix_spec.rb +24 -0
  40. data/test/spec/postgresql_spec.rb +24 -0
  41. data/test/spec/rabbitmq_spec.rb +24 -0
  42. data/test/spec/resque_scheduler_spec.rb +25 -0
  43. data/test/spec/resque_spec.rb +24 -0
  44. data/test/spec/spec_helper.rb +20 -0
  45. data/test/spec/ssh_spec.rb +24 -0
  46. data/test/spec/unicorn_spec.rb +24 -0
  47. metadata +167 -0
@@ -0,0 +1,17 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require 'poise_boiler/rakefile'
@@ -0,0 +1,63 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+ # Some code used under MIT license
17
+ # Copyright Alex Soto
18
+ #
19
+
20
+ default['monit']['notify_email'] = nil
21
+ default['monit']['alert_blacklist'] = %w( action instance pid ppid )
22
+
23
+ default['monit']['logfile'] = 'syslog facility log_daemon'
24
+
25
+ default['monit']['poll_period'] = 60
26
+
27
+ default['monit']['mail_format']['subject'] = '$SERVICE $EVENT'
28
+ default['monit']['mail_format']['from'] = "monit@#{node['fqdn']}"
29
+ default['monit']['mail_format']['message'] = <<-EOS
30
+ Monit $ACTION $SERVICE at $DATE on $HOST: $DESCRIPTION.
31
+ Yours sincerely,
32
+ monit
33
+ EOS
34
+
35
+ default['monit']['mailserver']['host'] = 'localhost'
36
+ default['monit']['mailserver']['hostname'] = nil
37
+ default['monit']['mailserver']['port'] = nil
38
+ default['monit']['mailserver']['username'] = nil
39
+ default['monit']['mailserver']['password'] = nil
40
+ default['monit']['mailserver']['password_suffix'] = nil
41
+ default['monit']['mailserver']['encryption'] = nil
42
+ default['monit']['mailserver']['timeout'] = 60
43
+
44
+ default['monit']['port'] = 3737
45
+ default['monit']['address'] = nil
46
+ default['monit']['ssl'] = false
47
+ default['monit']['cert'] = '/etc/monit/monit.pem'
48
+ default['monit']['allow'] = []
49
+ default['monit']['username'] = nil
50
+ default['monit']['password'] = nil
51
+
52
+ default['monit']['eventqueue']['set'] = true
53
+ default['monit']['eventqueue']['basedir'] = '/var/monit'
54
+ default['monit']['eventqueue']['slots'] = 1000
55
+
56
+ default['monit']['ssh_port'] = 22
57
+
58
+ default['monit']['postgres']['pid_file'] = '/var/run/postgresql/9.1-main.pid'
59
+ default['monit']['postgres']['unix_socket_path'] = '/var/run/postgresql/.s.PGSQL'
60
+ default['monit']['postgres']['port'] = 5432
61
+
62
+ default['monit']['unicorn']['pid_dir'] = '/path/to/pids'
63
+ default['monit']['unicorn']['worker_count'] = 1
@@ -0,0 +1,47 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ # Transfer over node attributes as needed.
18
+ node.force_override['poise-monit']['recipe']['daemon_interval'] = node['monit']['poll_period']
19
+ node.force_override['poise-monit']['recipe']['logfile'] = node['monit']['logfile']
20
+ node.force_override['poise-monit']['recipe']['var_path'] = node['monit']['eventqueue']['basedir']
21
+ node.force_override['poise-monit']['recipe']['event_slots'] = if node['monit']['eventqueue']['set']
22
+ node['monit']['eventqueue']['slots']
23
+ else
24
+ 0
25
+ end
26
+ # We're handling the HTTPD settings in the compat config if a password or allow is set.
27
+ if node['monit']['password'] || !node['monit']['allow'].empty?
28
+ node.force_override['poise-monit']['recipe']['httpd_port'] = false
29
+ else
30
+ node.force_override['monit']['port'] = false
31
+ end
32
+
33
+ # If no specific provider was requested, force it to use system.
34
+ unless node['poise-monit']['monit'] && node['poise-monit']['monit']['provider']
35
+ node.force_override['poise-monit']['monit']['provider'] = 'system'
36
+ end
37
+
38
+ # Reset the cache.
39
+ node.attributes.reset_cache('monit')
40
+ node.attributes.reset_cache('poise-monit')
41
+
42
+ include_recipe 'poise-monit'
43
+
44
+ monit_config 'compat' do
45
+ source 'compat.conf.erb'
46
+ variables node['monit']
47
+ end
@@ -0,0 +1,31 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+ # Some code used under MIT license
17
+ # Copyright Alex Soto
18
+ #
19
+
20
+ include_recipe 'monit'
21
+
22
+ monit_config 'postfix' do
23
+ content <<-EOH
24
+ CHECK PROCESS postfix WITH PIDFILE /var/spool/postfix/pid/master.pid
25
+ start program = "/etc/init.d/postfix start"
26
+ stop program = "/etc/init.d/postfix stop"
27
+ GROUP mail
28
+ IF FAILED PORT 25 PROTOCOL smtp THEN restart
29
+ IF 5 RESTARTS WITHIN 5 CYCLES THEN timeout
30
+ EOH
31
+ end
@@ -0,0 +1,32 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+ # Some code used under MIT license
17
+ # Copyright Alex Soto
18
+ #
19
+
20
+ include_recipe 'monit'
21
+
22
+ monit_config 'postgresql' do
23
+ content <<-EOH
24
+ CHECK PROCESS postgresql WITH PIDFILE #{node['monit']['postgres']['pid_file']}
25
+ start program = "/etc/init.d/postgresql start"
26
+ stop program = "/etc/init.d/postgresql stop"
27
+ GROUP postgres
28
+ IF FAILED UNIXSOCKET #{node['monit']['postgres']['unix_socket_path']}.#{node['monit']['postgres']['port']} PROTOCOL pgsql THEN RESTART
29
+ IF FAILED HOST 127.0.0.1 PORT #{node['monit']['postgres']['port']} PROTOCOL pgsql THEN RESTART
30
+ IF 5 RESTARTS WITHIN 5 CYCLES THEN TIMEOUT
31
+ EOH
32
+ end
@@ -0,0 +1,31 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+ # Some code used under MIT license
17
+ # Copyright Alex Soto
18
+ #
19
+
20
+ include_recipe 'monit'
21
+
22
+ monit_config 'rabbitmq' do
23
+ content <<-EOH
24
+ CHECK PROCESS rabbitmq-server WITH PIDFILE /var/run/rabbitmq/pid
25
+ GROUP rabbitmq
26
+ START PROGRAM "/usr/sbin/service rabbitmq-server start"
27
+ STOP PROGRAM "/usr/sbin/service rabbitmq-server stop"
28
+ IF DOES NOT EXIST FOR 3 CYCLES THEN RESTART
29
+ IF FAILED PORT 5672 4 TIMES WITHIN 6 CYCLES THEN RESTART
30
+ EOH
31
+ end
@@ -0,0 +1,25 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+ # Some code used under MIT license
17
+ # Copyright Alex Soto
18
+ #
19
+
20
+ include_recipe 'monit'
21
+
22
+ monit_config 'resque' do
23
+ source 'resque.conf.erb'
24
+ variables node['monit']['resque']
25
+ end
@@ -0,0 +1,30 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+ # Some code used under MIT license
17
+ # Copyright Alex Soto
18
+ #
19
+
20
+ include_recipe 'monit'
21
+
22
+ monit_config 'resque_scheduler' do
23
+ content <<-EOH
24
+ check process resque_scheduler with pidfile #{node['monit']['resque']['app_root']}/tmp/pids/resque_scheduler.pid
25
+ group resque_scheduler
26
+ start program = "/usr/bin/env PATH=/usr/local/bin:/usr/local/ruby/bin:/usr/bin:/bin:$PATH RACK_ENV=#{node.chef_environment} /bin/sh -l -c 'cd #{node['monit']['resque']['app_root']} && nohup bundle exec rake environment resque:scheduler RAILS_ENV=#{node.chef_environment} QUEUE=* VERBOSE=1 INITIALIZER_PATH=#{node['monit']['resque']['app_root']}/config/initializers/resque.rb PIDFILE=#{node['monit']['resque']['app_root']}/tmp/pids/resque_scheduler.pid & >> log/resque_scheduler.log 2>&1'" with timeout 90 seconds
27
+ stop program = "/bin/sh -c 'cd #{node['monit']['resque']['app_root']} && kill -9 `cat tmp/pids/resque_scheduler.pid` && rm -f tmp/pids/resque_scheduler.pid; exit 0;'"
28
+ if totalmem is greater than 300 MB for 10 cycles then restart
29
+ EOH
30
+ end
@@ -0,0 +1,30 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+ # Some code used under MIT license
17
+ # Copyright Alex Soto
18
+ #
19
+
20
+ include_recipe 'monit'
21
+
22
+ monit_config 'ssh' do
23
+ content <<-EOH
24
+ CHECK PROCESS sshd WITH PIDFILE /var/run/sshd.pid
25
+ START PROGRAM "/usr/sbin/service ssh start"
26
+ STOP PROGRAM "/usr/sbin/service ssh stop"
27
+ # under load a check may fail intermittently, so give it a few tries before restarting
28
+ IF FAILED PORT #{node['monit']['ssh_port']} PROTOCOL ssh 4 TIMES WITHIN 6 CYCLES THEN RESTART
29
+ EOH
30
+ end
@@ -0,0 +1,25 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+ # Some code used under MIT license
17
+ # Copyright Alex Soto
18
+ #
19
+
20
+ include_recipe 'monit'
21
+
22
+ monit_config 'unicorn' do
23
+ source 'unicorn.conf.erb'
24
+ variables node['monit']
25
+ end
@@ -0,0 +1,43 @@
1
+ <%- unless @notify_email.to_s.empty? -%>
2
+ set mailserver <%= @mailserver['host'] %><%= " port #{@mailserver['port']}" if @mailserver['port'] %>
3
+ <%- if @mailserver['hostname'] -%>
4
+ using HOSTNAME "<%= @mailserver['hostname'] %>"
5
+ <%- end -%>
6
+ <%- if @mailserver['username'] -%>
7
+ username "<%= @mailserver['username'] %>"
8
+ <%- end -%>
9
+ <%- if @mailserver['password'] -%>
10
+ password "<%= @mailserver['password'] %>"<%= " #{@mailserver['password_suffix']}" if @mailserver['password_suffix'] %>
11
+ <%- end -%>
12
+ <%- if @mailserver['encryption'] -%>
13
+ using <%= @mailserver['encryption'] %>
14
+ <%- end -%>
15
+ <%- if @mailserver['timeout'] -%>
16
+ with timeout <%= @mailserver['timeout'] %> seconds
17
+ <%- end -%>
18
+
19
+ set mail-format {
20
+ from: <%= @mail_format['from'] %>
21
+ subject: <%= @mail_format['subject'] %>
22
+ message: <%= @mail_format['message'] %>
23
+ }
24
+
25
+ set alert <%= @notify_email %><%= " NOT ON { #{@alert_blacklist.join(", ")} }" unless @alert_blacklist.empty? %>
26
+ <%- end -%>
27
+
28
+ <%- if @port -%>
29
+ set httpd port <%= @port %>
30
+ <%- if @address -%>
31
+ use address <%= @address %>
32
+ <%- end -%>
33
+ <%- @allow.each do |a| -%>
34
+ allow <%= a %>
35
+ <%- end -%>
36
+ <%- if @username -%>
37
+ allow <%= @username %>:<%= @password %>
38
+ <%- end -%>
39
+ <%- if @ssl -%>
40
+ ssl enable
41
+ pemfile <%= @cert %>
42
+ <%- end -%>
43
+ <%- end -%>
@@ -0,0 +1,21 @@
1
+ <% counter = 0 %>
2
+
3
+ <% (@queues || []).each do |queue| %>
4
+ <% queue['worker_count'].times do %>
5
+ check process resque_worker_<%= counter %> with pidfile <%= @app_root %>/tmp/pids/resque_worker.<%= counter %>.pid
6
+
7
+ group resque_workers
8
+
9
+ # Note: the priority of queues is determined by their order in the following statement:
10
+
11
+ start program = "/usr/bin/env PATH=/usr/local/bin:/usr/local/ruby/bin:/usr/bin:/bin:$PATH /bin/sh -l -c 'cd <%= @app_root %> && nohup bundle exec rake environment resque'work' RAILS_ENV=<%= node.chef_environment %> QUEUE=<%=
12
+ queue['queue_list'].join(',')
13
+ %> VERBOSE=1 PIDFILE=<%= @app_root %>/tmp/pids/resque_worker.<%= counter %>.pid >> log/resque_worker.<%= counter %>.log 2>&1 &'" with timeout 90 seconds
14
+
15
+ stop program = "/bin/sh -c 'cd <%= @app_root %> && kill -TERM `cat tmp/pids/resque_worker.<%= counter %>.pid` && rm -f tmp/pids/resque_worker.<%= counter %>.pid; exit 0;'"
16
+
17
+ if totalmem is greater than 1500 MB for 10 cycles then restart
18
+
19
+ <% counter += 1 %>
20
+ <% end %>
21
+ <% end %>
@@ -0,0 +1,17 @@
1
+ check process unicorn with pidfile <%= @unicorn["pid_dir"] %>/unicorn.pid
2
+ start program "/etc/init.d/unicorn start"
3
+ stop program "/etc/init.d/unicorn stop"
4
+ if 5 restarts within 5 cycles then timeout
5
+
6
+ <%- @unicorn['worker_count'].times do |i| -%>
7
+ check process unicorn_worker_<%= i %> with pidfile <%= ::File.join(@unicorn['pid_dir'], "unicorn.worker.#{i}.pid") %>
8
+ <%- unless @notify_email.to_s.empty? -%>
9
+ alert <%= @notify_email %> only on { pid }
10
+ <%- end -%>
11
+ if changed pid 2 times within 60 cycles then alert
12
+ if memory usage > 16% for 1 cycles then
13
+ exec "/etc/init.d/unicorn kill_worker 0"
14
+ if cpu is greater than 50% for 2 cycles then
15
+ exec "/etc/init.d/unicorn kill_worker 0"
16
+
17
+ <%- end -%>
@@ -0,0 +1,21 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+
18
+ module PoiseMonitCompat
19
+ autoload :Resources, 'poise_monit_compat/resources'
20
+ autoload :VERSION, 'poise_monit_compat/version'
21
+ end