Class: Livestatus::UnixHandler
- Inherits:
-
Object
- Object
- Livestatus::UnixHandler
- Defined in:
- lib/livestatus/handler/unix.rb
Instance Method Summary collapse
- #command(cmd, time = nil) ⇒ Object
- #get(model, options = {}) ⇒ Object
-
#initialize(connection, config) ⇒ UnixHandler
constructor
A new instance of UnixHandler.
Constructor Details
#initialize(connection, config) ⇒ UnixHandler
Returns a new instance of UnixHandler.
8 9 10 11 |
# File 'lib/livestatus/handler/unix.rb', line 8 def initialize(connection, config) @connection = connection @socket = UNIXSocket.open(config[:uri].sub(/^unix:\/\//, '')) end |
Instance Method Details
#command(cmd, time = nil) ⇒ Object
54 55 56 57 58 |
# File 'lib/livestatus/handler/unix.rb', line 54 def command(cmd, time = nil) time = Time.now.to_i unless time @socket.write("COMMAND [#{time}] #{cmd}\n\n") nil end |
#get(model, options = {}) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 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 51 52 |
# File 'lib/livestatus/handler/unix.rb', line 13 def get(model, = {}) .merge!({ :response_header => "fixed16", :output_format => "json", :keep_alive => "on", }) headers = .map do |k,v| if v.is_a?(Array) v.map do |e| "#{k.to_s.camelize}: #{e}" end else "#{k.to_s.camelize}: #{v}" end end.flatten.join("\n") headers += "\n" unless headers.empty? @socket.write("GET #{model.table_name}\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 data = Yajl::Parser.new.parse(@socket.read(length)) if .include?(:columns) columns = [:columns].split(" ") else columns = data.delete_at(0) end column_zip(columns, data).map do |d| model.new(d, @connection) end end |