icinga 0.1.7 → 0.1.8

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.
@@ -1,6 +1,7 @@
1
1
  require "icinga/version"
2
- require "icinga/server"
2
+ require "icinga/client"
3
3
  require "icinga/responder"
4
+ require "icinga/object"
4
5
 
5
6
  module Icinga
6
7
  end
@@ -1,7 +1,9 @@
1
1
  require 'net/http'
2
2
 
3
3
  module Icinga
4
- class Server
4
+
5
+ # Client connection to Icinga server
6
+ class Client
5
7
  attr_accessor :options
6
8
 
7
9
  @@default_options = {
@@ -11,26 +13,27 @@ module Icinga
11
13
  :format => "json",
12
14
  }
13
15
 
14
- def connection
15
- @conn ||= Net::HTTP.new(@options[:host], @options[:port])
16
+ # @param [Hash] options
17
+ def initialize(options = {})
18
+ @options = @@default_options.merge(options)
16
19
  end
17
20
 
18
- def new_request(path)
19
- req = Net::HTTP::Get.new(@options[:remote_path] + path + "&" + @options[:format] + "output")
20
- req.basic_auth @options[:user], @options[:password] if @options[:user]
21
- req
21
+ def hosts
22
+ Responder.create(Host, self, new_request("?hostgroup=all&style=hostdetail"))
22
23
  end
23
24
 
24
- def hosts
25
- Responder.new(self, new_request("?hostgroup=all&style=hostdetail"))
25
+ def services(host=nil)
26
+ Responder.create(Service, self, new_request("?host=#{host.nil? ? "all" : host}&style=servicedetail"))
26
27
  end
27
28
 
28
- def services
29
- Responder.new(self, new_request("?host=all&style=servicedetail"))
29
+ def connection
30
+ @conn ||= Net::HTTP.new(@options[:host], @options[:port])
30
31
  end
31
32
 
32
- def initialize(options = {})
33
- @options = @@default_options.merge(options)
33
+ def new_request(path)
34
+ req = Net::HTTP::Get.new(@options[:remote_path] + path + "&" + @options[:format] + "output")
35
+ req.basic_auth @options[:user], @options[:password] if @options[:user]
36
+ req
34
37
  end
35
38
  end
36
39
  end
@@ -0,0 +1,21 @@
1
+ module Icinga
2
+ class Object < Hash
3
+ require 'icinga/object/host'
4
+ require 'icinga/object/service'
5
+
6
+ # @param [Hash] data
7
+ def initialize(data)
8
+ merge!(data)
9
+ name_field = self.class.to_s.split("::").last.downcase
10
+ self["name"] = data[name_field]
11
+ end
12
+
13
+ def method_missing(method, *args, &block)
14
+ self[method.to_s]
15
+ end
16
+
17
+ def to_s
18
+ self["name"]
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,4 @@
1
+ module Icinga
2
+ class Host < Object
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Icinga
2
+ class Service < Object
3
+ end
4
+ end
@@ -1,32 +1,35 @@
1
- require 'csv'
2
- require 'json'
3
-
4
1
  module Icinga
5
2
  class Responder
6
- def initialize(server, request)
7
- @server = server
8
- @request = request
3
+ attr_reader :data
4
+
5
+ require 'icinga/responder/json'
6
+ require 'icinga/responder/csv'
7
+
8
+ def self.create(klass, client, request)
9
+ responder_klass = Icinga.const_get("#{client.options[:format].upcase}Responder")
10
+ responder_klass.new(klass, client, request)
9
11
  end
10
12
 
11
13
  def response
12
14
  begin
13
- @response ||= @server.connection.request(@request)
15
+ @response ||= @client.connection.request(@request)
14
16
  rescue Exception => e
15
17
  e
16
18
  end
17
19
  end
18
20
 
19
- def data
20
- if response.kind_of? Net::HTTPSuccess
21
- {:data => JSON.parse(response)} if @server.options[:format] == "json"
22
- {:data => CSV.parse(response)} if @server.options[:format] == "csv"
23
- else
24
- {:error => response.message}
25
- end
21
+ def initialize(klass, client, request)
22
+ @klass = klass
23
+ @client = client
24
+ @request = request
26
25
  end
27
26
 
28
27
  def to_s
29
- data[:data] || ""
28
+ data.to_s
29
+ end
30
+
31
+ def each
32
+ data.each {|item| yield(@klass.new(item))}
30
33
  end
31
34
  end
32
35
  end
@@ -0,0 +1,17 @@
1
+ require 'csv'
2
+
3
+ module Icinga
4
+ class CSVResponder < Responder
5
+ def data
6
+ parse_options = {:headers => :first_row, :col_sep => ";", :quote_char => "'"}
7
+
8
+ CSV.parse(response.body.strip, parse_options).collect do |row|
9
+ new_hash = {}
10
+ row.to_hash.each_pair do |k,v|
11
+ new_hash.merge!({k.downcase => v})
12
+ end
13
+ new_hash
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ require 'json'
2
+
3
+ module Icinga
4
+ class JSONResponder < Responder
5
+ def data
6
+ JSON.parse(response.body)["status"].first.last
7
+ end
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module Icinga
2
- VERSION = "0.1.7"
2
+ VERSION = "0.1.8"
3
3
  end
@@ -1,8 +1,8 @@
1
1
  require 'icinga'
2
2
 
3
- describe Icinga::Server do
3
+ describe Icinga::Client do
4
4
  before do
5
- @icinga = Icinga::Server.new
5
+ @icinga = Icinga::Client.new
6
6
  end
7
7
 
8
8
  describe "when initialized" do
@@ -0,0 +1,4 @@
1
+ require 'icinga'
2
+
3
+ describe Icinga::Host do
4
+ end
@@ -0,0 +1,4 @@
1
+ require 'icinga'
2
+
3
+ describe Icinga::Service do
4
+ end
@@ -0,0 +1,13 @@
1
+ require 'icinga'
2
+
3
+ describe Icinga::Object do
4
+ before do
5
+ @object = Icinga::Object.new({"object"=>"name"})
6
+ end
7
+
8
+ describe "when created" do
9
+ it "should populate name" do
10
+ @object.name.should match(/^name$/)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,4 @@
1
+ require 'icinga'
2
+
3
+ describe Icinga::CSVResponder do
4
+ end
@@ -0,0 +1,4 @@
1
+ require 'icinga'
2
+
3
+ describe Icinga::JSONResponder do
4
+ end
@@ -2,23 +2,11 @@ require 'icinga'
2
2
 
3
3
  describe Icinga::Responder do
4
4
  before do
5
- @server = Icinga::Server.new
6
- @responder = Icinga::Responder.new(@server, @server.new_request(""))
5
+ @client = Icinga::Client.new
6
+ @responder = Icinga::Responder.new(Icinga::Object, @client, @client.new_request(""))
7
7
  end
8
8
 
9
9
  it "should create a response" do
10
10
  @responder.response.should respond_to(:message)
11
11
  end
12
-
13
- it "should respond to data with parsed data" do
14
- @responder.data.should be_kind_of(Hash)
15
- end
16
-
17
- it "should respond to data with error info" do
18
- @responder.data[:error].should be_kind_of(String)
19
- end
20
-
21
- it "should respond with empty string for bad connection/request" do
22
- @responder.to_s.should match(/^$/)
23
- end
24
12
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: icinga
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-12-07 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &13055080 !ruby/object:Gem::Requirement
16
+ requirement: &15337900 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *13055080
24
+ version_requirements: *15337900
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &13054220 !ruby/object:Gem::Requirement
27
+ requirement: &15337000 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *13054220
35
+ version_requirements: *15337000
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: json
38
- requirement: &13053380 !ruby/object:Gem::Requirement
38
+ requirement: &15336140 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *13053380
46
+ version_requirements: *15336140
47
47
  description: Facilitates communication with Icinga servers.
48
48
  email:
49
49
  - jbussdieker@gmail.com
@@ -58,11 +58,21 @@ files:
58
58
  - Rakefile
59
59
  - icinga.gemspec
60
60
  - lib/icinga.rb
61
+ - lib/icinga/client.rb
62
+ - lib/icinga/object.rb
63
+ - lib/icinga/object/host.rb
64
+ - lib/icinga/object/service.rb
61
65
  - lib/icinga/responder.rb
62
- - lib/icinga/server.rb
66
+ - lib/icinga/responder/csv.rb
67
+ - lib/icinga/responder/json.rb
63
68
  - lib/icinga/version.rb
69
+ - test/icinga/client_spec.rb
70
+ - test/icinga/object/host_spec.rb
71
+ - test/icinga/object/service_spec.rb
72
+ - test/icinga/object_spec.rb
73
+ - test/icinga/responder/csv_spec.rb
74
+ - test/icinga/responder/json_spec.rb
64
75
  - test/icinga/responder_spec.rb
65
- - test/icinga/server_spec.rb
66
76
  - test/icinga_spec.rb
67
77
  homepage: http://github.com/jbussdieker/icinga
68
78
  licenses: []
@@ -89,6 +99,11 @@ signing_key:
89
99
  specification_version: 3
90
100
  summary: Icinga API tools
91
101
  test_files:
102
+ - test/icinga/client_spec.rb
103
+ - test/icinga/object/host_spec.rb
104
+ - test/icinga/object/service_spec.rb
105
+ - test/icinga/object_spec.rb
106
+ - test/icinga/responder/csv_spec.rb
107
+ - test/icinga/responder/json_spec.rb
92
108
  - test/icinga/responder_spec.rb
93
- - test/icinga/server_spec.rb
94
109
  - test/icinga_spec.rb