nagira 0.3.3 → 0.4.0

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.
@@ -8,16 +8,18 @@ require 'active_support/core_ext/hash/slice' # for Hash.slice
8
8
  require 'json'
9
9
  require 'yaml'
10
10
  require 'sinatra'
11
+ require 'sinatra/namespace'
11
12
  require 'sinatra/reloader'
12
13
 
13
14
  $: << File.dirname(__FILE__) << File.dirname(File.dirname(__FILE__))
14
15
 
15
16
  require 'config/defaults'
16
17
 
17
- require "app/routes/get/config"
18
- require "app/routes/get/objects"
19
- require "app/routes/get/status"
18
+ require "app/get/config"
19
+ require "app/get/objects"
20
+ require "app/get/status"
20
21
 
22
+ require "nagira/hostgroup"
21
23
 
22
24
  #
23
25
  # environment file must go after default, some settings override
@@ -30,17 +32,19 @@ class Nagira < Sinatra::Base
30
32
 
31
33
  VERSION = File.read(File.expand_path(File.dirname(__FILE__)) + '/../version.txt').strip
32
34
  GITHUB = "http://dmytro.github.com/nagira/"
33
-
35
+
36
+ register Sinatra::Namespace
37
+
34
38
  ##
35
39
  # When this prefix added to routes convert output to ActiveResource
36
40
  # compatible format (basically Array instead of Hash).
37
41
  #
38
42
  AR_PREFIX = "/ar"
39
43
 
40
- # Get all routes that Nagira provides.
41
- def api
44
+ # Get all routes that Nagira provides.
45
+ def api
42
46
  api = { }
43
-
47
+
44
48
  param_regex = Regexp.new '\(\[\^\\\\\/\?\#\]\+\)'
45
49
  Nagira.routes.keys.each do |method|
46
50
  api[method] ||= []
@@ -50,7 +54,7 @@ class Nagira < Sinatra::Base
50
54
  path.sub!(param_regex,":#{parm}")
51
55
  end
52
56
  path.gsub!('\\','')
53
- api[method] << path unless path.empty?
57
+ api[method] << path unless path.empty?
54
58
  end
55
59
  end
56
60
  api
@@ -0,0 +1,46 @@
1
+ class Nagira < Sinatra::Base
2
+ class Hostgroup
3
+ attr_reader :objects, :status, :name, :hostgroup, :data
4
+
5
+ def initialize(name)
6
+ @name = name
7
+ @objects = $nagios[:objects].objects
8
+ @status = $nagios[:status].status['hosts']
9
+ @hostgroup = @objects[:hostgroup][name]
10
+ @data = { }
11
+ end
12
+
13
+ def members
14
+ hostgroup[:members].split(",")
15
+ end
16
+
17
+ # i.e. both servcie and hoststatus
18
+ def full
19
+ members.each do |hostname|
20
+ data[hostname] = @status[hostname]
21
+ end
22
+ data
23
+ end
24
+
25
+
26
+ def status(mode)
27
+ members.each do |hostname|
28
+ begin
29
+ data[hostname] = @status[hostname][mode]
30
+ rescue NoMethodError
31
+ data[hostname] = { }
32
+ end
33
+ end
34
+ data
35
+ end
36
+
37
+ def service_status
38
+ status 'servicestatus'
39
+ end
40
+
41
+
42
+ def host_status
43
+ status 'hoststatus'
44
+ end
45
+ end
46
+ end
@@ -1,47 +1,47 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "Configuration" do
3
+ describe "Configuration" do
4
4
 
5
5
  set :environment, ENV['RACK_ENV'] || :test
6
6
 
7
- before {
7
+ before {
8
8
  @cfg = Nagios::Config.new(Nagira.settings.nagios_cfg)
9
9
  }
10
10
 
11
11
  context "nagios.cfg" do
12
12
 
13
- it { File.should exist @cfg.path }
14
-
13
+ it { expect(File).to exist(@cfg.path) }
14
+
15
15
  it "should be parseable" do
16
- lambda { @cfg.parse }.should_not raise_error
16
+ expect { @cfg.parse }.not_to raise_error
17
17
  end
18
18
 
19
- context "parsing nagios.cfg file" do
19
+ context "parsing nagios.cfg file" do
20
20
 
21
21
  before { @cfg.parse }
22
-
23
- it "should have PATH to objects file" do
24
- @cfg.object_cache_file.should be_a_kind_of String
22
+
23
+ it "should have PATH to objects file" do
24
+ expect(@cfg.object_cache_file).to be_a_kind_of String
25
25
  end
26
-
26
+
27
27
  it "should have PATH to status file" do
28
- @cfg.status_file.should be_a_kind_of String
28
+ expect(@cfg.status_file).to be_a_kind_of String
29
29
  end
30
30
 
31
31
  end # parsing nagios.cfg file
32
32
  end # nagios.cfg
33
-
34
- context "data files" do
33
+
34
+ context "data files" do
35
35
  before { @cfg.parse }
36
-
36
+
37
37
  context Nagios::Status do
38
-
38
+
39
39
  subject { Nagios::Status.new( Nagira.settings.status_cfg || @cfg.status_file ) }
40
40
 
41
- it { File.should exist( subject.path ) }
42
-
41
+ it { expect(File).to exist( subject.path ) }
42
+
43
43
  it "should be parseable" do
44
- lambda { subject.parse }.should_not raise_error
44
+ expect { subject.parse }.not_to raise_error
45
45
  end
46
46
  end # Nagios::Status
47
47
 
@@ -50,10 +50,10 @@ describe "Configuration" do
50
50
 
51
51
  subject { Nagios::Objects.new( Nagira.settings.objects_cfg || @cfg.object_cache_file) }
52
52
 
53
- it { File.should exist subject.path }
54
-
53
+ it { expect(File).to exist(subject.path) }
54
+
55
55
  it "should be parseable" do
56
- lambda { subject.parse }.should_not raise_error
56
+ expect { subject.parse }.not_to raise_error
57
57
  end
58
58
  end # Nagios::Objects
59
59
 
@@ -1,11 +1,11 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Nagira do
3
+ describe Nagira do
4
4
 
5
5
  set :environment, ENV['RACK_ENV'] || :test
6
6
 
7
7
  include Rack::Test::Methods
8
-
8
+
9
9
  def app
10
10
  @app ||= Nagira
11
11
  end
@@ -17,18 +17,18 @@ describe Nagira do
17
17
 
18
18
  context "simple page load" do
19
19
  TOP_PAGES.each do |page|
20
- it "/#{page} should load" do
20
+ it "/#{page} should load" do
21
21
  get "/#{page}"
22
- last_response.should be_ok
22
+ expect(last_response).to be_ok
23
23
  end
24
-
24
+
25
25
 
26
26
  # Check 3 different formats
27
27
  FORMATS.each do |format|
28
-
28
+
29
29
  it "should load '#{page}.#{format}' page" do
30
30
  get "/#{page}.#{format}"
31
- last_response.should be_ok
31
+ expect(last_response).to be_ok
32
32
  end
33
33
 
34
34
  end
@@ -39,46 +39,46 @@ describe Nagira do
39
39
 
40
40
  TOP_PAGES.each do |page|
41
41
  context page do
42
-
42
+
43
43
  FORMATS.each do |format|
44
44
 
45
45
  context format do
46
46
 
47
- before do
47
+ before do
48
48
  get "/#{page}.#{format}"
49
49
  @header = last_response.header
50
50
  @body = last_response.body
51
51
  end
52
-
52
+
53
53
  it "Content-type: application/#{format}" do
54
- @header['Content-Type'].should =~ /^application\/#{format}.*/
54
+ expect(@header['Content-Type']).to match(/^application\/#{format}.*/)
55
55
  end
56
56
 
57
57
  it "body should have content" do
58
- @body.should_not be_empty
58
+ expect(@body).to_not be_empty
59
59
  end
60
60
 
61
61
  it "#{format} should be parseable" do
62
62
  case format
63
63
  when 'json'
64
- lambda { JSON.parse @body }.should_not raise_error
64
+ expect { JSON.parse @body }.not_to raise_error
65
65
  when 'xml'
66
- lambda { Hash.from_xml @body }.should_not raise_error
66
+ expect { Hash.from_xml @body }.not_to raise_error
67
67
  when 'yaml'
68
- lambda { YAML.load @body }.should_not raise_error
68
+ expect { YAML.load @body }.not_to raise_error
69
69
  end
70
70
  end
71
71
  end
72
-
72
+
73
73
  end
74
-
75
- context 'default format' do
74
+
75
+ context 'default format' do
76
76
  it "/#{page}.#{Nagira.settings.format} response should be the same as /#{page}" do
77
77
  get "/#{page}.#{Nagira.settings.format}"
78
78
  a = last_response.body
79
- get "/#{page}"
79
+ get "/#{page}"
80
80
  b = last_response.body
81
- a.should eq b
81
+ expect(a).to eq b
82
82
  end
83
83
  end
84
84
 
@@ -88,7 +88,7 @@ describe Nagira do
88
88
  #
89
89
  # GET /config
90
90
  # ----------------------------------------
91
- context "/config" do
91
+ context "/config" do
92
92
 
93
93
  before do
94
94
  get "/_config.json"
@@ -103,15 +103,15 @@ describe Nagira do
103
103
  nagios_user nagios_group }.each do |key|
104
104
 
105
105
  it "attribute #{key} should be a String" do
106
- @data[key].should be_a_kind_of String
107
- end
106
+ expect(@data[key]).to be_a_kind_of String
107
+ end
108
108
  end
109
109
 
110
110
  # Congiration arrays
111
- %w{cfg_file cfg_dir}.each do |key|
111
+ %w{cfg_file cfg_dir}.each do |key|
112
112
  it "attribute #{key} should be an Array" do
113
- @data[key].should be_a_kind_of Array
114
- end
113
+ expect(@data[key]).to be_a_kind_of Array
114
+ end
115
115
  end
116
116
 
117
117
  end
@@ -4,7 +4,7 @@
4
4
 
5
5
  # Check these routes
6
6
  # ----------------------------------------
7
- # get "/config" do
7
+ # get "/config" do
8
8
  # get "/objects" do
9
9
  # get "/objects/:type" do |type|
10
10
  # get "/objects/:type/:name" do |type,name|
@@ -12,42 +12,42 @@
12
12
  # get "/status/:hostname/services" do |hostname|
13
13
  # get "/status/:hostname" do |hostname|
14
14
  # get "/status" do
15
- # get "/api" do
15
+ # get "/api" do
16
16
 
17
17
  require 'spec_helper'
18
18
 
19
19
 
20
- describe Nagira do
20
+ describe Nagira do
21
21
 
22
22
  set :environment, ENV['RACK_ENV'] || :test
23
23
 
24
24
  include Rack::Test::Methods
25
-
25
+
26
26
  def app
27
27
  @app ||= Nagira
28
28
  end
29
29
 
30
-
30
+
31
31
 
32
32
  #
33
33
  # GET /status
34
34
  # -----------------------------
35
35
 
36
- context 'data types' do
36
+ context 'data types' do
37
37
 
38
- {
38
+ {
39
39
  "/_status" => Hash,
40
40
  "/_status/_state" => Hash,
41
41
  "/_status/_list" => Array
42
42
  }.each do |url,klas|
43
-
44
- it "#{url} should return #{klas}" do
43
+
44
+ it "#{url} should return #{klas}" do
45
45
  get "#{url}.json"
46
- JSON.parse(last_response.body).should be_a_kind_of klas
46
+ expect(JSON.parse(last_response.body)).to be_a_kind_of klas
47
47
  end
48
48
 
49
49
  end
50
-
50
+
51
51
 
52
52
  end
53
53
  end
@@ -4,7 +4,7 @@
4
4
 
5
5
  # Check these routes
6
6
  # ----------------------------------------
7
- # get "/config" do
7
+ # get "/config" do
8
8
  # get "/objects" do
9
9
  # get "/objects/:type" do |type|
10
10
  # get "/objects/:type/:name" do |type,name|
@@ -12,16 +12,16 @@
12
12
  # get "/status/:hostname/services" do |hostname|
13
13
  # get "/status/:hostname" do |hostname|
14
14
  # get "/status" do
15
- # get "/api" do
15
+ # get "/api" do
16
16
 
17
17
  require 'spec_helper'
18
18
 
19
- describe Nagira do
19
+ describe Nagira do
20
20
 
21
21
  set :environment, ENV['RACK_ENV'] || :test
22
22
 
23
23
  include Rack::Test::Methods
24
-
24
+
25
25
  def app
26
26
  @app ||= Nagira
27
27
  end
@@ -31,7 +31,7 @@ describe Nagira do
31
31
  # ----------------------------------------
32
32
  context "/objects" do
33
33
 
34
- before :all do
34
+ before :all do
35
35
  get "/_objects"
36
36
  @data = JSON.parse last_response.body
37
37
 
@@ -40,39 +40,39 @@ describe Nagira do
40
40
  # $allkeys = (%w{host service contact timeperiod} + @data.keys).uniq
41
41
  end
42
42
 
43
-
43
+
44
44
  context 'hash key' do
45
45
  %w{host service contact timeperiod}.each do |obj|
46
- it "objects[#{obj}] should exist" do
47
- @data[obj].should be_a_kind_of Hash
46
+ it "objects[#{obj}] should exist" do
47
+ expect(@data[obj]).to be_a_kind_of Hash
48
48
  end
49
49
  end
50
50
  end
51
-
51
+
52
52
  %w{host service contact timeperiod}.each do |obj|
53
53
  context "/_objects/#{obj}" do
54
-
55
- it "should respond to HTTP resuest" do
54
+
55
+ it "should respond to HTTP resuest" do
56
56
  get "/_objects/#{obj}.json"
57
- last_response.should be_ok
57
+ expect(last_response).to be_ok
58
58
  end
59
-
60
- it "response to /objects/#{obj} should be Hash" do
59
+
60
+ it "response to /objects/#{obj} should be Hash" do
61
61
  get "/_objects/#{obj}.json"
62
- JSON.parse(last_response.body).should be_a_kind_of Hash
62
+ expect(JSON.parse(last_response.body)).to be_a_kind_of Hash
63
63
  end
64
64
  end
65
65
  end
66
- end
66
+ end
67
67
  # /objects --------------------
68
-
68
+
69
69
 
70
70
  #
71
71
  # GET /status
72
72
  # -----------------------------
73
73
 
74
- context '/status' do
75
- before :all do
74
+ context '/status' do
75
+ before :all do
76
76
  get "/_status"
77
77
  @data = JSON.parse last_response.body
78
78
 
@@ -82,18 +82,18 @@ describe Nagira do
82
82
  get "/_status/_state"
83
83
  @state = JSON.parse last_response.body
84
84
  end
85
-
86
- context "list of hosts should be the same" do
87
- it "full and state" do
88
- @data.keys.should == @state.keys
85
+
86
+ context "list of hosts should be the same" do
87
+ it "full and state" do
88
+ expect(@data.keys).to eq @state.keys
89
89
  end
90
90
 
91
- it "list and state" do
92
- @list.should == @state.keys
91
+ it "list and state" do
92
+ expect(@list).to eq @state.keys
93
93
  end
94
94
 
95
- it "list and data" do
96
- @list.should == @data.keys
95
+ it "list and data" do
96
+ expect(@list).to eq @data.keys
97
97
  end
98
98
  end
99
99