sensu-settings 5.2.0 → 6.0.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: d8924c88d3f2c09c838a8d63483dc5c5b43e4dfb
4
- data.tar.gz: 6602361eb730baa005e103d0f916dc73c387febc
3
+ metadata.gz: b4a1cb240875b92bbe08382f6b826cdd5842fbe8
4
+ data.tar.gz: a106106ad7f49e8431fef9ffcfa4ce0465b95a5a
5
5
  SHA512:
6
- metadata.gz: 51c1f51bd223da49a9cf4221ae0504758841572628359d491dba61e5a091a77fbe51957f2aca71a4deb4a00c612982c485075a110239e6ecf5d8ebcd41166bc7
7
- data.tar.gz: 343e7d524dc24d974007ee1bf190048084ab0005ed23ec1574bf801d31dc29f50083b62490f49991c80a0820c1ac54d2d68707278bbebd2ffefc17364032195e
6
+ metadata.gz: b835741087e47bd2a3aad317dfd9d00f162182348c5e545bc249fdc0d1a91775a05e3510cc3e13abc81a76d73ef3bad98619da29421545652f3632469722b316
7
+ data.tar.gz: 5546f23571be9b5d6414b730fc14913272f3493ec7e0aaa2cb4caf44f633504f5475a111579c6fec99a2b0adb01e4da4e8e6f713614f76d8fdced1191cca0f35
@@ -29,6 +29,23 @@ module Sensu
29
29
  self.class.create_category_methods
30
30
  end
31
31
 
32
+ # Auto-detected defaults for client definition
33
+ #
34
+ # Client name defaults to system hostname.
35
+ # Client address defaults to first detected non-loopback ipv4 address.
36
+ #
37
+ # Client subscriptions are intentionally omitted here as sensu-client
38
+ # will provide defaults using client name after final settings are
39
+ # loaded.
40
+ #
41
+ # @return [Hash] default client settings
42
+ def client_defaults
43
+ {
44
+ :name => system_hostname,
45
+ :address => system_address
46
+ }
47
+ end
48
+
32
49
  # Default settings.
33
50
  #
34
51
  # @return [Hash] settings.
@@ -47,6 +64,9 @@ module Sensu
47
64
  CATEGORIES.each do |category|
48
65
  default[category] = {}
49
66
  end
67
+ if ["client", "rspec"].include?(sensu_service_name)
68
+ default[:client] = client_defaults
69
+ end
50
70
  default
51
71
  end
52
72
 
@@ -139,8 +159,12 @@ module Sensu
139
159
  def load_directory(directory)
140
160
  warning("loading config files from directory", :directory => directory)
141
161
  path = directory.gsub(/\\(?=\S)/, "/")
142
- Dir.glob(File.join(path, "**{,/*/**}/*.json")).uniq.each do |file|
143
- load_file(file)
162
+ if File.readable?(path) && File.executable?(path)
163
+ Dir.glob(File.join(path, "**{,/*/**}/*.json")).uniq.each do |file|
164
+ load_file(file)
165
+ end
166
+ else
167
+ load_error("insufficient permissions for loading", :directory => directory)
144
168
  end
145
169
  end
146
170
 
@@ -249,20 +273,19 @@ module Sensu
249
273
  end
250
274
 
251
275
  # Load Sensu client settings from the environment. This method
252
- # loads client settings from several variables if
253
- # `SENSU_CLIENT_NAME` is set: `SENSU_CLIENT_NAME`,
254
- # `SENSU_CLIENT_ADDRESS`, and `SENSU_CLIENT_SUBSCRIPTIONS`. The
255
- # Sensu client address defaults to the current system hostname
256
- # and subscriptions defaults to an empty array.
276
+ # loads client settings from several variables:
277
+ # `SENSU_CLIENT_NAME`, `SENSU_CLIENT_ADDRESS`, and
278
+ # `SENSU_CLIENT_SUBSCRIPTIONS`.
279
+ #
280
+ # The client subscriptions defaults to an empty array.
257
281
  def load_client_env
258
- if ENV["SENSU_CLIENT_NAME"]
259
- @settings[:client] ||= {}
260
- @settings[:client][:name] = ENV["SENSU_CLIENT_NAME"]
261
- @settings[:client][:address] = ENV.fetch("SENSU_CLIENT_ADDRESS", system_hostname)
262
- @settings[:client][:subscriptions] = ENV.fetch("SENSU_CLIENT_SUBSCRIPTIONS", "").split(",")
282
+ @settings[:client][:name] = ENV["SENSU_CLIENT_NAME"] if ENV["SENSU_CLIENT_NAME"]
283
+ @settings[:client][:address] = ENV["SENSU_CLIENT_ADDRESS"] if ENV["SENSU_CLIENT_ADDRESS"]
284
+ @settings[:client][:subscriptions] = ENV.fetch("SENSU_CLIENT_SUBSCRIPTIONS", "").split(",")
285
+ if ENV.keys.any? {|k| k =~ /^SENSU_CLIENT/}
263
286
  warning("using sensu client environment variables", :client => @settings[:client])
264
- @indifferent_access = false
265
287
  end
288
+ @indifferent_access = false
266
289
  end
267
290
 
268
291
  # Load Sensu API settings from the environment. This method sets
@@ -372,6 +395,17 @@ module Sensu
372
395
  Socket.gethostname rescue "unknown"
373
396
  end
374
397
 
398
+ # Retrieve the system IP address. If a valid non-loopback
399
+ # IPv4 address cannot be found and an error is thrown,
400
+ # "unknown" will be returned.
401
+ #
402
+ # @return [String] system ip address
403
+ def system_address
404
+ Socket.ip_address_list.find { |address|
405
+ address.ipv4? && !address.ipv4_loopback?
406
+ }.ip_address rescue "unknown"
407
+ end
408
+
375
409
  # Record a warning.
376
410
  #
377
411
  # @param message [String] warning message.
@@ -113,10 +113,12 @@ module Sensu
113
113
  # Check that value items are all strings and not empty.
114
114
  #
115
115
  # @param value [Array] with items to check.
116
+ # @param regex [Regexp] to validate string items with.
116
117
  # @return [TrueClass, FalseClass]
117
- def items_must_be_strings(value)
118
+ def items_must_be_strings(value, regex=nil)
118
119
  value.all? do |item|
119
- item.is_a?(String) && !item.empty?
120
+ item.is_a?(String) && !item.empty? &&
121
+ (regex.nil? || item =~ regex)
120
122
  end
121
123
  end
122
124
 
@@ -97,12 +97,22 @@ module Sensu
97
97
  #
98
98
  # @param check [Hash] sensu check definition.
99
99
  def validate_check_aggregate(check)
100
- if is_a_string?(check[:aggregate])
101
- must_match_regex(/\A[\w\.-]+\z/, check[:aggregate]) ||
102
- invalid(check, "check aggregate cannot contain spaces or special characters")
103
- else
104
- must_be_boolean(check[:aggregate]) ||
105
- invalid(check, "check aggregate must be a string (name) or boolean")
100
+ if check[:aggregates]
101
+ if is_an_array?(check[:aggregates])
102
+ items_must_be_strings(check[:aggregates], /\A[\w\.:|-]+\z/) ||
103
+ invalid(check, "check aggregates items must be strings without spaces or special characters")
104
+ else
105
+ invalid(check, "check aggregates must be an array")
106
+ end
107
+ end
108
+ if check[:aggregate]
109
+ if is_a_string?(check[:aggregate])
110
+ must_match_regex(/\A[\w\.:|-]+\z/, check[:aggregate]) ||
111
+ invalid(check, "check aggregate cannot contain spaces or special characters")
112
+ else
113
+ must_be_boolean(check[:aggregate]) ||
114
+ invalid(check, "check aggregate must be a string (name) or boolean")
115
+ end
106
116
  end
107
117
  end
108
118
 
@@ -129,7 +139,7 @@ module Sensu
129
139
  validate_check_scheduling(check)
130
140
  validate_check_handling(check)
131
141
  validate_check_ttl(check) if check[:ttl]
132
- validate_check_aggregate(check) if check[:aggregate]
142
+ validate_check_aggregate(check)
133
143
  validate_check_flap_detection(check)
134
144
  validate_subdue(check) if check[:subdue]
135
145
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "sensu-settings"
5
- spec.version = "5.2.0"
5
+ spec.version = "6.0.0"
6
6
  spec.authors = ["Sean Porter"]
7
7
  spec.email = ["portertech@gmail.com"]
8
8
  spec.summary = "The Sensu settings library, loader and validator"
data/spec/loader_spec.rb CHANGED
@@ -25,6 +25,13 @@ describe "Sensu::Settings::Loader" do
25
25
  expect(failures.size).to eq(0)
26
26
  end
27
27
 
28
+ it "can load Sensu client settings with auto-detected defaults" do
29
+ @loader.load_env
30
+ expect(@loader.warnings.size).to eq(0)
31
+ expect(@loader.default_settings[:client][:name]).to be_kind_of(String)
32
+ expect(@loader.default_settings[:client][:address]).to be_kind_of(String)
33
+ end
34
+
28
35
  it "can load Sensu transport settings from the environment" do
29
36
  ENV["SENSU_TRANSPORT_NAME"] = "redis"
30
37
  @loader.load_env
@@ -77,6 +84,8 @@ describe "Sensu::Settings::Loader" do
77
84
  expect(client[:address]).to eq("127.0.0.1")
78
85
  expect(client[:subscriptions]).to eq(["foo", "bar", "baz"])
79
86
  ENV["SENSU_CLIENT_NAME"] = nil
87
+ ENV["SENSU_CLIENT_ADDRESS"] = nil
88
+ ENV["SENSU_CLIENT_SUBSCRIPTIONS"] = nil
80
89
  end
81
90
 
82
91
  it "can load Sensu API settings from the environment" do
@@ -153,10 +162,13 @@ describe "Sensu::Settings::Loader" do
153
162
  end
154
163
 
155
164
  it "can attempt to load settings from files in a nonexistent directory" do
156
- @loader.load_directory("/tmp/rottentomatos")
165
+ expect {
166
+ @loader.load_directory("/tmp/rottentomatos")
167
+ }.to raise_error(Sensu::Settings::Loader::Error)
157
168
  expect(@loader.warnings.size).to eq(1)
158
- warning = @loader.warnings.first
159
- expect(warning[:message]).to eq("loading config files from directory")
169
+ expect(@loader.warnings.first[:message]).to eq("loading config files from directory")
170
+ expect(@loader.errors.size).to eq(1)
171
+ expect(@loader.errors.first[:message]).to eq("insufficient permissions for loading")
160
172
  end
161
173
 
162
174
  it "can set environment variables for child processes" do
@@ -188,6 +188,27 @@ describe "Sensu::Settings::Validator" do
188
188
  check[:ttl] = 1
189
189
  @validator.validate_check(check)
190
190
  expect(@validator.reset).to eq(0)
191
+ check[:aggregates] = "string"
192
+ @validator.validate_check(check)
193
+ expect(@validator.reset).to eq(1)
194
+ check[:aggregates] = true
195
+ @validator.validate_check(check)
196
+ expect(@validator.reset).to eq(1)
197
+ check[:aggregates] = [1]
198
+ @validator.validate_check(check)
199
+ expect(@validator.reset).to eq(1)
200
+ check[:aggregates] = [true]
201
+ @validator.validate_check(check)
202
+ expect(@validator.reset).to eq(1)
203
+ check[:aggregates] = ["foo", "$bar$"]
204
+ @validator.validate_check(check)
205
+ expect(@validator.reset).to eq(1)
206
+ check[:aggregates] = ["foo", "bar"]
207
+ @validator.validate_check(check)
208
+ expect(@validator.reset).to eq(0)
209
+ check[:aggregates] = [":::some_custom_attr:::-checks", ":::another_custom_attr|default_value:::-checks"]
210
+ @validator.validate_check(check)
211
+ expect(@validator.reset).to eq(0)
191
212
  check[:aggregate] = 1
192
213
  @validator.validate_check(check)
193
214
  expect(@validator.reset).to eq(1)
@@ -200,6 +221,9 @@ describe "Sensu::Settings::Validator" do
200
221
  check[:aggregate] = "test"
201
222
  @validator.validate_check(check)
202
223
  expect(@validator.reset).to eq(0)
224
+ check[:aggregate] = ":::some_custom_attribute|magic:::"
225
+ @validator.validate_check(check)
226
+ expect(@validator.reset).to eq(0)
203
227
  check[:low_flap_threshold] = "25"
204
228
  @validator.validate_check(check)
205
229
  expect(@validator.reset).to eq(2)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sensu-settings
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.2.0
4
+ version: 6.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Porter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-03 00:00:00.000000000 Z
11
+ date: 2016-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sensu-json