Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 126 lines (104 sloc) 3.693 kb
66deb3a Working addition, removal and display of checks
Thom May authored
1 require 'excon'
2 require 'nokogiri'
3 require 'hobelar/exceptions'
4 require 'hobelar/parsers'
5
6 class Hobelar
7
8 unless const_defined?(:VERSION)
1998724 Update to 0.0.4
Thom May authored
9 VERSION = "0.0.4"
66deb3a Working addition, removal and display of checks
Thom May authored
10 end
11
12 attr_accessor :noit, :cert, :key
13 attr_reader :connect
14
9b22112 Update to support new Excon options
Gavin Sandie authored
15 def initialize(noit, cert, key, ca_file, opts={})
66deb3a Working addition, removal and display of checks
Thom May authored
16 @noit = noit
17 @cert = cert
18 @key = key
9b22112 Update to support new Excon options
Gavin Sandie authored
19 @ca_file = ca_file
20 Excon.defaults[:ssl_ca_file] = @ca_file
66deb3a Working addition, removal and display of checks
Thom May authored
21 Excon.ssl_verify_peer = false if opts[:no_peer]
4167493 Switch off verification before connecting
Gavin Sandie authored
22 @connect = Excon.new(noit, {:client_cert => @cert, :client_key => @key })
66deb3a Working addition, removal and display of checks
Thom May authored
23 end
24
25 def get_check(uuid, path=nil)
26 p = path ? "/checks/show/#{path}/#{uuid}" : "/checks/show/#{uuid}"
27 request({:method=>"GET", :path=>p, :parser => Hobelar::Parsers::GetCheck.new})
28 end
29
30 def set_check(uuid, attrs, path=nil)
31 p = path ? "/checks/set/#{path}/#{uuid}" : "/checks/set/#{uuid}"
32 if (c = attrs.delete(:config))
a284746 Add the ability to configure config inheritance.
Gavin Sandie authored
33 if c.has_key?(:inherit)
34 config = "<config inherit='#{c[:inherit]}'>"
35 c.delete(:inherit)
36 else
37 config = "<config>"
38 end
66deb3a Working addition, removal and display of checks
Thom May authored
39 c.each_pair do |k,v|
40 key = k.to_s.downcase
41 config += "<#{key}>#{v}</#{key}>"
42 end
43 config += "</config>"
44 end
45 attributes = "<attributes>"
46
47 # set some required attributes, without which noit won't bother responding
48 attrs = {:period=>"60000", :timeout=>"5000", :filterset=>"default"}.merge(attrs)
49
50 attrs.each_pair do |k,v|
51 key = k.to_s.downcase
52 attributes += "<#{key}>#{v}</#{key}>"
53 end
54 attributes += "</attributes>"
55
56 body = "<?xml version=\"1.0\" encoding=\"utf8\"?><check>#{attributes}"
30b920f clean up old debug output and only send config if required
Thom May authored
57 body += config if config
66deb3a Working addition, removal and display of checks
Thom May authored
58 body += "</check>"
59
8b24fed use excon expectations to raise errors correctly
Thom May authored
60 request({:method=>"PUT", :path=>p, :body => body, :parser => Hobelar::Parsers::GetCheck.new, :expects =>200})
66deb3a Working addition, removal and display of checks
Thom May authored
61 end
62
63 def del_check(uuid, path=nil)
64 p = path ? "/checks/delete/#{path}/#{uuid}" : "/checks/delete/#{uuid}"
65 request({:method=>"DELETE", :path=>p})
66 end
67
68 def get_filter(set, path=nil)
022a683 Support filters
Thom May authored
69 p = path ? "/filters/show/#{path}/#{set}" : "/filters/show/#{set}"
70 request({:method=>"GET", :path=>p, :parser => Hobelar::Parsers::GetFilter.new})
66deb3a Working addition, removal and display of checks
Thom May authored
71 end
72
73 def set_filter(set, rules, path=nil)
022a683 Support filters
Thom May authored
74 p = path ? "/filters/set/#{path}/#{set}" : "/filters/set/#{set}"
75 r = ""
76 rules.each do |rule|
77 r += "<rule type=\"#{rule[:type]}\" module=\"#{rule[:module]}\" metric=\"#{rule[:metric]}\" />"
78 end
79
80 body = "<?xml version=\"1.0\" encoding=\"utf8\"?><filterset>#{r}</filterset>"
8b24fed use excon expectations to raise errors correctly
Thom May authored
81 request({:method=>"PUT", :path=>p, :body => body, :parser => Hobelar::Parsers::GetFilter.new, :expects =>200})
66deb3a Working addition, removal and display of checks
Thom May authored
82 end
83
022a683 Support filters
Thom May authored
84 # deleting filters doesn't actually appear to work; the API gives the correct response
85 # but nothing happens
66deb3a Working addition, removal and display of checks
Thom May authored
86 def del_filter(set, path=nil)
022a683 Support filters
Thom May authored
87 p = path ? "/filters/delete/#{path}/#{set}" : "/filters/delete/#{set}"
88 request({:method=>"DELETE", :path=>p})
66deb3a Working addition, removal and display of checks
Thom May authored
89 end
90
91 def request(params, &block)
92
7d6c202 no longer pass a block to exconn's request method
Thom May authored
93 if block_given?
94 params[:response_block] = block
95 else
66deb3a Working addition, removal and display of checks
Thom May authored
96 if (parser = params.delete(:parser))
97 body = Nokogiri::XML::SAX::PushParser.new(parser)
7d6c202 no longer pass a block to exconn's request method
Thom May authored
98 params[:response_block] = lambda { |chunk, remaining, total| body << chunk }
66deb3a Working addition, removal and display of checks
Thom May authored
99 end
100 end
101
8b24fed use excon expectations to raise errors correctly
Thom May authored
102 begin
7d6c202 no longer pass a block to exconn's request method
Thom May authored
103 response = @connect.request(params)
8b24fed use excon expectations to raise errors correctly
Thom May authored
104 rescue Excon::Errors::InternalServerError => error
105 raise Hobelar::InternalServerError, error.response.body
106 end
66deb3a Working addition, removal and display of checks
Thom May authored
107
108 case response.status
109 when 200
110 if parser
111 body.finish
112 response.body = parser.response
113 end
114
115 response
116 when 404
8b24fed use excon expectations to raise errors correctly
Thom May authored
117 raise Hobelar::NotFound, response.body
66deb3a Working addition, removal and display of checks
Thom May authored
118 when 403
8b24fed use excon expectations to raise errors correctly
Thom May authored
119 raise Hobelar::PermissionDenied, response.body
120 when 500
121 raise Hobelar::InternalServerError, response.body
66deb3a Working addition, removal and display of checks
Thom May authored
122 end
123 end
124
125 end
Something went wrong with that request. Please try again.