Class: Sabisu::Server::Sensu

Inherits:
Object
  • Object
show all
Defined in:
lib/sabisu/sensu.rb

Overview

sensu passthrough api

Instance Method Summary collapse

Instance Method Details

#request(opts) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sabisu/sensu.rb', line 12

def request(opts)
  http = Net::HTTP.new(API_URL, API_PORT)
  http.read_timeout = 15
  http.open_timeout = 5
  if API_SSL == true
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  proxy_header = { 'api-proxy' => 'true' }
  case opts[:method].upcase
  when 'GET'
    req =  Net::HTTP::Get.new(opts[:path], proxy_header)
  when 'DELETE'
    req =  Net::HTTP::Delete.new(opts[:path], proxy_header)
  when 'POST'
    req =  Net::HTTP::Post.new(
      opts[:path],
      proxy_header.merge!('Content-Type' => 'application/json')
    )
    req.body = opts[:payload].to_json
  end
  req.basic_auth(API_USER, API_PASSWORD) unless API_USER.nil? && API_PASSWORD.nil?
  http.request(req)
end

#response(code, body, command = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sabisu/sensu.rb', line 37

def response(code, body, command = nil)
  case code
  when '200'
    JSON.parse(body)
  when '201'
    puts 'The stash has been created.' if command == 'stashes' || command == 'silence'
  when '202'
    puts 'The item was submitted for processing.'
  when '204'
    puts 'Sensu is healthy' if command == 'health'
    if command == 'aggregates' || command == 'stashes'
      puts 'The item was successfully deleted.'
    end
  when '400'
    puts 'The payload is malformed.'.color(:red)
  when '401'
    puts 'The request requires user authentication.'.color(:red)
  when '404'
    puts 'The item does not exist.'.color(:cyan)
  else
    if command == 'health'
      puts 'Sensu is not healthy.'.color(:red)
    else
      puts 'There was an error while trying to complete your request. ' +
           "Response code: #{code}".color(:red)
    end
  end
end