poise-monit 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3b8431aa0868c66f89d7de71426b2f9f1cfb3dbd
4
- data.tar.gz: ae851e48383783f8ee8c18731aea27310a9c2077
3
+ metadata.gz: f47b9d1c32a3af42021e1a8b7cdb98c39e00f4e8
4
+ data.tar.gz: 9ff65baa3d1da13f8987c783877aba69ac8cdcd6
5
5
  SHA512:
6
- metadata.gz: a057f285f6ab413378e3a6cc863ff11a98698ce8470f5e2b249d0cb2a0576aa28574c14bdb60cacc5d6d83d1c71d00368e12b168d675ca735a97a5454c4b3183
7
- data.tar.gz: 1a8a1a3895ac84e20bb6d747af4af3724362ac4737231fcf69af42f1b51e24e3613e28c07995fff485afcfe229253a4d9a526234fa005d93220e23c87d91ec16
6
+ metadata.gz: d7e83a4af5eedb1c12a6fb3fcd95abda4513dc74fa1210736a17afed1131433a42afa7c50888d31406d0f60c1d1d84c9aa68fb4f16a98c44360e828ab992230a
7
+ data.tar.gz: 425505c36a7d6e2931db851fb6c7d11c638c397cc3d5e920f13e6d0cc12b0cc771ceb3e8d43f78cb5af60369b906d1e688e0c1f58f514beacffec86dd159ca52
@@ -1,5 +1,9 @@
1
1
  # Poise-Monit Changelog
2
2
 
3
+ ## v1.2.0
4
+
5
+ * New resource `monit_check` for more easily creating service checks.
6
+
3
7
  ## v1.1.0
4
8
 
5
9
  * Improve compatibility with Chef 12.4 and earlier.
data/README.md CHANGED
@@ -146,6 +146,40 @@ end
146
146
 
147
147
  One of `source` or `content` is required.
148
148
 
149
+ ### `monit_check`
150
+
151
+ The `monit_check` resource writes out a Monit configuration file for a service
152
+ check. It is a subclass of `monit_config` and so inherits its actions and
153
+ properties. It defaults to being a process check.
154
+
155
+ ```ruby
156
+ monit_check 'httpd' do
157
+ check 'if failed port 80 protocol http request "/_status" then restart'
158
+ extra [
159
+ 'every 5 cycles',
160
+ 'group www',
161
+ ]
162
+ end
163
+ ```
164
+
165
+ #### Actions
166
+
167
+ * `:create` – Create and manage the configuration file. *(default)*
168
+ * `:delete` – Delete the configuration file.
169
+
170
+ #### Properties
171
+
172
+ * `check_type` – Type of check. *(default: process)*
173
+ * `with` – WITH-ish string for this check. This is the part that goes after the
174
+ check name. Set to false to disable. *(default: PIDFILE /var/run/check_name.pid)*
175
+ * `start_program` – Command to use to start the service for process checks. Set
176
+ to false disable. *(default: automatic)*
177
+ * `stop_program` – Command to use to stop the service for process checks. Set
178
+ to false disable. *(default: automatic)*
179
+ * `check` – Service health check or checks. `'IF '` will be prepended if not
180
+ given.
181
+ * `extra` – Line or lines to be added to the service definition as is.
182
+
149
183
  ## Monit Providers
150
184
 
151
185
  ### `binaries`
@@ -223,6 +257,23 @@ end
223
257
 
224
258
  To set the `monit` provider as the global default, use [`poise-sevice-monit`](https://github.com/poise/poise-service-monit).
225
259
 
260
+ ## Upgrading From `monit`
261
+
262
+ Upgrading from the older [`monit` cookbook](https://github.com/poise/poise-monit-compat)
263
+ is relatively straightforward. The `node['monit']` attributes can either be
264
+ converted to `node['poise-monit']['recipe']` if you want to use the default
265
+ recipe, or you can invoke the `monit` resource in your own recipe code if needed.
266
+
267
+ When switching cookbooks in-place on a server, make sure you check for any
268
+ `conf.d/` config files created by the old cookbook. Notably `conf.d/compat.conf`
269
+ may interfere with the configuration generation. You can remove it:
270
+
271
+ ```ruby
272
+ monit_config 'compat' do
273
+ action :delete
274
+ end
275
+ ```
276
+
226
277
  ## Sponsors
227
278
 
228
279
  Development sponsored by [Bloomberg](http://www.bloomberg.com/company/technology/).
@@ -0,0 +1,13 @@
1
+ CHECK <%= @new_resource.check_type.to_s.upcase %> <%= @new_resource.check_name %><% if @new_resource.with %> <%= @new_resource.with %><% end %>
2
+ <%- if @new_resource.start_program -%>
3
+ start program = "<%= @new_resource.start_program %>"
4
+ <%- end -%>
5
+ <%- if @new_resource.stop_program -%>
6
+ stop program = "<%= @new_resource.stop_program %>"
7
+ <%- end -%>
8
+ <%- Array(@new_resource.check).each do |check_line| -%>
9
+ <%= check_line !~ /^if/i ? 'IF ' : '' %><%= check_line.strip %>
10
+ <%- end -%>
11
+ <%- Array(@new_resource.extra).each do |extra_line| -%>
12
+ <%= extra_line.strip %>
13
+ <%- end -%>
@@ -1,4 +1,4 @@
1
- check process <%= @service_resource.service_name %> with pidfile <%= @pid_file %>
1
+ check process <%= @service_resource.service_name %> pidfile <%= @pid_file %>
2
2
  start program = "<%= @script_path %> start"
3
3
  stop program = "<%= @script_path %> stop"
4
4
  <%- Array(@options['checks']).each do |line| -%>
@@ -15,6 +15,7 @@
15
15
  #
16
16
 
17
17
  require 'poise_monit/resources/monit'
18
+ require 'poise_monit/resources/monit_check'
18
19
  require 'poise_monit/resources/monit_config'
19
20
  require 'poise_monit/resources/monit_service'
20
21
 
@@ -0,0 +1,158 @@
1
+ #
2
+ # Copyright 2016, 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_languages/utils'
18
+ require 'poise_monit/resources/monit_config'
19
+
20
+
21
+ module PoiseMonit
22
+ module Resources
23
+ # (see MonitCheck::Resource)
24
+ # @since 1.2.0
25
+ module MonitCheck
26
+ # A `monit_check` resource to write out a Monit configuration file with a
27
+ # service check.
28
+ #
29
+ # @provides monit_check
30
+ # @action create
31
+ # @action delete
32
+ # @example
33
+ # monit_check 'httpd' do
34
+ # check 'if failed port 80 protocol http request "/_status" then restart'
35
+ # end
36
+ class Resource < MonitConfig::Resource
37
+ provides(:monit_check)
38
+
39
+ attribute('', template: true, default_source: 'monit_check.conf.erb')
40
+ # @!attribute check_type
41
+ # Type of check. Not making this an explicit array to allow for new
42
+ # check types in Monit without a cookbook release.
43
+ # @return [String]
44
+ attribute(:check_type, kind_of: String, default: 'process')
45
+ # @!attribute with
46
+ # WITH-ish string for this check. This is the part that goes after the
47
+ # check name. Set to false to disable. Defaults to an automatic PID
48
+ # file for process checks and disabled for others.
49
+ # @return [String, nil, false]
50
+ # @example Process check
51
+ # monit_check 'httpd' do
52
+ # with 'PIDFILE /var/run/apache2.pid'
53
+ # end
54
+ # @example File check
55
+ # monit_check 'httpd_log' do
56
+ # check_type 'file'
57
+ # with 'PATH /var/log/apache2/error.log'
58
+ # end
59
+ attribute(:with, kind_of: [String, NilClass, FalseClass], default: lazy { default_with })
60
+ # @!attribute start_program
61
+ # Command to use to start the service for process checks. Set to false
62
+ # to disable. Defaults to an auto-detect using `systemctl`, `service`
63
+ # or `/etc/init.d/$name`.
64
+ # @return [String, nil, false]
65
+ attribute(:start_program, kind_of: [String, NilClass, FalseClass], default: lazy { default_start_program })
66
+ # @!attribute stop_program
67
+ # Command to use to stop the service for process checks. Set to false
68
+ # to disable. Defaults to an auto-detect using `systemctl`, `service`
69
+ # or `/etc/init.d/$name`.
70
+ # @return [String, nil, false]
71
+ attribute(:stop_program, kind_of: [String, NilClass, FalseClass], default: lazy { default_stop_program })
72
+ # @!attribute check
73
+ # Service health check or checks. `'IF '` will be prepended if not
74
+ # given.
75
+ # @return [String, Array<String>]
76
+ attribute(:check, kind_of: [String, Array], default: [])
77
+ # @!attribute extra
78
+ # Line or lines to be added to the service definition as is.
79
+ # @return [String, Array<String>]
80
+ attribute(:extra, kind_of: [String, Array], default: [])
81
+
82
+ # An alias for #check_name to make things more semantically meaningful.
83
+ alias_method :check_name, :config_name
84
+
85
+ # An alias for #if_ to allow writing things like look more like Monit
86
+ # configuration files. This can't be `if` because that's a keyword.
87
+ #
88
+ # @example
89
+ # monit_check 'httpd' do
90
+ # if_ 'failed port 80 protocol http request "/_status" then restart'
91
+ # end
92
+ alias_method :if_, :check
93
+
94
+ private
95
+
96
+ # Default WITH-ish value.
97
+ #
98
+ # @return [String]
99
+ def default_with
100
+ _if_process("PIDFILE /var/run/#{check_name}.pid")
101
+ end
102
+
103
+ # Default start program value.
104
+ #
105
+ # @return [String]
106
+ def default_start_program
107
+ _init_command('start')
108
+ end
109
+
110
+ # Default stop program value.
111
+ #
112
+ # @return [String]
113
+ def default_stop_program
114
+ _init_command('stop')
115
+ end
116
+
117
+ # Helper for default values that only apply to process checks.
118
+ #
119
+ # @param value [Object] Value to return for process checks.
120
+ # @return [Object, nil]
121
+ def _if_process(value)
122
+ if check_type.to_s.downcase == 'process'
123
+ value
124
+ else
125
+ nil
126
+ end
127
+ end
128
+
129
+ # Find the right command to control the init system. This checks
130
+ # systemctl, service, and then gives up and uses /etc/init.d.
131
+ #
132
+ # @param action [String] Init action to run.
133
+ # @return [String, nil]
134
+ def _init_command(action)
135
+ cmd = if systemctl = PoiseLanguages::Utils.which('systemctl')
136
+ "#{systemctl} #{action} #{check_name}"
137
+ elsif service = PoiseLanguages::Utils.which('service')
138
+ "#{service} #{check_name} #{action}"
139
+ else
140
+ # ¯\_(ツ)_/¯
141
+ "/etc/init.d/#{check_name} #{action}"
142
+ end
143
+ _if_process(cmd)
144
+ end
145
+ end
146
+
147
+ # The provider for `monit_check`.
148
+ #
149
+ # @see Resource
150
+ # @provides monit_check
151
+ class Provider < MonitConfig::Provider
152
+ provides(:monit_check)
153
+
154
+ # This space left intentionally blank. All behaviors are in the base.
155
+ end
156
+ end
157
+ end
158
+ end
@@ -16,5 +16,5 @@
16
16
 
17
17
 
18
18
  module PoiseMonit
19
- VERSION = '1.1.0'
19
+ VERSION = '1.2.0'
20
20
  end
@@ -0,0 +1,189 @@
1
+ #
2
+ # Copyright 2015-2016, 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 'spec_helper'
18
+
19
+ describe PoiseMonit::Resources::MonitCheck do
20
+ step_into(:monit_check)
21
+ let(:systemctl) { nil }
22
+ let(:service) { nil }
23
+ before do
24
+ allow(PoiseLanguages::Utils).to receive(:which).with('systemctl').and_return(systemctl)
25
+ allow(PoiseLanguages::Utils).to receive(:which).with('service').and_return(service)
26
+ end
27
+
28
+ context 'action :create' do
29
+ recipe do
30
+ monit 'monit'
31
+ monit_check 'httpd'
32
+ end
33
+
34
+ it { is_expected.to render_file('/etc/monit/conf.d/httpd.conf').with_content(<<-EOH) }
35
+ CHECK PROCESS httpd PIDFILE /var/run/httpd.pid
36
+ start program = "/etc/init.d/httpd start"
37
+ stop program = "/etc/init.d/httpd stop"
38
+ EOH
39
+
40
+ context 'with systemd' do
41
+ let(:systemctl) { '/sbin/systemctl' }
42
+
43
+ it { is_expected.to render_file('/etc/monit/conf.d/httpd.conf').with_content(<<-EOH) }
44
+ CHECK PROCESS httpd PIDFILE /var/run/httpd.pid
45
+ start program = "/sbin/systemctl start httpd"
46
+ stop program = "/sbin/systemctl stop httpd"
47
+ EOH
48
+ end # /context with systemd
49
+
50
+ context 'with service' do
51
+ let(:service) { '/sbin/service' }
52
+
53
+ it { is_expected.to render_file('/etc/monit/conf.d/httpd.conf').with_content(<<-EOH) }
54
+ CHECK PROCESS httpd PIDFILE /var/run/httpd.pid
55
+ start program = "/sbin/service httpd start"
56
+ stop program = "/sbin/service httpd stop"
57
+ EOH
58
+ end # /context with service
59
+
60
+ context 'with check string' do
61
+ recipe do
62
+ monit 'monit'
63
+ monit_check 'httpd' do
64
+ check 'if failed port 80 protocol http request "/_status" then restart'
65
+ end
66
+ end
67
+
68
+ it { is_expected.to render_file('/etc/monit/conf.d/httpd.conf').with_content(<<-EOH) }
69
+ CHECK PROCESS httpd PIDFILE /var/run/httpd.pid
70
+ start program = "/etc/init.d/httpd start"
71
+ stop program = "/etc/init.d/httpd stop"
72
+ if failed port 80 protocol http request "/_status" then restart
73
+ EOH
74
+ end # /context with check string
75
+
76
+ context 'with check array' do
77
+ recipe do
78
+ monit 'monit'
79
+ monit_check 'httpd' do
80
+ check [
81
+ 'if failed port 80 protocol http request "/_status" then restart',
82
+ 'if failed port 443 protocol https and certificate valid > 30 days then alert',
83
+ ]
84
+ end
85
+ end
86
+
87
+ it { is_expected.to render_file('/etc/monit/conf.d/httpd.conf').with_content(<<-EOH) }
88
+ CHECK PROCESS httpd PIDFILE /var/run/httpd.pid
89
+ start program = "/etc/init.d/httpd start"
90
+ stop program = "/etc/init.d/httpd stop"
91
+ if failed port 80 protocol http request "/_status" then restart
92
+ if failed port 443 protocol https and certificate valid > 30 days then alert
93
+ EOH
94
+ end # /context with check array
95
+
96
+ context 'with if_ string' do
97
+ recipe do
98
+ monit 'monit'
99
+ monit_check 'httpd' do
100
+ if_ 'failed port 80 protocol http request "/_status" then restart'
101
+ end
102
+ end
103
+
104
+ it { is_expected.to render_file('/etc/monit/conf.d/httpd.conf').with_content(<<-EOH) }
105
+ CHECK PROCESS httpd PIDFILE /var/run/httpd.pid
106
+ start program = "/etc/init.d/httpd start"
107
+ stop program = "/etc/init.d/httpd stop"
108
+ IF failed port 80 protocol http request "/_status" then restart
109
+ EOH
110
+ end # /context with if_ string
111
+
112
+ context 'with extra string' do
113
+ recipe do
114
+ monit 'monit'
115
+ monit_check 'httpd' do
116
+ extra 'MODE ACTIVE'
117
+ end
118
+ end
119
+
120
+ it { is_expected.to render_file('/etc/monit/conf.d/httpd.conf').with_content(<<-EOH) }
121
+ CHECK PROCESS httpd PIDFILE /var/run/httpd.pid
122
+ start program = "/etc/init.d/httpd start"
123
+ stop program = "/etc/init.d/httpd stop"
124
+ MODE ACTIVE
125
+ EOH
126
+ end # /context with extra string
127
+
128
+ context 'with extra array' do
129
+ recipe do
130
+ monit 'monit'
131
+ monit_check 'httpd' do
132
+ extra [
133
+ 'MODE ACTIVE',
134
+ 'GROUP www',
135
+ ]
136
+ end
137
+ end
138
+
139
+ it { is_expected.to render_file('/etc/monit/conf.d/httpd.conf').with_content(<<-EOH) }
140
+ CHECK PROCESS httpd PIDFILE /var/run/httpd.pid
141
+ start program = "/etc/init.d/httpd start"
142
+ stop program = "/etc/init.d/httpd stop"
143
+ MODE ACTIVE
144
+ GROUP www
145
+ EOH
146
+ end # /context with extra array
147
+
148
+ context 'with start_program' do
149
+ recipe do
150
+ monit 'monit'
151
+ monit_check 'httpd' do
152
+ start_program 'apachectl start'
153
+ end
154
+ end
155
+
156
+ it { is_expected.to render_file('/etc/monit/conf.d/httpd.conf').with_content(<<-EOH) }
157
+ CHECK PROCESS httpd PIDFILE /var/run/httpd.pid
158
+ start program = "apachectl start"
159
+ stop program = "/etc/init.d/httpd stop"
160
+ EOH
161
+ end # /context with start_program
162
+
163
+ context 'with stop_program' do
164
+ recipe do
165
+ monit 'monit'
166
+ monit_check 'httpd' do
167
+ stop_program 'apachectl stop'
168
+ end
169
+ end
170
+
171
+ it { is_expected.to render_file('/etc/monit/conf.d/httpd.conf').with_content(<<-EOH) }
172
+ CHECK PROCESS httpd PIDFILE /var/run/httpd.pid
173
+ start program = "/etc/init.d/httpd start"
174
+ stop program = "apachectl stop"
175
+ EOH
176
+ end # /context with stop_program
177
+ end # /context action :create
178
+
179
+ context 'action :delete' do
180
+ recipe do
181
+ monit 'monit'
182
+ monit_check 'httpd' do
183
+ action :delete
184
+ end
185
+ end
186
+
187
+ it { is_expected.to delete_file('/etc/monit/conf.d/httpd.conf') }
188
+ end # /context action :delete
189
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: poise-monit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Noah Kantrowitz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-11 00:00:00.000000000 Z
11
+ date: 2016-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: halite
@@ -114,6 +114,7 @@ files:
114
114
  - chef/attributes/default.rb
115
115
  - chef/recipes/default.rb
116
116
  - chef/templates/monit.conf.erb
117
+ - chef/templates/monit_check.conf.erb
117
118
  - chef/templates/monit_service.conf.erb
118
119
  - lib/poise_monit.rb
119
120
  - lib/poise_monit/cheftie.rb
@@ -125,6 +126,7 @@ files:
125
126
  - lib/poise_monit/monit_providers/system.rb
126
127
  - lib/poise_monit/resources.rb
127
128
  - lib/poise_monit/resources/monit.rb
129
+ - lib/poise_monit/resources/monit_check.rb
128
130
  - lib/poise_monit/resources/monit_config.rb
129
131
  - lib/poise_monit/resources/monit_service.rb
130
132
  - lib/poise_monit/resources/monit_test.rb
@@ -151,6 +153,7 @@ files:
151
153
  - test/spec/monit_providers/dummy_spec.rb
152
154
  - test/spec/monit_providers/system_spec.rb
153
155
  - test/spec/recipe_spec.rb
156
+ - test/spec/resources/monit_check_spec.rb
154
157
  - test/spec/resources/monit_config_spec.rb
155
158
  - test/spec/resources/monit_service_spec.rb
156
159
  - test/spec/resources/monit_spec.rb
@@ -176,7 +179,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
176
179
  version: '0'
177
180
  requirements: []
178
181
  rubyforge_project:
179
- rubygems_version: 2.4.8
182
+ rubygems_version: 2.5.2
180
183
  signing_key:
181
184
  specification_version: 4
182
185
  summary: A Chef cookbook for managing the Monit process manager.
@@ -200,6 +203,7 @@ test_files:
200
203
  - test/spec/monit_providers/dummy_spec.rb
201
204
  - test/spec/monit_providers/system_spec.rb
202
205
  - test/spec/recipe_spec.rb
206
+ - test/spec/resources/monit_check_spec.rb
203
207
  - test/spec/resources/monit_config_spec.rb
204
208
  - test/spec/resources/monit_service_spec.rb
205
209
  - test/spec/resources/monit_spec.rb