Class: Malevich::Responder::Http

Inherits:
Object
  • Object
show all
Includes:
Mixin
Defined in:
lib/malevich/responders/http.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixin

#log, #malevich

Constructor Details

#initializeHttp

Returns a new instance of Http.



9
10
11
12
13
# File 'lib/malevich/responders/http.rb', line 9

def initialize
  @host, @port = malevich.cmd[:'http-responder'].split(':')
  @started_at = Time.now.to_i
  @name = 'http api'
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/malevich/responders/http.rb', line 7

def name
  @name
end

Instance Method Details

#infoObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/malevich/responders/http.rb', line 33

def info
  {
      :cmd => malevich.cmd,
      :config => malevich.config,
      :plugins => malevich.monitor.plugins,
      :monitor => malevich.monitor.tasks.map { |x| {x[1].name => x[0].alive?} },
      :errors => (malevich.plugins.errors rescue {}),
      :histories => (malevich.plugins.histories rescue {}),
      :version => Malevich::VERSION,
      :ruby => "#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}",
      :uptime => Time.now.to_i - @started_at,
      :queue_size => malevich.riemann_events.size
  }.to_json + "\n"
end

#run!Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/malevich/responders/http.rb', line 15

def run!
  log :unknown, "Start http server at #{@host}:#{@port}"
  server = TCPServer.new(@host, @port)
  loop do
    client = server.accept
    log :unknown, "Accepted client: #{client.inspect}"

    response = info
    headers = "HTTP/1.1 200 OK\r\n" +
        "Server: Malevich Ruby\r\n" +
        "Content-Length: #{response.bytesize}\r\n" +
        "Content-Type: application/json\r\n\r\n"
    client.print headers
    client.print response
    client.close
  end
end