logstash/bin/search.rb
Pete Fritchman e909d241ec - less verbose
- make @close work
- main @id queue cannot be :exclusive (nobody else can write)
- properly make topic exchange auto_delete and exclusive
- refactor search.rb client, start towards a common SearchClient class
  (and work with amqp)
2009-10-19 05:52:18 +00:00

95 lines
2 KiB
Ruby
Executable file

#!/usr/bin/ruby
#
require 'rubygems'
require "socket"
require "lib/net/message"
require "lib/net/client"
require "lib/net/messages/directory"
require "lib/net/messages/indexevent"
require "lib/net/messages/search"
require "lib/net/messages/searchhits"
require "lib/net/messages/ping"
require "set"
Thread::abort_on_exception = true
$done = false
$lastid = nil
$count = 0
$time = 0
$start = Time.now.to_f
class SearchClient < LogStash::Net::MessageClient
attr_accessor :indexers
attr_reader :hits
attr_reader :responding
attr_reader :results
def initialize(opts)
@log_type = opts[:log_type]
@query = opts[:query]
@indexers = Array.new
@responding = Array.new
@hits = 0
@results = []
@result_mutex = Mutex.new
super(opts)
start
end
def start
# find our indexers
msg = LogStash::Net::Messages::DirectoryRequest.new
sendmsg("logstash-directory", msg)
run
end
def SearchResponseHandler(msg)
@result_mutex.synchronize do
msg.results.each do |result|
@results << result
end
if msg.finished
@responding << msg.indexer_id
if @responding.length == @indexers.length
close
end
end
end
end
def SearchHitsResponseHandler(msg)
@result_mutex.synchronize do
@hits += msg.hits
end
end
def DirectoryResponseHandler(msg)
hits_msg = LogStash::Net::Messages::SearchHitsRequest.new
hits_msg.log_type = @log_type
hits_msg.query = @query
search_msg = LogStash::Net::Messages::SearchRequest.new
search_msg.log_type = @log_type
search_msg.query = @query
@indexers = msg.indexers
@indexers.each do |i|
sendmsg(i, hits_msg)
sendmsg(i, search_msg)
end
end
end
def main(args)
client = SearchClient.new(:log_type => args[0], :query => args[1])
# Collate & print results.
puts "Hits: #{client.hits}"
puts ""
puts client.results.join("\n")
return 0
end
if $0 == __FILE__
exit main(ARGV)
end