about summary refs log tree commit homepage
path: root/examples
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-07-01 19:25:57 -0700
committerEric Wong <normalperson@yhbt.net>2010-07-01 19:29:41 -0700
commit3f7f59ff522bf322383f1bebaa18a979608804f7 (patch)
tree91a13993e7b98db65edbae79dc1a746aef4678a8 /examples
parentbb24816e1291bbbb26065af3fb5bd76a4cbb0c95 (diff)
downloadraindrops-3f7f59ff522bf322383f1bebaa18a979608804f7.tar.gz
It is a likely mistake to specify a local address such as
"127.0.0.1:#{port}" as an argument to linux-tcp-listener-stats
when our listener is listening on all addresses
("0.0.0.0:#{port}").

So make an effort to detect this mistake as all of our known
users on our mailing list seem to make it:

  http://mid.gmane.org/AANLkTinP5fBfcRa6Y3-MrMzDqjRRrgEOIunvn_h7q2WS@mail.gmail.com
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/linux-tcp-listener-stats.rb16
1 files changed, 16 insertions, 0 deletions
diff --git a/examples/linux-tcp-listener-stats.rb b/examples/linux-tcp-listener-stats.rb
index fb4489d..6497ca1 100755
--- a/examples/linux-tcp-listener-stats.rb
+++ b/examples/linux-tcp-listener-stats.rb
@@ -5,6 +5,7 @@
 
 require 'raindrops'
 require 'optparse'
+require 'ipaddr'
 usage = "Usage: #$0 [-d delay] ADDR..."
 ARGV.size > 0 or abort usage
 delay = false
@@ -19,6 +20,21 @@ opts = OptionParser.new('', 24, '  ') do |opts|
   opts.parse! ARGV
 end
 
+ARGV.each do |addr|
+  addr =~ %r{\A(127\..+):(\d+)\z} or next
+  host, port = $1, $2
+  hex_port = '%X' % port.to_i
+  ip_addr = IPAddr.new(host)
+  hex_host = ip_addr.hton.each_byte.inject('') { |s,o| s << '%02X' % o }
+  socks = File.readlines('/proc/net/tcp')
+  hex_addr = "#{hex_host}:#{hex_port}"
+  if socks.grep(/^\s+\d+:\s+#{hex_addr}\s+/).empty? &&
+     ! socks.grep(/^\s+\d+:\s+00000000:#{hex_port}\s+/).empty?
+    warn "W: #{host}:#{port} (#{hex_addr}) not found in /proc/net/tcp"
+    warn "W: Did you mean 0.0.0.0:#{port}?"
+  end
+end
+
 fmt = "% 19s % 10u % 10u\n"
 printf fmt.tr('u','s'), *%w(address active queued)