class Malevich::Responder::Http

Attributes

name[R]

Public Class Methods

new() click to toggle source
# 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

Public Instance Methods

info() click to toggle source
# 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!() click to toggle source
# 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