sensu-extensions 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sensu/extensions/filters/attributes.rb +71 -0
- data/sensu-extensions.gemspec +7 -2
- data/spec/assets/config.json +11 -0
- data/spec/extensions_spec.rb +1 -0
- data/spec/filters/attributes_spec.rb +53 -0
- data/spec/helpers.rb +51 -0
- data/spec/loader_spec.rb +2 -0
- metadata +50 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e4bc92d117b837c89057ba3c0dec38b2bd76db80
|
4
|
+
data.tar.gz: f3fc9a6727262dca6e5d84991e767d0e5c91fdcb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9cfeb24e06c88fb9966a8f882a70a5554bc92b33e9fd42986585a140890a9ac567cb36674fde37e147f6ba8019552edf09753bc9e59bc735dad71fd66b6e19a5
|
7
|
+
data.tar.gz: fb778e2d246b5764e37d25142b85a23ead28bd2001f5b40461b2e426d645e85d41248264bd5fdf1d3c23cdf589f7491f85b7927a17188a6163710a0c5ae1327e
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require "sensu/extension"
|
2
|
+
|
3
|
+
module Sensu
|
4
|
+
module Sandbox
|
5
|
+
def self.eval(eval_string, value=nil)
|
6
|
+
result = Proc.new do
|
7
|
+
$SAFE = (RUBY_VERSION < "2.1.0" ? 4 : 3)
|
8
|
+
Kernel.eval(eval_string)
|
9
|
+
end
|
10
|
+
result.call
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module Extension
|
15
|
+
class Attributes < Filter
|
16
|
+
def name
|
17
|
+
"attributes"
|
18
|
+
end
|
19
|
+
|
20
|
+
def description
|
21
|
+
"filters events using event attributes"
|
22
|
+
end
|
23
|
+
|
24
|
+
def run(event, options)
|
25
|
+
status = filter_event?(options[:filter_name], event) ? 0 : 1
|
26
|
+
yield "", status
|
27
|
+
end
|
28
|
+
|
29
|
+
def eval_attribute_value(raw_eval_string, value)
|
30
|
+
begin
|
31
|
+
eval_string = raw_eval_string.gsub(/^eval:(\s+)?/, "")
|
32
|
+
!!Sandbox.eval(eval_string, value)
|
33
|
+
rescue => error
|
34
|
+
@logger.error("filter attribute eval error", {
|
35
|
+
:raw_eval_string => raw_eval_string,
|
36
|
+
:value => value,
|
37
|
+
:error => error.to_s
|
38
|
+
})
|
39
|
+
false
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def attributes_match?(hash_one, hash_two)
|
44
|
+
hash_one.all? do |key, value_one|
|
45
|
+
value_two = hash_two[key]
|
46
|
+
case
|
47
|
+
when value_one == value_two
|
48
|
+
true
|
49
|
+
when value_one.is_a?(Hash) && value_two.is_a?(Hash)
|
50
|
+
attributes_match?(value_one, value_two)
|
51
|
+
when value_one.is_a?(String) && value_one.start_with?("eval:")
|
52
|
+
eval_attribute_value(value_one, value_two)
|
53
|
+
else
|
54
|
+
false
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def filter_event?(filter_name, event)
|
60
|
+
if @settings.filter_exists?(filter_name)
|
61
|
+
filter = @settings[:filters][filter_name]
|
62
|
+
matched = attributes_match?(filter[:attributes], event)
|
63
|
+
filter[:negate] ? matched : !matched
|
64
|
+
else
|
65
|
+
@logger.error("unknown filter", :filter_name => filter_name)
|
66
|
+
false
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/sensu-extensions.gemspec
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |spec|
|
4
4
|
spec.name = "sensu-extensions"
|
5
|
-
spec.version = "1.
|
5
|
+
spec.version = "1.1.0"
|
6
6
|
spec.authors = ["Sean Porter"]
|
7
7
|
spec.email = ["portertech@gmail.com"]
|
8
8
|
spec.summary = "The Sensu extension loader library"
|
@@ -16,11 +16,16 @@ Gem::Specification.new do |spec|
|
|
16
16
|
spec.require_paths = ["lib"]
|
17
17
|
|
18
18
|
spec.add_dependency "sensu-em"
|
19
|
+
spec.add_dependency "sensu-logger"
|
20
|
+
spec.add_dependency "sensu-settings"
|
19
21
|
spec.add_dependency "sensu-extension"
|
22
|
+
|
20
23
|
spec.add_dependency "multi_json"
|
21
24
|
|
22
25
|
spec.add_development_dependency "bundler", "~> 1.6"
|
26
|
+
spec.add_development_dependency "uuidtools"
|
23
27
|
spec.add_development_dependency "rake"
|
24
28
|
spec.add_development_dependency "rspec"
|
25
|
-
spec.add_development_dependency "
|
29
|
+
spec.add_development_dependency "bouncy-castle-java" if RUBY_PLATFORM =~ /java/
|
30
|
+
spec.add_development_dependency "codeclimate-test-reporter" unless RUBY_VERSION < "1.9"
|
26
31
|
end
|
data/spec/extensions_spec.rb
CHANGED
@@ -54,6 +54,7 @@ describe "Sensu::Extensions" do
|
|
54
54
|
|
55
55
|
it "can load the built-in extensions" do
|
56
56
|
extensions = Sensu::Extensions.load
|
57
|
+
expect(extensions.filter_exists?("attributes")).to be(true)
|
57
58
|
expect(extensions.mutator_exists?("json")).to be(true)
|
58
59
|
expect(extensions.mutator_exists?("ruby_hash")).to be(true)
|
59
60
|
expect(extensions.mutator_exists?("only_check_output")).to be(true)
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "helpers")
|
2
|
+
require "sensu/extensions/filters/attributes"
|
3
|
+
|
4
|
+
describe "Sensu::Extension::Attributes" do
|
5
|
+
include Helpers
|
6
|
+
|
7
|
+
before do
|
8
|
+
@extension = Sensu::Extension::Attributes.new
|
9
|
+
@extension.logger = logger
|
10
|
+
@extension.settings = settings
|
11
|
+
end
|
12
|
+
|
13
|
+
it "can determine if filter attributes match an event" do
|
14
|
+
attributes = {
|
15
|
+
:occurrences => 1
|
16
|
+
}
|
17
|
+
event = event_template
|
18
|
+
expect(@extension.attributes_match?(attributes, event)).to be(true)
|
19
|
+
event[:occurrences] = 2
|
20
|
+
expect(@extension.attributes_match?(attributes, event)).to be(false)
|
21
|
+
attributes[:occurrences] = "eval: value == 1 || value % 60 == 0"
|
22
|
+
event[:occurrences] = 1
|
23
|
+
expect(@extension.attributes_match?(attributes, event)).to be(true)
|
24
|
+
event[:occurrences] = 2
|
25
|
+
expect(@extension.attributes_match?(attributes, event)).to be(false)
|
26
|
+
event[:occurrences] = 120
|
27
|
+
expect(@extension.attributes_match?(attributes, event)).to be(true)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "can run, filtering an event with a nonexistent filter" do
|
31
|
+
event = event_template
|
32
|
+
options = {:filter_name => "nonexistent"}
|
33
|
+
@extension.safe_run(event, options) do |output, status|
|
34
|
+
expect(output).to eq("")
|
35
|
+
expect(status).to eq(1)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
it "can run, filtering an event with a filter" do
|
40
|
+
event = event_template
|
41
|
+
event[:client][:environment] = "development"
|
42
|
+
options = {:filter_name => "production"}
|
43
|
+
@extension.safe_run(event, options) do |output, status|
|
44
|
+
expect(output).to eq("")
|
45
|
+
expect(status).to eq(0)
|
46
|
+
event[:client][:environment] = "production"
|
47
|
+
@extension.safe_run(event, options) do |output, status|
|
48
|
+
expect(output).to eq("")
|
49
|
+
expect(status).to eq(1)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/spec/helpers.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
require "rspec"
|
2
2
|
require "eventmachine"
|
3
|
+
require "sensu/logger"
|
4
|
+
require "sensu/settings"
|
5
|
+
require "uuidtools"
|
3
6
|
|
4
7
|
unless RUBY_VERSION < "1.9" || RUBY_PLATFORM =~ /java/
|
5
8
|
require "codeclimate-test-reporter"
|
@@ -26,4 +29,52 @@ module Helpers
|
|
26
29
|
def async_done
|
27
30
|
EM.stop_event_loop
|
28
31
|
end
|
32
|
+
|
33
|
+
def logger
|
34
|
+
Sensu::Logger.get(:log_level => :fatal)
|
35
|
+
end
|
36
|
+
|
37
|
+
def settings
|
38
|
+
asset_dir = File.join(File.dirname(__FILE__), "assets")
|
39
|
+
config_file = File.join(asset_dir, "config.json")
|
40
|
+
Sensu::Settings.get(:config_file => config_file)
|
41
|
+
end
|
42
|
+
|
43
|
+
def epoch
|
44
|
+
Time.now.to_i
|
45
|
+
end
|
46
|
+
|
47
|
+
def client_template
|
48
|
+
{
|
49
|
+
:name => "i-424242",
|
50
|
+
:address => "127.0.0.1",
|
51
|
+
:subscriptions => [
|
52
|
+
"test"
|
53
|
+
]
|
54
|
+
}
|
55
|
+
end
|
56
|
+
|
57
|
+
def check_template
|
58
|
+
{
|
59
|
+
:name => "test",
|
60
|
+
:command => "echo WARNING && exit 1",
|
61
|
+
:issued => epoch
|
62
|
+
}
|
63
|
+
end
|
64
|
+
|
65
|
+
def event_template
|
66
|
+
client = client_template
|
67
|
+
client[:timestamp] = epoch
|
68
|
+
check = check_template
|
69
|
+
check[:output] = "WARNING"
|
70
|
+
check[:status] = 1
|
71
|
+
check[:history] = [1]
|
72
|
+
{
|
73
|
+
:id => UUIDTools::UUID.random_create.to_s,
|
74
|
+
:client => client,
|
75
|
+
:check => check,
|
76
|
+
:occurrences => 1,
|
77
|
+
:action => :create
|
78
|
+
}
|
79
|
+
end
|
29
80
|
end
|
data/spec/loader_spec.rb
CHANGED
@@ -79,6 +79,7 @@ describe "Sensu::Extensions::Loader" do
|
|
79
79
|
expect(@loader.all).to include(extension)
|
80
80
|
expect(@loader.handler_exists?("debug")).to be(true)
|
81
81
|
expect(@loader.mutator_exists?("only_check_output")).to be(true)
|
82
|
+
expect(@loader.filter_exists?("attributes")).to be(true)
|
82
83
|
end
|
83
84
|
|
84
85
|
it "can load specific extension categories for a sensu client" do
|
@@ -92,6 +93,7 @@ describe "Sensu::Extensions::Loader" do
|
|
92
93
|
@loader.load_instances("server")
|
93
94
|
expect(@loader.handler_exists?("debug")).to be(true)
|
94
95
|
expect(@loader.mutator_exists?("only_check_output")).to be(true)
|
96
|
+
expect(@loader.filter_exists?("attributes")).to be(true)
|
95
97
|
expect(@loader.check_exists?("mock_check")).to be(false)
|
96
98
|
end
|
97
99
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sensu-extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.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: 2014-
|
11
|
+
date: 2014-12-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sensu-em
|
@@ -24,6 +24,34 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sensu-logger
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: sensu-settings
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
27
55
|
- !ruby/object:Gem::Dependency
|
28
56
|
name: sensu-extension
|
29
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,6 +94,20 @@ dependencies:
|
|
66
94
|
- - "~>"
|
67
95
|
- !ruby/object:Gem::Version
|
68
96
|
version: '1.6'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: uuidtools
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
69
111
|
- !ruby/object:Gem::Dependency
|
70
112
|
name: rake
|
71
113
|
requirement: !ruby/object:Gem::Requirement
|
@@ -122,16 +164,19 @@ files:
|
|
122
164
|
- README.md
|
123
165
|
- Rakefile
|
124
166
|
- lib/sensu/extensions.rb
|
167
|
+
- lib/sensu/extensions/filters/attributes.rb
|
125
168
|
- lib/sensu/extensions/handlers/debug.rb
|
126
169
|
- lib/sensu/extensions/loader.rb
|
127
170
|
- lib/sensu/extensions/mutators/json.rb
|
128
171
|
- lib/sensu/extensions/mutators/only_check_output.rb
|
129
172
|
- lib/sensu/extensions/mutators/ruby_hash.rb
|
130
173
|
- sensu-extensions.gemspec
|
174
|
+
- spec/assets/config.json
|
131
175
|
- spec/assets/extensions/error.rb
|
132
176
|
- spec/assets/extensions/mock_check.rb
|
133
177
|
- spec/assets/extensions/test.rb
|
134
178
|
- spec/extensions_spec.rb
|
179
|
+
- spec/filters/attributes_spec.rb
|
135
180
|
- spec/handlers/debug_spec.rb
|
136
181
|
- spec/helpers.rb
|
137
182
|
- spec/loader_spec.rb
|
@@ -158,15 +203,17 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
158
203
|
version: '0'
|
159
204
|
requirements: []
|
160
205
|
rubyforge_project:
|
161
|
-
rubygems_version: 2.2.
|
206
|
+
rubygems_version: 2.2.2
|
162
207
|
signing_key:
|
163
208
|
specification_version: 4
|
164
209
|
summary: The Sensu extension loader library
|
165
210
|
test_files:
|
211
|
+
- spec/assets/config.json
|
166
212
|
- spec/assets/extensions/error.rb
|
167
213
|
- spec/assets/extensions/mock_check.rb
|
168
214
|
- spec/assets/extensions/test.rb
|
169
215
|
- spec/extensions_spec.rb
|
216
|
+
- spec/filters/attributes_spec.rb
|
170
217
|
- spec/handlers/debug_spec.rb
|
171
218
|
- spec/helpers.rb
|
172
219
|
- spec/loader_spec.rb
|