Class: Hobelar

Inherits:
Object
  • Object
show all
Defined in:
lib/hobelar.rb,
lib/hobelar/parsers.rb,
lib/hobelar/exceptions.rb,
lib/hobelar/parsers/getcheck.rb,
lib/hobelar/parsers/getfilter.rb

Defined Under Namespace

Classes: NotFound, Parsers, PermissionDenied

Constant Summary collapse

VERSION =
"0.0.4"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(noit, cert, key, opts = {}) ⇒ Hobelar

Returns a new instance of Hobelar.



15
16
17
18
19
20
21
# File 'lib/hobelar.rb', line 15

def initialize(noit, cert, key, opts={})
  @noit = noit
  @cert = cert
  @key = key
  @connect = Excon.new(noit, {:client_cert => @cert, :client_key => @key })
  Excon.ssl_verify_peer = false if opts[:no_peer]
end

Instance Attribute Details

#certObject

Returns the value of attribute cert.



12
13
14
# File 'lib/hobelar.rb', line 12

def cert
  @cert
end

#connectObject (readonly)

Returns the value of attribute connect.



13
14
15
# File 'lib/hobelar.rb', line 13

def connect
  @connect
end

#keyObject

Returns the value of attribute key.



12
13
14
# File 'lib/hobelar.rb', line 12

def key
  @key
end

#noitObject

Returns the value of attribute noit.



12
13
14
# File 'lib/hobelar.rb', line 12

def noit
  @noit
end

Instance Method Details

#del_check(uuid, path = nil) ⇒ Object



56
57
58
59
# File 'lib/hobelar.rb', line 56

def del_check(uuid, path=nil)
  p = path ? "/checks/delete/#{path}/#{uuid}" : "/checks/delete/#{uuid}" 
  request({:method=>"DELETE", :path=>p})
end

#del_filter(set, path = nil) ⇒ Object

deleting filters doesn’t actually appear to work; the API gives the correct response but nothing happens



79
80
81
82
# File 'lib/hobelar.rb', line 79

def del_filter(set, path=nil)
  p = path ? "/filters/delete/#{path}/#{set}" : "/filters/delete/#{set}" 
  request({:method=>"DELETE", :path=>p})
end

#get_check(uuid, path = nil) ⇒ Object



23
24
25
26
# File 'lib/hobelar.rb', line 23

def get_check(uuid, path=nil)
  p = path ? "/checks/show/#{path}/#{uuid}" : "/checks/show/#{uuid}" 
  request({:method=>"GET", :path=>p, :parser => Hobelar::Parsers::GetCheck.new})
end

#get_filter(set, path = nil) ⇒ Object



61
62
63
64
# File 'lib/hobelar.rb', line 61

def get_filter(set, path=nil)
  p = path ? "/filters/show/#{path}/#{set}" : "/filters/show/#{set}" 
  request({:method=>"GET", :path=>p, :parser => Hobelar::Parsers::GetFilter.new})
end

#request(params, &block) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/hobelar.rb', line 84

def request(params, &block)

  unless block_given?
    if (parser = params.delete(:parser))
      body = Nokogiri::XML::SAX::PushParser.new(parser)
      block = lambda { |chunk, remaining, total| body << chunk }
    end
  end

  response = @connect.request(params, &block)
  
  case response.status
  when 200
    if parser
      body.finish
      response.body = parser.response
    end

    response
  when 404
    raise Hobelar::NotFound
  when 403
    raise Hobelar::PermissionDenied
  end
end

#set_check(uuid, attrs, path = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/hobelar.rb', line 28

def set_check(uuid, attrs, path=nil)
  p = path ? "/checks/set/#{path}/#{uuid}" : "/checks/set/#{uuid}" 
  if (c = attrs.delete(:config))
    config = "<config>"
    c.each_pair do |k,v|
      key = k.to_s.downcase
      config += "<#{key}>#{v}</#{key}>"
    end
    config += "</config>"
  end
  attributes = "<attributes>"

  # set some required attributes, without which noit won't bother responding
  attrs = {:period=>"60000", :timeout=>"5000", :filterset=>"default"}.merge(attrs)

  attrs.each_pair do |k,v|
    key = k.to_s.downcase
    attributes += "<#{key}>#{v}</#{key}>"
  end
  attributes += "</attributes>"

  body = "<?xml version=\"1.0\" encoding=\"utf8\"?><check>#{attributes}"
  body += config if config
  body += "</check>"

  request({:method=>"PUT", :path=>p, :body => body, :parser => Hobelar::Parsers::GetCheck.new})
end

#set_filter(set, rules, path = nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/hobelar.rb', line 66

def set_filter(set, rules, path=nil)
  p = path ? "/filters/set/#{path}/#{set}" : "/filters/set/#{set}" 
  r = ""
  rules.each do |rule|
    r += "<rule type=\"#{rule[:type]}\" module=\"#{rule[:module]}\" metric=\"#{rule[:metric]}\" />"
  end

  body = "<?xml version=\"1.0\" encoding=\"utf8\"?><filterset>#{r}</filterset>"
  request({:method=>"PUT", :path=>p, :body => body, :parser => Hobelar::Parsers::GetFilter.new})
end