sensu-extensions 0.0.2 → 0.0.4

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: e9b7abb0a8e576a835a2162c0b1e6f070e5c340a
4
- data.tar.gz: 46547b38a70a2182b7894a3d27042b872075942f
3
+ metadata.gz: a70c9420e9ef4c2dab2a6cf6779ee760e0c5a0ef
4
+ data.tar.gz: 0321f2582670a20f127d149b769c1f8e7397a2c9
5
5
  SHA512:
6
- metadata.gz: 11bd8e02a097a891b41f42c5f85296fbc6d71188099e0140b8bdde1a843b257de8b46c9b5ef11446da7a7acb67e4a54927e13f4bfd67462615e70e356e8b839e
7
- data.tar.gz: 0e4f8e91af10254dea3fa887f82d5f0d56017d09aef5284bef0529ae34082aa47c3489e5c4275c1ac423ef793a11580aaa9488697f3e3bca8f7470b8eea65432
6
+ metadata.gz: 7cbdd74714cf3278ba63688c574e4b5de7797a402ff7b178dc68c9e303a9c2253041573f8a79a857be10e1fa5c86a62fe4316c6dceac26b4894e93cadd197de6
7
+ data.tar.gz: d0799591f7564b4059604201ad14807c78c7370e7e319d262e4034663ff3a82fb50d91adeec8046b9efe049baa9b658b68462f4ccafc74fc588e5a87cd7d0c1d
@@ -1,4 +1,6 @@
1
1
  require "sensu/extension"
2
+ require "sensu/extensions/mutators/json"
3
+ require "sensu/extensions/mutators/ruby_hash"
2
4
  require "sensu/extensions/mutators/only_check_output"
3
5
  require "sensu/extensions/handlers/debug"
4
6
 
@@ -84,8 +86,11 @@ module Sensu
84
86
  end
85
87
 
86
88
  # Load instances of the loaded extensions.
87
- def load_instances
88
- Extension::CATEGORIES.each do |category|
89
+ #
90
+ # @param [String] sensu service to load extensions for.
91
+ def load_instances(service=nil)
92
+ service ||= sensu_service_name
93
+ categories_to_load(service).each do |category|
89
94
  extension_type = category.to_s.chop
90
95
  Extension.const_get(extension_type.capitalize).descendants.each do |klass|
91
96
  extension = klass.new
@@ -120,6 +125,29 @@ module Sensu
120
125
  @extensions[category].has_key?(name)
121
126
  end
122
127
 
128
+ # Retrieve Sensu service name.
129
+ #
130
+ # @return [String] service name.
131
+ def sensu_service_name
132
+ File.basename($0).split("-").last
133
+ end
134
+
135
+ # Determine which extension categories to load for the given
136
+ # sensu service.
137
+ #
138
+ # @param [String] sensu service to load extensions for.
139
+ # @return [Array] extension categories.
140
+ def categories_to_load(service)
141
+ case service
142
+ when "client"
143
+ [:checks]
144
+ when "server"
145
+ Extension::CATEGORIES.reject { |category| category == :checks }
146
+ else
147
+ Extension::CATEGORIES
148
+ end
149
+ end
150
+
123
151
  # Record a warning.
124
152
  #
125
153
  # @param message [String] warning message.
@@ -0,0 +1,20 @@
1
+ require "sensu/extension"
2
+ require "multi_json"
3
+
4
+ module Sensu
5
+ module Extension
6
+ class Json < Mutator
7
+ def name
8
+ "json"
9
+ end
10
+
11
+ def description
12
+ "returns JSON formatted event data"
13
+ end
14
+
15
+ def run(event)
16
+ yield MultiJson.dump(event), 0
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ require "sensu/extension"
2
+
3
+ module Sensu
4
+ module Extension
5
+ class RubyHash < Mutator
6
+ def name
7
+ "ruby_hash"
8
+ end
9
+
10
+ def description
11
+ "returns ruby hash event data"
12
+ end
13
+
14
+ def run(event)
15
+ yield event, 0
16
+ end
17
+ end
18
+ end
19
+ end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "sensu-extensions"
5
- spec.version = "0.0.2"
5
+ spec.version = "0.0.4"
6
6
  spec.authors = ["Sean Porter"]
7
7
  spec.email = ["portertech@gmail.com"]
8
8
  spec.summary = "The Sensu extension loader library"
@@ -17,6 +17,7 @@ Gem::Specification.new do |spec|
17
17
 
18
18
  spec.add_dependency "sensu-em"
19
19
  spec.add_dependency "sensu-extension"
20
+ spec.add_dependency "multi_json"
20
21
 
21
22
  spec.add_development_dependency "bundler", "~> 1.6"
22
23
  spec.add_development_dependency "rake"
@@ -9,8 +9,8 @@ module Sensu
9
9
  "error extension"
10
10
  end
11
11
 
12
- def run(event, &callback)
13
- block.call(event, 0)
12
+ def run(event)
13
+ yield event, 0
14
14
  end
15
15
  end
16
16
  end
@@ -0,0 +1,17 @@
1
+ module Sensu
2
+ module Extension
3
+ class MockCheck < Check
4
+ def name
5
+ "mock_check"
6
+ end
7
+
8
+ def description
9
+ "returns 'foo'"
10
+ end
11
+
12
+ def run
13
+ yield 'foo', 0
14
+ end
15
+ end
16
+ end
17
+ end
@@ -9,8 +9,8 @@ module Sensu
9
9
  "test extension"
10
10
  end
11
11
 
12
- def run(event, &callback)
13
- block.call(event, 0)
12
+ def run(event)
13
+ yield event, 0
14
14
  end
15
15
  end
16
16
  end
@@ -28,18 +28,22 @@ describe "Sensu::Extensions" do
28
28
 
29
29
  it "can load extensions from a directory" do
30
30
  extensions = Sensu::Extensions.load(:extension_dir => @extension_dir)
31
- extensions.loaded_files.size.should eq(1)
31
+ extensions.loaded_files.size.should eq(2)
32
32
  extensions.handler_exists?("test").should be_true
33
+ extensions.check_exists?("mock_check").should be_true
33
34
  end
34
35
 
35
36
  it "can load extensions from one or more directories" do
36
37
  extensions = Sensu::Extensions.load(:extension_dirs => [@extension_dir])
37
- extensions.loaded_files.size.should eq(1)
38
+ extensions.loaded_files.size.should eq(2)
38
39
  extensions.handler_exists?("test").should be_true
40
+ extensions.check_exists?("mock_check").should be_true
39
41
  end
40
42
 
41
43
  it "can load the built-in extensions" do
42
44
  extensions = Sensu::Extensions.load
45
+ extensions.mutator_exists?("json").should be_true
46
+ extensions.mutator_exists?("ruby_hash").should be_true
43
47
  extensions.mutator_exists?("only_check_output").should be_true
44
48
  extensions.handler_exists?("debug").should be_true
45
49
  end
data/spec/loader_spec.rb CHANGED
@@ -14,7 +14,7 @@ describe "Sensu::Extensions::Loader" do
14
14
  it "can provide the extensions loader API" do
15
15
  @loader.should respond_to(:load_file, :load_directory, :load_instances,
16
16
  :warnings, :loaded_files, :[], :all,
17
- :generics, :bridges, :checks, :mutators, :handlers)
17
+ :bridges, :checks, :mutators, :handlers)
18
18
  end
19
19
 
20
20
  it "can load an extension from a file" do
@@ -41,10 +41,12 @@ describe "Sensu::Extensions::Loader" do
41
41
 
42
42
  it "can load extensions from a directory" do
43
43
  @loader.load_directory(@extension_dir)
44
- @loader.warnings.size.should eq(5)
45
- @loader.loaded_files.size.should eq(1)
44
+ @loader.warnings.size.should eq(6)
45
+ @loader.loaded_files.size.should eq(2)
46
46
  extension = Sensu::Extension::Test.new
47
47
  extension.should respond_to(:name, :description, :definition, :safe_run, :stop, :has_key?, :[])
48
+ extension = Sensu::Extension::MockCheck.new
49
+ extension.should respond_to(:name, :description, :definition, :safe_run, :stop, :has_key?, :[])
48
50
  end
49
51
 
50
52
  it "can attempt to load extensions from a nonexistent directory" do
@@ -61,6 +63,8 @@ describe "Sensu::Extensions::Loader" do
61
63
  extension.should respond_to(:name, :description, :definition, :safe_run, :stop, :has_key?, :[])
62
64
  @loader.handlers.should include(extension.definition)
63
65
  @loader.all.should include(extension)
66
+ @loader.mutator_exists?("json").should be_true
67
+ @loader.mutator_exists?("ruby_hash").should be_true
64
68
  @loader.mutator_exists?("only_check_output").should be_true
65
69
  end
66
70
 
@@ -76,4 +80,18 @@ describe "Sensu::Extensions::Loader" do
76
80
  @loader.handler_exists?("debug").should be_true
77
81
  @loader.mutator_exists?("only_check_output").should be_true
78
82
  end
83
+
84
+ it "can load specific extension categories for a sensu client" do
85
+ @loader.load_instances("client")
86
+ @loader.handler_exists?("debug").should be_false
87
+ @loader.mutator_exists?("only_check_output").should be_false
88
+ @loader.check_exists?("mock_check").should be_true
89
+ end
90
+
91
+ it "can load specific extension categories for a sensu server" do
92
+ @loader.load_instances("server")
93
+ @loader.handler_exists?("debug").should be_true
94
+ @loader.mutator_exists?("only_check_output").should be_true
95
+ @loader.check_exists?("mock_check").should be_false
96
+ end
79
97
  end
@@ -0,0 +1,24 @@
1
+ require File.join(File.dirname(__FILE__), "..", "helpers")
2
+ require "sensu/extensions/mutators/json"
3
+
4
+ describe "Sensu::Extension::Json" do
5
+ include Helpers
6
+
7
+ before do
8
+ @extension = Sensu::Extension::Json.new
9
+ end
10
+
11
+ it "can run, returning JSON event data" do
12
+ event = {
13
+ :client => {},
14
+ :check => {
15
+ :output => "foo",
16
+ :status => 0
17
+ }
18
+ }
19
+ @extension.safe_run(event) do |output, status|
20
+ output.should eq(MultiJson.dump(event))
21
+ status.should eq(0)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ require File.join(File.dirname(__FILE__), "..", "helpers")
2
+ require "sensu/extensions/mutators/ruby_hash"
3
+
4
+ describe "Sensu::Extension::RubyHash" do
5
+ include Helpers
6
+
7
+ before do
8
+ @extension = Sensu::Extension::RubyHash.new
9
+ end
10
+
11
+ it "can run, returning event data as a ruby hash" do
12
+ event = {
13
+ :client => {},
14
+ :check => {
15
+ :output => "foo",
16
+ :status => 0
17
+ }
18
+ }
19
+ @extension.safe_run(event) do |output, status|
20
+ output.should eq(event)
21
+ status.should eq(0)
22
+ end
23
+ end
24
+ 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: 0.0.2
4
+ version: 0.0.4
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-05-23 00:00:00.000000000 Z
11
+ date: 2014-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sensu-em
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: multi_json
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'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -110,15 +124,20 @@ files:
110
124
  - lib/sensu/extensions.rb
111
125
  - lib/sensu/extensions/handlers/debug.rb
112
126
  - lib/sensu/extensions/loader.rb
127
+ - lib/sensu/extensions/mutators/json.rb
113
128
  - lib/sensu/extensions/mutators/only_check_output.rb
129
+ - lib/sensu/extensions/mutators/ruby_hash.rb
114
130
  - sensu-extensions.gemspec
115
131
  - spec/assets/extensions/error.rb
132
+ - spec/assets/extensions/mock_check.rb
116
133
  - spec/assets/extensions/test.rb
117
134
  - spec/extensions_spec.rb
118
135
  - spec/handlers/debug_spec.rb
119
136
  - spec/helpers.rb
120
137
  - spec/loader_spec.rb
138
+ - spec/mutators/json_spec.rb
121
139
  - spec/mutators/only_check_output_spec.rb
140
+ - spec/mutators/ruby_hash_spec.rb
122
141
  homepage: https://github.com/sensu/sensu-extensions
123
142
  licenses:
124
143
  - MIT
@@ -145,9 +164,12 @@ specification_version: 4
145
164
  summary: The Sensu extension loader library
146
165
  test_files:
147
166
  - spec/assets/extensions/error.rb
167
+ - spec/assets/extensions/mock_check.rb
148
168
  - spec/assets/extensions/test.rb
149
169
  - spec/extensions_spec.rb
150
170
  - spec/handlers/debug_spec.rb
151
171
  - spec/helpers.rb
152
172
  - spec/loader_spec.rb
173
+ - spec/mutators/json_spec.rb
153
174
  - spec/mutators/only_check_output_spec.rb
175
+ - spec/mutators/ruby_hash_spec.rb