hobelar 0.0.1

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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ # A sample Gemfile
2
+ source "http://rubygems.org"
3
+
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,55 @@
1
+ = hobelar
2
+
3
+ Hobelar talks to {Reconnoiter's}[http://labs.omniti.com/labs/reconnoiter] REST interface.
4
+
5
+ == Getting Started
6
+
7
+ sudo gem install hobelar
8
+
9
+ Noit only supports https connections using a valid client certificate. The REST interface, by default, is on port 43191.
10
+
11
+ require 'rubygems'
12
+ require 'hobelar'
13
+ h = Hobelar.new("https://localhost:43191","client.crt","client.key")
14
+
15
+ Supported operations are #get_check, #set_check and #del_check. All operations take a UUID and an optional path.
16
+ #set_check also takes a hash structure that contains the attributes and config for the check you wish to add.
17
+
18
+ require 'uuid'
19
+ uuid = UUID.new.generate
20
+ attributes = {:config => {:url => "http://test.example.com/", :code=>200},
21
+ :module=>"http",
22
+ :target => "x.x.x.x" }
23
+ h.set_check(uuid, attributes)
24
+
25
+ If you don't have a valid CA, you'll need to disable peer validation when instantiating Hobelar
26
+
27
+ h = Hobelar.new("https://localhost:43191","client.crt","client.key", no_peer: true))
28
+
29
+ == Copyright
30
+
31
+ (The MIT License)
32
+
33
+ Copyright (c) 2010 {Thom May}[http://github.com/thommay]
34
+
35
+ Permission is hereby granted, free of charge, to any person obtaining
36
+ a copy of this software and associated documentation files (the
37
+ "Software"), to deal in the Software without restriction, including
38
+ without limitation the rights to use, copy, modify, merge, publish,
39
+ distribute, sublicense, and/or sell copies of the Software, and to
40
+ permit persons to whom the Software is furnished to do so, subject to
41
+ the following conditions:
42
+
43
+ The above copyright notice and this permission notice shall be
44
+ included in all copies or substantial portions of the Software.
45
+
46
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
47
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
48
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
49
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
50
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
51
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
52
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
53
+
54
+
55
+
data/hobelar.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ Gem::Specification.new do |s|
2
+ s.specification_version = 2 if s.respond_to? :specification_version=
3
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
4
+ s.rubygems_version = '1.3.5'
5
+
6
+ s.name = "hobelar"
7
+ s.version = "0.0.1"
8
+
9
+ s.summary = "reconnoiter rest interface wrapper"
10
+ s.description = "Hobelar talks to reconnoiter's noit rest interface"
11
+
12
+ s.authors = ["Thom May"]
13
+ s.email = "thom@clearairturbulence.org"
14
+ s.homepage = "https://github.com/thommay/hobelar"
15
+
16
+ s.require_paths = %w[lib]
17
+
18
+ ## Specify any RDoc options here. You'll want to add your README and
19
+ ## LICENSE files to the extra_rdoc_files list.
20
+ s.rdoc_options = ["--charset=UTF-8"]
21
+ s.extra_rdoc_files = %w[README.rdoc]
22
+
23
+ ## List your runtime dependencies here. Runtime dependencies are those
24
+ ## that are needed for an end user to actually USE your code.
25
+ s.add_dependency('builder')
26
+ s.add_dependency('excon', '>=0.5.5')
27
+ s.add_dependency('nokogiri', '>=1.4.4')
28
+
29
+ ## List your development dependencies here. Development dependencies are
30
+ ## those that are only needed during development
31
+ s.add_development_dependency('rake')
32
+ s.add_development_dependency('rspec', '1.3.1')
33
+
34
+ s.files = `git ls-files`.split("\n")
35
+ s.test_files = `git ls-files -- {spec,tests}/*`.split("\n")
36
+ end
data/lib/hobelar.rb ADDED
@@ -0,0 +1,100 @@
1
+ require 'excon'
2
+ require 'nokogiri'
3
+ require 'hobelar/exceptions'
4
+ require 'hobelar/parsers'
5
+
6
+ class Hobelar
7
+
8
+ unless const_defined?(:VERSION)
9
+ VERSION = "0.0.1"
10
+ end
11
+
12
+ attr_accessor :noit, :cert, :key
13
+ attr_reader :connect
14
+
15
+ def initialize(noit, cert, key, opts={})
16
+ @noit = noit
17
+ @cert = cert
18
+ @key = key
19
+ @connect = Excon.new(noit, {:client_cert => @cert, :client_key => @key })
20
+ Excon.ssl_verify_peer = false if opts[:no_peer]
21
+ end
22
+
23
+ def get_check(uuid, path=nil)
24
+ p = path ? "/checks/show/#{path}/#{uuid}" : "/checks/show/#{uuid}"
25
+ request({:method=>"GET", :path=>p, :parser => Hobelar::Parsers::GetCheck.new})
26
+ end
27
+
28
+ def set_check(uuid, attrs, path=nil)
29
+ p = path ? "/checks/set/#{path}/#{uuid}" : "/checks/set/#{uuid}"
30
+ if (c = attrs.delete(:config))
31
+ puts c
32
+ config = "<config>"
33
+ c.each_pair do |k,v|
34
+ key = k.to_s.downcase
35
+ config += "<#{key}>#{v}</#{key}>"
36
+ end
37
+ config += "</config>"
38
+ end
39
+ attributes = "<attributes>"
40
+
41
+ # set some required attributes, without which noit won't bother responding
42
+ attrs = {:period=>"60000", :timeout=>"5000", :filterset=>"default"}.merge(attrs)
43
+
44
+ attrs.each_pair do |k,v|
45
+ key = k.to_s.downcase
46
+ attributes += "<#{key}>#{v}</#{key}>"
47
+ end
48
+ attributes += "</attributes>"
49
+
50
+ body = "<?xml version=\"1.0\" encoding=\"utf8\"?><check>#{attributes}"
51
+ body += config.nil? ? "<config/>" : config
52
+ body += "</check>"
53
+
54
+ request({:method=>"PUT", :path=>p, :body => body, :parser => Hobelar::Parsers::GetCheck.new})
55
+ end
56
+
57
+ def del_check(uuid, path=nil)
58
+ p = path ? "/checks/delete/#{path}/#{uuid}" : "/checks/delete/#{uuid}"
59
+ request({:method=>"DELETE", :path=>p})
60
+ end
61
+
62
+ def get_filter(set, path=nil)
63
+
64
+ end
65
+
66
+ def set_filter(set, rules, path=nil)
67
+
68
+ end
69
+
70
+ def del_filter(set, path=nil)
71
+
72
+ end
73
+
74
+ def request(params, &block)
75
+
76
+ unless block_given?
77
+ if (parser = params.delete(:parser))
78
+ body = Nokogiri::XML::SAX::PushParser.new(parser)
79
+ block = lambda { |chunk| body << chunk }
80
+ end
81
+ end
82
+
83
+ response = @connect.request(params, &block)
84
+
85
+ case response.status
86
+ when 200
87
+ if parser
88
+ body.finish
89
+ response.body = parser.response
90
+ end
91
+
92
+ response
93
+ when 404
94
+ raise Hobelar::NotFound
95
+ when 403
96
+ raise Hobelar::PermissionDenied
97
+ end
98
+ end
99
+
100
+ end
File without changes
@@ -0,0 +1,5 @@
1
+ class Hobelar
2
+ class NotFound < RuntimeError; end
3
+
4
+ class PermissionDenied < RuntimeError; end
5
+ end
@@ -0,0 +1,2 @@
1
+ require 'nokogiri/xml/sax/document'
2
+ require 'hobelar/parsers/getcheck.rb'
@@ -0,0 +1,104 @@
1
+ class Hobelar
2
+ class Parsers
3
+ class GetCheck < Nokogiri::XML::SAX::Document
4
+
5
+ attr_reader :response
6
+
7
+ def initialize
8
+ @response = {}
9
+ @response[:attributes] = {}
10
+ @response[:config] = {}
11
+ @response[:state] = {}
12
+ @response[:state][:metrics] = {}
13
+ end
14
+
15
+ def characters(string)
16
+ @value ||= ''
17
+ @value << string.strip
18
+ end
19
+
20
+ def attr_value(name, attrs)
21
+ (entry = attrs.detect {|a,v| a == name }) && entry.last
22
+ end
23
+
24
+ def start_element(name, attrs = [])
25
+ @value = nil
26
+
27
+ case name
28
+ when "attributes"
29
+ @in_attrs = true
30
+ when "config"
31
+ @in_config = true
32
+ when "state"
33
+ # the state set contains an element called state. This is unhelpful.
34
+ if @in_state
35
+ @bloody_state = true
36
+ else
37
+ @in_state = true
38
+ end
39
+ when "metrics"
40
+ @in_metrics = true
41
+ when "module","period","timeout","filterset"
42
+ @inherited = attr_value("inherited", attrs)
43
+ when "last_run"
44
+ begin
45
+ @time_now = DateTime.strptime(attr_value("now", attrs), "%s")
46
+ rescue
47
+ @time_now = nil
48
+ end
49
+ when "metric"
50
+ @m_name = attr_value("name", attrs)
51
+ @m_type = attr_value("type", attrs)
52
+ end
53
+ end
54
+
55
+ def end_element(name)
56
+ case name
57
+ when "attributes"
58
+ @in_attrs = false
59
+ when "config"
60
+ @in_config = false
61
+ when "state"
62
+ if @bloody_state
63
+ @bloody_state = false
64
+ @response[:state][:state] = @value
65
+ else
66
+ @in_state = false
67
+ end
68
+ when "metrics"
69
+ @in_metrics = false
70
+ when "uuid"
71
+ @response[:uuid] = @value if @in_attrs
72
+ when "name"
73
+ @response[:name] = @value if @in_attrs
74
+ when "module"
75
+ @response[:module] = @value if @in_attrs
76
+ when "target"
77
+ @response[:target] = @value if @in_attrs
78
+ when "last_run"
79
+ @response[:now] = @time_now
80
+ begin
81
+ @response[:last_run] = DateTime.strptime(@value, "%s")
82
+ rescue
83
+ @response[:last_run] = nil
84
+ end
85
+ else
86
+ if @in_attrs
87
+ @response[:attributes][name.to_sym] = @value
88
+ elsif @in_config
89
+ @response[:config][name.to_sym] = @value
90
+ elsif @in_state && @in_metrics
91
+ if @m_name
92
+ @response[:state][:metrics][@m_name.to_sym] = @value
93
+ else
94
+ @response[:state][:metrics][name.to_sym] = @value
95
+ end
96
+ @m_name = nil
97
+ elsif @in_state
98
+ @response[:state][name.to_sym] = @value
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hobelar
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Thom May
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-28 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: builder
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: excon
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: 0.5.5
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: nokogiri
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 1.4.4
47
+ type: :runtime
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: rake
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ type: :development
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: rspec
62
+ prerelease: false
63
+ requirement: &id005 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - "="
67
+ - !ruby/object:Gem::Version
68
+ version: 1.3.1
69
+ type: :development
70
+ version_requirements: *id005
71
+ description: Hobelar talks to reconnoiter's noit rest interface
72
+ email: thom@clearairturbulence.org
73
+ executables: []
74
+
75
+ extensions: []
76
+
77
+ extra_rdoc_files:
78
+ - README.rdoc
79
+ files:
80
+ - Gemfile
81
+ - README.rdoc
82
+ - hobelar.gemspec
83
+ - lib/hobelar.rb
84
+ - lib/hobelar/core.rb
85
+ - lib/hobelar/exceptions.rb
86
+ - lib/hobelar/parsers.rb
87
+ - lib/hobelar/parsers/getcheck.rb
88
+ has_rdoc: true
89
+ homepage: https://github.com/thommay/hobelar
90
+ licenses: []
91
+
92
+ post_install_message:
93
+ rdoc_options:
94
+ - --charset=UTF-8
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: "0"
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: "0"
109
+ requirements: []
110
+
111
+ rubyforge_project:
112
+ rubygems_version: 1.6.0
113
+ signing_key:
114
+ specification_version: 2
115
+ summary: reconnoiter rest interface wrapper
116
+ test_files: []
117
+