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.
- data/lib/icinga.rb +2 -1
- data/lib/icinga/{server.rb → client.rb} +16 -13
- data/lib/icinga/object.rb +21 -0
- data/lib/icinga/object/host.rb +4 -0
- data/lib/icinga/object/service.rb +4 -0
- data/lib/icinga/responder.rb +18 -15
- data/lib/icinga/responder/csv.rb +17 -0
- data/lib/icinga/responder/json.rb +9 -0
- data/lib/icinga/version.rb +1 -1
- data/test/icinga/{server_spec.rb → client_spec.rb} +2 -2
- data/test/icinga/object/host_spec.rb +4 -0
- data/test/icinga/object/service_spec.rb +4 -0
- data/test/icinga/object_spec.rb +13 -0
- data/test/icinga/responder/csv_spec.rb +4 -0
- data/test/icinga/responder/json_spec.rb +4 -0
- data/test/icinga/responder_spec.rb +2 -14
- metadata +25 -10
data/lib/icinga.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
require 'net/http'
|
2
2
|
|
3
3
|
module Icinga
|
4
|
-
|
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
|
-
|
15
|
-
|
16
|
+
# @param [Hash] options
|
17
|
+
def initialize(options = {})
|
18
|
+
@options = @@default_options.merge(options)
|
16
19
|
end
|
17
20
|
|
18
|
-
def
|
19
|
-
|
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
|
25
|
-
Responder.
|
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
|
29
|
-
|
29
|
+
def connection
|
30
|
+
@conn ||= Net::HTTP.new(@options[:host], @options[:port])
|
30
31
|
end
|
31
32
|
|
32
|
-
def
|
33
|
-
|
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
|
data/lib/icinga/responder.rb
CHANGED
@@ -1,32 +1,35 @@
|
|
1
|
-
require 'csv'
|
2
|
-
require 'json'
|
3
|
-
|
4
1
|
module Icinga
|
5
2
|
class Responder
|
6
|
-
|
7
|
-
|
8
|
-
|
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 ||= @
|
15
|
+
@response ||= @client.connection.request(@request)
|
14
16
|
rescue Exception => e
|
15
17
|
e
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
19
|
-
def
|
20
|
-
|
21
|
-
|
22
|
-
|
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
|
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
|
data/lib/icinga/version.rb
CHANGED
@@ -2,23 +2,11 @@ require 'icinga'
|
|
2
2
|
|
3
3
|
describe Icinga::Responder do
|
4
4
|
before do
|
5
|
-
@
|
6
|
-
@responder = Icinga::Responder.new(@
|
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.
|
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: &
|
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: *
|
24
|
+
version_requirements: *15337900
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
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: *
|
35
|
+
version_requirements: *15337000
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: json
|
38
|
-
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: *
|
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/
|
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
|