nagira 0.2.9 → 0.2.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eee44c66e1e2ad2e7bac86476df0bf1b7e718125
4
- data.tar.gz: 0053b898f641d501c7f1f4eb6f25c55c4fd4dfba
3
+ metadata.gz: d5fea0d0e27dede1688541ffcbbd087db1ba7c25
4
+ data.tar.gz: ad4cbc6c1d5f21d554b0040d02294bc3657fb719
5
5
  SHA512:
6
- metadata.gz: 5949c8483f123c1d32e6343ff034dafdc889a0f2884997fa9685896e7dde8b2e306bea7ee82f96cceca5ca1323a033a12b35bc643ec8557d0454bd49e749801f
7
- data.tar.gz: dfd9e7e8e3cb152ec5e499be73b97a2fc853945a4524812bdf104bcfcecc6ec4ed71352d18a32cbfa75ca37723693574378c6bb1d540de786d640cc24a029d17
6
+ metadata.gz: 58f1915265edf73b6d8cee206b7143b90ae6b04f911d1260cb878eda659d54825da715546ba711274650f1ceaac4334f953c2bb76c480312ef6ef7e16abf548d
7
+ data.tar.gz: 5e0a46cf6d93eccf637f282aa27c7fcf4e7786decc0cf5cfeaf52577774e41877bb80a9aa15857d10ac65e340f8de0b05dd12f809e38e906903b6d89bda829cf
data/History.md CHANGED
@@ -1,3 +1,15 @@
1
+ ### v.0.2.10
2
+ * Fri Jul 19 2013 -- Dmytro Kovalov
3
+ - JSON 1.7.7 dependency conflict fix (#25)
4
+ - Upgrade to latest ruby-nagios 0.2.0
5
+ * Fri Jul 12 2013 -- Dmytro Kovalov
6
+ - rspec tests
7
+ - PUT method specs for /_status _services
8
+ - JSON structure specs for PUT methods
9
+ - return 400 if PUT not success
10
+ - specs for nagios.cmd writes
11
+ ### v.0.2.9
12
+ - Bugfix - library loading order change, for @format processing in PUT routes.
1
13
  ### v.0.2.8
2
14
  * Wed Apr 17 2013 -- Dmytro Kovalov
3
15
  - Bugfix - prevent tracedumps on nonexisting hostnames
data/lib/app.rb CHANGED
@@ -254,6 +254,7 @@ class Nagira < Sinatra::Base
254
254
  end
255
255
  end
256
256
 
257
+
257
258
  ##
258
259
  # @method object_not_found
259
260
  # @overload after("Object not found or bad request")
@@ -264,7 +265,6 @@ class Nagira < Sinatra::Base
264
265
  #
265
266
  #
266
267
  after do
267
- # return unless request["REQUEST_METHOD"] == 'PUT'
268
268
  if ! @data || @data.empty?
269
269
  halt [404, {
270
270
  :message => "Object not found or bad request",
@@ -275,6 +275,18 @@ class Nagira < Sinatra::Base
275
275
  end
276
276
 
277
277
 
278
+ ##
279
+ # @method argument_error
280
+ # @overload after("ArgumentError")
281
+ #
282
+ # Return 400 if result of PUT operation is not success.
283
+ #
284
+ after do
285
+ return unless request.put?
286
+ halt [400, @data.send("to_#{@format}") ] if ! @data[:result]
287
+ end
288
+
289
+
278
290
  ##
279
291
  # @method convert_to_active_resource
280
292
  # @overload after("Return Array for ActiveResouce routes")
@@ -358,5 +370,6 @@ class Nagira < Sinatra::Base
358
370
 
359
371
  end
360
372
 
361
- require "app/routes/put"
362
373
  require "app/routes/put/status"
374
+ require "app/routes/put/host"
375
+ require "app/routes/put"
@@ -18,6 +18,7 @@ class Nagira < Sinatra::Base
18
18
  # Make sure we always return an Array
19
19
  @input = [@input] if @input.is_a? Hash
20
20
  end
21
+ @input
21
22
  end
22
23
 
23
24
  # Define helpers for put methods
@@ -31,7 +32,7 @@ class Nagira < Sinatra::Base
31
32
  # FIXME: This only accepts single service. Modify to use Arrays too
32
33
  def put_update action, params
33
34
  res = $nagios[:commands].write(params.merge({ :action => action }))
34
- { :status => res[:result], :object => res[:data]}
35
+ { :result => res[:result], :object => res[:data]}
35
36
  end
36
37
  end
37
38
 
@@ -0,0 +1,52 @@
1
+ #
2
+ # PUT routes for host status.
3
+ #
4
+ class Nagira < Sinatra::Base
5
+
6
+ # @method put_status
7
+ # @overload put("/_status")
8
+ #
9
+ # Submit JSON Hash for multiple services, on multiple hosts.
10
+ put "/_status" do
11
+ "TODO: Not implemented"
12
+ end
13
+
14
+ # @method put_status_host_name
15
+ # @overload put("/_status/:host_name")
16
+ #
17
+ # Update hoststatus information only for the given host. URL
18
+ # hostname always override hostname given in the JSON file.
19
+ #
20
+ # == Example
21
+ #
22
+ # $ curl -i -H "Accept: application/json" -d @host.json -X
23
+ # PUT http://localhost:4567/_status/svaroh
24
+ #
25
+ # => {"status": true, "object": [{"data": {"host_name":"svaroh",
26
+ # "status_code": "0", "plugin_output": "ping OK", "action":
27
+ # "PROCESS_HOST_CHECK_RESULT"}, "result":true, "messages": []}]}
28
+ #
29
+ # == Example JSON
30
+ #
31
+ # {
32
+ # "status_code":"0",
33
+ # "plugin_output" : "ping OK"
34
+ # }
35
+ put "/_status/:host_name" do
36
+ @data = update_host_status @input.first.merge({
37
+ 'host_name' => params['host_name']
38
+ })
39
+ nil
40
+ end
41
+
42
+ # Same as /_status/:host_name (Not implemented)
43
+ #
44
+ # @method put__host_status_host_name
45
+ # @overload put("/_host_status/:host_name")
46
+ #
47
+ put "/_host_status/:host_name" do
48
+ "Not implemented: TODO"
49
+ end
50
+
51
+
52
+ end
@@ -1,51 +1,8 @@
1
+ #
2
+ # PUT method routes for services status.
3
+ #
1
4
  class Nagira < Sinatra::Base
2
5
 
3
-
4
- # @method put_status
5
- # @overload put("/_status")
6
- #
7
- # Submit JSON Hash for multiple services, on multiple hosts.
8
- put "/_status" do
9
- "TODO: Not implemented"
10
- end
11
-
12
- # @method put_status_host_name
13
- # @overload put("/_status/:host_name")
14
- #
15
- # Update hoststatus information only for the given host. URL
16
- # hostname always override hostname given in the JSON file.
17
- #
18
- # == Example
19
- #
20
- # $ curl -i -H "Accept: application/json" -d @host.json -X
21
- # PUT http://localhost:4567/_status/svaroh
22
- #
23
- # => {"status": true, "object": [{"data": {"host_name":"svaroh",
24
- # "status_code": "0", "plugin_output": "ping OK", "action":
25
- # "PROCESS_HOST_CHECK_RESULT"}, "result":true, "messages": []}]}
26
- #
27
- # == Example JSON
28
- #
29
- # {
30
- # "status_code":"0",
31
- # "plugin_output" : "ping OK"
32
- # }
33
- put "/_status/:host_name" do
34
- @data = update_host_status @input.first.merge({
35
- 'host_name' => params['host_name']
36
- })
37
- nil
38
- end
39
-
40
- # Same as /_status/:host_name (Not implemented)
41
- #
42
- # @method put__host_status_host_name
43
- # @overload put("/_host_status/:host_name")
44
- #
45
- put "/_host_status/:host_name" do
46
- "Not implemented: TODO"
47
- end
48
-
49
6
  # @method put_status_host_name_services
50
7
  # @overload put("/_status/:host_name/_services")
51
8
  #
@@ -59,16 +16,16 @@ class Nagira < Sinatra::Base
59
16
  # $ curl -i -H "Accept: application/json" -d @dat_m.json -X PUT
60
17
  # http://localhost:4567/_status/svaroh/_services
61
18
  #
62
- # [{"status":true, "object":[{"data":{"host_name":"svaroh",
63
- # "service_description":"PING", "return_code":"0",
64
- # "plugin_output":"OK",
65
- # "action":"PROCESS_SERVICE_CHECK_RESULT"}, "result":true,
66
- # "messages":[]}]}, {"status":true,
67
- # "object":[{"data":{"host_name":"svaroh",
68
- # "service_description":"Apache", "return_code":"r20",
69
- # "plugin_output":"cwfailedOK",
70
- # "action":"PROCESS_SERVICE_CHECK_RESULT"}, "result":true,
71
- # "messages":[]}]}]%
19
+ # {"result"=>true,
20
+ # "object"=>
21
+ # [{"data"=>
22
+ # {"return_code"=>0,
23
+ # "plugin_output"=>"All OK",
24
+ # "service_description"=>"PING",
25
+ # "host_name"=>"archive",
26
+ # "action"=>"PROCESS_SERVICE_CHECK_RESULT"},
27
+ # "result"=>true,
28
+ # "messages"=>[]}]}
72
29
  #
73
30
  # == Example JSON for submit
74
31
  #
@@ -96,16 +53,19 @@ class Nagira < Sinatra::Base
96
53
  #
97
54
  put "/_status/:host_name/_services" do
98
55
 
99
- data = []
56
+ data, result = [], true
57
+
100
58
  @input.each do |datum|
101
59
  # FIXME - this calls update for each service. Should be batching them together
102
- data << update_service_status(
103
- datum.merge({
104
- 'host_name' => params['host_name']
105
- })
106
- )
60
+ update = update_service_status(
61
+ datum.merge({
62
+ 'host_name' => params['host_name']
63
+ })
64
+ )
65
+ data << update[:object].first
66
+ result &&= update[:result]
107
67
  end
108
- @data = data
68
+ @data = { result: result, object: data }
109
69
  nil
110
70
 
111
71
  end
@@ -131,7 +91,6 @@ class Nagira < Sinatra::Base
131
91
  # -X PUT http://localhost:4567/_status/viy/_services/PING/_return_code/0/_plugin_output/OK
132
92
  # # => ok
133
93
  put "/_status/:host_name/_services/:service_description/_return_code/:return_code/_plugin_output/:plugin_output" do
134
-
135
94
  @data = update_service_status params
136
95
  nil
137
96
  end
@@ -1,5 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
+ #
4
+ # Test resonse to /_api GET
5
+ #
3
6
  describe Nagira do
4
7
 
5
8
  set :environment, ENV['RACK_ENV'] || :test
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Nagira do
4
+
5
+ set :environment, ENV['RACK_ENV'] || :test
6
+ include Rack::Test::Methods
7
+ def app
8
+ @app ||= Nagira
9
+ end
10
+
11
+ context "/_hosts" do
12
+
13
+ before :each do
14
+ get "/_status/_list"
15
+ @data = JSON.parse(last_response.body)
16
+ end
17
+
18
+ it "return hosts list " do
19
+ @data.should be_a_kind_of Array
20
+ end
21
+
22
+ it "hostname is a string" do
23
+ @data.first.should be_a_kind_of String
24
+ end
25
+
26
+ end
27
+
28
+
29
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Nagira do
4
+
5
+ set :environment, ENV['RACK_ENV'] || :test
6
+ include Rack::Test::Methods
7
+ def app
8
+ @app ||= Nagira
9
+ end
10
+
11
+ before :all do
12
+ get "/_status/_list.json"
13
+ @host = JSON.parse(last_response.body).first
14
+ end
15
+
16
+ context "/_hosts/:host/_services" do
17
+
18
+ before :each do
19
+ get "/_status/#{@host}/_services"
20
+ @data = JSON.parse(last_response.body)
21
+ end
22
+
23
+ it "return services list " do
24
+ @data.should be_a_kind_of Hash
25
+ end
26
+ end
27
+
28
+
29
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe Nagira do
4
+
5
+ set :environment, :test # This is potentially desctructive test, run only in test mode
6
+ include Rack::Test::Methods
7
+ def app
8
+ @app ||= Nagira
9
+ end
10
+
11
+ let (:content_type) { {'Content-Type' => 'application/json'} }
12
+
13
+ before :all do
14
+ @data = {
15
+ "status_code" => 0,
16
+ "plugin_output" => "Plugin said: Bla"
17
+ }
18
+
19
+ get "/_status/_list.json"
20
+ @host = JSON.parse(last_response.body).first
21
+
22
+ end
23
+
24
+ context "updates /_status/:host" do
25
+ let (:url) { "/_status/#{@host}" }
26
+
27
+ context "single check" do
28
+
29
+ it "with no hostname" do
30
+ pending
31
+ # put url, @data, content_type
32
+
33
+ # pp [url, @data, last_response.body]
34
+ # last_response.should be_ok
35
+ end
36
+
37
+ it "with hostname" do
38
+ put url, @data, content_type
39
+ pp last_response.body
40
+ last_response.should_not be_ok
41
+ end
42
+
43
+
44
+ end
45
+
46
+ it "Multiple checks" do
47
+ pending
48
+ end
49
+
50
+ end # /_status/:host
51
+
52
+ end
@@ -0,0 +1,131 @@
1
+ require 'spec_helper'
2
+ require_relative 'support'
3
+
4
+ describe Nagira do
5
+
6
+ set :environment, :test # This is potentially desctructive test, run only in test mode
7
+ include Rack::Test::Methods
8
+ def app
9
+ @app ||= Nagira
10
+ end
11
+
12
+ let (:content_type) { {'Content-Type' => 'application/json'} }
13
+
14
+ before :all do
15
+ get "/_status/_list.json"
16
+
17
+ @host = JSON.parse(last_response.body).first
18
+ get "/_status/#{@host}/_services.json"
19
+
20
+ @input = {
21
+ "service_description" => "Host Service",
22
+ "return_code" => 0,
23
+ "plugin_output" => "Plugin said: Bla"
24
+ }
25
+
26
+ @input2 = {
27
+ "service_description" => "Another Host Service",
28
+ "return_code" => 2,
29
+ "plugin_output" => "Plugin said: Bla and Bla"
30
+ }
31
+
32
+ end
33
+
34
+ before :each do
35
+ File.delete nagios_cmd rescue nil
36
+ end
37
+
38
+ let (:host) { @host }
39
+
40
+ # ==================================================================
41
+ # Tests
42
+ #
43
+
44
+ context "/_status/:host/_services" do
45
+
46
+ context :single do
47
+
48
+ let (:url) { "/_status/#{host}/_services" }
49
+ before (:each) do
50
+ put url, @input.to_json, content_type
51
+ end
52
+
53
+ it_should_behave_like :put_status
54
+
55
+ it "URL param hostname is higher priority than JSON" do
56
+ pending
57
+ # put url, @input.merge({ "host_name" => "fake_host"}).to_json, content_type
58
+
59
+ # out = JSON.parse last_response.body
60
+ # pp out
61
+ # out["object"]["data"]["host_name"].should eq host
62
+ end
63
+
64
+ it "update with valid data" do
65
+ last_response.should be_ok
66
+ end
67
+
68
+ it 'Fails with missing data' do
69
+ @input.keys.each do |key|
70
+ data = @input.dup
71
+ data.delete key
72
+ put url, data.to_json, content_type
73
+ last_response.status.should be 400
74
+ end
75
+ end
76
+ end
77
+
78
+ context :multiple do
79
+ let (:url) { "/_status/#{host}/_services" }
80
+ before do
81
+ put url, [@input, @input2].to_json, content_type
82
+ end
83
+
84
+ let (:out) { JSON.parse last_response.body }
85
+
86
+ it_should_behave_like :put_status
87
+
88
+ context "data check" do
89
+ subject { out["object"] }
90
+
91
+ it { should be_a_kind_of Array }
92
+ it { subject.size.should eq 2 }
93
+ end
94
+ end
95
+ end
96
+
97
+ context "/_status/:host_name/_services/:service_description" do
98
+ let(:url) { "/_status/#{host}/_services/PING" }
99
+ before do
100
+ put url, [{ return_code: 0, plugin_output: "All OK"}].to_json, content_type
101
+ end
102
+
103
+ it "writes status" do
104
+ last_response.should be_ok
105
+ end
106
+ it_should_behave_like :put_status
107
+
108
+ end
109
+
110
+
111
+
112
+ # context 'host does not exist' do
113
+ # let (:host) { "some_nonexisting_host" }
114
+ # let (:url) { "/_status/#{host}/_services" }
115
+
116
+ # it_should_behave_like :put_status
117
+ # it { pending "Add validaton for host existence in Ruby-Nagios for PUT"}
118
+
119
+ # # it "fails with valid data" do
120
+ # # end
121
+ # # it 'fails with invalid data' do
122
+ # # end
123
+
124
+ # end # host does not exist
125
+
126
+
127
+ context "/_status/:host_name/_services/:service_description/_return_code/:return_code/_plugin_output/:plugin_output" do
128
+ it { pending "To be depreciated "}
129
+ end
130
+
131
+ end # update /_hosts
@@ -0,0 +1,64 @@
1
+ shared_examples_for :json_response do
2
+ before (:each) do
3
+ @ret = JSON.parse(last_response.body)
4
+ end
5
+ subject { @ret }
6
+
7
+ it { should be_a_kind_of Hash }
8
+ it { should have_key "result" }
9
+ it { should have_key "object" }
10
+
11
+ let (:obj) { subject["object"] }
12
+
13
+ context :object do
14
+ # subject { obj }
15
+
16
+ it { obj.should be_a_kind_of Array }
17
+ it {
18
+ obj.each { |x| x.should be_a_kind_of Hash }
19
+ }
20
+
21
+ context :data_keys do
22
+
23
+ it do
24
+ obj.each do |x|
25
+ x.should have_key "data"
26
+ x.should have_key "result"
27
+ x.should have_key "messages"
28
+ [true, false].should include x["result"]
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ shared_examples_for :json_success_response do
36
+ before (:each) do
37
+ @ret = JSON.parse(last_response.body)
38
+ end
39
+ subject { @ret }
40
+
41
+ #it { subject.}
42
+ end
43
+
44
+ shared_examples_for :write_to_nagios_cmd_file do
45
+ before (:all) do
46
+ File.delete $nagios[:commands].path rescue nil
47
+ end
48
+ let (:cmd) { $nagios[:commands].path }
49
+
50
+ it "writes to nagios.cmd file" do
51
+ File.should exist(cmd)
52
+ File.read(cmd).should =~ /^\[\d+\] PROCESS_SERVICE_CHECK_RESULT;#{host}/
53
+ end
54
+
55
+ after (:each) do
56
+ File.delete $nagios[:commands].path rescue nil
57
+ end
58
+ end
59
+
60
+
61
+ shared_examples_for :put_status do
62
+ it_should_behave_like :json_response
63
+ it_should_behave_like :write_to_nagios_cmd_file
64
+ end
@@ -1 +1 @@
1
- 0.2.9
1
+ 0.2.10
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nagira
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.9
4
+ version: 0.2.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmytro Kovalov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-09 00:00:00.000000000 Z
11
+ date: 2013-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -276,6 +276,7 @@ files:
276
276
  - lib/app/routes/get/config.rb
277
277
  - lib/app/routes/get/objects.rb
278
278
  - lib/app/routes/get/status.rb
279
+ - lib/app/routes/put/host.rb
279
280
  - lib/app/routes/put/status.rb
280
281
  - lib/app/routes/put.rb
281
282
  - lib/app.rb
@@ -284,11 +285,15 @@ files:
284
285
  - lib/nagira/timed_parse.rb
285
286
  - lib/nagira.rb
286
287
  - spec/00_configuration_spec.rb
287
- - spec/01_nagira_response_spec.rb
288
- - spec/02_0_status_spec.rb
289
- - spec/02_nagira_data_spec.rb
290
- - spec/03_api_spec.rb
291
- - spec/integration/10_put_service_spec.rb
288
+ - spec/01_data_format/01_nagira_response_spec.rb
289
+ - spec/01_data_format/02_0_status_spec.rb
290
+ - spec/01_data_format/02_nagira_data_spec.rb
291
+ - spec/01_data_format/03_api_spec.rb
292
+ - spec/get/hosts_spec.rb
293
+ - spec/get/services_spec.rb
294
+ - spec/put/host_spec.rb
295
+ - spec/put/status_spec.rb
296
+ - spec/put/support.rb
292
297
  - spec/spec_helper.rb
293
298
  - config/defaults.rb
294
299
  - config/environment.rb
@@ -312,7 +317,8 @@ files:
312
317
  - test/data/status.dat
313
318
  - bin/nagira-setup
314
319
  homepage: http://dmytro.github.com/nagira
315
- licenses: []
320
+ licenses:
321
+ - MIT
316
322
  metadata: {}
317
323
  post_install_message:
318
324
  rdoc_options: []
@@ -1,25 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Nagira do
4
-
5
- set :environment, ENV['RACK_ENV'] || :test
6
-
7
- include Rack::Test::Methods
8
-
9
- def app
10
- @app ||= Nagira
11
- end
12
-
13
-
14
-
15
- context "Update services" do
16
-
17
- context "/_status/:host_name/_services"
18
-
19
- it "should ignore 404 for some services" do
20
- pending
21
- end
22
- end
23
-
24
-
25
- end