Skip to content

Instantly share code, notes, and snippets.

@oholiab
Created June 25, 2012 09:01
Show Gist options
  • Save oholiab/2987501 to your computer and use it in GitHub Desktop.
Save oholiab/2987501 to your computer and use it in GitHub Desktop.
reqid stat
def req(body, agent=nil, options=false, waitfor=0)
if body.is_a?(Message)
agent = body.agent
options = body.options
waitfor = body.discovered_hosts.size || 0
end
stat = {:starttime => Time.now.to_f, :discoverytime => 0, :blocktime => 0, :totaltime => 0}
options = @options unless options
compount_timeout = timeout_for_compound_filter(options[:filter]["compound"])
timeout = options[:timeout] + compount_timeout
STDOUT.sync = true
hosts_responded = 0
begin
Timeout.timeout(timeout) do
reqid = sendreq(body, agent, options[:filter])
loop do
resp = receive(reqid)
hosts_responded += 1
yield(resp.payload)
break if (waitfor != 0 && hosts_responded >= waitfor)
end
stat[:reqid] = reqid
end
rescue Interrupt => e
rescue Timeout::Error => e
ensure
unsubscribe(agent, :reply)
end
stat[:totaltime] = Time.now.to_f - stat[:starttime]
stat[:blocktime] = stat[:totaltime] - stat[:discoverytime]
stat[:responses] = hosts_responded
stat[:noresponsefrom] = []
@stats = stat
return stat
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment