2 # -*- encoding: binary -*-
3 $stdout.sync = $stderr.sync = true
4 # this is used to show or watch the number of active and queued
5 # connections on any listener socket from the command line
14 $stderr.puts "Aggregate missing, USR1 and USR2 handlers unavailable"
17 usage = "Usage: #$0 [-d DELAY] [-t QUEUED_THRESHOLD] ADDR..."
18 ARGV.size > 0 or abort usage
21 # "normal" exits when driven on the command-line
22 trap(:INT) { exit 130 }
23 trap(:PIPE) { exit 0 }
25 agg_active = agg_queued = nil
26 if defined?(Aggregate)
27 agg_active = Aggregate.new
28 agg_queued = Aggregate.new
30 def dump_aggregate(label, agg)
31 $stderr.write "--- #{label} ---\n"
32 %w(count min max outliers_low outliers_high mean std_dev).each do |f|
33 $stderr.write "#{f}=#{agg.__send__ f}\n"
35 $stderr.write "#{agg}\n\n"
39 dump_aggregate "active", agg_active
40 dump_aggregate "queued", agg_queued
43 agg_active = Aggregate.new
44 agg_queued = Aggregate.new
46 $stderr.puts "USR1(dump_aggregate) and USR2(reset) handlers ready for PID=#$$"
49 opts = OptionParser.new('', 24, ' ') do |opts|
51 opts.on('-d', '--delay=DELAY', Float) { |n| delay = n }
52 opts.on('-t', '--queued-threshold=INT', Integer) { |n| queued_thresh = n }
57 addr =~ %r{\A(127\..+):(\d+)\z} or next
59 hex_port = '%X' % port.to_i
60 ip_addr = IPAddr.new(host)
61 hex_host = ip_addr.hton.each_byte.inject('') { |s,o| s << '%02X' % o }
62 socks = File.readlines('/proc/net/tcp')
63 hex_addr = "#{hex_host}:#{hex_port}"
64 if socks.grep(/^\s+\d+:\s+#{hex_addr}\s+/).empty? &&
65 ! socks.grep(/^\s+\d+:\s+00000000:#{hex_port}\s+/).empty?
66 warn "W: #{host}:#{port} (#{hex_addr}) not found in /proc/net/tcp"
67 warn "W: Did you mean 0.0.0.0:#{port}?"
75 bs = addr.respond_to?(:bytesize) ? addr.bytesize : addr.size
77 (addr =~ %r{\A/} ? unix : tcp) << addr
80 tcp = nil if tcp.empty?
81 unix = nil if unix.empty?
84 fmt = "%20s % #{len}s % 10u % 10u\n"
85 $stderr.printf fmt.tr('u','s'), *%w(timestamp address active queued)
92 tcp and combined.merge! Raindrops::Linux.tcp_listener_stats(tcp)
93 unix and combined.merge! Raindrops::Linux.unix_listener_stats(unix)
94 combined.each do |addr,stats|
95 active, queued = stats.active, stats.queued
100 next if queued < queued_thresh
101 printf fmt, now ||= Time.now.utc.iso8601, addr, active, queued
103 end while delay && sleep(delay)