Class: Livestatus::UnixHandler

Inherits:
BaseHandler show all
Defined in:
lib/livestatus/handler/unix.rb

Instance Method Summary collapse

Methods inherited from BaseHandler

#command

Constructor Details

#initialize(config) ⇒ UnixHandler

Returns a new instance of UnixHandler.



8
9
10
# File 'lib/livestatus/handler/unix.rb', line 8

def initialize(config)
  @socket = UNIXSocket.open(config[:uri].sub(/^unix:\/\//, ''))
end

Instance Method Details

#get(table_name, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/livestatus/handler/unix.rb', line 12

def get(table_name, options = {})
  data = super

  if options.include?(:columns)
    columns = options[:columns].split(" ")
  else
    columns = data.delete_at(0)
  end

  column_zip(columns, data)
end

#query(method, query, headers = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/livestatus/handler/unix.rb', line 24

def query(method, query, headers = {})
  headers.merge!({
    :response_header => "fixed16",
    :output_format => "json",
    :keep_alive => "on",
  })

  headers = headers.map { |k,v| "#{k.to_s.camelize}: #{v}" }.join("\n")
  headers += "\n" unless headers.empty?

  case method
  when :get
    @socket.write("#{method.to_s.upcase} #{query}\n#{headers}\n")

    res = @socket.read(16)
    status, length = res[0..2].to_i, res[4..14].chomp.to_i

    unless status == 200
      raise HandlerException, "livestatus query failed with status #{status}"
    end

    Yajl::Parser.new.parse(@socket.read(length))
  when :command
    @socket.write("#{method.to_s.upcase} #{query}\n\n")
    nil
  end
end